-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw_cpu.py
359 lines (311 loc) · 12 KB
/
draw_cpu.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
import os
import argparse
import math
import time
import pandas as pd
from perfetto.trace_processor import TraceProcessor, TraceProcessorConfig
from tqdm import tqdm # Ensure tqdm is installed
def calculate_cpu_load_sliding_window_base(
df: pd.DataFrame,
window_size_ns: int,
window_move_ns: int,
num_cpus=None,
per_core=False,
progress_bar=None,
):
"""
Calculate CPU load using a sliding window approach.
Parameters:
- df (pd.DataFrame): DataFrame containing running slices with columns ['ts_ns', 'dur', 'ucpu']
- window_size_ns (int): Size of the window in nanoseconds.
- window_move_ns (int): Step size to move the window in nanoseconds.
- num_cpus (int): Number of unique CPU cores (required if per_core is False).
- per_core (bool): Whether to calculate per-core CPU load.
- progress_bar (tqdm.tqdm): Progress bar instance.
Returns:
- pd.DataFrame: DataFrame with window start time (ms) and CPU load percentage.
"""
# Determine trace duration
trace_end_ns = df["ts_end_ns"].max()
trace_start_ns = df["ts_ns"].min()
# Total trace duration
total_duration_ns = trace_end_ns - trace_start_ns
# Generate window start times
num_windows = (
int(math.ceil((total_duration_ns - window_size_ns) / window_move_ns)) + 1
) - 4 # Last 4 windows are not accurate
window_start_ns = [i * window_move_ns for i in range(num_windows)]
# Initialize list to store CPU load per window
cpu_loads = []
def calculate_load_percentage(slice_df, start_ns, end_ns, num_cpus):
overlapping = slice_df[
(slice_df["ts_ns"] < end_ns)
& (slice_df["ts_end_ns"] > start_ns)
]
if overlapping.empty:
return 0
else:
overlapping = overlapping.copy()
overlapping["effective_start_ns"] = overlapping["ts_ns"].clip(
lower=start_ns
)
overlapping["effective_end_ns"] = (
overlapping["ts_end_ns"]
).clip(upper=end_ns)
overlapping["overlap_ns"] = (
overlapping["effective_end_ns"] - overlapping["effective_start_ns"]
)
overlapping["overlap_ns"] = overlapping["overlap_ns"].clip(lower=0)
total_running_ns = overlapping["overlap_ns"].sum()
cpu_load_percentage = (total_running_ns / (num_cpus * window_size_ns)) * 100
cpu_load_percentage = min(cpu_load_percentage, 100.0)
return cpu_load_percentage
if per_core:
cpu_list = sorted(df["ucpu"].unique())
for ucpu in cpu_list:
if progress_bar:
window_iter = tqdm(
window_start_ns, desc=f"Calculating CPU {ucpu} Load", unit="win"
)
else:
window_iter = window_start_ns
for start_ns in window_iter:
end_ns = start_ns + window_size_ns
cpu_slices = df[df["ucpu"] == ucpu]
cpu_load_percentage = calculate_load_percentage(
cpu_slices, start_ns, end_ns, 1
)
cpu_loads.append(
{
"window_start_ms": start_ns / 1_000_000,
"ucpu": ucpu,
"cpu_load_percentage": cpu_load_percentage,
}
)
else:
if progress_bar:
window_iter = tqdm(window_start_ns, desc="Calculating CPU Load", unit="win")
else:
window_iter = window_start_ns
for start_ns in window_iter:
end_ns = start_ns + window_size_ns
cpu_load_percentage = calculate_load_percentage(df, start_ns, end_ns, num_cpus)
cpu_loads.append(
{
"window_start_ms": start_ns / 1_000_000,
"cpu_load_percentage": cpu_load_percentage,
}
)
cpu_load_df = pd.DataFrame(cpu_loads)
return cpu_load_df
def calculate_dynamic_window_params(total_duration_ns, desired_points=200):
"""
Calculate window size and move in nanoseconds to achieve approximately desired_points.
Parameters:
- total_duration_ns (int): Total duration of the trace in nanoseconds.
- desired_points (int): Desired number of points/windows.
Returns:
- tuple: (window_size_ns, window_move_ns)
"""
# To have desired_points, window_move_ns = total_duration_ns / desired_points
window_move_ns = total_duration_ns / desired_points
# Set window_size_ns equal to window_move_ns for 100% overlap
window_size_ns = window_move_ns
return int(window_size_ns), int(window_move_ns)
def main():
parser = argparse.ArgumentParser(
description="Calculate CPU load over time from a Perfetto trace file."
)
parser.add_argument(
"-f", "--file", help="Absolute path to trace", type=str, required=True
)
parser.add_argument(
"-b",
"--binary",
help="Absolute path to a trace processor binary",
type=str,
required=True,
)
parser.add_argument(
"-p",
"--plot",
help="Plot the CPU load curves using matplotlib (optional)",
action="store_true",
)
parser.add_argument(
"--window_size_ms",
help="Window size in milliseconds. If not provided, calculated to have ~200 points.",
type=int,
default=None,
)
parser.add_argument(
"--window_move_ms",
help="Window move (step) in milliseconds. If not provided, calculated based on window size.",
type=int,
default=None,
)
parser.add_argument(
"--output",
help="Base path to save the CPU load DataFrames as CSV (optional)",
type=str,
default=None,
)
args = parser.parse_args()
# Initialize TraceProcessor
config = TraceProcessorConfig(bin_path=args.binary)
try:
print('Loading trace...')
processor = TraceProcessor(trace=args.file, config=config)
except Exception as e:
raise RuntimeError(f"Failed to initialize TraceProcessor: {e}")
sql_path = os.path.join(
os.path.dirname(__file__), "sql", "thread_running_slices.sql"
)
with open(sql_path, "r") as f:
query = f.read()
# Execute the query and convert to DataFrame
try:
df = processor.query(query).as_pandas_dataframe()
except Exception as e:
processor.close()
raise RuntimeError(f"Failed to execute SQL query: {e}")
print("Queried data frame: ")
print(" size: ", df.shape)
print(" columns: ", df.columns)
print(" head: ", df.head())
# Close the TraceProcessor
processor.close()
# Validate necessary columns
required_columns = {"ts_ns", "ts_end_ns", "ucpu"}
if not required_columns.issubset(df.columns):
raise ValueError(f"The DataFrame must contain columns: {required_columns}")
# Determine trace duration
trace_end_ns = df["ts_end_ns"].max()
trace_start_ns = df["ts_ns"].min()
total_duration_ns = trace_end_ns - trace_start_ns
# Check for excessively large durations
max_windows = 2000
if total_duration_ns <= 0:
raise ValueError("Invalid trace duration calculated.")
# Calculate window parameters
if args.window_size_ms and args.window_move_ms:
window_size_ns = args.window_size_ms * 1_000_000
window_move_ns = args.window_move_ms * 1_000_000
num_windows = (
int(math.ceil((total_duration_ns - window_size_ns) / window_move_ns)) + 1
)
else:
# Calculate window size and move to have approximately 200 points
window_size_ns, window_move_ns = calculate_dynamic_window_params(
total_duration_ns, desired_points=200
)
num_windows = (
int(math.ceil((total_duration_ns - window_size_ns) / window_move_ns)) + 1
)
if num_windows > max_windows:
raise ValueError(
f"Number of windows ({num_windows}) exceeds the maximum allowed ({max_windows}). Please specify larger window size or larger window move."
)
# If window_size_ms or window_move_ms were not provided, convert the calculated values to ms
if args.window_size_ms is None or args.window_move_ms is None:
window_size_ms = window_size_ns / 1_000_000
window_move_ms = window_move_ns / 1_000_000
print(f"\nCalculated window parameters for ~200 points:")
print(f" Window Size: {window_size_ms:.2f} ms")
print(f" Window Move: {window_move_ms:.2f} ms")
else:
window_size_ms = args.window_size_ms
window_move_ms = args.window_move_ms
# Calculate number of CPUs
num_cpus = df["ucpu"].nunique()
if num_cpus == 0:
raise ValueError("No CPUs found in the data.")
# Calculate overall CPU load using sliding window with progress bar
print("\nCalculating overall CPU load...")
start = time.time()
cpu_load_df = calculate_cpu_load_sliding_window_base(
df,
window_size_ns=window_size_ns,
window_move_ns=window_move_ns,
num_cpus=num_cpus,
per_core=False,
progress_bar=True,
)
print("Overall CPU Load Data Frame:")
print(cpu_load_df.head())
# Calculate per-CPU load using sliding window with progress bar
print("\nCalculating per-CPU load...")
cpu_load_per_core_df = calculate_cpu_load_sliding_window_base(
df,
window_size_ns=window_size_ns,
window_move_ns=window_move_ns,
num_cpus=num_cpus,
per_core=True,
progress_bar=True,
)
print("Per-CPU Load Data Frame:")
print(cpu_load_per_core_df.head())
end = time.time()
print(f'\nTime taken: {end - start:.2f} seconds')
# Optionally save the CPU load DataFrames to CSV files
if args.output:
try:
# Ensure the output directory exists
output_dir = os.path.dirname(args.output)
if output_dir:
os.makedirs(output_dir, exist_ok=True)
# Save overall CPU load
overall_output_path = args.output + "_overall.csv"
cpu_load_df.to_csv(overall_output_path, index=False)
print(f"\nOverall CPU load data saved to {overall_output_path}")
# Save per-CPU load
per_core_output_path = args.output + "_per_core.csv"
cpu_load_per_core_df.to_csv(per_core_output_path, index=False)
print(f"Per-CPU load data saved to {per_core_output_path}")
except Exception as e:
print(f"Failed to save CSV files: {e}")
if not args.plot:
return
# Optionally, plot the CPU load curves
try:
import matplotlib.pyplot as plt
# Create a figure with two subplots
fig, axs = plt.subplots(2, 1, figsize=(15, 12), sharex=True)
# Plot Overall CPU Load
axs[0].plot(
cpu_load_df["window_start_ms"],
cpu_load_df["cpu_load_percentage"],
marker="o",
linestyle="-",
color="blue",
markersize=3,
)
axs[0].set_title("Overall CPU Load Over Time")
axs[0].set_ylabel("CPU Load (%)")
axs[0].grid(True)
# Plot Per-CPU Load
for ucpu in sorted(cpu_load_per_core_df["ucpu"].unique()):
core_data = cpu_load_per_core_df[cpu_load_per_core_df["ucpu"] == ucpu]
axs[1].plot(
core_data["window_start_ms"],
core_data["cpu_load_percentage"],
marker="o",
linestyle="-",
label=f"CPU {ucpu}",
markersize=3,
)
axs[1].set_title("Per-CPU Load Over Time")
axs[1].set_xlabel("Time (ms)")
axs[1].set_ylabel("CPU Load (%)")
axs[1].legend()
axs[1].grid(True)
plt.tight_layout()
plt.show()
except ImportError:
print(
"\nmatplotlib is not installed. Install it to visualize the CPU load curves."
)
except Exception as e:
print(f"\nFailed to plot CPU load curves: {e}")
if __name__ == "__main__":
main()