forked from GaetanoCarlucci/CPULoadGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPULoadGenerator.py
More file actions
executable file
·156 lines (123 loc) · 3.93 KB
/
Copy pathCPULoadGenerator.py
File metadata and controls
executable file
·156 lines (123 loc) · 3.93 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
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
#!/usr/bin/env python
#Authors: Gaetano Carlucci
# Giuseppe Cofano
import os, psutil,math
import threading
import time
import numpy as np
from collections import deque
import matplotlib.pyplot as plt
from twisted.python import usage
class Options(usage.Options):
optParameters = [
["CPU load", "l", 0.2, None, float],
["duration", "d", 20, None, float],
]
class MonitorThread(threading.Thread):
def __init__(self):
self.interval = 0.1;
self.sample = 0;
self.cpu = 0;
self.running = 1;
self.alpha = 0.1;
self.cpu_log = list()
super(MonitorThread, self).__init__()
def get_cpu_load(self):
return self.cpu
def run(self):
p = psutil.Process(os.getpid())
p.cpu_affinity([0]) #the process is forced to run only on CPU 0
while self.running:
self.sample = p.cpu_percent(interval=self.interval)
self.cpu = self.alpha * self.sample + (1-self.alpha)*self.cpu
self.cpu_log.append(self.cpu)
class ControllerThread(threading.Thread):
def __init__(self):
self.running = 1;
self.sleepTime = 0.0;
self.CT = 0.20; #default value
self.cpu = 0;
self.ki = -1;
self.kp = -0.5;
self.int_err = 0;
self.last_ts = time.time();
super(ControllerThread, self).__init__()
def getSleepTime(self):
return self.sleepTime
def getCpuTarget(self):
return self.CT
def setCpu(self, cpu):
self.cpu = cpu
def setCpuTarget(self, CT):
self.CT = CT
def run(self):
#through this timer this cycle has the same sampling interval as the cycle in MonitorThread
while self.running:
time.sleep(0.1)
self.err = self.CT - self.cpu*0.01
ts = time.time()
samp_int = ts - self.last_ts
self.int_err = self.int_err + self.err*samp_int
self.last_ts = ts
self.sleepTime = self.kp*self.err + self.ki*self.int_err
if self.sleepTime < 0:
self.sleepTime = 0;
self.int_err = self.int_err - self.err*samp_int
if __name__ == "__main__":
import sys
options = Options()
try:
options.parseOptions()
except Exception, e:
print '%s: %s' % (sys.argv[0], e)
print '%s: Try --help for usage details.' % (sys.argv[0])
sys.exit(1)
else:
if options['CPU load'] < 0 or options['CPU load'] > 1:
raise(ValueError, "CPU load setpoint out of the range [0,1]")
if options['duration'] < 0:
raise(ValueError, "Invalid negative duration")
monitor = MonitorThread()
monitor.start()
SAMPLES=1000;
control = ControllerThread()
control.start()
control.setCpuTarget(options['CPU load'])
last_ts = time.time()
a1 = deque([0]*SAMPLES)
target = deque([0]*SAMPLES)
tempoReale = deque([0]*SAMPLES)
x = plt.axes(xlim=(0, options['duration']), ylim=(0, 100))
line1, = plt.plot(a1)
line2, = plt.plot(target)
plt.ion()
plt.ylim([0,100])
plt.show()
tempo = 0;
plt.xlabel('Time(sec)')
plt.ylabel('CPU load [0] %')
while tempo < options['duration']:
for i in range(1,2):
pr = 213123 + 324234 * 23423423
control.setCpu(monitor.get_cpu_load())
sleep_time = control.getSleepTime()
time.sleep(sleep_time)
ts = time.time()
delta = ts - last_ts
last_ts = ts
tempo += delta
a1.appendleft(monitor.get_cpu_load())
target.appendleft(control.getCpuTarget()*100)
tempoReale.appendleft(tempo)
a1.pop()
target.pop()
tempoReale.pop()
line1.set_ydata(a1)
line1.set_xdata(tempoReale)
line2.set_xdata(tempoReale)
line2.set_ydata(target)
plt.draw()
monitor.running = 0;
control.running = 0;
monitor.join()
control.join()