Commit | Line | Data |
---|---|---|
4ed24f86 JD |
1 | # The MIT License (MIT) |
2 | # | |
a3fa57c0 | 3 | # Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com> |
cee855a2 | 4 | # 2015 - Philippe Proulx <pproulx@efficios.com> |
0b250a71 | 5 | # 2015 - Antoine Busque <abusque@efficios.com> |
4ed24f86 JD |
6 | # |
7 | # Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | # of this software and associated documentation files (the "Software"), to deal | |
9 | # in the Software without restriction, including without limitation the rights | |
10 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | # copies of the Software, and to permit persons to whom the Software is | |
12 | # furnished to do so, subject to the following conditions: | |
13 | # | |
14 | # The above copyright notice and this permission notice shall be included in | |
15 | # all copies or substantial portions of the Software. | |
16 | # | |
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | # SOFTWARE. | |
24 | ||
323b3fd6 | 25 | import argparse |
a0acc08c | 26 | import json |
4ac5e240 | 27 | import os |
a0acc08c | 28 | import re |
0b250a71 AB |
29 | import sys |
30 | import subprocess | |
810e6be8 | 31 | import traceback |
0b250a71 | 32 | from babeltrace import TraceCollection |
9079847d | 33 | from . import mi, progressbar |
9463174b | 34 | from .. import __version__ |
0b250a71 | 35 | from ..core import analysis |
9079847d | 36 | from ..common import ( |
e4a48ea8 | 37 | format_utils, parse_utils, trace_utils, version_utils |
9079847d | 38 | ) |
0b250a71 | 39 | from ..linuxautomaton import automaton |
323b3fd6 PP |
40 | |
41 | ||
42 | class Command: | |
a0acc08c PP |
43 | _MI_BASE_TAGS = ['linux-kernel', 'lttng-analyses'] |
44 | _MI_AUTHORS = [ | |
45 | 'Julien Desfossez', | |
46 | 'Antoine Busque', | |
47 | 'Philippe Proulx', | |
48 | ] | |
49 | _MI_URL = 'https://github.com/lttng/lttng-analyses' | |
9463174b | 50 | _VERSION = version_utils.Version.new_from_string(__version__) |
810e6be8 | 51 | _DEBUG_ENV_VAR = 'LTTNG_ANALYSES_DEBUG' |
a0acc08c PP |
52 | |
53 | def __init__(self, mi_mode=False): | |
b6d9132b AB |
54 | self._analysis = None |
55 | self._analysis_conf = None | |
56 | self._args = None | |
57 | self._handles = None | |
58 | self._traces = None | |
a0acc08c PP |
59 | self._ticks = 0 |
60 | self._mi_mode = mi_mode | |
810e6be8 | 61 | self._debug_mode = os.environ.get(self._DEBUG_ENV_VAR) |
5bc26da4 PP |
62 | self._run_step('create automaton', self._create_automaton) |
63 | self._run_step('setup MI', self._mi_setup) | |
a0acc08c PP |
64 | |
65 | @property | |
66 | def mi_mode(self): | |
67 | return self._mi_mode | |
323b3fd6 | 68 | |
5bc26da4 | 69 | def _run_step(self, action_title, fn): |
74d112b5 | 70 | try: |
5bc26da4 | 71 | fn() |
74d112b5 | 72 | except KeyboardInterrupt: |
5bc26da4 | 73 | self._print('Cancelled by user') |
74d112b5 | 74 | sys.exit(0) |
5bc26da4 | 75 | except Exception as e: |
810e6be8 AB |
76 | if self._debug_mode: |
77 | traceback.print_exc() | |
78 | ||
5bc26da4 PP |
79 | self._gen_error('Cannot {}: {}'.format(action_title, e)) |
80 | ||
81 | def run(self): | |
82 | self._run_step('parse arguments', self._parse_args) | |
83 | self._run_step('open trace', self._open_trace) | |
84 | self._run_step('create analysis', self._create_analysis) | |
85 | ||
78f6647f | 86 | if not self._mi_mode or not self._args.test_compatibility: |
5bc26da4 PP |
87 | self._run_step('run analysis', self._run_analysis) |
88 | ||
89 | self._run_step('close trace', self._close_trace) | |
b6d9132b | 90 | |
8311b968 PP |
91 | def _mi_error(self, msg, code=None): |
92 | print(json.dumps(mi.get_error(msg, code))) | |
93 | ||
94 | def _non_mi_error(self, msg): | |
dc3ccf03 AB |
95 | if self._args.color: |
96 | try: | |
97 | import termcolor | |
d6c76c60 | 98 | |
dc3ccf03 AB |
99 | msg = termcolor.colored(msg, 'red', attrs=['bold']) |
100 | except ImportError: | |
101 | pass | |
d6c76c60 | 102 | |
323b3fd6 | 103 | print(msg, file=sys.stderr) |
8311b968 | 104 | |
45d44f25 | 105 | def _error(self, msg, exit_code=1): |
8311b968 PP |
106 | if self._mi_mode: |
107 | self._mi_error(msg) | |
108 | else: | |
109 | self._non_mi_error(msg) | |
110 | ||
323b3fd6 PP |
111 | sys.exit(exit_code) |
112 | ||
113 | def _gen_error(self, msg, exit_code=1): | |
114 | self._error('Error: {}'.format(msg), exit_code) | |
115 | ||
116 | def _cmdline_error(self, msg, exit_code=1): | |
117 | self._error('Command line error: {}'.format(msg), exit_code) | |
118 | ||
a0acc08c PP |
119 | def _print(self, msg): |
120 | if not self._mi_mode: | |
121 | print(msg) | |
122 | ||
123 | def _mi_create_result_table(self, table_class_name, begin, end, | |
124 | subtitle=None): | |
125 | return mi.ResultTable(self._mi_table_classes[table_class_name], | |
126 | begin, end, subtitle) | |
127 | ||
128 | def _mi_setup(self): | |
129 | self._mi_table_classes = {} | |
130 | ||
131 | for tc_tuple in self._MI_TABLE_CLASSES: | |
132 | table_class = mi.TableClass(tc_tuple[0], tc_tuple[1], tc_tuple[2]) | |
133 | self._mi_table_classes[table_class.name] = table_class | |
134 | ||
135 | self._mi_clear_result_tables() | |
136 | ||
137 | def _mi_print_metadata(self): | |
138 | tags = self._MI_BASE_TAGS + self._MI_TAGS | |
9463174b | 139 | infos = mi.get_metadata(version=self._VERSION, title=self._MI_TITLE, |
a0acc08c PP |
140 | description=self._MI_DESCRIPTION, |
141 | authors=self._MI_AUTHORS, url=self._MI_URL, | |
142 | tags=tags, | |
143 | table_classes=self._mi_table_classes.values()) | |
144 | print(json.dumps(infos)) | |
145 | ||
146 | def _mi_append_result_table(self, result_table): | |
147 | if not result_table or not result_table.rows: | |
148 | return | |
149 | ||
150 | tc_name = result_table.table_class.name | |
151 | self._mi_get_result_tables(tc_name).append(result_table) | |
152 | ||
153 | def _mi_append_result_tables(self, result_tables): | |
154 | if not result_tables: | |
155 | return | |
156 | ||
157 | for result_table in result_tables: | |
158 | self._mi_append_result_table(result_table) | |
159 | ||
160 | def _mi_clear_result_tables(self): | |
161 | self._result_tables = {} | |
162 | ||
163 | def _mi_get_result_tables(self, table_class_name): | |
164 | if table_class_name not in self._result_tables: | |
165 | self._result_tables[table_class_name] = [] | |
166 | ||
167 | return self._result_tables[table_class_name] | |
168 | ||
169 | def _mi_print(self): | |
170 | results = [] | |
171 | ||
172 | for result_tables in self._result_tables.values(): | |
173 | for result_table in result_tables: | |
174 | results.append(result_table.to_native_object()) | |
175 | ||
176 | obj = { | |
177 | 'results': results, | |
178 | } | |
179 | ||
180 | print(json.dumps(obj)) | |
181 | ||
182 | def _create_summary_result_tables(self): | |
183 | pass | |
184 | ||
bd3cd7c5 JD |
185 | def _open_trace(self): |
186 | traces = TraceCollection() | |
b6d9132b | 187 | handles = traces.add_traces_recursive(self._args.path, 'ctf') |
ced36aab | 188 | if handles == {}: |
b6d9132b | 189 | self._gen_error('Failed to open ' + self._args.path, -1) |
ced36aab | 190 | self._handles = handles |
bd3cd7c5 | 191 | self._traces = traces |
dd2efe70 PP |
192 | self._ts_begin = traces.timestamp_begin |
193 | self._ts_end = traces.timestamp_end | |
652bc6b7 | 194 | self._process_date_args() |
ee6a5866 | 195 | self._read_tracer_version() |
b6d9132b | 196 | if not self._args.skip_validation: |
d3014022 | 197 | self._check_lost_events() |
bd3cd7c5 JD |
198 | |
199 | def _close_trace(self): | |
ced36aab AB |
200 | for handle in self._handles.values(): |
201 | self._traces.remove_trace(handle) | |
bd3cd7c5 | 202 | |
ee6a5866 | 203 | def _read_tracer_version(self): |
4ac5e240 | 204 | kernel_path = None |
2dca9c55 JD |
205 | # remove the trailing / |
206 | while self._args.path.endswith('/'): | |
207 | self._args.path = self._args.path[:-1] | |
4ac5e240 AB |
208 | for root, _, _ in os.walk(self._args.path): |
209 | if root.endswith('kernel'): | |
210 | kernel_path = root | |
211 | break | |
212 | ||
213 | if kernel_path is None: | |
214 | self._gen_error('Could not find kernel trace directory') | |
215 | ||
ee6a5866 | 216 | try: |
0349f942 | 217 | ret, metadata = subprocess.getstatusoutput( |
4ac5e240 | 218 | 'babeltrace -o ctf-metadata "%s"' % kernel_path) |
ee6a5866 AB |
219 | except subprocess.CalledProcessError: |
220 | self._gen_error('Cannot run babeltrace on the trace, cannot read' | |
221 | ' tracer version') | |
222 | ||
0349f942 JD |
223 | # fallback to reading the text metadata if babeltrace failed to |
224 | # output the CTF metadata | |
225 | if ret != 0: | |
226 | try: | |
227 | metadata = subprocess.getoutput( | |
228 | 'cat "%s"' % os.path.join(kernel_path, 'metadata')) | |
229 | except subprocess.CalledProcessError: | |
230 | self._gen_error('Cannot read the metadata of the trace, cannot' | |
231 | 'extract tracer version') | |
232 | ||
233 | major_match = re.search(r'tracer_major = "*(\d+)"*', metadata) | |
234 | minor_match = re.search(r'tracer_minor = "*(\d+)"*', metadata) | |
235 | patch_match = re.search(r'tracer_patchlevel = "*(\d+)"*', metadata) | |
ee6a5866 AB |
236 | |
237 | if not major_match or not minor_match or not patch_match: | |
238 | self._gen_error('Malformed metadata, cannot read tracer version') | |
239 | ||
240 | self.state.tracer_version = version_utils.Version( | |
241 | int(major_match.group(1)), | |
242 | int(minor_match.group(1)), | |
243 | int(patch_match.group(1)), | |
244 | ) | |
245 | ||
d3014022 | 246 | def _check_lost_events(self): |
73f9d005 PP |
247 | msg = 'Checking the trace for lost events...' |
248 | self._print(msg) | |
249 | ||
250 | if self._mi_mode and self._args.output_progress: | |
251 | mi.print_progress(0, msg) | |
252 | ||
d3014022 | 253 | try: |
e0bc16fe | 254 | subprocess.check_output('babeltrace "%s"' % self._args.path, |
d3014022 JD |
255 | shell=True) |
256 | except subprocess.CalledProcessError: | |
b9f05f8d AB |
257 | self._gen_error('Cannot run babeltrace on the trace, cannot verify' |
258 | ' if events were lost during the trace recording') | |
a0acc08c PP |
259 | |
260 | def _pre_analysis(self): | |
261 | pass | |
262 | ||
263 | def _post_analysis(self): | |
264 | if not self._mi_mode: | |
265 | return | |
266 | ||
267 | if self._ticks > 1: | |
268 | self._create_summary_result_tables() | |
269 | ||
270 | self._mi_print() | |
d3014022 | 271 | |
73f9d005 | 272 | def _pb_setup(self): |
dd2efe70 PP |
273 | if self._args.no_progress: |
274 | return | |
275 | ||
276 | ts_end = self._ts_end | |
277 | ||
278 | if self._analysis_conf.end_ts is not None: | |
279 | ts_end = self._analysis_conf.end_ts | |
73f9d005 | 280 | |
73f9d005 | 281 | if self._mi_mode: |
dd2efe70 | 282 | cls = progressbar.MiProgress |
73f9d005 | 283 | else: |
dd2efe70 PP |
284 | cls = progressbar.FancyProgressBar |
285 | ||
286 | self._progress = cls(self._ts_begin, ts_end, self._args.path, | |
287 | self._args.progress_use_size) | |
288 | ||
289 | def _pb_update(self, event): | |
290 | if self._args.no_progress: | |
291 | return | |
292 | ||
293 | self._progress.update(event) | |
73f9d005 PP |
294 | |
295 | def _pb_finish(self): | |
dd2efe70 PP |
296 | if self._args.no_progress: |
297 | return | |
298 | ||
299 | self._progress.finalize() | |
73f9d005 | 300 | |
b6d9132b | 301 | def _run_analysis(self): |
a0acc08c | 302 | self._pre_analysis() |
73f9d005 | 303 | self._pb_setup() |
b6d9132b | 304 | |
bd3cd7c5 | 305 | for event in self._traces.events: |
dd2efe70 | 306 | self._pb_update(event) |
bd3cd7c5 | 307 | self._analysis.process_event(event) |
b6d9132b AB |
308 | if self._analysis.ended: |
309 | break | |
47ba125c | 310 | self._automaton.process_event(event) |
bd3cd7c5 | 311 | |
73f9d005 | 312 | self._pb_finish() |
b6d9132b | 313 | self._analysis.end() |
a0acc08c | 314 | self._post_analysis() |
bd3cd7c5 | 315 | |
3664e4b0 | 316 | def _print_date(self, begin_ns, end_ns): |
9079847d AB |
317 | time_range_str = format_utils.format_time_range( |
318 | begin_ns, end_ns, print_date=True, gmt=self._args.gmt | |
319 | ) | |
320 | date = 'Timerange: {}'.format(time_range_str) | |
321 | ||
a0acc08c | 322 | self._print(date) |
3664e4b0 | 323 | |
9079847d AB |
324 | def _format_timestamp(self, timestamp): |
325 | return format_utils.format_timestamp( | |
326 | timestamp, print_date=self._args.multi_day, gmt=self._args.gmt | |
327 | ) | |
328 | ||
dbbdd963 PP |
329 | def _get_uniform_freq_values(self, durations): |
330 | if self._args.uniform_step is not None: | |
331 | return (self._args.uniform_min, self._args.uniform_max, | |
332 | self._args.uniform_step) | |
333 | ||
334 | if self._args.min is not None: | |
335 | self._args.uniform_min = self._args.min | |
336 | else: | |
337 | self._args.uniform_min = min(durations) | |
338 | if self._args.max is not None: | |
339 | self._args.uniform_max = self._args.max | |
340 | else: | |
341 | self._args.uniform_max = max(durations) | |
342 | ||
343 | # ns to µs | |
344 | self._args.uniform_min /= 1000 | |
345 | self._args.uniform_max /= 1000 | |
346 | self._args.uniform_step = ( | |
347 | (self._args.uniform_max - self._args.uniform_min) / | |
348 | self._args.freq_resolution | |
349 | ) | |
350 | ||
351 | return self._args.uniform_min, self._args.uniform_max, \ | |
650e7f57 | 352 | self._args.uniform_step |
dbbdd963 | 353 | |
bd3cd7c5 | 354 | def _validate_transform_common_args(self, args): |
83ad157b AB |
355 | refresh_period_ns = None |
356 | if args.refresh is not None: | |
357 | try: | |
9079847d | 358 | refresh_period_ns = parse_utils.parse_duration(args.refresh) |
83ad157b AB |
359 | except ValueError as e: |
360 | self._cmdline_error(str(e)) | |
361 | ||
b6d9132b | 362 | self._analysis_conf = analysis.AnalysisConfig() |
83ad157b | 363 | self._analysis_conf.refresh_period = refresh_period_ns |
43a3c04c AB |
364 | self._analysis_conf.period_begin_ev_name = args.period_begin |
365 | self._analysis_conf.period_end_ev_name = args.period_end | |
05684c5e | 366 | self._analysis_conf.period_begin_key_fields = \ |
007d3fe0 | 367 | args.period_begin_key.split(',') |
05684c5e AB |
368 | |
369 | if args.period_end_key: | |
370 | self._analysis_conf.period_end_key_fields = \ | |
007d3fe0 | 371 | args.period_end_key.split(',') |
05684c5e AB |
372 | else: |
373 | self._analysis_conf.period_end_key_fields = \ | |
007d3fe0 | 374 | self._analysis_conf.period_begin_key_fields |
05684c5e AB |
375 | |
376 | if args.period_key_value: | |
377 | self._analysis_conf.period_key_value = \ | |
007d3fe0 | 378 | tuple(args.period_key_value.split(',')) |
05684c5e | 379 | |
a621ba35 AB |
380 | if args.cpu: |
381 | self._analysis_conf.cpu_list = args.cpu.split(',') | |
382 | self._analysis_conf.cpu_list = [int(cpu) for cpu in | |
383 | self._analysis_conf.cpu_list] | |
b6d9132b | 384 | |
810e6be8 AB |
385 | if args.debug: |
386 | self._debug_mode = True | |
387 | ||
b6d9132b AB |
388 | # convert min/max args from µs to ns, if needed |
389 | if hasattr(args, 'min') and args.min is not None: | |
390 | args.min *= 1000 | |
391 | self._analysis_conf.min_duration = args.min | |
392 | if hasattr(args, 'max') and args.max is not None: | |
393 | args.max *= 1000 | |
394 | self._analysis_conf.max_duration = args.max | |
395 | ||
396 | if hasattr(args, 'procname'): | |
47ba125c | 397 | if args.procname: |
43b66dd6 | 398 | self._analysis_conf.proc_list = args.procname.split(',') |
28ad5ec8 | 399 | |
43b66dd6 AB |
400 | if hasattr(args, 'tid'): |
401 | if args.tid: | |
402 | self._analysis_conf.tid_list = args.tid.split(',') | |
403 | self._analysis_conf.tid_list = [int(tid) for tid in | |
404 | self._analysis_conf.tid_list] | |
f89605f0 | 405 | |
1a68e04c AB |
406 | if hasattr(args, 'freq'): |
407 | args.uniform_min = None | |
408 | args.uniform_max = None | |
409 | args.uniform_step = None | |
410 | ||
dbbdd963 PP |
411 | if args.freq_series: |
412 | # implies uniform buckets | |
413 | args.freq_uniform = True | |
414 | ||
a0acc08c | 415 | if self._mi_mode: |
1ab6b93a PP |
416 | # print MI version if required |
417 | if args.mi_version: | |
418 | print(mi.get_version_string()) | |
419 | sys.exit(0) | |
420 | ||
a0acc08c PP |
421 | # print MI metadata if required |
422 | if args.metadata: | |
423 | self._mi_print_metadata() | |
424 | sys.exit(0) | |
425 | ||
426 | # validate path argument (required at this point) | |
427 | if not args.path: | |
428 | self._cmdline_error('Please specify a trace path') | |
429 | ||
430 | if type(args.path) is list: | |
431 | args.path = args.path[0] | |
432 | ||
b6d9132b AB |
433 | def _validate_transform_args(self, args): |
434 | pass | |
f89605f0 | 435 | |
323b3fd6 PP |
436 | def _parse_args(self): |
437 | ap = argparse.ArgumentParser(description=self._DESC) | |
438 | ||
439 | # common arguments | |
83ad157b AB |
440 | ap.add_argument('-r', '--refresh', type=str, |
441 | help='Refresh period, with optional units suffix ' | |
442 | '(default units: s)') | |
a0acc08c PP |
443 | ap.add_argument('--gmt', action='store_true', |
444 | help='Manipulate timestamps based on GMT instead ' | |
445 | 'of local time') | |
73b71522 | 446 | ap.add_argument('--skip-validation', action='store_true', |
d3014022 | 447 | help='Skip the trace validation') |
bd3cd7c5 JD |
448 | ap.add_argument('--begin', type=str, help='start time: ' |
449 | 'hh:mm:ss[.nnnnnnnnn]') | |
450 | ap.add_argument('--end', type=str, help='end time: ' | |
451 | 'hh:mm:ss[.nnnnnnnnn]') | |
43a3c04c AB |
452 | ap.add_argument('--period-begin', type=str, |
453 | help='Analysis period start marker event name') | |
454 | ap.add_argument('--period-end', type=str, | |
455 | help='Analysis period end marker event name ' | |
456 | '(requires --period-begin)') | |
05684c5e | 457 | ap.add_argument('--period-begin-key', type=str, default='cpu_id', |
b9f05f8d AB |
458 | help='Optional, list of event field names used to ' |
459 | 'match period markers (default: cpu_id)') | |
05684c5e AB |
460 | ap.add_argument('--period-end-key', type=str, |
461 | help='Optional, list of event field names used to ' | |
462 | 'match period marker. If none specified, use the same ' | |
463 | ' --period-begin-key') | |
464 | ap.add_argument('--period-key-value', type=str, | |
465 | help='Optional, define a fixed key value to which a' | |
466 | ' period must correspond to be considered.') | |
a621ba35 AB |
467 | ap.add_argument('--cpu', type=str, |
468 | help='Filter the results only for this list of ' | |
469 | 'CPU IDs') | |
a0acc08c PP |
470 | ap.add_argument('--timerange', type=str, help='time range: ' |
471 | '[begin,end]') | |
dd2efe70 PP |
472 | ap.add_argument('--progress-use-size', action='store_true', |
473 | help='use trace size to approximate progress') | |
323b3fd6 | 474 | ap.add_argument('-V', '--version', action='version', |
9463174b | 475 | version='LTTng Analyses v{}'.format(self._VERSION)) |
810e6be8 AB |
476 | ap.add_argument('--debug', action='store_true', |
477 | help='Enable debug mode (or set {} environment variable)'.format( | |
478 | self._DEBUG_ENV_VAR)) | |
dc3ccf03 AB |
479 | ap.add_argument('--no-color', action='store_false', dest='color', |
480 | help='Disable colored output') | |
323b3fd6 | 481 | |
a0acc08c PP |
482 | # MI mode-dependent arguments |
483 | if self._mi_mode: | |
1ab6b93a PP |
484 | ap.add_argument('--mi-version', action='store_true', |
485 | help='Print MI version') | |
a0acc08c | 486 | ap.add_argument('--metadata', action='store_true', |
1ab6b93a | 487 | help='Print analysis\' metadata') |
ee39b192 PP |
488 | ap.add_argument('--test-compatibility', action='store_true', |
489 | help='Check if the provided trace is supported and exit') | |
b9f05f8d AB |
490 | ap.add_argument('path', metavar='<path/to/trace>', |
491 | help='trace path', nargs='*') | |
73f9d005 PP |
492 | ap.add_argument('--output-progress', action='store_true', |
493 | help='Print progress indication lines') | |
a0acc08c PP |
494 | else: |
495 | ap.add_argument('--no-progress', action='store_true', | |
496 | help='Don\'t display the progress bar') | |
b9f05f8d AB |
497 | ap.add_argument('path', metavar='<path/to/trace>', |
498 | help='trace path') | |
a0acc08c | 499 | |
b6d9132b AB |
500 | # Used to add command-specific args |
501 | self._add_arguments(ap) | |
323b3fd6 | 502 | |
b6d9132b | 503 | args = ap.parse_args() |
dd2efe70 PP |
504 | |
505 | if self._mi_mode: | |
506 | args.no_progress = True | |
507 | ||
508 | if args.output_progress: | |
509 | args.no_progress = False | |
510 | ||
bd3cd7c5 | 511 | self._validate_transform_common_args(args) |
b6d9132b | 512 | self._validate_transform_args(args) |
323b3fd6 PP |
513 | self._args = args |
514 | ||
b6d9132b AB |
515 | @staticmethod |
516 | def _add_proc_filter_args(ap): | |
517 | ap.add_argument('--procname', type=str, | |
518 | help='Filter the results only for this list of ' | |
519 | 'process names') | |
43b66dd6 AB |
520 | ap.add_argument('--tid', type=str, |
521 | help='Filter the results only for this list of TIDs') | |
b6d9132b AB |
522 | |
523 | @staticmethod | |
524 | def _add_min_max_args(ap): | |
525 | ap.add_argument('--min', type=float, | |
526 | help='Filter out durations shorter than min usec') | |
527 | ap.add_argument('--max', type=float, | |
528 | help='Filter out durations longer than max usec') | |
529 | ||
530 | @staticmethod | |
531 | def _add_freq_args(ap, help=None): | |
532 | if not help: | |
533 | help = 'Output the frequency distribution' | |
534 | ||
535 | ap.add_argument('--freq', action='store_true', help=help) | |
536 | ap.add_argument('--freq-resolution', type=int, default=20, | |
537 | help='Frequency distribution resolution ' | |
538 | '(default 20)') | |
1a68e04c AB |
539 | ap.add_argument('--freq-uniform', action='store_true', |
540 | help='Use a uniform resolution across distributions') | |
86ea0394 | 541 | ap.add_argument('--freq-series', action='store_true', |
650e7f57 AB |
542 | help='Consolidate frequency distribution histogram ' |
543 | 'as a single one') | |
b6d9132b AB |
544 | |
545 | @staticmethod | |
546 | def _add_log_args(ap, help=None): | |
547 | if not help: | |
548 | help = 'Output the events in chronological order' | |
549 | ||
550 | ap.add_argument('--log', action='store_true', help=help) | |
551 | ||
b9f05f8d AB |
552 | @staticmethod |
553 | def _add_top_args(ap, help=None): | |
554 | if not help: | |
555 | help = 'Output the top results' | |
556 | ||
557 | ap.add_argument('--limit', type=int, default=10, | |
558 | help='Limit to top X (default = 10)') | |
559 | ap.add_argument('--top', action='store_true', help=help) | |
560 | ||
b6d9132b AB |
561 | @staticmethod |
562 | def _add_stats_args(ap, help=None): | |
563 | if not help: | |
564 | help = 'Output statistics' | |
565 | ||
566 | ap.add_argument('--stats', action='store_true', help=help) | |
567 | ||
568 | def _add_arguments(self, ap): | |
569 | pass | |
570 | ||
652bc6b7 | 571 | def _process_date_args(self): |
9079847d AB |
572 | def parse_date(date): |
573 | try: | |
574 | ts = parse_utils.parse_trace_collection_date( | |
575 | self._traces, date, self._args.gmt | |
576 | ) | |
577 | except ValueError as e: | |
578 | self._cmdline_error(str(e)) | |
b6d9132b AB |
579 | |
580 | return ts | |
581 | ||
9079847d AB |
582 | self._args.multi_day = trace_utils.is_multi_day_trace_collection( |
583 | self._traces | |
584 | ) | |
602ac199 PP |
585 | begin_ts = None |
586 | end_ts = None | |
587 | ||
588 | if self._args.timerange: | |
9079847d AB |
589 | try: |
590 | begin_ts, end_ts = ( | |
591 | parse_utils.parse_trace_collection_time_range( | |
592 | self._traces, self._args.timerange, self._args.gmt | |
593 | ) | |
594 | ) | |
595 | except ValueError as e: | |
596 | self._cmdline_error(str(e)) | |
652bc6b7 | 597 | else: |
b6d9132b | 598 | if self._args.begin: |
9079847d | 599 | begin_ts = parse_date(self._args.begin) |
b6d9132b | 600 | if self._args.end: |
9079847d | 601 | end_ts = parse_date(self._args.end) |
652bc6b7 | 602 | |
93c7af7d AB |
603 | # We have to check if timestamp_begin is None, which |
604 | # it always is in older versions of babeltrace. In | |
605 | # that case, the test is simply skipped and an invalid | |
606 | # --end value will cause an empty analysis | |
dd2efe70 PP |
607 | if self._ts_begin is not None and \ |
608 | end_ts < self._ts_begin: | |
b6d9132b AB |
609 | self._cmdline_error( |
610 | '--end timestamp before beginning of trace') | |
611 | ||
602ac199 PP |
612 | self._analysis_conf.begin_ts = begin_ts |
613 | self._analysis_conf.end_ts = end_ts | |
b6d9132b AB |
614 | |
615 | def _create_analysis(self): | |
616 | notification_cbs = { | |
a0acc08c | 617 | analysis.Analysis.TICK_CB: self._analysis_tick_cb |
b6d9132b AB |
618 | } |
619 | ||
620 | self._analysis = self._ANALYSIS_CLASS(self.state, self._analysis_conf) | |
621 | self._analysis.register_notification_cbs(notification_cbs) | |
93c7af7d | 622 | |
323b3fd6 | 623 | def _create_automaton(self): |
56936af2 | 624 | self._automaton = automaton.Automaton() |
6e01ed18 | 625 | self.state = self._automaton.state |
bfb81992 | 626 | |
a0acc08c | 627 | def _analysis_tick_cb(self, **kwargs): |
b6d9132b AB |
628 | begin_ns = kwargs['begin_ns'] |
629 | end_ns = kwargs['end_ns'] | |
630 | ||
a0acc08c PP |
631 | self._analysis_tick(begin_ns, end_ns) |
632 | self._ticks += 1 | |
b6d9132b | 633 | |
a0acc08c | 634 | def _analysis_tick(self, begin_ns, end_ns): |
b6d9132b | 635 | raise NotImplementedError() |