New code structure
[deliverable/lttng-analyses.git] / lttnganalysescli / lttnganalysescli / iotop.py
1 from .command import Command
2 import lttnganalyses.iotop
3 import random
4
5
6 class Iotop(Command):
7 _VERSION = '1.2.3'
8 _DESC = """The iotop command blabla bla.
9 It also does this and that."""
10
11 def __init__(self):
12 super().__init__(self._add_arguments, True)
13
14 def _validate_transform_args(self):
15 # split?
16 self._arg_split_count = None
17 self._arg_split = self._args.split
18
19 # split count if splitting?
20 if self._arg_split:
21 if self._args.split_count is None:
22 self._cmdline_error('you must specify --split-count '
23 'with --split')
24 else:
25 self._arg_split_count = self._args.split_count
26
27 # path
28 self._arg_path = self._args.path
29
30 def run(self):
31 # parse arguments first
32 self._parse_args()
33
34 # validate, transform and save specific arguments
35 self._validate_transform_args()
36
37 # everything processed at this point
38 print('begin: {}'.format(self._arg_begin))
39 print('end: {}'.format(self._arg_end))
40 print('uuu: {}'.format(self._arg_uuu))
41 print('split: {}'.format(self._arg_split))
42 print('split-count: {}'.format(self._arg_split_count))
43 print('path: {}'.format(self._arg_path))
44 print('---')
45
46 # create the appropriate analysis/analyses
47 self._create_analysis()
48
49 # run the analysis
50 self._run_analysis()
51
52 # print results
53 self._print_results()
54
55 def _create_analysis(self):
56 self._analysis = lttnganalyses.iotop.Iotop(self._automaton.state,
57 self._arg_split,
58 self._arg_split_count)
59
60 def _run_analysis(self):
61 # event (demo)
62 class Event:
63 def __init__(self):
64 self.cond = bool(random.getrandbits(1))
65 self.name = 'hello'
66
67 # loop of events here
68 for i in range(random.randint(5000, 10000)):
69 ev = Event()
70
71 # feed automaton
72 self._automaton.process_event(ev)
73
74 # feed analysis
75 self._analysis.process_event(ev)
76
77 def _print_results(self):
78 print('event count: {}'.format(self._analysis.event_count))
79 print('buckets:')
80
81 for index, count in enumerate(self._analysis.buckets):
82 print(' {:04d}: {}'.format(index, count))
83
84 def _add_arguments(self, ap):
85 # specific argument
86 ap.add_argument('-s', '--split', action='store_true', help='split')
87 ap.add_argument('-c', '--split-count', type=int, help='split count')
88
89 # could be a common argument too
90 ap.add_argument('path', help='trace path')
91
92
93 # entry point
94 def run():
95 # create command
96 iotopcmd = Iotop()
97
98 # execute command
99 iotopcmd.run()
This page took 0.032968 seconds and 5 git commands to generate.