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>
25 # From http://git.kernel.org/?p=utils/util-linux-ng/util-linux-ng.git;a=blob;
26 # f=configure.ac;h=770eb45ae85d32757fc3cff1d70a7808a627f9f7;hb=HEAD#l354
27 # i386 bit userspace under an x86_64 kernel will have its uname() appear as
28 # 'x86_64' but it will use the i386 syscall number, that's why we consider both
29 # the architecture name and the word size.
30 IOPRIO_GET_ARCH_SYSCALL
= [
35 ('parisc*', '*', 268),
36 ('powerpc*', '*', 274),
40 ('x86_64*', '32bit', 290),
41 ('x86_64*', '64bit', 252),
44 IOPRIO_SET_ARCH_SYSCALL
= [
49 ('parisc*', '*', 267),
50 ('powerpc*', '*', 273),
54 ('x86_64*', '32bit', 289),
55 ('x86_64*', '64bit', 251),
58 def find_ioprio_syscall_number(syscall_list
):
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
):
67 class IoprioSetError(Exception):
68 def __init__(self
, err
):
70 self
.err
= os
.strerror(err
)
74 __NR_ioprio_get
= find_ioprio_syscall_number(IOPRIO_GET_ARCH_SYSCALL
)
75 __NR_ioprio_set
= find_ioprio_syscall_number(IOPRIO_SET_ARCH_SYSCALL
)
78 ctypes_handle
= ctypes
.CDLL(None, use_errno
=True)
80 ctypes_handle
= ctypes
.CDLL(None)
82 syscall
= ctypes_handle
.syscall
84 PRIORITY_CLASSES
= [None, 'rt', 'be', 'idle']
86 IOPRIO_WHO_PROCESS
= 1
87 IOPRIO_CLASS_SHIFT
= 13
88 IOPRIO_PRIO_MASK
= (1 << IOPRIO_CLASS_SHIFT
) - 1
90 def ioprio_value(ioprio_class
, ioprio_data
):
92 ioprio_class
= PRIORITY_CLASSES
.index(ioprio_class
)
94 ioprio_class
= PRIORITY_CLASSES
.index(None)
95 return (ioprio_class
<< IOPRIO_CLASS_SHIFT
) | ioprio_data
97 def ioprio_class(ioprio
):
98 return PRIORITY_CLASSES
[ioprio
>> IOPRIO_CLASS_SHIFT
]
100 def ioprio_data(ioprio
):
101 return ioprio
& IOPRIO_PRIO_MASK
103 sched_getscheduler
= ctypes_handle
.sched_getscheduler
104 SCHED_OTHER
, SCHED_FIFO
, SCHED_RR
, SCHED_BATCH
, SCHED_ISO
, SCHED_IDLE
= range(6)
106 getpriority
= ctypes_handle
.getpriority
109 def get_ioprio_from_sched(pid
):
110 scheduler
= sched_getscheduler(pid
)
111 nice
= getpriority(PRIO_PROCESS
, pid
)
112 ioprio_nice
= (nice
+ 20) / 5
114 if scheduler
in (SCHED_FIFO
, SCHED_RR
):
115 return 'rt/%d' % ioprio_nice
116 elif scheduler
== SCHED_IDLE
:
119 return 'be/%d' % ioprio_nice
122 if __NR_ioprio_get
is None:
125 ioprio
= syscall(__NR_ioprio_get
, IOPRIO_WHO_PROCESS
, pid
)
129 prio_class
= ioprio_class(ioprio
)
131 return get_ioprio_from_sched(pid
)
132 if prio_class
== 'idle':
134 return '%s/%d' % (prio_class
, ioprio_data(ioprio
))
136 def set_ioprio(which
, who
, ioprio_class
, ioprio_data
):
137 if __NR_ioprio_set
is None:
138 raise IoprioSetError('No ioprio_set syscall found')
140 ioprio_val
= ioprio_value(ioprio_class
, ioprio_data
)
141 ret
= syscall(__NR_ioprio_set
, which
, who
, ioprio_val
, use_errno
=True)
144 err
= ctypes
.get_errno()
145 except AttributeError:
146 err
= 'Unknown error (errno support not available before Python2.6)'
147 raise IoprioSetError(err
)
154 if key
.startswith('rt/'):
156 elif key
.startswith('be/'):
158 prio
= int(key
.split('/')[1])
163 return (1 << (shift
* IOPRIO_CLASS_SHIFT
)) + prio
165 def to_class_and_data(ioprio_str
):
166 if '/' in ioprio_str
:
167 split
= ioprio_str
.split('/')
168 return (split
[0], int(split
[1]))
169 elif ioprio_str
== 'idle':
173 if __name__
== '__main__':
175 if len(sys
.argv
) == 2:
176 pid
= int(sys
.argv
[1])
180 print 'ioprio:', get(pid
)