@cryptotaxi247 / netdata-1 / commits / 4f25e1c2b

python.d.plugin better iteration timing

Costa Tsaousis (ktsaou) committed Nov 14, 2016 at 23:19 UTC 4f25e1c2b2401c60a0b6b041573a75bd1bde77e2
1 file changed +21 -24
python.d/python_modules/base.py
+21 -24
@@ -116,21 +116,10 @@ class SimpleService(threading.Thread):
116 Return value presents exit status of update()
117 :return: boolean
118 """
119 - t_start = time.time()
120 - timetable = self.timetable
119 + t_start = float(time.time())
120 chart_name = self.chart_name
121
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)
123 if self.__first_run:
124 since_last = 0
125
@@ -138,14 +127,10 @@ class SimpleService(threading.Thread):
127 self.error("update function failed.")
128 return False
129
141 - t_end = time.time()
142 - self.timetable['next'] = t_end - (t_end % timetable['freq']) + timetable['freq']
143 -
130 # 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)
132 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)))
134
135 self.debug(chart_name, "updated in", str(run_time), "ms")
136 self.timetable['last'] = t_start
@@ -158,26 +143,38 @@ class SimpleService(threading.Thread):
143 Exits when job failed or timed out.
144 :return: None
145 """
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))
149 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
161 try:
162 status = self._run_once()
163 except Exception as e:
164 self.alert("internal error - aborting data collection: " + str(e))
165 return
166
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
169 self.retries_left = self.retries
170 else:
171 + # it failed
172 self.retries_left -= 1
173 if self.retries_left <= 0:
174 self.alert("failed to collect data - no more retries allowed - aborting data collection")
175 return
176 else:
177 self.error("failed to collect data. " + str(self.retries_left) + " retries left.")
180 - time.sleep(self.timetable['freq'])
178
179 # --- CHART ---
180