author | wenzelm |
Tue, 18 Jul 2023 19:51:12 +0200 | |
changeset 78396 | 7853d9072d1b |
parent 78356 | 974dbe256a37 |
child 78435 | a623cb346b4a |
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 { |
|
77549 | 15 |
/* process policy via numactl tool */ |
16 |
||
17 |
def numactl(node: Int): String = "numactl -m" + node + " -N" + node |
|
18 |
def numactl_ok(node: Int): Boolean = Isabelle_System.bash(numactl(node) + " true").ok |
|
19 |
||
20 |
def process_policy(options: Options, numa_node: Option[Int]): Options = |
|
21 |
numa_node match { |
|
22 |
case None => options |
|
23 |
case Some(node) => |
|
24 |
options.string("process_policy") = if (numactl_ok(node)) numactl(node) else "" |
|
25 |
} |
|
26 |
||
27 |
||
77475 | 28 |
/* allocated resources */ |
29 |
||
30 |
object Node_Info { def none: Node_Info = Node_Info("", None) } |
|
31 |
||
77526 | 32 |
sealed case class Node_Info(hostname: String, numa_node: Option[Int]) { |
33 |
override def toString: String = |
|
34 |
hostname + if_proper(numa_node, "/" + numa_node.get.toString) |
|
77550 | 35 |
|
36 |
def process_policy(options: Options): Options = |
|
37 |
Host.process_policy(options, numa_node) |
|
77526 | 38 |
} |
77476 | 39 |
|
40 |
||
77477 | 41 |
/* available NUMA nodes */ |
42 |
||
43 |
private val numa_info_linux: Path = Path.explode("/sys/devices/system/node/online") |
|
44 |
||
45 |
def numa_nodes(enabled: Boolean = true, ssh: SSH.System = SSH.Local): List[Int] = { |
|
46 |
val Single = """^(\d+)$""".r |
|
47 |
val Multiple = """^(\d+)-(\d+)$""".r |
|
48 |
||
49 |
def parse(s: String): List[Int] = |
|
50 |
s match { |
|
51 |
case Single(Value.Int(i)) => List(i) |
|
52 |
case Multiple(Value.Int(i), Value.Int(j)) => (i to j).toList |
|
53 |
case _ => error("Cannot parse CPU NUMA node specification: " + quote(s)) |
|
54 |
} |
|
55 |
||
56 |
val numa_info = if (ssh.isabelle_platform.is_linux) Some(numa_info_linux) else None |
|
57 |
for { |
|
58 |
path <- numa_info.toList |
|
59 |
if enabled && ssh.is_file(path) |
|
60 |
s <- space_explode(',', ssh.read(path).trim) |
|
61 |
n <- parse(s) |
|
62 |
} yield n |
|
63 |
} |
|
64 |
||
77548 | 65 |
def numa_node0(): Option[Int] = |
66 |
try { |
|
67 |
numa_nodes() match { |
|
68 |
case ns if ns.length >= 2 && numactl_ok(ns.head) => Some(ns.head) |
|
69 |
case _ => None |
|
70 |
} |
|
71 |
} |
|
72 |
catch { case ERROR(_) => None } |
|
73 |
||
74 |
||
77477 | 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 |
||
78396
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents:
78356
diff
changeset
|
98 |
object private_data extends SQL.Data("isabelle_host") { |
78174
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
99 |
val database: Path = Path.explode("$ISABELLE_HOME_USER/host.db") |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
100 |
|
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78174
diff
changeset
|
101 |
override lazy val tables = SQL.Tables(Node_Info.table) |
77476 | 102 |
|
103 |
object Node_Info { |
|
104 |
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
|
105 |
val numa_next = SQL.Column.int("numa_next") |
77476 | 106 |
|
78266 | 107 |
val table = make_table(List(hostname, numa_next), name = "node_info") |
77476 | 108 |
} |
109 |
||
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
110 |
def read_numa_next(db: SQL.Database, hostname: String): Int = |
77552 | 111 |
db.execute_query_statementO[Int]( |
77525
de6fb423fd4b
clarified database content: store actual value instead of index;
wenzelm
parents:
77483
diff
changeset
|
112 |
Node_Info.table.select(List(Node_Info.numa_next), |
77552 | 113 |
sql = Node_Info.hostname.where_equal(hostname)), |
114 |
res => res.int(Node_Info.numa_next) |
|
115 |
).getOrElse(0) |
|
77476 | 116 |
|
78174
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
117 |
def write_numa_next(db: SQL.Database, hostname: String, numa_next: Int): Unit = { |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
118 |
db.execute_statement(Node_Info.table.delete(sql = Node_Info.hostname.where_equal(hostname))) |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
119 |
db.execute_statement(Node_Info.table.insert(), body = |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
120 |
{ stmt => |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
121 |
stmt.string(1) = hostname |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
122 |
stmt.int(2) = numa_next |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
123 |
}) |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
124 |
} |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
125 |
} |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
126 |
|
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
127 |
def next_numa_node( |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
128 |
db: SQL.Database, |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
129 |
hostname: String, |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
130 |
available_nodes: List[Int], |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
131 |
used_nodes: => Set[Int] |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
132 |
): Option[Int] = |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
133 |
if (available_nodes.isEmpty) None |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
134 |
else { |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
135 |
val available = available_nodes.zipWithIndex |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
136 |
val used = used_nodes |
78396
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents:
78356
diff
changeset
|
137 |
private_data.transaction_lock(db, create = true, label = "Host.next_numa_node") { |
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents:
78356
diff
changeset
|
138 |
val numa_next = private_data.read_numa_next(db, hostname) |
78174
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
139 |
val numa_index = available.collectFirst({ case (n, i) if n == numa_next => i }).getOrElse(0) |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
140 |
val candidates = available.drop(numa_index) ::: available.take(numa_index) |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
141 |
val (n, i) = |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
142 |
candidates.find({ case (n, i) => i == numa_index && !used(n) }) orElse |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
143 |
candidates.find({ case (n, _) => !used(n) }) getOrElse candidates.head |
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
144 |
|
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
145 |
val numa_next1 = available_nodes((i + 1) % available_nodes.length) |
78396
7853d9072d1b
renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents:
78356
diff
changeset
|
146 |
if (numa_next != numa_next1) private_data.write_numa_next(db, hostname, numa_next1) |
78174
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
147 |
|
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
148 |
Some(n) |
77476 | 149 |
} |
78174
cc0bd66eb498
separate host.db for independent db.transaction_lock;
wenzelm
parents:
77552
diff
changeset
|
150 |
} |
77475 | 151 |
} |