3 # The MIT License (MIT)
5 # Copyright (C) 2015 - Julien Desfossez <jdesfossez@efficios.com>
6 # 2015 - Antoine Busque <abusque@efficios.com>
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 from .command
import Command
27 from ..core
import cputop
28 from ..ascii_graph
import Pyasciigraph
32 class Cputop(Command
):
33 _DESC
= """The cputop command."""
36 super().__init
__(self
._add
_arguments
, enable_proc_filter_args
=True)
38 def _validate_transform_args(self
):
42 # parse arguments first
44 # validate, transform and save specific arguments
45 self
._validate
_transform
_args
()
48 # create the appropriate analysis/analyses
49 self
._create
_analysis
()
51 self
._run
_analysis
(self
._reset
_total
, self
._refresh
)
55 self
._print
_results
(self
.start_ns
, self
.trace_end_ts
)
59 def _create_analysis(self
):
60 self
._analysis
= cputop
.Cputop(self
.state
)
62 def _compute_stats(self
):
63 self
._analysis
.compute_stats(self
.start_ns
, self
.end_ns
)
65 def _reset_total(self
, start_ts
):
66 self
._analysis
.reset(start_ts
)
68 def _refresh(self
, begin
, end
):
70 self
._print
_results
(begin
, end
)
71 self
._reset
_total
(end
)
73 def _filter_process(self
, proc
):
78 if self
._arg
_proc
_list
and proc
.comm
not in self
._arg
_proc
_list
:
83 def _print_results(self
, begin_ns
, end_ns
):
84 self
._print
_date
(begin_ns
, end_ns
)
85 self
._print
_per
_tid
_usage
()
86 self
._print
_per
_cpu
_usage
()
87 self
._print
_total
_cpu
_usage
()
89 def _print_per_tid_usage(self
):
91 limit
= self
._arg
_limit
92 graph
= Pyasciigraph()
95 for tid
in sorted(self
._analysis
.tids
.values(),
96 key
=operator
.attrgetter('usage_percent'),
98 if not self
._filter
_process
(tid
):
101 output_str
= '%s (%d)' % (tid
.comm
, tid
.tid
)
102 if tid
.migrate_count
> 0:
103 output_str
+= ', %d migrations' % (tid
.migrate_count
)
105 values
.append((output_str
, tid
.usage_percent
))
108 if limit
> 0 and count
>= limit
:
111 for line
in graph
.graph('Per-TID CPU Usage', values
, unit
=' %'):
114 def _print_per_cpu_usage(self
):
115 graph
= Pyasciigraph()
118 for cpu
in sorted(self
._analysis
.cpus
.values(),
119 key
=operator
.attrgetter('usage_percent'),
121 values
.append(('CPU %d' % cpu
.cpu_id
, cpu
.usage_percent
))
123 for line
in graph
.graph('Per-CPU Usage', values
, unit
=' %'):
126 def _print_total_cpu_usage(self
):
127 cpu_count
= len(self
.state
.cpus
)
130 for cpu
in sorted(self
._analysis
.cpus
.values(),
131 key
=operator
.attrgetter('usage_percent'),
133 usage_percent
+= cpu
.usage_percent
136 usage_percent
/= cpu_count
137 print('\nTotal CPU Usage: %0.02f%%\n' % usage_percent
)
139 def _add_arguments(self
, ap
):
This page took 0.034335 seconds and 5 git commands to generate.