Commit | Line | Data |
---|---|---|
9e5ccbbf PP |
1 | #!/usr/bin/python3 |
2 | ||
3 | import json | |
4 | import string | |
5 | import random | |
6 | import argparse | |
7 | from lttnganalyses.cli import mi | |
8 | ||
9 | ||
10 | _TABLE_CLASS_PER_PROC = 'per-proc' | |
11 | _TABLE_CLASS_PER_SYSCALL = 'per-syscall' | |
12 | _TABLE_CLASS_PER_IRQ = 'per-irq' | |
13 | _TABLE_CLASSES = { | |
14 | _TABLE_CLASS_PER_PROC: mi.TableClass( | |
15 | _TABLE_CLASS_PER_PROC, | |
16 | 'Per-process stuff', [ | |
17 | ('proc', 'Process', mi.Process), | |
18 | ('count', 'Count', mi.Integer, 'things'), | |
19 | ('flag', 'Flag', mi.Boolean), | |
20 | ('value', 'Value', mi.Float, 'thou'), | |
21 | ('name', 'Name', mi.String), | |
22 | ('ratio', 'Ratio', mi.Ratio), | |
23 | ('ts', 'Timestamp', mi.Timestamp), | |
24 | ] | |
25 | ), | |
26 | _TABLE_CLASS_PER_SYSCALL: mi.TableClass( | |
27 | _TABLE_CLASS_PER_SYSCALL, | |
28 | 'Per-syscall stuff', [ | |
29 | ('syscall', 'System call', mi.Syscall), | |
30 | ('duration', 'Duration', mi.Duration), | |
31 | ('size', 'Size', mi.Size), | |
32 | ('bitrate', 'Bitrate', mi.Bitrate), | |
33 | ('time_range', 'Time range', mi.TimeRange), | |
34 | ] | |
35 | ), | |
36 | _TABLE_CLASS_PER_IRQ: mi.TableClass( | |
37 | _TABLE_CLASS_PER_IRQ, | |
38 | 'Per-interrupt stuff', [ | |
39 | ('interrupt', 'Interrupt', mi.Irq), | |
40 | ('fd', 'File descriptor', mi.Fd), | |
41 | ('path', 'File path', mi.Path), | |
42 | ('cpu', 'CPU', mi.Cpu), | |
43 | ('disk', 'Disk', mi.Disk), | |
44 | ('part', 'Partition', mi.Partition), | |
45 | ('netif', 'Network interface', mi.NetIf), | |
46 | ] | |
47 | ) | |
48 | } | |
49 | ||
50 | ||
51 | def _print_metadata(): | |
52 | infos = mi.get_metadata(version=[1, 2, 3, 'dev'], title='LAMI test', | |
53 | description='LTTng analyses machine interface test', | |
54 | authors=['Phil Proulx'], url='http://perdu.com', | |
55 | tags=['lami', 'test'], | |
56 | table_classes=_TABLE_CLASSES.values()) | |
57 | print(json.dumps(infos)) | |
58 | ||
59 | ||
60 | def _parse_args(): | |
61 | ap = argparse.ArgumentParser() | |
62 | ap.add_argument('--metadata', action='store_true') | |
63 | ap.add_argument('--begin', type=int, default=1000) | |
64 | ap.add_argument('--end', type=int, default=2000) | |
65 | ap.add_argument('-d', '--dynamic', action='store_true') | |
66 | ap.add_argument('-r', '--dynamic-rows', type=int, default=25) | |
67 | ap.add_argument('-c', '--dynamic-columns', type=int, default=10) | |
68 | ||
69 | return ap.parse_args() | |
70 | ||
71 | ||
72 | def _print_tables(tables): | |
73 | obj = { | |
74 | 'results': [t.to_native_object() for t in tables], | |
75 | } | |
76 | ||
77 | print(json.dumps(obj)) | |
78 | ||
79 | ||
80 | def _print_dynamic_table(begin, end, rows, columns): | |
81 | def gen_irq_name(size=6, chars=string.ascii_uppercase + string.digits): | |
82 | return ''.join(random.choice(chars) for _ in range(size)) | |
83 | ||
84 | column_tuples = [ | |
85 | ('irq', 'Interrupt', mi.Irq), | |
86 | ] | |
87 | ||
88 | for i in range(columns): | |
89 | column_tuples.append(( | |
90 | 'count{}'.format(i), | |
91 | 'Count ({} to {})'.format(i * 5, (i + 1) * 5), | |
92 | mi.Integer, | |
93 | 'interrupts' | |
94 | )) | |
95 | ||
96 | table_class = mi.TableClass(None, 'What a dynamic table!', column_tuples) | |
97 | result_table = mi.ResultTable(table_class, begin, end) | |
98 | ||
99 | for i in range(rows): | |
100 | row_tuple = [ | |
101 | mi.Irq(bool(random.getrandbits(1)), i, gen_irq_name()) | |
102 | ] | |
103 | ||
104 | for j in range(columns): | |
105 | row_tuple.append(mi.Integer(random.randint(0, 5000))) | |
106 | ||
107 | result_table.append_row_tuple(tuple(row_tuple)) | |
108 | ||
109 | _print_tables([result_table]) | |
110 | ||
111 | ||
112 | def _print_static_tables(begin, end): | |
113 | per_proc_table = mi.ResultTable(_TABLE_CLASSES[_TABLE_CLASS_PER_PROC], begin, end) | |
114 | per_syscall_table = mi.ResultTable(_TABLE_CLASSES[_TABLE_CLASS_PER_SYSCALL], begin, end) | |
115 | per_irq_table = mi.ResultTable(_TABLE_CLASSES[_TABLE_CLASS_PER_IRQ], begin, end) | |
116 | per_irq_table_sub = mi.ResultTable(_TABLE_CLASSES[_TABLE_CLASS_PER_IRQ], begin, end, | |
117 | 'with overridden title') | |
118 | ||
119 | # per-process | |
120 | per_proc_table.append_row_tuple(( | |
121 | mi.Process('zsh', pid=23), | |
122 | mi.Integer(23), | |
123 | mi.Boolean(False), | |
124 | mi.Float(17.2832), | |
125 | mi.String('typical'), | |
126 | mi.Ratio(0.154), | |
127 | mi.Timestamp(817232), | |
128 | )) | |
129 | per_proc_table.append_row_tuple(( | |
130 | mi.Process('chromium', tid=4987), | |
131 | mi.Integer(19), | |
132 | mi.Boolean(False), | |
133 | mi.Float(-19457.15), | |
134 | mi.String('beam'), | |
135 | mi.Ratio(0.001), | |
136 | mi.Timestamp(1194875), | |
137 | )) | |
138 | per_proc_table.append_row_tuple(( | |
139 | mi.Process('terminator'), | |
140 | mi.Integer(-145), | |
141 | mi.Unknown(), | |
142 | mi.Float(22.22), | |
143 | mi.String('dry'), | |
144 | mi.Ratio(0.94), | |
145 | mi.Timestamp(984987658), | |
146 | )) | |
147 | per_proc_table.append_row_tuple(( | |
148 | mi.Process(pid=1945, tid=4497), | |
149 | mi.Integer(31416), | |
150 | mi.Boolean(True), | |
151 | mi.Float(17.34), | |
152 | mi.Empty(), | |
153 | mi.Ratio(1.5), | |
154 | mi.Timestamp(154484512), | |
155 | )) | |
156 | ||
157 | # per-syscall | |
158 | per_syscall_table.append_row_tuple(( | |
159 | mi.Syscall('read'), | |
160 | mi.Duration(2398123), | |
161 | mi.Size(8123982), | |
162 | mi.Bitrate(223232), | |
163 | mi.TimeRange(98233, 1293828), | |
164 | )) | |
165 | per_syscall_table.append_row_tuple(( | |
166 | mi.Syscall('write'), | |
167 | mi.Duration(412434), | |
168 | mi.Size(5645), | |
169 | mi.Bitrate(25235343), | |
170 | mi.TimeRange(5454, 2354523), | |
171 | )) | |
172 | per_syscall_table.append_row_tuple(( | |
173 | mi.Syscall('sync'), | |
174 | mi.Duration(2312454), | |
175 | mi.Size(23433), | |
176 | mi.Empty(), | |
177 | mi.TimeRange(12, 645634545454), | |
178 | )) | |
179 | per_syscall_table.append_row_tuple(( | |
180 | mi.Syscall('fstat'), | |
181 | mi.Unknown(), | |
182 | mi.Size(2343334), | |
183 | mi.Bitrate(5864684), | |
184 | mi.TimeRange(2134, 645634545), | |
185 | )) | |
186 | per_syscall_table.append_row_tuple(( | |
187 | mi.Syscall('sync'), | |
188 | mi.Duration(564533), | |
189 | mi.Size(56875), | |
190 | mi.Bitrate(4494494494), | |
191 | mi.Empty(), | |
192 | )) | |
193 | ||
194 | # per-interrupt | |
195 | per_irq_table.append_row_tuple(( | |
196 | mi.Irq(True, 15, 'keyboard'), | |
197 | mi.Fd(3), | |
198 | mi.Path('/etc/passwd'), | |
199 | mi.Cpu(2), | |
200 | mi.Disk('sda'), | |
201 | mi.Partition('sdb3'), | |
202 | mi.NetIf('eth0'), | |
203 | )) | |
204 | per_irq_table.append_row_tuple(( | |
205 | mi.Irq(False, 7, 'soft-timer'), | |
206 | mi.Fd(1), | |
207 | mi.Path('/dev/null'), | |
208 | mi.Unknown(), | |
209 | mi.Disk('hda'), | |
210 | mi.Partition('mmcblk0p2'), | |
211 | mi.NetIf('enp3s25'), | |
212 | )) | |
213 | per_irq_table.append_row_tuple(( | |
214 | mi.Irq(True, 34), | |
215 | mi.Empty(), | |
216 | mi.Empty(), | |
217 | mi.Cpu(1), | |
218 | mi.Disk('sdc'), | |
219 | mi.Partition('sdc3'), | |
220 | mi.NetIf('lo'), | |
221 | )) | |
222 | ||
223 | # per-interrupt with subtitle | |
224 | per_irq_table_sub.append_row_tuple(( | |
225 | mi.Irq(False, 12, 'soft-like-silk'), | |
226 | mi.Fd(10), | |
227 | mi.Path('/home/bob/meowmix.txt'), | |
228 | mi.Cpu(0), | |
229 | mi.Disk('sdb'), | |
230 | mi.Partition('sdb2'), | |
231 | mi.NetIf('eth1'), | |
232 | )) | |
233 | per_irq_table_sub.append_row_tuple(( | |
234 | mi.Irq(True, 1, 'mouse2'), | |
235 | mi.Fd(5), | |
236 | mi.Empty(), | |
237 | mi.Cpu(7), | |
238 | mi.Disk('vda'), | |
239 | mi.Partition('vda3'), | |
240 | mi.NetIf('wlp3s0'), | |
241 | )) | |
242 | ||
243 | ||
244 | _print_tables([ | |
245 | per_proc_table, | |
246 | per_syscall_table, | |
247 | per_irq_table, | |
248 | per_irq_table_sub, | |
249 | ]) | |
250 | ||
251 | ||
252 | def _mitest(): | |
253 | args = _parse_args() | |
254 | ||
255 | if args.metadata: | |
256 | _print_metadata() | |
257 | return | |
258 | ||
259 | if args.dynamic: | |
260 | _print_dynamic_table(args.begin, args.end, | |
261 | args.dynamic_rows, args.dynamic_columns) | |
262 | else: | |
263 | _print_static_tables(args.begin, args.end) | |
264 | ||
265 | ||
266 | if __name__ == '__main__': | |
267 | _mitest() |