diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2008-08-27 20:05:32 -0300 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2008-08-27 20:05:32 -0300 |
commit | 6149d67139731c06d26ff121d70ee322da9f8ec7 (patch) | |
tree | d9c1aaa0b6c4553a632c2bd6c06da0635ea842a5 | |
parent | 39cff62c6bbc41b3d1818a70a413b5522513c2c4 (diff) | |
download | python-schedutils-6149d67139731c06d26ff121d70ee322da9f8ec7.tar.gz |
examples: Introduce pchrt and ptaskset to test the bindingsv0.2
Should be a clone of chrt and taskset, in util-linux.
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
-rw-r--r-- | MANIFEST | 2 | ||||
-rwxr-xr-x | pchrt.py | 107 | ||||
-rwxr-xr-x | ptaskset.py | 162 | ||||
-rw-r--r-- | rpm/SPECS/python-schedutils.spec | 21 |
4 files changed, 288 insertions, 4 deletions
@@ -1,4 +1,6 @@ COPYING +pchrt.py +ptaskset.py setup.py rpm/SPECS/python-schedutils.spec MANIFEST diff --git a/pchrt.py b/pchrt.py new file mode 100755 index 0000000..b8eab24 --- /dev/null +++ b/pchrt.py @@ -0,0 +1,107 @@ +#! /usr/bin/python +# -*- python -*- +# -*- coding: utf-8 -*- +# Copyright (C) 2008 Red Hat Inc. +# +# Arnaldo Carvalho de Melo <acme@redhat.com> +# +# This application is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2. +# +# This application is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +import os, schedutils, sys + +def usage(): + print '''pchrt (python-schedutils) +usage: chrt [options] [prio] [pid | cmd [args...]] +manipulate real-time attributes of a process + -b, --batch set policy to SCHED_BATCH + -f, --fifo set policy to SCHED_FIFO + -p, --pid operate on existing given pid + -m, --max show min and max valid priorities + -o, --other set policy to SCHED_OTHER + -r, --rr set policy to SCHED_RR (default) + -h, --help display this help + +You must give a priority if changing policy. + +Report bugs and send patches to <acme@ghostprotocols.net>''' + return + +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 show_all_priority_limits(): + for policy in (schedutils.SCHED_OTHER, schedutils.SCHED_FIFO, + schedutils.SCHED_RR, schedutils.SCHED_BATCH): + show_priority_limits(policy) + +def show_settings(pid): + policy = schedutils.get_scheduler(pid) + spolicy = schedutils.schedstr(policy) + rtprio = schedutils.get_priority(pid) + print '''pid %d's current scheduling policy: %s +pid %d's current scheduling priority: %d''' % (pid, spolicy, pid, rtprio) + +def change_settings(pid, policy, rtprio): + try: + schedutils.set_scheduler(pid, policy, rtprio) + except SystemError, err: + print "sched_setscheduler: %s" % err[1] + print "failed to set pid %d's policy" % pid + +def main(): + + args = sys.argv[1:] + if not args: + usage() + return + + policy = schedutils.SCHED_RR + while True: + o = args.pop(0) + try: + priority = int(o) + break + except: + pass + + if o in ("-h", "--help"): + usage() + return + elif o in ("-b", "--batch"): + policy = schedutils.SCHED_BATCH + elif o in ("-f", "--fifo"): + policy = schedutils.SCHED_FIFO + elif o in ("-m", "--max"): + show_all_priority_limits() + return + elif o in ("-o", "--other"): + policy = schedutils.SCHED_OTHER + elif o in ("-r", "--rr"): + policy = schedutils.SCHED_RR + elif o in ("-p", "--pid"): + if len(args) > 1: + priority = int(args.pop(0)) + pid = int(args.pop(0)) + change_settings(pid, policy, priority) + else: + pid = int(args.pop(0)) + show_settings(pid) + return + else: + usage() + return + + schedutils.set_scheduler(0, policy, priority) + os.execvp(args[0], args) + +if __name__ == '__main__': + main() diff --git a/ptaskset.py b/ptaskset.py new file mode 100755 index 0000000..15f0ec5 --- /dev/null +++ b/ptaskset.py @@ -0,0 +1,162 @@ +#! /usr/bin/python +# -*- python -*- +# -*- coding: utf-8 -*- +# Copyright (C) 2008 Red Hat Inc. +# +# Arnaldo Carvalho de Melo <acme@redhat.com> +# +# This application is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; version 2. +# +# This application is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. + +import os, schedutils, sys + +def usage(): + print '''ptaskset (python-schedutils) +usage: taskset [options] [mask | cpu-list] [pid | cmd [args...]] +set or get the affinity of a process + + -p, --pid operate on existing given pid + -c, --cpu-list display and specify cpus in list format + -h, --help display this help''' + + return + +def hexbitmask(l): + hexbitmask = [] + bit = 0 + mask = 0 + nr_entries = 1 << max(l) + for entry in range(nr_entries): + if entry in l: + mask |= (1 << bit) + bit += 1 + if bit == 32: + bit = 0 + hexbitmask.insert(0, mask) + mask = 0 + + if bit < 32 and mask != 0: + hexbitmask.insert(0, mask) + + return hexbitmask + +def find_last_bit(n, wordsize = 32): + bit = wordsize - 1 + while bit != 0: + if n & (1 << bit): + return bit + bit -= 1 + return 0 + +def bitmasklist(line): + fields = line.strip().split(",") + bitmasklist = [] + while int(fields[0], 16) == 0: + fields.pop(0) + + if not fields: + return [] + + nr_entries = (len(fields) - 1) * 32 + find_last_bit(int(fields[0], 16)) + 1 + + entry = 0 + for i in range(len(fields) - 1, -1, -1): + mask = int(fields[i], 16) + while mask != 0: + if mask & 1: + bitmasklist.append(entry) + mask >>= 1 + entry += 1 + if entry == nr_entries: + break + if entry == nr_entries: + break + return bitmasklist + +def cpustring_to_list(cpustr): + """Convert a string of numbers to an integer list. + + Given a string of comma-separated numbers and number ranges, + return a simple sorted list of the integers it represents. + + This function will throw exceptions for badly-formatted strings. + + Returns a list of integers.""" + + fields = cpustr.strip().split(",") + cpu_list = [] + for field in fields: + ends = field.split("-") + if len(ends) > 2: + raise "Syntax error" + if len(ends) == 2: + cpu_list += range(int(ends[0]), int(ends[1])+1) + else: + cpu_list += [int(ends[0])] + return list(set(cpu_list)) + +def show_settings(pid, when, cpu_list_mode): + affinity = schedutils.get_affinity(pid) + if cpu_list_mode: + mask = ",".join([str(a) for a in affinity]) + else: + mask = ",".join(["%x" % a for a in hexbitmask(affinity)]) + print "pid %d's %s affinity mask: %s" % (pid, when, mask) + +def change_settings(pid, affinity, cpu_list_mode): + if cpu_list_mode: + try: + affinity = [ int(a) for a in affinity.split(",") ] + except: + affinity = cpustring_to_list(affinity) + else: + affinity = bitmasklist(affinity) + + try: + schedutils.set_affinity(pid, affinity) + except SystemError, err: + print "sched_setaffinity: %s" % err[1] + print "failed to set pid %d's affinity" % pid + +def main(): + + args = sys.argv[1:] + if not args: + usage() + return + + cpu_list_mode = False + while True: + o = args.pop(0) + + if o in ("-h", "--help"): + usage() + return + elif o in ("-c", "--cpu-list"): + cpu_list_mode = True + elif o in ("-p", "--pid"): + if len(args) > 1: + affinity = args.pop(0) + pid = int(args.pop(0)) + show_settings(pid, "current", cpu_list_mode) + change_settings(pid, affinity, cpu_list_mode) + show_settings(pid, "new", cpu_list_mode) + else: + pid = int(args.pop(0)) + show_settings(pid, "current", cpu_list_mode) + return + else: + break + + affinity = o + change_settings(0, affinity, cpu_list_mode) + os.execvp(args[0], args) + +if __name__ == '__main__': + main() diff --git a/rpm/SPECS/python-schedutils.spec b/rpm/SPECS/python-schedutils.spec index 173a2dc..28ac21d 100644 --- a/rpm/SPECS/python-schedutils.spec +++ b/rpm/SPECS/python-schedutils.spec @@ -3,10 +3,11 @@ Summary: Linux scheduler python bindings Name: python-schedutils -Version: 0.1 -Release: 3%{?dist} -Source0: %{name}-%{version}.tar.bz2 -License: GPLv2+ +Version: 0.2 +Release: 1%{?dist} +License: GPLv2 +URL: http://git.kernel.org/?p=linux/kernel/git/acme/python-schedutils.git +Source: http://userweb.kernel.org/~acme/python-schedutils/%{name}-%{version}.tar.bz2 Group: System Environment/Libraries BuildRequires: python-devel BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n) @@ -24,6 +25,9 @@ make %install rm -rf $RPM_BUILD_ROOT make DESTDIR=${RPM_BUILD_ROOT} install +mkdir -p %{buildroot}%{_bindir} +cp -f pchrt.py %{buildroot}%{_bindir}/pchrt +cp -f ptaskset.py %{buildroot}%{_bindir}/ptaskset %clean rm -rf $RPM_BUILD_ROOT @@ -31,12 +35,21 @@ rm -rf $RPM_BUILD_ROOT %files %defattr(-,root,root) %doc COPYING +%{_bindir}/pchrt +%{_bindir}/ptaskset %{python_sitearch}/schedutils.so %if "%{python_ver}" >= "2.5" %{python_sitearch}/*.egg-info %endif %changelog +* Wed Aug 27 2008 Arnaldo Carvalho de Melo <acme@redhat.com> - 0.2-1 +- Add get_priority_{min,max} methods +- Add constants for SCHED_{BATCH,FIFO,OTHER,RR} +- Implement get_priority method +- Add pchrt utility for testing the bindings, chrt clone +- Add ptaskset utility for testing the bindings, taskset clone + * Tue Jun 10 2008 Arnaldo Carvalho de Melo <acme@redhat.com> - 0.1-3 - add dist to the release tag |