Skip to contentSkip to conversation

tuna: Add functionality to set the idle state of specified CPUs when running tuna

PATCH New Anubhav Shelat 1 participant 1 message Aug 9, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tuna: Add functionality to set the idle state of specified CPUs when running tuna

We would like to be able to set the idle state of CPUs while running tuna.

This patch adds the file cpupower.py and idle-set subcommand to tuna-cmd.py. cpupower.py provides the infrastructure to interface with the cpupower command, and the idle-set subcommand in tuna-cmd.py lets the user specify the idle state depth to be set and the cpu list to set it on, or to display information about idle states.

---
 tuna-cmd.py      |  35 +++++++++++++-
 tuna/cpupower.py | 120 +++++++++++++++++++++++++++++++++++++++++++++++
 tuna/tuna.py     |   2 +
 3 files changed, 155 insertions(+), 2 deletions(-)
 create mode 100644 tuna/cpupower.py
tuna-cmd.py +33 -2
index f37e286bffdb..090aff0cb972 100755
@@ -22,9 +22,11 @@ import tuna.new_eth as ethtool
 import tuna.tuna_sched as tuna_sched
 import procfs
 from tuna import tuna, sysfs
+from tuna.cpupower import Cpupower
 import logging
 import time
 import shutil
+from multiprocessing import cpu_count
 
 def get_loglevel(level):
     if level.isdigit() and int(level) in range(0,5):
@@ -116,7 +118,12 @@ def gen_parser():
             "disable_perf": dict(action='store_true', help="Explicitly disable usage of perf in GUI for process view"),
             "refresh": dict(default=2500, metavar='MSEC', type=int, help="Refresh the GUI every MSEC milliseconds"),
             "priority": dict(default=(None, None), metavar="POLICY:RTPRIO", type=tuna.get_policy_and_rtprio, help="Set thread scheduler tunables: POLICY and RTPRIO"),
-            "background": dict(action='store_true', help="Run command as background task")
+            "background": dict(action='store_true', help="Run command as background task"),
+            "idle_state": dict(dest='idle_state', metavar='IDLESTATE', help='Set cpus in CPU-LIST to an idle state depth. If CPU-LIST is not specified, default to all cpus.'),
+            "all_idlestates": dict(dest='all_idlestates', action='store_true', default=False,
+                                   help='Enable all idle states on CPU-LIST.'),
+            "info": dict(dest='info', default=False, action='store_true',
+                          help='Display information about available CPU idle states.'),
          }
 
     parser = HelpMessageParser(description="tuna - Application Tuning Program")
@@ -147,6 +154,8 @@ def gen_parser():
     show_threads = subparser.add_parser('show_threads', description='Show thread list', help='Show thread list')
     show_irqs = subparser.add_parser('show_irqs', description='Show IRQ list', help='Show IRQ list')
     show_configs = subparser.add_parser('show_configs', description='List preloaded profiles', help='List preloaded profiles')
+    idle_set = subparser.add_parser('idle-set', description='Set all idle states, up to and including the given idle state, on a given CPU list.',
+                                    help='Set all idle states, up to and including the given idle state, on CPU-LIST.')
 
     what_is = subparser.add_parser('what_is', description='Provides help about selected entities', help='Provides help about selected entities')
     gui = subparser.add_parser('gui', description="Start the GUI", help="Start the GUI")
@@ -219,6 +228,12 @@ def gen_parser():
     show_irqs_group.add_argument('-S', '--sockets', **MODS['sockets'])
     show_irqs.add_argument('-q', '--irqs', **MODS['irqs'])
 
+    idle_set_group = idle_set.add_mutually_exclusive_group(required=True)
+    idle_set.add_argument('-c', '--cpus', **MODS['cpus'])
+    idle_set_group.add_argument('-i', '--state', **MODS['idle_state'])
+    idle_set_group.add_argument('-E', '--enable-all', **MODS['all_idlestates'])
+    idle_set_group.add_argument('--info', **MODS['info'])
+
     what_is.add_argument('thread_list', **POS['thread_list'])
 
     gui.add_argument('-d', '--disable_perf', **MODS['disable_perf'])
@@ -649,7 +664,6 @@ def main():
     parser = gen_parser()
     # Set all necessary defaults for gui subparser if no arguments provided
     args = parser.parse_args() if len(sys.argv) > 1 else parser.parse_args(['gui'])
-
     if args.debug:
         my_logger = setup_logging("my_logger")
         my_logger.addHandler(add_handler("DEBUG", tofile=False))
@@ -667,6 +681,23 @@ def main():
             print("Valid log levels: NOTSET, DEBUG, INFO, WARNING, ERROR")
             print("Log levels may be specified numerically (0-4)\n")
 
+    if args.command == 'idle-set':
+        if args.cpu_list and args.info:
+            print("Error: must specify idle state with -i/--state or -E/--enable-all when using -c/--cpus.")
+            sys.exit(1)
+
+        if args.info:
+            Cpupower.get_idle_info()
+        elif args.idle_state or args.all_idlestates:
+            if args.cpu_list == [] or args.cpu_list == ['all']:
+                args.cpu_list = list(range(cpu_count()))
+            cpulist = tuna.list_to_cpustring(args.cpu_list)
+
+            if args.idle_state:
+                Cpupower(cpulist, args.idle_state).enable_idle_state()
+            elif args.all_idlestates:
+                Cpupower(cpulist, 0).enable_all_idle_states()
+
     if 'irq_list' in vars(args):
         ps = procfs.pidstats()
         if tuna.has_threaded_irqs(ps):
tuna/cpupower.py new file +120 -0

120 lines not on this page · raw message

tuna/tuna.py +2 -0
index bd678e2dc7ae..52e4c3ec356d 100755
@@ -122,6 +122,8 @@ def cpustring_to_list(cpustr):
     Returns a list of integers."""
 
     fields = cpustr.strip().split(",")
+    if fields == ['all']:
+        return fields
     cpu_list = []
     for field in fields:
         ends = [int(a, 0) for a in field.split("-")]
Signature · 1 line
2.45.2