Cleanup license (MIT)
[deliverable/lttng-analyses.git] / lttnganalysescli / lttnganalysescli / syscallstats.py
1 #!/usr/bin/env python3
2 #
3 # The MIT License (MIT)
4 #
5 # Copyright (C) 2015 - Julien Desfossez <jdesfosez@efficios.com>
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
25 from .command import Command
26 import lttnganalyses.syscalls
27 from linuxautomaton import common
28 import operator
29
30
31 class SyscallsAnalysis(Command):
32 _VERSION = '0.1.0'
33 _DESC = """The I/O command."""
34
35 def __init__(self):
36 super().__init__(self._add_arguments,
37 enable_proc_filter_args=True)
38 # enable_max_min_args=True,
39 # enable_max_min_size_arg=True,
40 # enable_freq_arg=True,
41 # enable_log_arg=True,
42 # enable_stats_arg=True)
43
44 def _validate_transform_args(self):
45 pass
46
47 def run(self):
48 # parse arguments first
49 self._parse_args()
50 # validate, transform and save specific arguments
51 self._validate_transform_args()
52 # open the trace
53 self._open_trace()
54 # create the appropriate analysis/analyses
55 self._create_analysis()
56 # run the analysis
57 self._run_analysis(self._reset_total, self._refresh)
58 # process the results
59 self._compute_stats()
60 # print results
61 self._print_results(self.start_ns, self.trace_end_ts, final=1)
62 # close the trace
63 self._close_trace()
64
65 def _create_analysis(self):
66 self._analysis = lttnganalyses.syscalls.SyscallsAnalysis(
67 self._automaton.state)
68
69 def _compute_stats(self):
70 self.state = self._automaton.state
71 pass
72
73 def _refresh(self, begin, end):
74 self._compute_stats()
75 self._print_results(begin, end, final=0)
76 self._reset_total(end)
77
78 def filter_process(self, proc):
79 if self._arg_proc_list and proc.comm not in self._arg_proc_list:
80 return False
81 if self._arg_pid_list and str(proc.pid) not in self._arg_pid_list:
82 return False
83 return True
84
85 def _print_results(self, begin_ns, end_ns, final=0):
86 count = 0
87 limit = self._arg_limit
88 print('Timerange: [%s, %s]' % (
89 common.ns_to_hour_nsec(begin_ns, gmt=self._arg_gmt,
90 multi_day=True),
91 common.ns_to_hour_nsec(end_ns, gmt=self._arg_gmt,
92 multi_day=True)))
93 print("Per-TID syscalls usage")
94 for tid in sorted(self.state.tids.values(),
95 key=operator.attrgetter('total_syscalls'),
96 reverse=True):
97 if not self.filter_process(tid):
98 continue
99 print("%s (%d), %d syscalls:" % (tid.comm, tid.pid,
100 tid.total_syscalls))
101 for syscall in sorted(tid.syscalls.values(),
102 key=operator.attrgetter('count'),
103 reverse=True):
104 print("- %s : %d" % (syscall.name, syscall.count))
105 count = count + 1
106 if limit > 0 and count >= limit:
107 break
108 print("")
109
110 print("\nTotal syscalls: %d" % (self.state.syscalls["total"]))
111
112 def _reset_total(self, start_ts):
113 pass
114
115 def _add_arguments(self, ap):
116 # specific argument
117 pass
118
119
120 # entry point
121 def run():
122 # create command
123 syscallscmd = SyscallsAnalysis()
124
125 # execute command
126 syscallscmd.run()
This page took 0.035623 seconds and 5 git commands to generate.