]> rtime.felk.cvut.cz Git - jailhouse.git/blob - tools/jailhouse-cell-list
jailhouse: inmates: bench: Add -R option -- repeats count.
[jailhouse.git] / tools / jailhouse-cell-list
1 #!/usr/bin/env python
2
3 # Jailhouse, a Linux-based partitioning hypervisor
4 #
5 # Copyright (c) Siemens AG, 2014
6 #
7 # Authors:
8 #  Jan Kiszka <jan.kiszka@siemens.com>
9 #
10 # This work is licensed under the terms of the GNU GPL, version 2.  See
11 # the COPYING file in the top-level directory.
12
13 import glob
14 import os
15 import sys
16
17
18 def read_cpus(path):
19     return int(open(path).readline().strip().replace(",", ""), 16)
20
21
22 def finish_cpu_list(last_cpu, cpu):
23     if last_cpu is not None:
24         if cpu-last_cpu > 1:
25             return "%s%d," % ("-" if cpu-last_cpu > 2 else ",", cpu-1)
26         else:
27             return ","
28     else:
29         return ""
30
31
32 def print_cpus(mask):
33     last_cpu = None
34     cpu = 0
35     output = ""
36     while mask > 0:
37         if mask & 1:
38             if last_cpu is None:
39                 last_cpu = cpu
40                 output += str(cpu)
41         else:
42             output += finish_cpu_list(last_cpu, cpu)
43             last_cpu = None
44         mask >>= 1
45         cpu += 1
46     output += finish_cpu_list(last_cpu, cpu)
47     return output.strip(",")
48
49
50 if len(sys.argv) > 1:
51     print("usage: %s" % os.path.basename(sys.argv[0]).replace("-", " "))
52     exit(0 if sys.argv[1] in ("--help", "-h") else 1)
53
54 cells = []
55 for cell_path in glob.glob('/sys/devices/jailhouse/cells/*'):
56     cells.append({
57         'name': os.path.basename(cell_path),
58         'id': open(cell_path + "/id").readline().strip(),
59         'state': open(cell_path + "/state").readline().strip(),
60         'cpus_assigned': read_cpus(cell_path + "/cpus_assigned"),
61         'cpus_failed': read_cpus(cell_path + "/cpus_failed")
62         })
63
64 line_format = "%-8s%-24s%-16s%-24s%-24s"
65 if not cells == []:
66     print(line_format % ("ID", "Name", "State",
67                          "Assigned CPUs", "Failed CPUs"))
68 for cell in sorted(cells, key=lambda cell: cell['id']):
69     print(line_format % (cell['id'], cell['name'], cell['state'],
70                          print_cpus(cell['cpus_assigned']),
71                          print_cpus(cell['cpus_failed'])))