]> rtime.felk.cvut.cz Git - jailhouse.git/blob - tools/jailhouse-cell-list
tools: config-create: Initial support for non-Intel boards
[jailhouse.git] / tools / jailhouse-cell-list
1 #!/usr/bin/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 and cpu-last_cpu > 1:
24         return "%s%d," % ("-" if cpu-last_cpu > 2 else ",", cpu-1)
25     else:
26         return ""
27
28
29 def print_cpus(mask):
30     last_cpu = None
31     cpu = 0
32     output = ""
33     while mask > 0:
34         if mask & 1:
35             if last_cpu is None:
36                 last_cpu = cpu
37                 output += str(cpu)
38         else:
39             output += finish_cpu_list(last_cpu, cpu)
40             last_cpu = None
41         mask >>= 1
42         cpu += 1
43     output += finish_cpu_list(last_cpu, cpu)
44     return output.strip(",")
45
46
47 if len(sys.argv) > 1:
48     print("usage: %s" % os.path.basename(sys.argv[0]).replace("-", " "))
49     exit(0 if sys.argv[1] in ("--help", "-h") else 1)
50
51 cells = []
52 for cell_path in glob.glob('/sys/devices/jailhouse/cells/*'):
53     cells.append({
54         'name': os.path.basename(cell_path),
55         'id': open(cell_path + "/id").readline().strip(),
56         'state': open(cell_path + "/state").readline().strip(),
57         'cpus_assigned': read_cpus(cell_path + "/cpus_assigned"),
58         'cpus_failed': read_cpus(cell_path + "/cpus_failed")
59         })
60
61 line_format = "%-8s%-24s%-16s%-24s%-24s"
62 if not cells == []:
63     print(line_format % ("ID", "Name", "State",
64                          "Assigned CPUs", "Failed CPUs"))
65 for cell in sorted(cells, key=lambda cell: cell['id']):
66     print(line_format % (cell['id'], cell['name'], cell['state'],
67                          print_cpus(cell['cpus_assigned']),
68                          print_cpus(cell['cpus_failed'])))