You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/index.md
+44-31Lines changed: 44 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,53 +5,66 @@ title: CPU Load Generator
5
5
6
6
# CPU Load Generator Project
7
7
8
-
Welcome to the CPU Load Generator project, a Python-based tool designed to generate a fixed CPU load for a finite period using a PID regulator. This tool is particularly useful for system administrators, developers, and researchers.
8
+
Welcome to the CPU Load Generator project — a Python-based tool that generates a fixed CPU load for a finite period using a PID regulator.
-**Performance Testing**: Stress test applications and systems by generating a specific CPU load, helping identify performance bottlenecks and ensuring robustness under different load conditions.
15
-
-**Resource Allocation Optimization**: Simulate various load scenarios to optimize resource allocation and ensure critical applications receive the necessary computational power.
16
-
-**Benchmarking**: Provide a consistent and repeatable load for precise benchmarking, facilitating the comparison of different systems, CPUs, or configurations.
17
-
-**Education and Training**: Demonstrate the effects of CPU load on system performance, offering hands-on experience for students and trainees. University students can also understand the practical application of a PID regulator in a discrete environment.
18
-
-**Thermal and Power Consumption Analysis**: Study the thermal behavior and power consumption of CPUs under controlled load conditions.
14
+
This project is ideal for:
19
15
20
-
## PID Regulator for Controlling CPU Load
16
+
-**Performance Testing**
17
+
-**Resource Allocation Optimization**
18
+
-**Benchmarking**
19
+
-**Education & Training**
20
+
-**Thermal & Power Analysis**
21
21
22
-
The design of the CPU Load Generator involves several components:
22
+
## 🧠 Architecture
23
23
24
-
-**CPU Target Load**: Desired percentage of CPU load on a specific core.
25
-
-**Monitor Thread**: Measures the percentage of actual CPU load on the specific core.
26
-
-**Error**: Percentage error of measured CPU load with respect to the target.
27
-
-**Controller Thread**: PID regulator with integral and proportional actions.
28
-
-**Controller Output**: The actuator signal that adjusts the CPU core load based on the measured error.
29
-
-**CPU Load**: The actual plant is the specific CPU core load.
30
-
-**Disturb**: Any other process that can disturb the CPU core load.
31
-
-**Process Variable**: The actual CPU load measured by the Monitor Thread after the control action.
24
+
The system consists of:
32
25
33
-
### Monitor Thread
26
+
-**CPU Target Load**
27
+
-**Monitor Thread** for actual CPU usage measurement
28
+
-**Controller Thread** (PID regulator)
29
+
-**Actuator** adjusting sleep time for control
30
+
-**Disturbances** (e.g., other processes)
34
31
35
-
The Monitor Thread samples the CPU load at regular intervals, filters the measurements with a first-order filter, and logs various parameters over time. The value measured by the Monitor Thread is calculated using the `psutil` method `cpu_percent(interval)`, which calculates the CPU usage during the specified interval. The call to `cpu_percent` blocks the thread for the duration of the interval, ensuring that the loop naturally waits for the specified sampling time before proceeding to the next iteration. In this case, `psutil.Process` is used, meaning that the method measures the CPU usage generated by this particular process.
32
+
## 🧪 Monitor Thread
36
33
37
-
### Controller Thread
38
-
39
-
The Controller Thread compares the CPU load measured by the Monitor Thread and the target CPU load (set point). Based on the difference between them (tracking error), the PID regulator computes the control signal and sends it to the actuation device, which in turn drives the plant to the desired process value (set point).
34
+
Uses `psutil` to sample CPU load:
35
+
```python
36
+
psutil.cpu_percent(interval)
37
+
```
40
38
41
-
When the error is fed to the PID regulator, it computes the:
39
+
This blocks the thread for the sampling interval, ensuring timing consistency.
42
40
43
-
-**P** (Proportional)
44
-
-**I** (Integral)
45
-
-**D** (Derivative)
41
+
## 🔧 Controller Thread (PID Regulator)
46
42
47
-
contributions of this error signal with respect to time. In this project, the derivative contribution is not used. The proportional and integral components are weighted by coefficients and then summed up. The output of this operation is the actuator signal, which in our case is the sleep time used in the actuator function that generates CPU load.
43
+
Calculates a control signal to modulate load. If error is positive (load too low), it **reduces sleep** to increase load, and vice versa.
48
44
49
45
```python
50
46
defgenerate_load(self, sleep_time):
51
47
interval = time.time() +self.period - sleep_time
52
-
# generates some CPU load for interval seconds
53
48
while time.time() < interval:
54
-
pr =213123# generates some load
49
+
pr =213123
55
50
_ = pr * pr
56
-
pr = pr +1
51
+
pr +=1
57
52
time.sleep(sleep_time)
53
+
```
54
+
55
+
Tuned experimentally with `kp` and `ki`.
56
+
57
+
## 📊 Results
58
+
59
+
The figure below shows the controller maintaining 50% CPU load.
60
+
61
+

62
+
63
+
## 📎 Resources
64
+
65
+
-[Source Code on GitHub](https://github.com/GaetanoCarlucci/CPULoadGenerator)
0 commit comments