def set_irq_columns(self, iter, irq, irq_info, nics): new_value = [None] * self.nr_columns users = tuna.get_irq_users(self.irqs, irq, nics) if self.has_threaded_irqs: irq_re = tuna.threaded_irq_re(irq) pids = self.ps.find_by_regex(irq_re) if pids: pid = pids[0] prio = int(self.ps[pid]["stat"]["rt_priority"]) sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] else: sched = "" pid = -1 prio = -1 new_value[self.COL_PID] = pid new_value[self.COL_POL] = sched new_value[self.COL_PRI] = prio new_value[self.COL_NUM] = irq new_value[self.COL_AFF] = tuna.get_irq_affinity_text(self.irqs, irq) new_value[self.COL_EVENTS] = reduce(lambda a, b: a + b, irq_info["cpu"]) new_value[self.COL_USERS] = ",".join(users) gui.set_store_columns(self.list_store, iter, new_value)
def ps_show_thread(self, pid, affect_children, ps, has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups): try: affinity = self.format_affinity(schedutils.get_affinity(pid)) except ( SystemError, OSError ) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return raise e sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] rtprio = int(ps[pid]["stat"]["rt_priority"]) cgout = ps[pid]["cgroups"] cmd = ps[pid]["stat"]["comm"] users = "" if tuna.is_irq_thread(cmd): try: if not self.irqs: self.irqs = procfs.interrupts() if cmd[:4] == "IRQ-": users = self.irqs[tuna.irq_thread_number(cmd)]["users"] for u in users: if u in self.get_nics(): users[users.index( u)] = "%s(%s)" % (u, ethtool.get_module(u)) users = ",".join(users) else: u = cmd[cmd.find('-') + 1:] if u in self.get_nics(): users = ethtool.get_module(u) except: users = "Not found in /proc/interrupts!" ctxt_switch_info = "" if has_ctxt_switch_info: voluntary_ctxt_switches = int( ps[pid]["status"]["voluntary_ctxt_switches"]) nonvoluntary_ctxt_switches = int( ps[pid]["status"]["nonvoluntary_ctxt_switches"]) ctxt_switch_info = " %9d %12s" % (voluntary_ctxt_switches, nonvoluntary_ctxt_switches) if affect_children: print " %-5d " % pid, else: print " %-5d" % pid, print "%6s %5d %8s%s %15s %s" % (sched, rtprio, affinity, ctxt_switch_info, cmd, users), if cgroups: print " %9s" % cgout, print "" if sock_inodes: self.ps_show_sockets(pid, ps, sock_inodes, sock_inode_re, affect_children and 3 or 4) if affect_children and ps[pid].has_key("threads"): for tid in ps[pid]["threads"].keys(): self.ps_show_thread(tid, False, ps[pid]["threads"], has_ctxt_switch_info, sock_inodes, sock_inode_re)
def _set_rt(self, pid, sched, prio): sched_str = schedutils.schedstr(sched) log.debug("Setting scheduler policy to '%s' and priority to '%d' of PID '%d'." % (sched_str, prio, pid)) try: prio_min = schedutils.get_priority_min(sched) prio_max = schedutils.get_priority_max(sched) if prio < prio_min or prio > prio_max: log.error("Priority for %s must be in range %d - %d. '%d' was given." % (sched_str, prio_min, prio_max, prio)) # Workaround for old (pre-0.4) python-schedutils which raised # SystemError instead of OSError except (SystemError, OSError) as e: log.error("Failed to get allowed priority range: %s" % e) try: schedutils.set_scheduler(pid, sched, prio) except (SystemError, OSError) as e: if hasattr(e, "errno") and e.errno == errno.ESRCH: log.debug("Failed to set scheduling parameters of PID %d, the task vanished." % pid) else: log.error("Failed to set scheduling parameters of PID %d: %s" % (pid, e))
def _get_rt(self, pid): scheduler = schedutils.get_scheduler(pid) sched_str = schedutils.schedstr(scheduler) priority = schedutils.get_priority(pid) log.debug("Read scheduler policy '%s' and priority '%d' of PID '%d'" % (sched_str, priority, pid)) return (scheduler, priority)
def show_settings(pid): policy = schedutils.get_scheduler(pid) spolicy = schedutils.schedstr(policy) rtprio = schedutils.get_priority(pid) reset_on_fork = "" if policy & schedutils.SCHED_RESET_ON_FORK: reset_on_fork = "|SCHED_RESET_ON_FORK" print '''pid %d's current scheduling policy: %s%s pid %d's current scheduling priority: %d''' % (pid, spolicy, reset_on_fork, pid, rtprio)
def ps_show_thread(pid, affect_children, ps, has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups): global irqs try: affinity = format_affinity(schedutils.get_affinity(pid)) except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return raise e sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] rtprio = int(ps[pid]["stat"]["rt_priority"]) cgout = ps[pid]["cgroups"] cmd = ps[pid]["stat"]["comm"] users = "" if tuna.is_irq_thread(cmd): try: if not irqs: irqs = procfs.interrupts() if cmd[:4] == "IRQ-": users = irqs[tuna.irq_thread_number(cmd)]["users"] for u in users: if u in get_nics(): users[users.index(u)] = "%s(%s)" % (u, ethtool.get_module(u)) users = ",".join(users) else: u = cmd[cmd.find('-') + 1:] if u in get_nics(): users = ethtool.get_module(u) except: users = "Not found in /proc/interrupts!" ctxt_switch_info = "" if has_ctxt_switch_info: voluntary_ctxt_switches = int(ps[pid]["status"]["voluntary_ctxt_switches"]) nonvoluntary_ctxt_switches = int(ps[pid]["status"]["nonvoluntary_ctxt_switches"]) ctxt_switch_info = " %9d %12s" % (voluntary_ctxt_switches, nonvoluntary_ctxt_switches) if affect_children: print " %-5d " % pid, else: print " %-5d" % pid, print "%6s %5d %8s%s %15s %s" % (sched, rtprio, affinity, ctxt_switch_info, cmd, users), if cgroups: print " %9s" % cgout, print "" if sock_inodes: ps_show_sockets(pid, ps, sock_inodes, sock_inode_re, affect_children and 3 or 4) if affect_children and ps[pid].has_key("threads"): for tid in ps[pid]["threads"].keys(): ps_show_thread(tid, False, ps[pid]["threads"], has_ctxt_switch_info, sock_inodes, sock_inode_re, cgroups)
def create_policy_model(self, policy): (COL_TEXT, COL_SCHED) = range(2) list_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_UINT) renderer = gtk.CellRendererText() policy.pack_start(renderer, True) policy.add_attribute(renderer, "text", COL_TEXT) for pol in range(4): row = list_store.append() list_store.set(row, COL_TEXT, schedutils.schedstr(pol), COL_SCHED, pol) policy.set_model(list_store)
def create_policy_model(self, policy): ( COL_TEXT, COL_SCHED ) = range(2) list_store = gtk.ListStore(gobject.TYPE_STRING, gobject.TYPE_UINT) renderer = gtk.CellRendererText() policy.pack_start(renderer, True) policy.add_attribute(renderer, "text", COL_TEXT) for pol in range(4): row = list_store.append() list_store.set(row, COL_TEXT, schedutils.schedstr(pol), COL_SCHED, pol) policy.set_model(list_store)
def show(ps, cpuinfo, irqs): ps_list = [] for pid in ps.keys(): if schedutils.get_scheduler(pid) == 0: continue ps_list.append(pid) ps_list.sort() nics = ethtool.get_active_devices() for pid in ps_list: thread_affinity_list = schedutils.get_affinity(pid) if len(thread_affinity_list) <= 4: thread_affinity = ",".join(str(a) for a in thread_affinity_list) else: thread_affinity = ",".join( str(hex(a)) for a in procfs.hexbitmask( schedutils.get_affinity(pid), cpuinfo.nr_cpus)) sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] rtprio = int(ps[pid]["stat"]["rt_priority"]) cmd = ps[pid]["stat"]["comm"] users = "" if cmd[:4] == "IRQ-": try: users = irqs[cmd[4:]]["users"] for u in users: if u in nics: users[users.index( u)] = "%s(%s)" % (u, ethtool.get_module(u)) users = ",".join(users) except: users = "Not found in /proc/interrupts!" try: voluntary_ctxt_switches = int( ps[pid]["status"]["voluntary_ctxt_switches"]) nonvoluntary_ctxt_switches = int( ps[pid]["status"]["nonvoluntary_ctxt_switches"]) except: voluntary_ctxt_switches = -1 nonvoluntary_ctxt_switches = -1 print "%5d %6s %5d %8s %9d %12s %15s %s" % ( pid, sched, rtprio, thread_affinity, voluntary_ctxt_switches, nonvoluntary_ctxt_switches, cmd, users)
def set_thread_columns(self, iter, tid, thread_info): new_value = [ None ] * self.nr_columns new_value[self.COL_PRI] = int(thread_info["stat"]["rt_priority"]) new_value[self.COL_POL] = schedutils.schedstr(schedutils.get_scheduler(tid))[6:] thread_affinity_list = schedutils.get_affinity(tid) new_value[self.COL_PID] = tid new_value[self.COL_AFF] = tuna.list_to_cpustring(thread_affinity_list) try: new_value[self.COL_VOLCTXT] = int(thread_info["status"]["voluntary_ctxt_switches"]) new_value[self.COL_NONVOLCTXT] = int(thread_info["status"]["nonvoluntary_ctxt_switches"]) new_value[self.COL_CGROUP] = thread_info["cgroups"] except: pass new_value[self.COL_CMDLINE] = procfs.process_cmdline(thread_info) gui.set_store_columns(self.tree_store, iter, new_value)
def show(ps, cpuinfo, irqs): ps_list = [] for pid in ps.keys(): if schedutils.get_scheduler(pid) == 0: continue ps_list.append(pid) ps_list.sort() nics = ethtool.get_active_devices() for pid in ps_list: thread_affinity_list = schedutils.get_affinity(pid) if len(thread_affinity_list) <= 4: thread_affinity = ",".join(str(a) for a in thread_affinity_list) else: thread_affinity = ",".join(str(hex(a)) for a in procfs.hexbitmask(schedutils.get_affinity(pid), cpuinfo.nr_cpus)) sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] rtprio = int(ps[pid]["stat"]["rt_priority"]) cmd = ps[pid]["stat"]["comm"] users = "" if cmd[:4] == "IRQ-": try: users = irqs[cmd[4:]]["users"] for u in users: if u in nics: users[users.index(u)] = "%s(%s)" % (u, ethtool.get_module(u)) users = ",".join(users) except: users = "Not found in /proc/interrupts!" try: voluntary_ctxt_switches = int(ps[pid]["status"]["voluntary_ctxt_switches"]) nonvoluntary_ctxt_switches = int(ps[pid]["status"]["nonvoluntary_ctxt_switches"]) except: voluntary_ctxt_switches = -1 nonvoluntary_ctxt_switches = -1 print "%5d %6s %5d %8s %9d %12s %15s %s" % (pid, sched, rtprio, thread_affinity, voluntary_ctxt_switches, nonvoluntary_ctxt_switches, cmd, users)
def set_thread_columns(self, iter, tid, thread_info): new_value = [None] * self.nr_columns new_value[self.COL_PRI] = int(thread_info["stat"]["rt_priority"]) new_value[self.COL_POL] = schedutils.schedstr( schedutils.get_scheduler(tid))[6:] thread_affinity_list = schedutils.get_affinity(tid) new_value[self.COL_PID] = tid new_value[self.COL_AFF] = tuna.list_to_cpustring(thread_affinity_list) try: new_value[self.COL_VOLCTXT] = int( thread_info["status"]["voluntary_ctxt_switches"]) new_value[self.COL_NONVOLCTXT] = int( thread_info["status"]["nonvoluntary_ctxt_switches"]) new_value[self.COL_CGROUP] = thread_info["cgroups"] except: pass new_value[self.COL_CMDLINE] = procfs.process_cmdline(thread_info) gui.set_store_columns(self.tree_store, iter, new_value)
def set_irq_columns(self, iter, irq, irq_info, nics): new_value = [ None ] * self.nr_columns users = tuna.get_irq_users(self.irqs, irq, nics) if self.has_threaded_irqs: irq_re = tuna.threaded_irq_re(irq) pids = self.ps.find_by_regex(irq_re) if pids: pid = pids[0] prio = int(self.ps[pid]["stat"]["rt_priority"]) sched = schedutils.schedstr(schedutils.get_scheduler(pid))[6:] else: sched = "" pid = -1 prio = -1 new_value[self.COL_PID] = pid new_value[self.COL_POL] = sched new_value[self.COL_PRI] = prio new_value[self.COL_NUM] = irq new_value[self.COL_AFF] = tuna.get_irq_affinity_text(self.irqs, irq) new_value[self.COL_EVENTS] = reduce(lambda a, b: a + b, irq_info["cpu"]) new_value[self.COL_USERS] = ",".join(users) gui.set_store_columns(self.list_store, iter, new_value)
def show_priority_limits(policy): print "%-32.32s: %d/%d" % ("%s min/max priority" % schedutils.schedstr(policy), schedutils.get_priority_min(policy), schedutils.get_priority_max(policy))
def generate_rtgroups(filename, kthreads, nr_cpus): f = file(filename, "w") f.write('''# Generated by tuna # # Use it with rtctl: # # rtctl --file %s reset # # Please use 'man rtctl' for more operations # # Associate processes into named groups with default priority and # scheduling policy. # # Format is: <groupname>:<sched>:<prio>:<regex> # # groupname must start at beginning of line. # sched must be one of: 'f' (fifo) # 'b' (batch) # 'r' (round-robin) # 'o' (other) # '*' (leave alone) # regex is an awk regex # # The regex is matched against process names as printed by "ps -eo cmd". ''' % filename) f.write("kthreads:*:1:*:\[.*\]$\n\n") per_cpu_kthreads = [] names = kthreads.keys() names.sort() for name in names: kt = kthreads[name] try: idx = name.index("/") common = name[:idx] if common in per_cpu_kthreads: continue per_cpu_kthreads.append(common) name = common if common[:5] == "sirq-": common = "(sirq|softirq)" + common[4:] elif common[:8] == "softirq-": common = "(sirq|softirq)" + common[7:] name = "s" + name[4:] regex = common + "\/.*" except: idx = 0 regex = name pass if kt.percpu or idx != 0 or name == "posix_cpu_timer": # Don't mess with workqueues, etc # posix_cpu_timer is too long and doesn't # have PF_THREAD_BOUND in its per process # flags... mask = "*" else: mask = ",".join([hex(a) for a in \ procfs.hexbitmask(kt.affinity, nr_cpus)]) f.write("%s:%c:%d:%s:\[%s\]$\n" % (name, schedutils.schedstr(kt.policy)[6].lower(), kt.rtprio, mask, regex)) f.close()
def thread_set_attributes(pid_info, new_policy, new_prio, new_affinity, nr_cpus): pid = pid_info.pid changed = False curr_policy = schedutils.get_scheduler(pid) curr_prio = int(pid_info["stat"]["rt_priority"]) if new_policy == schedutils.SCHED_OTHER: new_prio = 0 if curr_policy != new_policy or curr_prio != new_prio: try: schedutils.set_scheduler(pid, new_policy, new_prio) except: dialog = gtk.MessageDialog( None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, _("Invalid parameters!")) dialog.run() dialog.destroy() return False curr_policy = schedutils.get_scheduler(pid) if curr_policy != new_policy: print _("couldn't change pid %(pid)d from %(cpol)s(%(cpri)d) to %(npol)s(%(npri)d)!") % \ { 'pid': pid, 'cpol': schedutils.schedstr(curr_policy), 'cpri': curr_prio, 'npol': schedutils.schedstr(new_policy), 'npri': new_prio} else: changed = True try: curr_affinity = schedutils.get_affinity(pid) except ( SystemError, OSError ) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return False raise e try: new_affinity = [int(a) for a in new_affinity.split(",")] except: try: new_affinity = tuna.cpustring_to_list(new_affinity) except: new_affinity = procfs.bitmasklist(new_affinity, nr_cpus) new_affinity.sort() if curr_affinity != new_affinity: try: schedutils.set_affinity(pid, new_affinity) except: return invalid_affinity() try: curr_affinity = schedutils.get_affinity(pid) except ( SystemError, OSError ) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return False raise e if curr_affinity != new_affinity: print _("couldn't change pid %(pid)d from %(caff)s to %(naff)s!") % \ { 'pid':pid, 'caff':curr_affinity, 'naff':new_affinity } else: changed = True return changed
def thread_set_attributes(pid_info, new_policy, new_prio, new_affinity, nr_cpus): pid = pid_info.pid changed = False curr_policy = schedutils.get_scheduler(pid) curr_prio = int(pid_info["stat"]["rt_priority"]) if new_policy == schedutils.SCHED_OTHER: new_prio = 0 if curr_policy != new_policy or curr_prio != new_prio: try: schedutils.set_scheduler(pid, new_policy, new_prio) except: dialog = gtk.MessageDialog(None, gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_WARNING, gtk.BUTTONS_OK, _("Invalid parameters!")) dialog.run() dialog.destroy() return False curr_policy = schedutils.get_scheduler(pid) if curr_policy != new_policy: print _("couldn't change pid %(pid)d from %(cpol)s(%(cpri)d) to %(npol)s(%(npri)d)!") % \ { 'pid': pid, 'cpol': schedutils.schedstr(curr_policy), 'cpri': curr_prio, 'npol': schedutils.schedstr(new_policy), 'npri': new_prio} else: changed = True try: curr_affinity = schedutils.get_affinity(pid) except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return False raise e try: new_affinity = [ int(a) for a in new_affinity.split(",") ] except: try: new_affinity = tuna.cpustring_to_list(new_affinity) except: new_affinity = procfs.bitmasklist(new_affinity, nr_cpus) new_affinity.sort() if curr_affinity != new_affinity: try: schedutils.set_affinity(pid, new_affinity) except: return invalid_affinity() try: curr_affinity = schedutils.get_affinity(pid) except (SystemError, OSError) as e: # (3, 'No such process') old python-schedutils incorrectly raised SystemError if e[0] == 3: return False raise e if curr_affinity != new_affinity: print _("couldn't change pid %(pid)d from %(caff)s to %(naff)s!") % \ { 'pid':pid, 'caff':curr_affinity, 'naff':new_affinity } else: changed = True return changed