Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

add support for execute process in switches scheduler config

the scheduler config to use is detected from the the executable
name or used as run parameter
  • Loading branch information...
commit aeb50276917d3cee5536be26a2b244eab102d370 1 parent 934cd0d
Daniel Poelzleithner authored March 17, 2011
1  run-game
1  run-single-task
91  ulatency
@@ -27,8 +27,11 @@ DEFAULT_TREE = "cpu"
27 27
 CGROUP_PATHS = {}
28 28
 CGROUP_SUBSYS = []
29 29
 
  30
+CONFIG_SINGLE_TASK = "single_task"
  31
+
  32
+
30 33
 def prnt(arg):
31  
-  sys.stdout.write(PY3 and str(arg) or arg.encode("utf-8"))
  34
+  sys.stdout.write(PY3 and str(arg) or unicode(arg).encode("utf-8"))
32 35
   sys.stdout.write("\n")
33 36
 
34 37
 if PY3:
@@ -315,6 +318,22 @@ class Session:
315 318
             prnt(e)
316 319
             sys.exit(2)
317 320
 
  321
+    def add_flag(self, pid, **values):
  322
+        if not self.init_system():
  323
+          return ""
  324
+        system = dbus.Interface(self.system, 'org.quamquam.ulatencyd.System')
  325
+        try:
  326
+            rv = []
  327
+            data = system.addFlag(pid, 0, values["name"], 
  328
+                                          unicode(values.get("reason", "")),
  329
+                                          long(values.get("timeout", 0)), 
  330
+                                          int(values.get("priority", 0)), 
  331
+                                          long(values.get("value", 0)), 
  332
+                                          long(values.get("treshold", 0)),
  333
+                                          bool(values.get("inherit", False)))
  334
+        except dbus.exceptions.DBusException as e:
  335
+            prnt(e)
  336
+            raise e
318 337
 
319 338
 
320 339
 class Tree(object):
@@ -789,15 +808,51 @@ class QtGui(Gui):
789 808
 
790 809
 
791 810
 
  811
+def command(args, config=CONFIG_SINGLE_TASK, as_root=False):
  812
+    pid = os.getpid()
  813
+    uid = None
  814
+    if os.getuid() == 0:
  815
+        uid = (not as_root and int(os.environ.get("SUDO_UID", 0))) or os.getuid()
  816
+        #prnt("you need to run: sudo comand")
  817
+        #sys.exit(1)
  818
+    session = Session()
  819
+    old_config = session.get_config()
  820
+    prnt("ulatency: old config %s" %old_config)
  821
+    session.add_flag(pid, name="single_task", inherit=True)
  822
+    #session.set_config(CONFIG_SINGLE_TASK)
  823
+    prnt("ulatency: execute '%s'" %" ".join(args))
  824
+    prnt("-"*30)
  825
+    if uid is not None:
  826
+        prnt("Execute as: %s", uid)
  827
+        old_uid = os.getuid()
  828
+        os.seteuid(uid)
  829
+    try:
  830
+        retcode = subprocess.call(args)
  831
+        prnt("-"*30)
  832
+        if retcode < 0:
  833
+            print >>sys.stderr, "Child was terminated by signal", -retcode
  834
+        else:
  835
+            print >>sys.stderr, "Child returned", retcode
  836
+    except OSError, e:
  837
+        print >>sys.stderr, "Execution failed:", e
  838
+    if uid is not None:
  839
+        os.seteuid(old_uid)
  840
+    prnt("ulatency: restore config %s" %old_config)
  841
+    session.set_config(old_config)
  842
+
  843
+
792 844
 
793 845
 EPILOG="""commands:
794  
-  tree [cgroup]     show a cgroup tree
795  
-  get [key]         get daemon setting
796  
-  set [key value]   set daemon value
797  
-  flags [process]   list flags of a process or all
798  
-  sysflags          list system flags
799  
-  mounts            lists all mounted cgroups
800  
-  clear cgroup|ALL  clears a cgroup
  846
+  tree [cgroup]              show a cgroup tree
  847
+  get [key]                  get daemon setting
  848
+  set [key value]            set daemon value
  849
+  flags [process]            list flags of a process or all
  850
+  sysflags                   list system flags
  851
+  mounts                     lists all mounted cgroups
  852
+  clear cgroup|ALL           clears a cgroup
  853
+  run -- [COMMAND]           run task under different scheduler config
  854
+  game -- [COMMAND]          run task under game scheduler
  855
+  single_task -- [COMMAND]   run task under single_task scheduler
801 856
 """
802 857
 
803 858
 def main():
@@ -820,8 +875,22 @@ def main():
820 875
         default=False, help="show flags on tree output")
821 876
     parser.add_option("--cmd", dest="cmdline", action="store_true",
822 877
         default=False, help="show cmdline")
  878
+    parser.add_option("--as-root", dest="as_root", action="store_true",
  879
+        default=False, help="run command as root")
823 880
     parser.add_option("--no-processes", dest="processes", action="store_false",
824 881
         default=True, help="hide all processes")
  882
+    parser.add_option("--scheduler", dest="scheduler", action="store",
  883
+        default=CONFIG_SINGLE_TASK, help="scheduler config to use")
  884
+
  885
+    prog = os.path.basename(sys.argv[0])
  886
+
  887
+    if prog[:4] in ("run-", "run_"):
  888
+        command(sys.argv[1:], prog[4:].replace("-", "_"))
  889
+        return
  890
+
  891
+    if prog == 'run_single_task':
  892
+        command(sys.argv[1:], "single_task")
  893
+        return
825 894
 
826 895
     options, args = parser.parse_args()
827 896
 
@@ -851,6 +920,12 @@ def main():
851 920
         else: prnt(",".join(Session().gets.keys()))
852 921
         return
853 922
 
  923
+
  924
+    if args and args[0] == 'run':
  925
+        command(args[1:], options.scheduler, options.as_root)
  926
+        return
  927
+
  928
+
854 929
     if not args or args[0] == 'tree':
855 930
         kwargs = dict(utf = options.utf, color = options.color,
856 931
             show_all = options.all, processes = options.processes,

0 notes on commit aeb5027

Please sign in to comment.
Something went wrong with that request. Please try again.