Improve the message that is printed when Linux taskstats are not found
[iotop.git] / iotop / ioprio.py
blobcc139c21e8e904ee523e4646db3f8b90360e3c7a
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 import ctypes
20 import fnmatch
21 import os
22 import platform
24 # From https://git.kernel.org/?p=utils/util-linux-ng/util-linux-ng.git;a=blob;
25 # f=configure.ac;h=770eb45ae85d32757fc3cff1d70a7808a627f9f7;hb=HEAD#l354
26 # i386 bit userspace under an x86_64 kernel will have its uname() appear as
27 # 'x86_64' but it will use the i386 syscall number, that's why we consider both
28 # the architecture name and the word size.
29 IOPRIO_GET_ARCH_SYSCALL = [
30 ('alpha', '*', 443),
31 ('arm*', '*', 315),
32 ('i*86', '*', 290),
33 ('ia64*', '*', 1275),
34 ('mips*', '32bit', 4315),
35 ('mips*', '64bit', 5274),
36 ('parisc*', '*', 268),
37 ('powerpc*', '*', 274),
38 ('s390*', '*', 283),
39 ('sparc*', '*', 218),
40 ('sh*', '*', 289),
41 ('x86_64*', '32bit', 290),
42 ('x86_64*', '64bit', 252),
45 IOPRIO_SET_ARCH_SYSCALL = [
46 ('alpha', '*', 442),
47 ('arm*', '*', 314),
48 ('i*86', '*', 289),
49 ('ia64*', '*', 1274),
50 ('mips*', '32bit', 4314),
51 ('mips*', '64bit', 5273),
52 ('parisc*', '*', 267),
53 ('powerpc*', '*', 273),
54 ('s390*', '*', 282),
55 ('sparc*', '*', 196),
56 ('sh*', '*', 288),
57 ('x86_64*', '32bit', 289),
58 ('x86_64*', '64bit', 251),
62 def find_ioprio_syscall_number(syscall_list):
63 arch = os.uname()[4]
64 bits = platform.architecture()[0]
66 for candidate_arch, candidate_bits, syscall_nr in syscall_list:
67 if fnmatch.fnmatch(arch, candidate_arch) and \
68 fnmatch.fnmatch(bits, candidate_bits):
69 return syscall_nr
72 class IoprioSetError(Exception):
73 def __init__(self, err):
74 try:
75 self.err = os.strerror(err)
76 except TypeError:
77 self.err = err
79 __NR_ioprio_get = find_ioprio_syscall_number(IOPRIO_GET_ARCH_SYSCALL)
80 __NR_ioprio_set = find_ioprio_syscall_number(IOPRIO_SET_ARCH_SYSCALL)
82 try:
83 ctypes_handle = ctypes.CDLL(None, use_errno=True)
84 except TypeError:
85 ctypes_handle = ctypes.CDLL(None)
87 syscall = ctypes_handle.syscall
89 PRIORITY_CLASSES = [None, 'rt', 'be', 'idle']
91 IOPRIO_WHO_PROCESS = 1
92 IOPRIO_CLASS_SHIFT = 13
93 IOPRIO_PRIO_MASK = (1 << IOPRIO_CLASS_SHIFT) - 1
96 def ioprio_value(ioprio_class, ioprio_data):
97 try:
98 ioprio_class = PRIORITY_CLASSES.index(ioprio_class)
99 except ValueError:
100 ioprio_class = PRIORITY_CLASSES.index(None)
101 return (ioprio_class << IOPRIO_CLASS_SHIFT) | ioprio_data
104 def ioprio_class(ioprio):
105 return PRIORITY_CLASSES[ioprio >> IOPRIO_CLASS_SHIFT]
108 def ioprio_data(ioprio):
109 return ioprio & IOPRIO_PRIO_MASK
111 sched_getscheduler = ctypes_handle.sched_getscheduler
112 SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, SCHED_IDLE = \
113 range(6)
115 getpriority = ctypes_handle.getpriority
116 PRIO_PROCESS = 0
119 def get_ioprio_from_sched(pid):
120 scheduler = sched_getscheduler(pid)
121 nice = getpriority(PRIO_PROCESS, pid)
122 ioprio_nice = (nice + 20) / 5
124 if scheduler in (SCHED_FIFO, SCHED_RR):
125 return 'rt/%d' % ioprio_nice
126 elif scheduler == SCHED_IDLE:
127 return 'idle'
128 else:
129 return 'be/%d' % ioprio_nice
132 def get(pid):
133 if __NR_ioprio_get is None:
134 return '?sys'
136 ioprio = syscall(__NR_ioprio_get, IOPRIO_WHO_PROCESS, pid)
137 if ioprio < 0:
138 return '?err'
140 prio_class = ioprio_class(ioprio)
141 if not prio_class:
142 return get_ioprio_from_sched(pid)
143 if prio_class == 'idle':
144 return prio_class
145 return '%s/%d' % (prio_class, ioprio_data(ioprio))
148 def set_ioprio(which, who, ioprio_class, ioprio_data):
149 if __NR_ioprio_set is None:
150 raise IoprioSetError('No ioprio_set syscall found')
152 ioprio_val = ioprio_value(ioprio_class, ioprio_data)
153 ret = syscall(__NR_ioprio_set, which, who, ioprio_val, use_errno=True)
154 if ret < 0:
155 try:
156 err = ctypes.get_errno()
157 except AttributeError:
158 err = \
159 'Unknown error (errno support not available before Python2.6)'
160 raise IoprioSetError(err)
163 def sort_key(key):
164 if key[0] == '?':
165 return -ord(key[1])
167 if '/' in key:
168 if key.startswith('rt/'):
169 shift = 0
170 elif key.startswith('be/'):
171 shift = 1
172 prio = int(key.split('/')[1])
173 elif key == 'idle':
174 shift = 2
175 prio = 0
177 return (1 << (shift * IOPRIO_CLASS_SHIFT)) + prio
180 def to_class_and_data(ioprio_str):
181 if '/' in ioprio_str:
182 split = ioprio_str.split('/')
183 return (split[0], int(split[1]))
184 elif ioprio_str == 'idle':
185 return ('idle', 0)
186 return (None, None)
188 if __name__ == '__main__':
189 import sys
190 if len(sys.argv) == 2:
191 pid = int(sys.argv[1])
192 else:
193 pid = os.getpid()
194 print('pid:', pid)
195 print('ioprio:', get(pid))