-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu_one.py
More file actions
65 lines (52 loc) · 2.59 KB
/
Copy pathcpu_one.py
File metadata and controls
65 lines (52 loc) · 2.59 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
import multiprocessing
import time
import psutil
import datetime
import sounddevice as sd
import scipy.io.wavfile as wav
import numpy as np
# Ensure compatibility with Windows multiprocessing
multiprocessing.set_start_method("spawn", force=True)
def log_system_metrics():
"""Logs current CPU usage."""
cpu_usage = psutil.cpu_percent(interval=0.1)
timestamp = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print(f"[{timestamp}] CPU Usage: {cpu_usage}%")
def heavy_computation(_):
"""Performs CPU-intensive operations for a fixed time."""
start_time = time.time()
while time.time() - start_time < 0.8: # Ensure the task runs for 0.8 seconds
[x * x for x in range(50_000_000)]
def cpu_intensive_task(num_workers=None):
"""Runs CPU load using multiple workers in a process pool."""
if num_workers is None:
num_workers = multiprocessing.cpu_count() # Use all available cores
print(f"[{datetime.datetime.now()}] CPU-intensive task started with {num_workers} workers.")
with multiprocessing.Pool(num_workers) as pool:
pool.map(heavy_computation, range(num_workers))
log_system_metrics()
print(f"[{datetime.datetime.now()}] CPU-intensive task completed.")
def record_audio(duration, samplerate=44100):
"""Records audio and returns the audio data."""
print(f"[{datetime.datetime.now()}] Recording started.")
recording = sd.rec(int(duration * samplerate), samplerate=samplerate, channels=1, dtype='float32')
sd.wait() # Block until recording is done
print(f"[{datetime.datetime.now()}] Recording complete.")
return recording, samplerate
def run_experiment(iterations=5):
"""Runs the experiment with CPU workload alternating with idle periods, while recording audio."""
print(f"[{datetime.datetime.now()}] Experiment started.")
total_duration = iterations * (0.8 + 0.4) # Total recording time
recording, samplerate = record_audio(total_duration) # Start recording synchronously
for i in range(iterations):
print(f"[{datetime.datetime.now()}] Iteration {i+1}: Starting active workload period.")
cpu_intensive_task()
print(f"[{datetime.datetime.now()}] Iteration {i+1}: Starting inactive period.")
time.sleep(0.4)
# Save recorded audio after experiment is done
filename = "cpu_leakage.wav"
wav.write(filename, samplerate, (recording * 32767).astype(np.int16)) # Convert to 16-bit PCM
print(f"[{datetime.datetime.now()}] Recording saved as {filename}")
print(f"[{datetime.datetime.now()}] Experiment complete.")
if __name__ == "__main__":
run_experiment()