Skip to content

Commit c990653

Browse files
Update index.md
1 parent 9d5cc17 commit c990653

File tree

1 file changed

+44
-31
lines changed

1 file changed

+44
-31
lines changed

docs/index.md

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,66 @@ title: CPU Load Generator
55

66
# CPU Load Generator Project
77

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 projecta Python-based tool that generates a fixed CPU load for a finite period using a PID regulator.
99

10-
## Motivation
10+
![PID Regulator Block Diagram](https://gaetanocarlucci.altervista.org/wp-content/uploads/2024/07/pid-1.png)
1111

12-
The CPU Load Generator serves multiple purposes:
12+
## 💡 Motivation
1313

14-
- **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:
1915

20-
## PID Regulator for Controlling CPU Load
16+
- **Performance Testing**
17+
- **Resource Allocation Optimization**
18+
- **Benchmarking**
19+
- **Education & Training**
20+
- **Thermal & Power Analysis**
2121

22-
The design of the CPU Load Generator involves several components:
22+
## 🧠 Architecture
2323

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:
3225

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)
3431

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
3633

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+
```
4038

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.
4240

43-
- **P** (Proportional)
44-
- **I** (Integral)
45-
- **D** (Derivative)
41+
## 🔧 Controller Thread (PID Regulator)
4642

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.
4844

4945
```python
5046
def generate_load(self, sleep_time):
5147
interval = time.time() + self.period - sleep_time
52-
# generates some CPU load for interval seconds
5348
while time.time() < interval:
54-
pr = 213123 # generates some load
49+
pr = 213123
5550
_ = pr * pr
56-
pr = pr + 1
51+
pr += 1
5752
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+
![50% Target CPU Load](https://gaetanocarlucci.altervista.org/wp-content/uploads/2024/07/50-Target-Load.png)
62+
63+
## 📎 Resources
64+
65+
- [Source Code on GitHub](https://github.com/GaetanoCarlucci/CPULoadGenerator)
66+
- [Original Article](https://gaetanocarlucci.altervista.org/cpu-load-generator-project/)
67+
68+
---
69+
70+
Thanks for checking out this project! Feel free to contribute or raise issues on the repository.

0 commit comments

Comments
 (0)