Make pep8 happy.
[iotop.git] / iotop / ioprio.py
blob07d7c523f5731f5c4c08fd34b3340c9c734fc3d1
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 http://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 ('parisc*', '*', 268),
35 ('powerpc*', '*', 274),
36 ('s390*', '*', 283),
37 ('sparc*', '*', 218),
38 ('sh*', '*', 289),
39 ('x86_64*', '32bit', 290),
40 ('x86_64*', '64bit', 252),
43 IOPRIO_SET_ARCH_SYSCALL = [
44 ('alpha', '*', 442),
45 ('arm*', '*', 314),
46 ('i*86', '*', 289),
47 ('ia64*', '*', 1274),
48 ('parisc*', '*', 267),
49 ('powerpc*', '*', 273),
50 ('s390*', '*', 282),
51 ('sparc*', '*', 196),
52 ('sh*', '*', 288),
53 ('x86_64*', '32bit', 289),
54 ('x86_64*', '64bit', 251),
58 def find_ioprio_syscall_number(syscall_list):
59 arch = os.uname()[4]
60 bits = platform.architecture()[0]
62 for candidate_arch, candidate_bits, syscall_nr in syscall_list:
63 if fnmatch.fnmatch(arch, candidate_arch) and \
64 fnmatch.fnmatch(bits, candidate_bits):
65 return syscall_nr
68 class IoprioSetError(Exception):
69 def __init__(self, err):
70 try:
71 self.err = os.strerror(err)
72 except TypeError:
73 self.err = err
75 __NR_ioprio_get = find_ioprio_syscall_number(IOPRIO_GET_ARCH_SYSCALL)
76 __NR_ioprio_set = find_ioprio_syscall_number(IOPRIO_SET_ARCH_SYSCALL)
78 try:
79 ctypes_handle = ctypes.CDLL(None, use_errno=True)
80 except TypeError:
81 ctypes_handle = ctypes.CDLL(None)
83 syscall = ctypes_handle.syscall
85 PRIORITY_CLASSES = [None, 'rt', 'be', 'idle']
87 IOPRIO_WHO_PROCESS = 1
88 IOPRIO_CLASS_SHIFT = 13
89 IOPRIO_PRIO_MASK = (1 << IOPRIO_CLASS_SHIFT) - 1
92 def ioprio_value(ioprio_class, ioprio_data):
93 try:
94 ioprio_class = PRIORITY_CLASSES.index(ioprio_class)
95 except ValueError:
96 ioprio_class = PRIORITY_CLASSES.index(None)
97 return (ioprio_class << IOPRIO_CLASS_SHIFT) | ioprio_data
100 def ioprio_class(ioprio):
101 return PRIORITY_CLASSES[ioprio >> IOPRIO_CLASS_SHIFT]
104 def ioprio_data(ioprio):
105 return ioprio & IOPRIO_PRIO_MASK
107 sched_getscheduler = ctypes_handle.sched_getscheduler
108 SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, SCHED_IDLE = \
109 range(6)
111 getpriority = ctypes_handle.getpriority
112 PRIO_PROCESS = 0
115 def get_ioprio_from_sched(pid):
116 scheduler = sched_getscheduler(pid)
117 nice = getpriority(PRIO_PROCESS, pid)
118 ioprio_nice = (nice + 20) / 5
120 if scheduler in (SCHED_FIFO, SCHED_RR):
121 return 'rt/%d' % ioprio_nice
122 elif scheduler == SCHED_IDLE:
123 return 'idle'
124 else:
125 return 'be/%d' % ioprio_nice
128 def get(pid):
129 if __NR_ioprio_get is None:
130 return '?sys'
132 ioprio = syscall(__NR_ioprio_get, IOPRIO_WHO_PROCESS, pid)
133 if ioprio < 0:
134 return '?err'
136 prio_class = ioprio_class(ioprio)
137 if not prio_class:
138 return get_ioprio_from_sched(pid)
139 if prio_class == 'idle':
140 return prio_class
141 return '%s/%d' % (prio_class, ioprio_data(ioprio))
144 def set_ioprio(which, who, ioprio_class, ioprio_data):
145 if __NR_ioprio_set is None:
146 raise IoprioSetError('No ioprio_set syscall found')
148 ioprio_val = ioprio_value(ioprio_class, ioprio_data)
149 ret = syscall(__NR_ioprio_set, which, who, ioprio_val, use_errno=True)
150 if ret < 0:
151 try:
152 err = ctypes.get_errno()
153 except AttributeError:
154 err = \
155 'Unknown error (errno support not available before Python2.6)'
156 raise IoprioSetError(err)
159 def sort_key(key):
160 if key[0] == '?':
161 return -ord(key[1])
163 if '/' in key:
164 if key.startswith('rt/'):
165 shift = 0
166 elif key.startswith('be/'):
167 shift = 1
168 prio = int(key.split('/')[1])
169 elif key == 'idle':
170 shift = 2
171 prio = 0
173 return (1 << (shift * IOPRIO_CLASS_SHIFT)) + prio
176 def to_class_and_data(ioprio_str):
177 if '/' in ioprio_str:
178 split = ioprio_str.split('/')
179 return (split[0], int(split[1]))
180 elif ioprio_str == 'idle':
181 return ('idle', 0)
182 return (None, None)
184 if __name__ == '__main__':
185 import sys
186 if len(sys.argv) == 2:
187 pid = int(sys.argv[1])
188 else:
189 pid = os.getpid()
190 print('pid:', pid)
191 print('ioprio:', get(pid))