1
0
Fork 0

Merge pull request #569 from zacikpa/get-instances

Add API functions to retrieve active instances and their devices
This commit is contained in:
Jaroslav Škarvada 2023-11-14 16:37:03 +01:00 committed by GitHub
commit 5d3b469a9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 113 additions and 1 deletions

View file

@ -227,4 +227,24 @@
</defaults>
</action>
<action id="com.redhat.tuned.get_instances">
<description>Get list of active TuneD plugin instances</description>
<message>Authentication is required to get list of active TuneD plugin instances</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
</action>
<action id="com.redhat.tuned.instance_get_devices">
<description>Get list of devices assigned to the instance</description>
<message>Authentication is required to get list of devices assigned to the instance</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
</action>
</policyconfig>

View file

@ -24,7 +24,7 @@
tuned\-adm - command line tool for switching between different tuning profiles
.SH SYNOPSIS
.B tuned\-adm
.RB [ list " | " active " | " "profile \fI[profile]\fP..." " | " "profile_info \fI[profile]\fP..." " | " off " | " auto_profile " | " profile_mode " | " "verify \fI[\-i | \-\-ignore\-missing]\fP" " | " recommend " | " "instance_acquire_devices \fIdevices\fP \fIinstance\fP" ]
.RB [ list " | " active " | " "profile \fI[profile]\fP..." " | " "profile_info \fI[profile]\fP..." " | " off " | " auto_profile " | " profile_mode " | " "verify \fI[\-i | \-\-ignore\-missing]\fP" " | " recommend " | " "instance_acquire_devices \fIdevices\fP \fIinstance\fP" " | " "get_instances \fI[plugin]\fP" " | " "instance_get_devices \fIinstance\fP" ]
.SH DESCRIPTION
This command line utility allows you to switch between user definable tuning
@ -119,6 +119,14 @@ The devices are specified using a comma-separated list. When moving a set of CPU
it is possible to use the cpulist syntax by including the 'cpulist:' prefix.
For instance, 'cpulist:0,2-4' will move the devices cpu0, cpu1, cpu2, and cpu4.
.TP
.B "get_instances \fI[plugin]\fP"
List active instances of a given plugin or all active instances if no plugin is specified.
.TP
.B "instance_get_devices \fIinstance\fP"
List devices currently assigned to a given instance.
.TP
.B off
Unload tunings.

View file

@ -102,6 +102,14 @@ if __name__ == "__main__":
parser_instance_acquire_devices.add_argument("devices", metavar="devices", type=str, help="comma-separated list of device names; may use the cpulist syntax if prefixed with 'cpulist:'")
parser_instance_acquire_devices.add_argument("instance", metavar="instance", type=str, help="name of the plugin instance which should acquire the devices")
parser_get_instances = subparsers.add_parser("get_instances", help="list active instances of a given plugin or all active instances if no plugin is specified")
parser_get_instances.set_defaults(action="get_instances")
parser_get_instances.add_argument("plugin_name", metavar="plugin_name", type=str, nargs="?", default="", help="name of the plugin to restrict the list of instances to")
parser_instance_get_devices = subparsers.add_parser("instance_get_devices", help="list devices assigned to a given instance")
parser_instance_get_devices.set_defaults(action="instance_get_devices")
parser_instance_get_devices.add_argument("instance", metavar="instance", type=str, help="name of the plugin instance")
args = parser.parse_args(sys.argv[1:])
options = vars(args)

View file

@ -449,3 +449,29 @@ class Admin(object):
def _action_instance_acquire_devices(self, devices, instance):
print("Not supported in no_daemon mode.")
return False
def _action_dbus_get_instances(self, plugin_name):
(ret, msg, pairs) = self._controller.get_instances(plugin_name)
if not ret:
self._error("Unable to list instances: %s" % msg)
return self._controller.exit(False)
for instance, plugin in pairs:
print("%s (%s)" % (instance, plugin))
return self._controller.exit(True)
def _action_get_instances(self, plugin_name):
print("Not supported in no_daemon mode.")
return False
def _action_dbus_instance_get_devices(self, instance):
(ret, msg, devices) = self._controller.instance_get_devices(instance)
if not ret:
self._error("Unable to list devices: %s" % msg)
return self._controller.exit(False)
for device in devices:
print(device)
return self._controller.exit(True)
def _action_instance_get_devices(self, instance):
print("Not supported in no_daemon mode.")
return False

View file

@ -166,6 +166,12 @@ class DBusController(object):
def instance_acquire_devices(self, devices, instance):
return self._call("instance_acquire_devices", devices, instance)
def get_instances(self, plugin_name):
return self._call("get_instances", plugin_name)
def instance_get_devices(self, instance):
return self._call("instance_get_devices", instance)
def exit(self, ret):
self.set_action(None)
self._ret = ret

View file

@ -373,3 +373,47 @@ class Controller(tuned.exports.interfaces.ExportableInterface):
log.info(rets)
return (False, rets)
return (True, "OK")
@exports.export("s", "(bsa(ss))")
def get_instances(self, plugin_name, caller = None):
"""Return a list of active instances of a plugin or all active instances
Parameters:
plugin_name -- name of the plugin or an empty string
Return:
bool -- True on success
string -- error message or "OK"
list of string pairs -- [(instance_name, plugin_name)]
"""
if caller == "":
return (False, "Unauthorized", [])
if plugin_name != "" and plugin_name not in self.get_all_plugins().keys():
rets = "Plugin '%s' does not exist" % plugin_name
log.error(rets)
return (False, rets, [])
instances = filter(lambda instance: instance.active, self._daemon._unit_manager.instances)
if plugin_name != "":
instances = filter(lambda instance: instance.plugin.name == plugin_name, instances)
return (True, "OK", list(map(lambda instance: (instance.name, instance.plugin.name), instances)))
@exports.export("s", "(bsas)")
def instance_get_devices(self, instance_name, caller = None):
"""Return a list of devices assigned to an instance
Parameters:
instance_name -- name of the instance
Return:
bool -- True on success
string -- error message or "OK"
list of strings -- device names
"""
if caller == "":
return (False, "Unauthorized", [])
for instance in self._daemon._unit_manager.instances:
if instance.name == instance_name:
return (True, "OK", sorted(list(instance.processed_devices)))
rets = "Instance '%s' not found" % instance_name
log.error(rets)
return (False, rets, [])