author | wenzelm |
Mon, 06 Mar 2023 15:38:50 +0100 | |
changeset 77541 | 9d9b30741fc4 |
parent 77526 | e3ed231fa214 |
child 77543 | 97b5547f2b17 |
permissions | -rw-r--r-- |
77475 | 1 |
/* Title: Pure/System/host.scala |
2 |
Author: Makarius |
|
3 |
||
77477 | 4 |
Information about compute hosts, including NUMA: Non-Uniform Memory Access of |
5 |
separate CPU nodes. |
|
6 |
||
77478 | 7 |
See also https://www.open-mpi.org/projects/hwloc --- notably "lstopo" or |
8 |
"hwloc-ls" (e.g. via Ubuntu package "hwloc"). |
|
77475 | 9 |
*/ |
10 |
||
11 |
package isabelle |
|
12 |
||
13 |
||
14 |
object Host { |
|
15 |
/* allocated resources */ |
|
16 |
||
17 |
object Node_Info { def none: Node_Info = Node_Info("", None) } |
|
18 |
||
77526 | 19 |
sealed case class Node_Info(hostname: String, numa_node: Option[Int]) { |
20 |
override def toString: String = |
|
21 |
hostname + if_proper(numa_node, "/" + numa_node.get.toString) |
|
22 |
} |
|
77476 | 23 |
|
24 |
||
77477 | 25 |
/* available NUMA nodes */ |
26 |
||
27 |
private val numa_info_linux: Path = Path.explode("/sys/devices/system/node/online") |
|
28 |
||
29 |
def numa_nodes(enabled: Boolean = true, ssh: SSH.System = SSH.Local): List[Int] = { |
|
30 |
val Single = """^(\d+)$""".r |
|
31 |
val Multiple = """^(\d+)-(\d+)$""".r |
|
32 |
||
33 |
def parse(s: String): List[Int] = |
|
34 |
s match { |
|
35 |
case Single(Value.Int(i)) => List(i) |
|
36 |
case Multiple(Value.Int(i), Value.Int(j)) => (i to j).toList |
|
37 |
case _ => error("Cannot parse CPU NUMA node specification: " + quote(s)) |
|
38 |
} |
|
39 |
||
40 |
val numa_info = if (ssh.isabelle_platform.is_linux) Some(numa_info_linux) else None |
|
41 |
for { |
|
42 |
path <- numa_info.toList |
|
43 |
if enabled && ssh.is_file(path) |
|
44 |
s <- space_explode(',', ssh.read(path).trim) |
|
45 |
n <- parse(s) |
|
46 |
} yield n |
|
47 |
} |
|
48 |
||
49 |
||
50 |
/* process policy via numactl tool */ |
|
51 |
||
52 |
def numactl(node: Int): String = "numactl -m" + node + " -N" + node |
|
53 |
def numactl_ok(node: Int): Boolean = Isabelle_System.bash(numactl(node) + " true").ok |
|
54 |
||
55 |
def process_policy(node: Int): String = if (numactl_ok(node)) numactl(node) else "" |
|
56 |
||
57 |
def process_policy_options(options: Options, numa_node: Option[Int]): Options = |
|
58 |
numa_node match { |
|
59 |
case None => options |
|
77483 | 60 |
case Some(n) => options.string("process_policy") = process_policy(n) |
77477 | 61 |
} |
62 |
||
63 |
def perhaps_process_policy_options(options: Options): Options = { |
|
64 |
val numa_node = |
|
65 |
try { |
|
66 |
numa_nodes() match { |
|
67 |
case ns if ns.length >= 2 && numactl_ok(ns.head) => Some(ns.head) |
|
68 |
case _ => None |
|
69 |
} |
|
70 |
} |
|
71 |
catch { case ERROR(_) => None } |
|
72 |
process_policy_options(options, numa_node) |
|
73 |
} |
|
74 |
||
75 |
||
76 |
/* shuffling of NUMA nodes */ |
|
77 |
||
78 |
def numa_check(progress: Progress, enabled: Boolean): Boolean = { |
|
79 |
def warning = |
|
80 |
numa_nodes() match { |
|
81 |
case ns if ns.length < 2 => Some("no NUMA nodes available") |
|
82 |
case ns if !numactl_ok(ns.head) => Some("bad numactl tool") |
|
83 |
case _ => None |
|
84 |
} |
|
85 |
||
86 |
enabled && |
|
87 |
(warning match { |
|
88 |
case Some(s) => |
|
89 |
progress.echo_warning("Shuffling of NUMA CPU nodes is disabled: " + s) |
|
90 |
false |
|
91 |
case _ => true |
|
92 |
}) |
|
93 |
} |
|
94 |
||
95 |
||
77476 | 96 |
/* SQL data model */ |
97 |
||
98 |
object Data { |
|
99 |
def make_table(name: String, columns: List[SQL.Column], body: String = ""): SQL.Table = |
|
100 |
SQL.Table("isabelle_host" + if_proper(name, "_" + name), columns, body = body) |
|
101 |
||
102 |
object Node_Info { |
|
103 |
val hostname = SQL.Column.string("hostname").make_primary_key |
|
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
104 |
val numa_next = SQL.Column.int("numa_next") |
77476 | 105 |
|
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
106 |
val table = make_table("node_info", List(hostname, numa_next)) |
77476 | 107 |
} |
108 |
||
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
109 |
def read_numa_next(db: SQL.Database, hostname: String): Int = |
77476 | 110 |
db.using_statement( |
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
111 |
Node_Info.table.select(List(Node_Info.numa_next), |
77476 | 112 |
sql = Node_Info.hostname.where_equal(hostname)) |
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
113 |
)(stmt => stmt.execute_query().iterator(_.int(Node_Info.numa_next)).nextOption.getOrElse(0)) |
77476 | 114 |
|
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
115 |
def update_numa_next(db: SQL.Database, hostname: String, numa_next: Int): Boolean = |
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
116 |
if (read_numa_next(db, hostname) != numa_next) { |
77541 | 117 |
db.execute_statement( |
118 |
Node_Info.table.delete(sql = Node_Info.hostname.where_equal(hostname))) |
|
119 |
db.execute_statement(Node_Info.table.insert(), body = |
|
120 |
{ stmt => |
|
121 |
stmt.string(1) = hostname |
|
122 |
stmt.int(2) = numa_next |
|
123 |
}) |
|
77476 | 124 |
true |
125 |
} |
|
126 |
else false |
|
127 |
} |
|
77475 | 128 |
} |