3535@author: Stijn De Weirdt (Ghent University)
3636"""
3737import ctypes
38+ import logging
3839import os
3940from ctypes .util import find_library
40- from vsc .utils .fancylogger import getLogger
41-
42- _logger = getLogger ("affinity" )
4341
4442_libc_lib = find_library ('c' )
4543_libc = ctypes .cdll .LoadLibrary (_libc_lib )
@@ -113,7 +111,6 @@ class cpu_set_t(ctypes.Structure):
113111
114112 def __init__ (self , * args , ** kwargs ):
115113 super (cpu_set_t , self ).__init__ (* args , ** kwargs )
116- self .log = getLogger (self .__class__ .__name__ )
117114 self .cpus = None
118115
119116 def __str__ (self ):
@@ -128,14 +125,17 @@ def convert_hr_bits(self, txt):
128125
129126 # sanity check
130127 if indices [1 ] < indices [0 ]:
131- self .log .raiseException ("convert_hr_bits: end is lower then start in '%s'" % rng )
128+ logging .error ("convert_hr_bits: end is lower then start in '%s'" , rng )
129+ raise Exception ("convert_hr_bits: end is lower then start" )
132130 elif indices [0 ] < 0 :
133- self .log .raiseException ("convert_hr_bits: negative start in '%s'" % rng )
131+ logging .error ("convert_hr_bits: negative start in '%s'" , rng )
132+ raise Exception ("convert_hr_bits: negative start" )
134133 elif indices [1 ] > CPU_SETSIZE + 1 : # also covers start, since end > start
135- self .log .raiseException ("convert_hr_bits: end larger then max %s in '%s'" % (CPU_SETSIZE , rng ))
134+ logging .error ("convert_hr_bits: end larger then max %s in '%s'" , CPU_SETSIZE , rng )
135+ raise Exception ("convert_hr_bits: end larger then max" )
136136
137137 self .cpus [indices [0 ]:indices [1 ] + 1 ] = [1 ] * (indices [1 ] + 1 - indices [0 ])
138- self . log . debug ("convert_hr_bits: converted %s into cpus %s" % ( txt , self .cpus ) )
138+ logging . debug ("convert_hr_bits: converted %s into cpus %s" , txt , self .cpus )
139139
140140 def convert_bits_hr (self ):
141141 """Convert __bits into human readable text"""
@@ -168,8 +168,8 @@ def set_cpus(self, cpus_list):
168168 """Given list, set it as cpus"""
169169 nr_cpus = len (cpus_list )
170170 if nr_cpus > CPU_SETSIZE :
171- self . log . warning ("set_cpus: length cpu list %s is larger then cpusetsize %s. Truncating to cpusetsize" %
172- ( nr_cpus , CPU_SETSIZE ) )
171+ logging . warning ("set_cpus: length cpu list %s is larger then cpusetsize %s. Truncating to cpusetsize" ,
172+ nr_cpus , CPU_SETSIZE )
173173 cpus_list = cpus_list [:CPU_SETSIZE ]
174174 elif nr_cpus < CPU_SETSIZE :
175175 cpus_list .extend ([0 ] * (CPU_SETSIZE - nr_cpus ))
@@ -188,11 +188,12 @@ def set_bits(self, cpus=None):
188188 __bits [idx ] = cpu_mask_t (sum (cpus ))
189189 # sanity check
190190 if prev_cpus == self .get_cpus ():
191- self . log . debug ("set_bits: new set to %s" % self .convert_bits_hr ())
191+ logging . debug ("set_bits: new set to %s" , self .convert_bits_hr ())
192192 else :
193193 # get_cpus() rescans
194- self .log .raiseException ("set_bits: something went wrong: previous cpus %s; current ones %s" %
195- (prev_cpus [:20 ], self .cpus [:20 ]))
194+ logging .error ("set_bits: something went wrong: previous cpus %s; current ones %s" ,
195+ prev_cpus [:20 ], self .cpus [:20 ])
196+ raise Exception ("set_bits: something went wrong: previous cpus / current ones" )
196197
197198 def str_cpus (self ):
198199 """Return a string representation of the cpus"""
@@ -234,9 +235,9 @@ def sched_getaffinity(cs=None, pid=None):
234235
235236 ec = _libc .sched_getaffinity (pid_t (pid ), ctypes .sizeof (cpu_set_t ), ctypes .pointer (cs ))
236237 if ec == 0 :
237- _logger .debug ("sched_getaffinity for pid %s returned cpuset %s" % ( pid , cs ) )
238+ logging .debug ("sched_getaffinity for pid %s returned cpuset %s" , pid , cs )
238239 else :
239- _logger .error ("sched_getaffinity failed for pid %s ec %s" % ( pid , ec ) )
240+ logging .error ("sched_getaffinity failed for pid %s ec %s" , pid , ec )
240241 return cs
241242
242243
@@ -250,9 +251,9 @@ def sched_setaffinity(cs, pid=None):
250251
251252 ec = _libc .sched_setaffinity (pid_t (pid ), ctypes .sizeof (cpu_set_t ), ctypes .pointer (cs ))
252253 if ec == 0 :
253- _logger .debug ("sched_setaffinity for pid %s and cpuset %s" % ( pid , cs ) )
254+ logging .debug ("sched_setaffinity for pid %s and cpuset %s" , pid , cs )
254255 else :
255- _logger .error ("sched_setaffinity failed for pid %s cpuset %s ec %s" % ( pid , cs , ec ) )
256+ logging .error ("sched_setaffinity failed for pid %s cpuset %s ec %s" , pid , cs , ec )
256257
257258
258259# /* Get index of currently used CPU. */
@@ -290,13 +291,14 @@ def getpriority(which=None, who=None):
290291 if which is None :
291292 which = PRIO_PROCESS
292293 elif which not in (PRIO_PROCESS , PRIO_PGRP , PRIO_USER ,):
293- _logger .raiseException ("getpriority: which %s not in correct range" % which )
294+ logging .error ("getpriority: which %s not in correct range" , which )
295+ raise Exception ("getpriority: which not in correct range" )
294296 if who is None :
295297 who = 0 # current which-ever
296298 prio = _libc .getpriority (priority_which_t (which ),
297299 id_t (who ),
298300 )
299- _logger .debug ("getpriority prio %s for which %s who %s" % ( prio , which , who ) )
301+ logging .debug ("getpriority prio %s for which %s who %s" , prio , which , who )
300302
301303 return prio
302304
@@ -315,23 +317,26 @@ def setpriority(prio, which=None, who=None):
315317 if which is None :
316318 which = PRIO_PROCESS
317319 elif which not in (PRIO_PROCESS , PRIO_PGRP , PRIO_USER ,):
318- _logger .raiseException ("setpriority: which %s not in correct range" % which )
320+ logging .error ("setpriority: which %s not in correct range" , which )
321+ raise Exception ("setpriority: which not in correct range" )
319322 if who is None :
320323 who = 0 # current which-ever
321324
322325 try :
323326 prio = int (prio )
324327 except ValueError :
325- _logger .raiseException ("setpriority: failed to convert priority %s into int" % prio )
328+ logging .error ("setpriority: failed to convert priority %s into int" , prio )
329+ raise Exception ("setpriority: failed to convert priority into int" )
326330
327331 if prio < PRIO_MIN or prio > PRIO_MAX :
328- _logger .raiseException ("setpriority: prio not in allowed range MIN %s MAX %s" % (PRIO_MIN , PRIO_MAX ))
332+ logging .error ("setpriority: prio not in allowed range MIN %s MAX %s" , PRIO_MIN , PRIO_MAX )
333+ raise Exception ("setpriority: prio not in allowed range MIN MAX" )
329334
330335 ec = _libc .setpriority (priority_which_t (which ),
331336 id_t (who ),
332337 ctypes .c_int (prio )
333338 )
334339 if ec == 0 :
335- _logger .debug ("setpriority for which %s who %s prio %s" % ( which , who , prio ) )
340+ logging .debug ("setpriority for which %s who %s prio %s" , which , who , prio )
336341 else :
337- _logger .error ("setpriority failed for which %s who %s prio %s" % ( which , who , prio ) )
342+ logging .error ("setpriority failed for which %s who %s prio %s" , which , who , prio )
0 commit comments