-
Notifications
You must be signed in to change notification settings - Fork 0
/
tpgen.py
executable file
·132 lines (104 loc) · 3.58 KB
/
tpgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/env python3
import random, string, sys
def add_field(type, args):
fields.append("{}({})".format(type, ", ".join(args)))
def clever_name():
name = ""
for i in range(random.randint(2, 5)):
name += random.choice("bcdfghjklmnpqrstvwxyz")
name += random.choice("aeiouy")
return name
def add_ctf_numeric(ctf_type, c_type):
v = ["tp_{}_{}".format(c_type, i+1) for i in range(random.randint(1,2))]
vars.update(v)
add_field(ctf_type, [c_type, clever_name(), " + ".join(v)])
def add_ctf_integer_int():
add_ctf_numeric("ctf_integer", "int")
def add_ctf_integer_long():
add_ctf_numeric("ctf_integer", "long")
def add_ctf_float_float():
add_ctf_numeric("ctf_float", "float")
def add_ctf_float_double():
add_ctf_numeric("ctf_float", "double")
def add_ctf_string():
vars.add("tp_charptr_1")
add_field("ctf_string", [clever_name(), "tp_charptr_1"])
def add_ctf_sequence_char():
vars.add("tp_charptr_1")
vars.add("tp_charptr_1_size")
add_field("ctf_sequence", ["char", clever_name(), "tp_charptr_1",
"size_t", "tp_charptr_1_size"])
def add_ctf_sequence_text_char():
vars.add("tp_charptr_1")
vars.add("tp_charptr_1_size")
add_field("ctf_sequence_text", ["char", clever_name(), "tp_charptr_1",
"size_t", "tp_charptr_1_size"])
def add_ctf_array_char():
vars.add("tp_charptr_1")
add_field("ctf_array", ["char", clever_name(), "tp_charptr_1",
str(c_vars["tp_charptr_1_size"][1])])
def add_ctf_array_text_char():
vars.add("tp_charptr_1")
add_field("ctf_array_text", ["char", clever_name(), "tp_charptr_1",
str(c_vars["tp_charptr_1_size"][1])])
def gen_tp(provider, name, args, fields):
return """TRACEPOINT_EVENT(
{},
{},
TP_ARGS({}),
TP_FIELDS(
{}
)
)""".format(provider, name, ", ".join(args), "\n\t\t".join(fields))
fun = [add_ctf_integer_int,
add_ctf_integer_long,
add_ctf_float_float,
add_ctf_float_double,
add_ctf_string,
add_ctf_sequence_char,
add_ctf_sequence_text_char,
add_ctf_array_char,
add_ctf_array_text_char]
if len(sys.argv) < 2:
print("Usage: {} num_tp".format(sys.argv[0]), file=sys.stderr)
exit(1)
num_tp = int(sys.argv[1])
random.seed(10)
def randint():
return random.randint(-2**31+1, 2**31-1)
c_tp = []
for i in range(1, num_tp+1):
vars = set()
fields = []
random_string = "".join(random.choice(string.ascii_letters)
for i in range(random.randint(2, 128)))
c_vars = { "tp_int_1": ["int", randint()],
"tp_int_2": ["int", randint()],
"tp_long_1": ["long", randint()],
"tp_long_2": ["long", randint()],
"tp_float_1": ["float", random.random()],
"tp_float_2": ["float", random.random()],
"tp_double_1": ["double", random.random()],
"tp_double_2": ["double", random.random()],
"tp_charptr_1": ["char *", '"{}"'.format(random_string)],
"tp_charptr_1_size": ["size_t", len(random_string)] }
for j in range(random.randint(1,10)):
random.choice(fun)()
sorted_vars = list(vars)
sorted_vars.sort()
provider = "benchmark_tracepoint_" + str(i)
tp_name = "random_tp_" + str(i)
c_args = ", ".join([str(c_vars[name][1]) for name in sorted_vars])
c_tp.append("tracepoint({}, {}, {});".format(provider, tp_name, c_args))
tp_args = ["{}, {}".format(c_vars[name][0], name) for name in sorted_vars]
filename = tp_name + ".tp"
f = open(filename, "w")
f.write(gen_tp(provider, tp_name, tp_args, fields))
f.close()
includes = ["#include \"random_tp_{}.tp.h\"".format(i)
for i in range(1, num_tp+1)]
filename = "generated_tp.h"
f = open(filename, "w")
f.write("\n".join(includes) + "\n\n")
f.write("#define GENERATED_TP\\\n\t" + "\\\n\t".join(c_tp) + "\n")
f.close()