better timing
paulfantom committed
Jun 12, 2016 at 13:31 UTC
8ac5e86ffdb70b779223438cd9a2aa33846f8926
1 file changed
+39
-35
plugins.d/python.d.plugin
+39
-35
@@ -18,12 +18,15 @@ except AssertionError:
18
class PythonCharts(object):
19
20
def __init__(self,
21
- interval=1,
21
+ interval=None,
22
modules=[],
23
modules_path='../python.d/',
24
modules_configs='../conf.d/'):
25
self.first_run = True
26
- self.interval = interval
26
+ if interval is None:
27
+ self.interval = 1
28
+ else:
29
+ self.interval = interval
30
self.default_priority = 90000
31
# check if plugin directory exists
32
if not os.path.isdir(modules_path):
@@ -54,22 +57,18 @@ class PythonCharts(object):
57
# set last execution and execution frequency dict
58
self.timetable = {}
59
for m in self.modules:
57
- try:
58
- interval = m.update_every
59
- interval = int(interval)
60
- except AttributeError:
61
- interval = self.interval
62
- except ValueError:
63
- interval = self.interval
64
- debug(m.__name__ +
65
- ": wrong value of 'update_every' setting to " +
66
- str(self.interval))
67
- # ensure module update frequency is not less than execution of this program
68
- if interval < self.interval:
60
+ if self.interval is not None:
61
interval = self.interval
70
- # charts updates are twice as frequent as user specified
71
- self.timetable[m.__name__] = [0, interval/2.0]
72
-
62
+ else:
63
+ try:
64
+ interval = int(m.update_every)
65
+ except (AttributeError, ValueError):
66
+ interval = 1
67
+
68
+ now = time.time()
69
+ self.timetable[m.__name__] = {'last' : 0,
70
+ 'next' : now,
71
+ 'freq' : interval}
72
# set priorities
73
for m in self.modules:
74
try:
@@ -159,7 +158,7 @@ class PythonCharts(object):
158
" '' 'Execution time for " +
159
chart +
160
" plugin' 'milliseconds / run' python.d netdata.plugin_python area 145000 " +
162
- str(self.timetable[mod.__name__][1]) +
161
+ str(self.timetable[mod.__name__]['freq']) +
162
'\n')
163
sys.stdout.write("DIMENSION run_time 'run time' absolute 1 1\n\n")
164
sys.stdout.flush()
@@ -169,16 +168,16 @@ class PythonCharts(object):
168
self.disable_module(mod, "misbehaving")
169
170
def update_module(self, mod):
172
- t1 = time.time()
171
+ t_start = time.time()
172
# check if it is time to execute module update() function
174
- if (t1 - self.timetable[mod.__name__][0]) < self.timetable[mod.__name__][1]:
173
+ if self.timetable[mod.__name__]['next'] > t_start:
174
return
175
try:
177
- if not self.first_run:
178
- t = ""
176
+ if self.first_run:
177
+ since_last = 0
178
else:
180
- t=int((t1- self.timetable[mod.__name__][0]) * 1000000)
181
- if not mod.update(t):
179
+ since_last = int((t_start - self.timetable[mod.__name__]['last']) * 1000000)
180
+ if not mod.update(since_last):
181
self.disable_module(mod, "update failed")
182
return
183
except AttributeError:
@@ -187,23 +186,27 @@ class PythonCharts(object):
186
except UnboundLocalError:
187
self.disable_module(mod, "misbehaving")
188
return
190
- t2 = time.time()
191
- if not self.first_run:
192
- dt = ""
189
+ t_end = time.time()
190
+ self.timetable[mod.__name__]['next'] = t_end - (t_end % self.timetable[mod.__name__]['freq']) + self.timetable[mod.__name__]['freq']
191
+ if self.first_run:
192
+ dt = 0
193
else:
194
- dt = int((t2 - self.timetable[mod.__name__][0]) * 1000000)
195
- sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(dt)+'\n')
196
- sys.stdout.write("SET run_time = " + str(int((t2 - t1) * 1000)) + '\n')
194
+ dt = int((t_end - self.timetable[mod.__name__]['last']) * 1000000)
195
+ sys.stdout.write("BEGIN netdata.plugin_pythond_"+mod.__name__+" "+str(since_last)+'\n')
196
+ sys.stdout.write("SET run_time = " + str(int((t_end - t_start) * 1000)) + '\n')
197
sys.stdout.write("END\n")
198
sys.stdout.flush()
199
- self.timetable[mod.__name__][0] = t1
199
+ self.timetable[mod.__name__]['last'] = t_start
200
201
def update(self):
202
+ self.first_run = True
203
while True:
204
t_begin = time.time()
205
+ next_runs = []
206
for mod in self.modules:
207
self.update_module(mod)
206
- time.sleep((self.interval - (time.time() - t_begin)) / 2)
208
+ next_runs.append(self.timetable[mod.__name__]['next'])
209
+ time.sleep(min(next_runs) - time.time())
210
self.first_run = False
211
212
@@ -314,14 +317,15 @@ def run():
317
# parse passed command line arguments
318
out = parse_cmdline(modules_dir, *sys.argv)
319
modules = out['modules']
317
- if out['interval'] is not None:
318
- interval = out['interval']
320
+# if out['interval'] is not None:
321
+# interval = out['interval']
322
323
# configure environement to run modules
324
#sys.path.append(modules_dir+"python_modules") # append path to directory with modules dependencies
325
326
# run plugins
324
- charts = PythonCharts(interval, modules, modules_dir, modules_conf)
327
+# charts = PythonCharts(interval, modules, modules_dir, modules_conf)
328
+ charts = PythonCharts(out['interval'], modules, modules_dir, modules_conf)
329
charts.check()
330
charts.create()
331
charts.update()