forked from chamilad/process-metrics-collector
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon.py
More file actions
279 lines (241 loc) · 9.87 KB
/
common.py
File metadata and controls
279 lines (241 loc) · 9.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: GPL-3.0-or-later
# treecript, a process tree metrics gatherer.
# Copyright (C) 2026 Barcelona Supercomputing Center, José M. Fernández
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import copy
import logging
import pathlib
import re
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import (
Any,
Final,
List,
Mapping,
MutableMapping,
MutableSequence,
Sequence,
Set,
Tuple,
Union,
)
from typing_extensions import (
TypeAlias,
)
CPUInfo: TypeAlias = MutableMapping[str, Any]
import pandas as pd
from .tdp_sources import (
CRAWLERS_TDP_COLUMN,
HONORED_KEY_COLUMNS_CRAWLERS,
)
CPU_DETAILS_FILENAME: "Final[str]" = "cpu_details.json"
CORE_AFFINITY_FILENAME: "Final[str]" = "core_affinity.json"
REFERENCE_PID_FILENAME: "Final[str]" = "reference_pid.txt"
SAMPLING_PERIOD_FILENAME: "Final[str]" = "sampling-period-seconds.txt"
PIDS_FILENAME: "Final[str]" = "pids.txt"
AGGREGATION_METRICS_FILENAME: "Final[str]" = "agg_metrics.tsv"
METRICS_CSV_FILENAME_TEMPLATE: "Final[str]" = "metrics-{0}_{1}.csv"
COMMAND_TXT_FILENAME_TEMPLATE: "Final[str]" = "command-{0}_{1}.txt"
COMMAND_JSON_FILENAME_TEMPLATE: "Final[str]" = "command-{0}_{1}.json"
HONORED_KEY_COLUMNS_CPU_SPEC_DATASET: "Final[Sequence[str]]" = (
"ProcessorNumber",
"Processor Number",
"Name",
"CpuName",
)
HONORED_KEY_COLUMNS: "Final[Sequence[str]]" = (
*HONORED_KEY_COLUMNS_CPU_SPEC_DATASET,
*HONORED_KEY_COLUMNS_CRAWLERS,
)
logger = logging.getLogger(__name__)
def parse_cpuinfo(
cpuinfo_filename: "str" = "/proc/cpuinfo",
) -> "Tuple[Mapping[str, CPUInfo], Mapping[str, Tuple[str, str]]]":
kvsplitter = re.compile(r"\s*:\s*")
entries = []
curr_entry: "CPUInfo" = dict()
cpu_hash: "MutableMapping[str, CPUInfo]" = {}
processor2corecpu: "MutableMapping[str, Tuple[str, str]]" = {}
with open(cpuinfo_filename, mode="r", encoding="latin1") as cH:
for line in cH:
line = line.rstrip("\n")
tokens = kvsplitter.split(line)
if len(tokens) < 2:
entries.append(curr_entry)
physical_id = curr_entry.get("physical id")
assert isinstance(physical_id, str)
processor = curr_entry.get("processor")
assert isinstance(processor, str)
if physical_id in cpu_hash:
curr_cpu = cpu_hash[physical_id]
else:
curr_cpu = copy.copy(curr_entry)
curr_cpu["processors"] = []
cpu_hash[physical_id] = curr_cpu
curr_cpu["processors"].append(processor)
core_id = curr_entry.get("core id")
assert isinstance(core_id, str)
processor2corecpu[processor] = (physical_id, core_id)
curr_entry = dict()
else:
curr_entry[tokens[0]] = tokens if len(tokens) > 2 else tokens[1]
if curr_entry:
entries.append(curr_entry)
physical_id = curr_entry.get("physical id")
assert isinstance(physical_id, str)
processor = curr_entry.get("processor")
assert isinstance(processor, str)
if physical_id in cpu_hash:
curr_cpu = cpu_hash[physical_id]
else:
curr_cpu = copy.copy(curr_entry)
curr_cpu["processors"] = []
curr_cpu["processors"].append(processor)
core_id = curr_entry.get("core id")
assert isinstance(core_id, str)
processor2corecpu[processor] = (physical_id, core_id)
return cpu_hash, processor2corecpu
def _tdp_finder_from_model_name(
model_name: "str",
key_column: "str",
cpus_df: "pd.DataFrame",
processors_file: "pathlib.Path",
) -> "Tuple[str, str, float, pathlib.Path]":
if key_column not in cpus_df:
errmsg = f"Unable to find a valid processor identification column in file {processors_file.as_posix()}"
logger.error(errmsg)
raise KeyError(errmsg)
elif (
key_column in HONORED_KEY_COLUMNS_CRAWLERS
and CRAWLERS_TDP_COLUMN not in cpus_df
):
errmsg = f"Unable to find a valid processor TDP column in file {processors_file.as_posix()}"
logger.error(errmsg)
raise KeyError(errmsg)
filtered_cpus = cpus_df[cpus_df[key_column].apply(lambda pn: str(pn) in model_name)]
if len(filtered_cpus) == 0:
errmsg = f"Unable to match a valid processor row for {model_name} in file {processors_file.as_posix()}"
logger.warning(errmsg)
raise LookupError(errmsg)
matches: "List[Tuple[str, Union[str, float, int]]]" = []
tried_match = False
if key_column in HONORED_KEY_COLUMNS_CRAWLERS:
column = filtered_cpus[CRAWLERS_TDP_COLUMN]
if not column.hasnans:
putative_tdp_val = column.values[0]
if isinstance(putative_tdp_val, (str, float, int)):
tried_match = True
matches.append((CRAWLERS_TDP_COLUMN, putative_tdp_val))
else:
for column_name, column in filtered_cpus.items():
if not column.hasnans:
putative_tdp_str = column.values[0]
if isinstance(putative_tdp_str, str):
tried_match = True
matched = re.search(
r"^(?:[0-9]+(?:\.[0-9]+])?-)?([0-9]+(?:\.[0-9]+])?) W",
putative_tdp_str,
)
if matched:
matches.append((str(column_name), matched.group(1)))
if len(matches) == 0:
if tried_match:
submsg = "found model description but not the consumption"
else:
submsg = "no match on model description"
errmsg = f"Unable to find processor package consumption values for {model_name} in file {processors_file.as_posix()} ({submsg})"
logger.warning(errmsg)
raise ValueError(errmsg)
elif len(matches) > 1:
# Now, sort by consumption
matches.sort(key=lambda t: t[1], reverse=True)
return (model_name, matches[0][0], float(matches[0][1]), processors_file)
def tdp_finder_from_model_name(
model_name: "str", processors_files: "Sequence[pathlib.Path]"
) -> "Tuple[str, str, float, pathlib.Path]":
errors = []
notfound = []
for processors_file in processors_files:
# low_memory is needed to avoid a warning in some CSV files with mixed data
cpus = pd.read_csv(processors_file, low_memory=False)
for key_column in HONORED_KEY_COLUMNS:
if key_column in cpus:
break
else:
errors.append(
f"Unable to find a valid processor identification column in file {processors_file.as_posix()}"
)
continue
try:
return _tdp_finder_from_model_name(
model_name, key_column, cpus, processors_file
)
except LookupError:
# We are recovering for this case, where
errmsg = f"Nothing found for {model_name} under {key_column} in {processors_file.as_posix()}"
logger.debug(errmsg)
notfound.append(errmsg)
if len(errors) > 0 or len(notfound) > 0:
for error in (*errors, *notfound):
logger.error(error)
raise Exception()
def tdp_finder_from_cpuinfo(
cpu_details: "Sequence[CPUInfo]", processors_files: "Sequence[pathlib.Path]"
) -> "Sequence[Tuple[str, str, float, pathlib.Path]]":
# First, account for the number of different model names
unique_model_names: "Set[str]" = set()
for cpu_details_cpu in cpu_details:
model_name = cpu_details_cpu["model name"]
unique_model_names.add(model_name)
errors = []
found_tdp: "MutableSequence[Tuple[str, str, float, pathlib.Path]]" = []
seen_model_names: "Set[str]" = set()
for processors_file in processors_files:
# low_memory is needed to avoid a warning in some CSV files with mixed data
cpus = pd.read_csv(processors_file, low_memory=False)
# print(f"COLUMNS {processors_file.as_posix()} {list(cpus.columns)}")
for key_column in HONORED_KEY_COLUMNS:
if key_column in cpus:
break
else:
errors.append(
f"Unable to find a valid processor identification column in file {processors_file.as_posix()}"
)
continue
for model_name in unique_model_names:
if model_name not in seen_model_names:
try:
found_tdp.append(
_tdp_finder_from_model_name(
model_name, key_column, cpus, processors_file
)
)
seen_model_names.add(model_name)
except LookupError:
# We are recovering for this case, where
logger.debug(
f"Nothing found for {model_name} under {key_column} in {processors_file.as_posix()}"
)
# Once all matches are found, answer
if len(found_tdp) == len(unique_model_names):
break
if len(found_tdp) == 0 and len(errors) > 0:
for error in errors:
logger.error(error)
raise Exception()
return found_tdp