Port to Python 3
[iotop.git] / iotop / ioprio.py
blobafe605403746805afc0a535c56e9273dc4c39b1f
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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),
57 def find_ioprio_syscall_number(syscall_list):
58 arch = os.uname()[4]
59 bits = platform.architecture()[0]
61 for candidate_arch, candidate_bits, syscall_nr in syscall_list:
62 if fnmatch.fnmatch(arch, candidate_arch) and \
63 fnmatch.fnmatch(bits, candidate_bits):
64 return syscall_nr
66 class IoprioSetError(Exception):
67 def __init__(self, err):
68 try:
69 self.err = os.strerror(err)
70 except TypeError:
71 self.err = err
73 __NR_ioprio_get = find_ioprio_syscall_number(IOPRIO_GET_ARCH_SYSCALL)
74 __NR_ioprio_set = find_ioprio_syscall_number(IOPRIO_SET_ARCH_SYSCALL)
76 try:
77 ctypes_handle = ctypes.CDLL(None, use_errno=True)
78 except TypeError:
79 ctypes_handle = ctypes.CDLL(None)
81 syscall = ctypes_handle.syscall
83 PRIORITY_CLASSES = [None, 'rt', 'be', 'idle']
85 IOPRIO_WHO_PROCESS = 1
86 IOPRIO_CLASS_SHIFT = 13
87 IOPRIO_PRIO_MASK = (1 << IOPRIO_CLASS_SHIFT) - 1
89 def ioprio_value(ioprio_class, ioprio_data):
90 try:
91 ioprio_class = PRIORITY_CLASSES.index(ioprio_class)
92 except ValueError:
93 ioprio_class = PRIORITY_CLASSES.index(None)
94 return (ioprio_class << IOPRIO_CLASS_SHIFT) | ioprio_data
96 def ioprio_class(ioprio):
97 return PRIORITY_CLASSES[ioprio >> IOPRIO_CLASS_SHIFT]
99 def ioprio_data(ioprio):
100 return ioprio & IOPRIO_PRIO_MASK
102 sched_getscheduler = ctypes_handle.sched_getscheduler
103 SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, SCHED_IDLE = range(6)
105 getpriority = ctypes_handle.getpriority
106 PRIO_PROCESS = 0
108 def get_ioprio_from_sched(pid):
109 scheduler = sched_getscheduler(pid)
110 nice = getpriority(PRIO_PROCESS, pid)
111 ioprio_nice = (nice + 20) / 5
113 if scheduler in (SCHED_FIFO, SCHED_RR):
114 return 'rt/%d' % ioprio_nice
115 elif scheduler == SCHED_IDLE:
116 return 'idle'
117 else:
118 return 'be/%d' % ioprio_nice
120 def get(pid):
121 if __NR_ioprio_get is None:
122 return '?sys'
124 ioprio = syscall(__NR_ioprio_get, IOPRIO_WHO_PROCESS, pid)
125 if ioprio < 0:
126 return '?err'
128 prio_class = ioprio_class(ioprio)
129 if not prio_class:
130 return get_ioprio_from_sched(pid)
131 if prio_class == 'idle':
132 return prio_class
133 return '%s/%d' % (prio_class, ioprio_data(ioprio))
135 def set_ioprio(which, who, ioprio_class, ioprio_data):
136 if __NR_ioprio_set is None:
137 raise IoprioSetError('No ioprio_set syscall found')
139 ioprio_val = ioprio_value(ioprio_class, ioprio_data)
140 ret = syscall(__NR_ioprio_set, which, who, ioprio_val, use_errno=True)
141 if ret < 0:
142 try:
143 err = ctypes.get_errno()
144 except AttributeError:
145 err = 'Unknown error (errno support not available before Python2.6)'
146 raise IoprioSetError(err)
148 def sort_key(key):
149 if key[0] == '?':
150 return -ord(key[1])
152 if '/' in key:
153 if key.startswith('rt/'):
154 shift = 0
155 elif key.startswith('be/'):
156 shift = 1
157 prio = int(key.split('/')[1])
158 elif key == 'idle':
159 shift = 2
160 prio = 0
162 return (1 << (shift * IOPRIO_CLASS_SHIFT)) + prio
164 def to_class_and_data(ioprio_str):
165 if '/' in ioprio_str:
166 split = ioprio_str.split('/')
167 return (split[0], int(split[1]))
168 elif ioprio_str == 'idle':
169 return ('idle', 0)
170 return (None, None)
172 if __name__ == '__main__':
173 import sys
174 if len(sys.argv) == 2:
175 pid = int(sys.argv[1])
176 else:
177 pid = os.getpid()
178 print('pid:', pid)
179 print('ioprio:', get(pid))