From 7ab7be15d03ede96a36b6779188b5e1c80065066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaroslav=20=C5=A0karvada?= Date: Thu, 16 Jun 2016 16:54:58 +0200 Subject: [PATCH] functions: added cpulist_pack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Packs CPU list, i.e. 1, 2, 3 will be converted to 1-3. The cpulist_unpack is used as a preprocessor, so it always returns optimal results. For details about input syntax see cpulist_unpack. Signed-off-by: Jaroslav Škarvada --- .../functions/function_cpulist_pack.py | 21 ++++++++++++++++ tuned/utils/commands.py | 24 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tuned/profiles/functions/function_cpulist_pack.py diff --git a/tuned/profiles/functions/function_cpulist_pack.py b/tuned/profiles/functions/function_cpulist_pack.py new file mode 100644 index 0000000..7018307 --- /dev/null +++ b/tuned/profiles/functions/function_cpulist_pack.py @@ -0,0 +1,21 @@ +import os +import tuned.logs +import base +from tuned.utils.commands import commands + +log = tuned.logs.get() + +class cpulist_pack(base.Function): + """ + Conversion function: packs CPU list in form 1,2,3,5 to 1-3,5. + The cpulist_unpack is used as a preprocessor, so it always returns + optimal results. For details about input syntax see cpulist_unpack. + """ + def __init__(self): + # arbitrary number of arguments + super(self.__class__, self).__init__("cpulist_pack", 0) + + def execute(self, args): + if not super(self.__class__, self).execute(args): + return None + return ",".join(str(v) for v in self._cmd.cpulist_pack(",,".join(args))) diff --git a/tuned/utils/commands.py b/tuned/utils/commands.py index 17375cf..0cbe023 100644 --- a/tuned/utils/commands.py +++ b/tuned/utils/commands.py @@ -211,6 +211,30 @@ class commands: return [] return sorted(list(set(rl))) + # Packs CPU list, i.e. 1, 2, 3 will be converted to 1-3. It unpacks the + # CPU list through cpulist_unpack first, so see its description about the + # details of the input syntax + def cpulist_pack(self, l): + if l is None or len(l) == 0: + return l + l = self.cpulist_unpack(l) + i = 0 + j = i + rl = [] + while i + 1 < len(l): + if l[i + 1] - l[i] != 1: + if j != i: + rl.append(str(l[j]) + "-" + str(l[i])) + else: + rl.append(str(l[i])) + j = i + 1 + i += 1 + if j + 1 < len(l): + rl.append(str(l[j]) + "-" + str(l[-1])) + else: + rl.append(str(l[-1])) + return rl + # Inverts CPU list (i.e. makes its complement) def cpulist_invert(self, l): cpus = self.cpulist_unpack(l)