|
42 | 42 | if TYPE_CHECKING: |
43 | 43 | from typing import ( |
44 | 44 | Any, |
45 | | - Final, |
46 | 45 | Mapping, |
47 | 46 | MutableMapping, |
48 | 47 | MutableSequence, |
|
52 | 51 | Tuple, |
53 | 52 | ) |
54 | 53 |
|
55 | | - from typing_extensions import ( |
56 | | - TypeAlias, |
57 | | - ) |
58 | | - |
59 | | - CPUInfo: TypeAlias = MutableMapping[str, Any] |
60 | | - |
61 | | -CPU_DETAILS_FILENAME: "Final[str]" = "cpu_details.json" |
62 | | -CORE_AFFINITY_FILENAME: "Final[str]" = "core_affinity.json" |
63 | | -REFERENCE_PID_FILENAME: "Final[str]" = "reference_pid.txt" |
64 | | -SAMPLING_PERIOD_FILENAME: "Final[str]" = "sampling-period-seconds.txt" |
65 | | -PIDS_FILENAME: "Final[str]" = "pids.txt" |
66 | | -AGGREGATION_METRICS_FILENAME: "Final[str]" = "agg_metrics.tsv" |
67 | | - |
68 | | -METRICS_CSV_FILENAME_TEMPLATE: "Final[str]" = "metrics-{0}_{1}.csv" |
69 | | -COMMAND_TXT_FILENAME_TEMPLATE: "Final[str]" = "command-{0}_{1}.txt" |
70 | | -COMMAND_JSON_FILENAME_TEMPLATE: "Final[str]" = "command-{0}_{1}.json" |
| 54 | +from .common import ( |
| 55 | + AGGREGATION_METRICS_FILENAME, |
| 56 | + COMMAND_JSON_FILENAME_TEMPLATE, |
| 57 | + COMMAND_TXT_FILENAME_TEMPLATE, |
| 58 | + CPU_DETAILS_FILENAME, |
| 59 | + CORE_AFFINITY_FILENAME, |
| 60 | + METRICS_CSV_FILENAME_TEMPLATE, |
| 61 | + PIDS_FILENAME, |
| 62 | + REFERENCE_PID_FILENAME, |
| 63 | + SAMPLING_PERIOD_FILENAME, |
| 64 | + parse_cpuinfo, |
| 65 | +) |
71 | 66 |
|
72 | 67 | # Module level logger |
73 | 68 | logger = logging.getLogger(__name__) |
74 | 69 |
|
75 | 70 |
|
76 | | -def parse_cpuinfo( |
77 | | - cpuinfo_filename: "str" = "/proc/cpuinfo", |
78 | | -) -> "Tuple[Mapping[str, CPUInfo], Mapping[str, Tuple[str, str]]]": |
79 | | - kvsplitter = re.compile(r"\s*:\s*") |
80 | | - |
81 | | - entries = [] |
82 | | - curr_entry: "CPUInfo" = dict() |
83 | | - cpu_hash: "MutableMapping[str, CPUInfo]" = {} |
84 | | - processor2corecpu: "MutableMapping[str, Tuple[str, str]]" = {} |
85 | | - with open(cpuinfo_filename, mode="r", encoding="latin1") as cH: |
86 | | - for line in cH: |
87 | | - line = line.rstrip("\n") |
88 | | - tokens = kvsplitter.split(line) |
89 | | - if len(tokens) < 2: |
90 | | - entries.append(curr_entry) |
91 | | - physical_id = curr_entry.get("physical id") |
92 | | - assert isinstance(physical_id, str) |
93 | | - processor = curr_entry.get("processor") |
94 | | - assert isinstance(processor, str) |
95 | | - if physical_id in cpu_hash: |
96 | | - curr_cpu = cpu_hash[physical_id] |
97 | | - else: |
98 | | - curr_cpu = copy.copy(curr_entry) |
99 | | - curr_cpu["processors"] = [] |
100 | | - cpu_hash[physical_id] = curr_cpu |
101 | | - curr_cpu["processors"].append(processor) |
102 | | - core_id = curr_entry.get("core id") |
103 | | - assert isinstance(core_id, str) |
104 | | - processor2corecpu[processor] = (physical_id, core_id) |
105 | | - curr_entry = dict() |
106 | | - else: |
107 | | - curr_entry[tokens[0]] = tokens if len(tokens) > 2 else tokens[1] |
108 | | - if curr_entry: |
109 | | - entries.append(curr_entry) |
110 | | - physical_id = curr_entry.get("physical id") |
111 | | - assert isinstance(physical_id, str) |
112 | | - processor = curr_entry.get("processor") |
113 | | - assert isinstance(processor, str) |
114 | | - if physical_id in cpu_hash: |
115 | | - curr_cpu = cpu_hash[physical_id] |
116 | | - else: |
117 | | - curr_cpu = copy.copy(curr_entry) |
118 | | - curr_cpu["processors"] = [] |
119 | | - curr_cpu["processors"].append(processor) |
120 | | - core_id = curr_entry.get("core id") |
121 | | - assert isinstance(core_id, str) |
122 | | - processor2corecpu[processor] = (physical_id, core_id) |
123 | | - |
124 | | - return cpu_hash, processor2corecpu |
125 | | - |
126 | | - |
127 | 71 | def analyse_list_of_processes( |
128 | 72 | children: "Sequence[psutil.Process]", |
129 | 73 | processor2corecpu: "Mapping[str, Tuple[str, str]]", |
|
0 commit comments