iotopソース分析2
import ctypes
import fnmatch
import os
import platform
import time
IOPRIO_GET_ARCH_SYSCALL = [
('alpha', '*', 443),
('i*86', '*', 290),
('ia64*', '*', 1275),
('ppc*', '*', 274),
('s390*', '*', 283),
('sparc*', '*', 218),
('sh*', '*', 289),
('x86_64*', '32bit', 290),
('x86_64*', '64bit', 252),
]
IOPRIO_SET_ARCH_SYSCALL = [
('alpha', '*', 442),
('i*86', '*', 289),
('ia64*', '*', 1274),
('ppc*', '*', 273),
('s390*', '*', 282),
('sparc*', '*', 196),
('sh*', '*', 288),
('x86_64*', '32bit', 289),
('x86_64*', '64bit', 251),
]
# CPU
def find_ioprio_syscall_number(syscall_list):
arch = os.uname()[4]
bits = platform.architecture()[0]
for candidate_arch, candidate_bits, syscall_nr in syscall_list:
if fnmatch.fnmatch(arch,candidate_arch) and fnmatch.fnmatch(bits,candidate_bits):
return syscall_nr
class IoprioSetError(Exception):
def __init__(self,err):
try:
self.err = os.strerror(err)
except TypeError:
self.err = err
__NR_ioprio_get = find_ioprio_syscall_number(IOPRIO_GET_ARCH_SYSCALL)
__NR_ioprio_set = find_ioprio_syscall_number(IOPRIO_SET_ARCH_SYSCALL)
#print __NR_ioprio_get,__NR_ioprio_set
try:
ctypes_handle = ctypes.CDLL(None, user_errno=True)
except TypeError:
ctypes_handle = ctypes.CDLL(None)
syscall = ctypes_handle.syscall
# I/O
PRIORITY_CLASS = [None, 'rt', 'be', 'idle']
IOPRIO_WHO_PROCESS = 1
IOPRIO_CLASS_SHIFT = 13
IOPRIO_PRIO_MASK = (1 <> IOPRIO_CLASS_SHIFT]
def ioprio_data(ioprio):
return ioprio & IOPRIO_PRIO_MASK
sched_getscheduler = ctypes_handle.sched_getscheduler
SCHED_OTHER, SCHED_FIFO, SCHED_RR, SCHED_BATCH, SCHED_ISO, SCHED_IDLE = range(6)
getpriority = ctypes_handle.getpriority
PRIO_PROCESS = 0
# PID I/O
def get_ioprio_from_sched(pid):
scheduler = sched_getscheduler(pid)
nice = getpriority(PRIO_PROCESS,pid)
ioprio_nice = (nice + 20)/5
if scheduler in (SCHED_FIFO,SCHED_RR):
return 'rt/%d' % ioprio_nice
elif scheduler == SCHED_IDLE:
return 'idle'
else:
return 'be/%d' % ioprio_nice
def get(pid):
if __NR_ioprio_get is None:
return '?sys'
ioprio = syscall(__NR_ioprio_get, IOPRIO_WHO_PROCESS, pid)
if ioprio
:
$ python ioprio.py
pid: 11628
ioprio: be/4
$ python ioprio.py 32600
pid: 32600
ioprio: be/4
このプログラムはLinuxのI/Oスケジューリングとプロセススケジューリングに する