1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
15 # See the COPYING file for license information.
17 # Copyright (c) 2007 Guillaume Chazarain <guichaz@gmail.com>
19 # Allow printing with same syntax in Python 2/3
20 from __future__
import print_function
32 # Check for requirements:
33 # o Linux >= 2.6.20 with I/O accounting and VM event counters
36 ioaccounting
= os
.path
.exists('/proc/self/io')
39 from iotop
.vmstat
import VmStat
42 vm_event_counters
= False
44 vm_event_counters
= True
46 if not ioaccounting
or not vm_event_counters
:
47 print('Could not run iotop as some of the requirements are not met:')
48 print('- Linux >= 2.6.20 with')
50 print(' - I/O accounting support ' \
51 '(CONFIG_TASKSTATS, CONFIG_TASK_DELAY_ACCT, ' \
52 'CONFIG_TASK_IO_ACCOUNTING)')
53 if not vm_event_counters
:
54 print(' - VM event counters (CONFIG_VM_EVENT_COUNTERS)')
57 from iotop
import ioprio
, vmstat
58 from iotop
.netlink
import Connection
, NETLINK_GENERIC
, U32Attr
, NLM_F_REQUEST
59 from iotop
.genetlink
import Controller
, GeNlMessage
62 class DumpableObject(object):
63 """Base class for objects that allows easy introspection when printed"""
65 return '%s: %s>' % (str(type(self
))[:-1],
66 pprint
.pformat(self
.__dict
__))
70 # Interesting fields in a taskstats output
73 class Stats(DumpableObject
):
75 ('blkio_delay_total', 40),
76 ('swapin_delay_total', 56),
79 ('cancelled_write_bytes', 264)
82 has_blkio_delay_total
= False
84 def __init__(self
, task_stats_buffer
):
86 for name
, offset
in Stats
.members_offsets
:
87 data
= task_stats_buffer
[offset
:offset
+ 8]
88 sd
[name
] = struct
.unpack('Q', data
)[0]
90 # This is a heuristic to detect if CONFIG_TASK_DELAY_ACCT is enabled in
92 if not Stats
.has_blkio_delay_total
:
93 Stats
.has_blkio_delay_total
= self
.blkio_delay_total
!= 0
95 def accumulate(self
, other_stats
, destination
, coeff
=1):
96 """Update destination from operator(self, other_stats)"""
97 dd
= destination
.__dict
__
99 od
= other_stats
.__dict
__
100 for member
, offset
in Stats
.members_offsets
:
101 dd
[member
] = sd
[member
] + coeff
* od
[member
]
103 def delta(self
, other_stats
, destination
):
104 """Update destination with self - other_stats"""
105 return self
.accumulate(other_stats
, destination
, coeff
=-1)
107 def is_all_zero(self
):
109 for name
, offset
in Stats
.members_offsets
:
115 def build_all_zero():
116 stats
= Stats
.__new
__(Stats
)
118 for name
, offset
in Stats
.members_offsets
:
123 # Netlink usage for taskstats
126 TASKSTATS_CMD_GET
= 1
127 TASKSTATS_CMD_ATTR_PID
= 1
128 TASKSTATS_TYPE_AGGR_PID
= 4
129 TASKSTATS_TYPE_PID
= 1
130 TASKSTATS_TYPE_STATS
= 3
133 class TaskStatsNetlink(object):
134 # Keep in sync with format_stats() and pinfo.did_some_io()
136 def __init__(self
, options
):
137 self
.options
= options
138 self
.connection
= Connection(NETLINK_GENERIC
)
139 controller
= Controller(self
.connection
)
140 self
.family_id
= controller
.get_family_id('TASKSTATS')
142 def build_request(self
, tid
):
143 return GeNlMessage(self
.family_id
, cmd
=TASKSTATS_CMD_GET
,
144 attrs
=[U32Attr(TASKSTATS_CMD_ATTR_PID
, tid
)],
147 def get_single_task_stats(self
, thread
):
148 thread
.task_stats_request
.send(self
.connection
)
150 reply
= GeNlMessage
.recv(self
.connection
)
152 if e
.errno
== errno
.ESRCH
:
153 # OSError: Netlink error: No such process (3)
156 for attr_type
, attr_value
in reply
.attrs
.items():
157 if attr_type
== TASKSTATS_TYPE_AGGR_PID
:
158 reply
= attr_value
.nested()
162 taskstats_data
= reply
[TASKSTATS_TYPE_STATS
].data
163 if len(taskstats_data
) < 272:
166 taskstats_version
= struct
.unpack('H', taskstats_data
[:2])[0]
167 assert taskstats_version
>= 4
168 return Stats(taskstats_data
)
175 def find_uids(options
):
176 """Build options.uids from options.users by resolving usernames to UIDs"""
179 for u
in options
.users
or []:
184 passwd
= pwd
.getpwnam(u
)
186 print('Unknown user:', u
, file=sys
.stderr
)
191 options
.uids
.append(uid
)
196 def parse_proc_pid_status(pid
):
199 for line
in open('/proc/%d/status' % pid
):
200 key
, value
= line
.split(':\t', 1)
201 result_dict
[key
] = value
.strip()
203 pass # No such process
207 def safe_utf8_decode(s
):
209 return s
.decode('utf-8')
210 except UnicodeDecodeError:
211 return s
.encode('string_escape')
212 except AttributeError:
216 class ThreadInfo(DumpableObject
):
217 """Stats for a single thread"""
218 def __init__(self
, tid
, taskstats_connection
):
221 self
.stats_total
= None
222 self
.stats_delta
= Stats
.__new
__(Stats
)
223 self
.task_stats_request
= taskstats_connection
.build_request(tid
)
225 def get_ioprio(self
):
226 return ioprio
.get(self
.tid
)
228 def set_ioprio(self
, ioprio_class
, ioprio_data
):
229 return ioprio
.set_ioprio(ioprio
.IOPRIO_WHO_PROCESS
, self
.tid
,
230 ioprio_class
, ioprio_data
)
232 def update_stats(self
, stats
):
233 if not self
.stats_total
:
234 self
.stats_total
= stats
235 stats
.delta(self
.stats_total
, self
.stats_delta
)
236 self
.stats_total
= stats
239 class ProcessInfo(DumpableObject
):
240 """Stats for a single process (a single line in the output): if
241 options.processes is set, it is a collection of threads, otherwise a single
243 def __init__(self
, pid
):
247 self
.threads
= {} # {tid: ThreadInfo}
248 self
.stats_delta
= Stats
.build_all_zero()
249 self
.stats_accum
= Stats
.build_all_zero()
250 self
.stats_accum_timestamp
= time
.time()
252 def is_monitored(self
, options
):
253 if (options
.pids
and not options
.processes
and
254 self
.pid
not in options
.pids
):
255 # We only monitor some threads, not this one
258 if options
.uids
and self
.get_uid() not in options
.uids
:
259 # We only monitor some users, not this one
267 # uid in (None, 0) means either we don't know the UID yet or the
268 # process runs as root so it can change its UID. In both cases it means
269 # we have to find out its current UID.
271 uid
= os
.stat('/proc/%d' % self
.pid
)[stat
.ST_UID
]
273 # The process disappeared
276 # Maybe the process called setuid()
283 if uid
is not None and not self
.user
:
285 self
.user
= safe_utf8_decode(pwd
.getpwuid(uid
).pw_name
)
286 except (KeyError, AttributeError):
288 return self
.user
or '{none}'
290 def get_cmdline(self
):
291 # A process may exec, so we must always reread its cmdline
293 proc_cmdline
= open('/proc/%d/cmdline' % self
.pid
)
294 cmdline
= proc_cmdline
.read(4096)
296 return '{no such process}'
297 proc_status
= parse_proc_pid_status(self
.pid
)
299 # Probably a kernel thread, get its name from /proc/PID/status
300 proc_status_name
= proc_status
.get('Name', '')
302 proc_status_name
= '[%s]' % proc_status_name
304 proc_status_name
= '{no name}'
305 return proc_status_name
307 tgid
= int(proc_status
.get('Tgid', self
.pid
))
309 # Not the main thread, maybe it has a custom name
310 tgid_name
= parse_proc_pid_status(tgid
).get('Name', '')
311 thread_name
= proc_status
.get('Name', '')
312 if thread_name
!= tgid_name
:
313 suffix
+= ' [%s]' % thread_name
314 parts
= cmdline
.split('\0')
315 if parts
[0].startswith('/'):
316 first_command_char
= parts
[0].rfind('/') + 1
317 parts
[0] = parts
[0][first_command_char
:]
318 cmdline
= ' '.join(parts
).strip()
319 return safe_utf8_decode(cmdline
+ suffix
)
321 def did_some_io(self
, accumulated
):
323 return not self
.stats_accum
.is_all_zero()
324 for t
in self
.threads
.values():
325 if not t
.stats_delta
.is_all_zero():
329 def get_ioprio(self
):
330 priorities
= set(t
.get_ioprio() for t
in self
.threads
.values())
331 if len(priorities
) == 1:
332 return priorities
.pop()
335 def set_ioprio(self
, ioprio_class
, ioprio_data
):
336 for thread
in self
.threads
.values():
337 thread
.set_ioprio(ioprio_class
, ioprio_data
)
339 def ioprio_sort_key(self
):
340 return ioprio
.sort_key(self
.get_ioprio())
342 def get_thread(self
, tid
, taskstats_connection
):
343 thread
= self
.threads
.get(tid
, None)
345 thread
= ThreadInfo(tid
, taskstats_connection
)
346 self
.threads
[tid
] = thread
349 def update_stats(self
):
350 stats_delta
= Stats
.build_all_zero()
351 for tid
, thread
in self
.threads
.items():
353 stats_delta
.accumulate(thread
.stats_delta
, stats_delta
)
354 self
.threads
= dict([(tid
, thread
) for tid
, thread
in
355 self
.threads
.items() if not thread
.mark
])
357 nr_threads
= len(self
.threads
)
361 stats_delta
.blkio_delay_total
/= nr_threads
362 stats_delta
.swapin_delay_total
/= nr_threads
364 self
.stats_delta
= stats_delta
365 self
.stats_accum
.accumulate(self
.stats_delta
, self
.stats_accum
)
370 class ProcessList(DumpableObject
):
371 def __init__(self
, taskstats_connection
, options
):
374 self
.taskstats_connection
= taskstats_connection
375 self
.options
= options
376 self
.timestamp
= time
.time()
377 self
.vmstat
= vmstat
.VmStat()
379 # A first time as we are interested in the delta
380 self
.update_process_counts()
382 def get_process(self
, pid
):
383 """Either get the specified PID from self.processes or build a new
384 ProcessInfo if we see this PID for the first time"""
385 process
= self
.processes
.get(pid
, None)
387 process
= ProcessInfo(pid
)
388 self
.processes
[pid
] = process
390 if process
.is_monitored(self
.options
):
393 def list_tgids(self
):
394 if self
.options
.pids
:
395 return self
.options
.pids
397 tgids
= os
.listdir('/proc')
398 if self
.options
.processes
:
399 return [int(tgid
) for tgid
in tgids
if '0' <= tgid
[0] <= '9']
403 if '0' <= tgid
[0] <= '9':
406 os
.listdir('/proc/' + tgid
+ '/task')))
412 def list_tids(self
, tgid
):
413 if not self
.options
.processes
:
417 tids
= list(map(int, os
.listdir('/proc/%d/task' % tgid
)))
421 if self
.options
.pids
:
422 tids
= list(set(self
.options
.pids
).intersection(set(tids
)))
426 def update_process_counts(self
):
427 new_timestamp
= time
.time()
428 self
.duration
= new_timestamp
- self
.timestamp
429 self
.timestamp
= new_timestamp
431 total_read
= total_write
= 0
433 for tgid
in self
.list_tgids():
434 process
= self
.get_process(tgid
)
437 for tid
in self
.list_tids(tgid
):
438 thread
= process
.get_thread(tid
, self
.taskstats_connection
)
439 stats
= self
.taskstats_connection
.get_single_task_stats(thread
)
441 thread
.update_stats(stats
)
442 delta
= thread
.stats_delta
443 total_read
+= delta
.read_bytes
444 total_write
+= delta
.write_bytes
446 return (total_read
, total_write
), self
.vmstat
.delta()
448 def refresh_processes(self
):
449 for process
in self
.processes
.values():
450 for thread
in process
.threads
.values():
453 total_read_and_write
= self
.update_process_counts()
455 self
.processes
= dict([(pid
, process
) for pid
, process
in
456 self
.processes
.items() if
457 process
.update_stats()])
459 return total_read_and_write