Skip to content

Commit 4f25e1c

Browse files
committed
python.d.plugin better iteration timing
1 parent f2db420 commit 4f25e1c

1 file changed

Lines changed: 21 additions & 24 deletions

File tree

python.d/python_modules/base.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -116,36 +116,21 @@ def _run_once(self):
116116
Return value presents exit status of update()
117117
:return: boolean
118118
"""
119-
t_start = time.time()
120-
timetable = self.timetable
119+
t_start = float(time.time())
121120
chart_name = self.chart_name
122121

123-
# check if it is time to execute job update() function
124-
if timetable['next'] > t_start:
125-
self.debug(chart_name, "will be run in", str(int((timetable['next'] - t_start) * 1000)), "ms")
126-
return True
127-
128-
since_last = int((t_start - timetable['last']) * 1000000)
129-
self.debug(chart_name,
130-
"ready to run, after", str(int((t_start - timetable['last']) * 1000)),
131-
"ms (update_every:", str(timetable['freq'] * 1000),
132-
"ms, latency:", str(int((t_start - timetable['next']) * 1000)), "ms")
133-
122+
since_last = int((t_start - self.timetable['last']) * 1000000)
134123
if self.__first_run:
135124
since_last = 0
136125

137126
if not self.update(since_last):
138127
self.error("update function failed.")
139128
return False
140129

141-
t_end = time.time()
142-
self.timetable['next'] = t_end - (t_end % timetable['freq']) + timetable['freq']
143-
144130
# draw performance graph
145-
run_time = str(int((t_end - t_start) * 1000))
146-
# noinspection SqlNoDataSourceInspection
131+
run_time = int((time.time() - t_start) * 1000)
147132
print("BEGIN netdata.plugin_pythond_%s %s\nSET run_time = %s\nEND\n" %
148-
(self.chart_name, str(since_last), run_time))
133+
(self.chart_name, str(since_last), str(run_time)))
149134

150135
self.debug(chart_name, "updated in", str(run_time), "ms")
151136
self.timetable['last'] = t_start
@@ -158,26 +143,38 @@ def run(self):
158143
Exits when job failed or timed out.
159144
:return: None
160145
"""
161-
self.timetable['last'] = time.time()
162-
self.debug("starting data collection - update frequency: " + str(self.update_every) + ", retries allowed: " + str(self.retries))
146+
step = float(self.timetable['freq'])
147+
self.timetable['last'] = float(time.time() - step)
148+
self.debug("starting data collection - update frequency:", str(step), ", retries allowed:", str(self.retries))
163149
while True: # run forever, unless something is wrong
150+
now = float(time.time())
151+
next = self.timetable['next'] = now - (now % step) + step + (step / 3) # add 1/3 into the iteration to sync with netdata
152+
153+
# it is important to do this in a loop
154+
# sleep() is interruptable
155+
while now < next:
156+
self.debug("sleeping for", str(next - now), "secs to reach frequency of", str(step), "secs, now:", str(now), ", next:", str(next))
157+
time.sleep(next - now)
158+
now = float(time.time())
159+
160+
# do the job
164161
try:
165162
status = self._run_once()
166163
except Exception as e:
167164
self.alert("internal error - aborting data collection: " + str(e))
168165
return
169166

170-
if status: # handle retries if update failed
171-
time.sleep(max (0, self.timetable['next'] - time.time()))
167+
if status:
168+
# it is good
172169
self.retries_left = self.retries
173170
else:
171+
# it failed
174172
self.retries_left -= 1
175173
if self.retries_left <= 0:
176174
self.alert("failed to collect data - no more retries allowed - aborting data collection")
177175
return
178176
else:
179177
self.error("failed to collect data. " + str(self.retries_left) + " retries left.")
180-
time.sleep(self.timetable['freq'])
181178

182179
# --- CHART ---
183180

0 commit comments

Comments
 (0)