def _sys_stats_monitor(context): import gc from gevent.hub import _get_hub from gevent import sleep context = weakref.ref(context) # give gc a hand end = faststat.nanotime( ) # current time throws off duration stats less than 0 while 1: start = faststat.nanotime() tmp = context() if tmp is None or tmp.stopping: return # tmp.stats['gc.garbage'].add(len(gc.garbage)) # NOTE: gc.garbage() only does something if gc module has debug flag set counts = gc.get_count() for i in range(len(counts)): tmp.stats['gc.count' + str(i)].add(counts[i]) tmp.stats['greenlets.active'].add(_get_hub().loop.activecnt) tmp.stats['greenlets.pending'].add(_get_hub().loop.pendingcnt) try: tmp.stats['queues.cpu_bound.depth'].add( len(tmp.thread_locals.cpu_bound_thread.in_q)) except AttributeError: pass try: tmp.stats['queues.io_bound.depth'].add( tmp.thread_locals.io_bound_thread.task_queue._qsize()) except AttributeError: pass interval = tmp.monitor_interval end, prev = faststat.nanotime(), end # keep a rough measure of the fraction of time spent on monitoring if prev == end: tmp.stats['monitoring.overhead'].add(0) else: tmp.stats['monitoring.overhead'].add((end - start) / (end - prev)) tmp.durations['monitoring.duration'].end(start) tmp = None sleep(interval)
def _sys_stats_monitor(context): import gc from gevent.hub import _get_hub from gevent import sleep context = weakref.ref(context) # give gc a hand end = faststat.nanotime() # current time throws off duration stats less than 0 while 1: start = faststat.nanotime() tmp = context() if tmp is None or tmp.stopping: return # tmp.stats['gc.garbage'].add(len(gc.garbage)) # NOTE: gc.garbage() only does something if gc module has debug flag set counts = gc.get_count() for i in range(len(counts)): tmp.stats['gc.count' + str(i)].add(counts[i]) tmp.stats['greenlets.active'].add(_get_hub().loop.activecnt) tmp.stats['greenlets.pending'].add(_get_hub().loop.pendingcnt) try: tmp.stats['queues.cpu_bound.depth'].add( len(tmp.thread_locals.cpu_bound_thread.in_q)) except AttributeError: pass try: tmp.stats['queues.io_bound.depth'].add( tmp.thread_locals.io_bound_thread.task_queue._qsize()) except AttributeError: pass interval = tmp.monitor_interval end, prev = faststat.nanotime(), end # keep a rough measure of the fraction of time spent on monitoring if prev == end: tmp.stats['monitoring.overhead'].add(0) else: tmp.stats['monitoring.overhead'].add((end - start)/(end - prev)) tmp.durations['monitoring.duration'].end(start) tmp = None sleep(interval)
def _greenlet_spin_trace(self, why, gs): self.spin_count += 1 if self.ctx.running and why: if self.cur_pid != os.getpid(): self._start_thread() self.cur_pid = os.getpid() lt = self.last_spin ct = faststat.nanotime() self.last_spin = ct if not lt: return the_time = (ct - lt) * 1e6 if gs[0] is gevent.hub.get_hub(): self.ctx.stats['greenlet_idle(ms)'].add(the_time) else: self.ctx.stats['greenlet_switch(ms)'].add(the_time) ml.ld4("{1} {0}", why, the_time)
def do_read(self): # invoked via BaseServer._do_read. Whereas # StreamServer.do_read calls self.socket.accept, we just # need to pop off our queue if not self._watcher: return if not self._watcher.queue: raise RuntimeError('QUEUE DISAPPEARED') client_socket, address, exc, pushed_at = self._watcher.queue.pop() age = nanotime() - pushed_at context.get_context().stats[CONN_AGE_STATS].add(age / 1e6) if exc is not None: # raise the Exception raise exc return gevent.socket.socket(_sock=client_socket), address
def _thread_spin_monitor(self): while 1: time.sleep(0.05) if not self.ctx.tracing or self is not self.MAIN_INSTANCE: return if not self.last_spin: continue ct = faststat.nanotime() dur = ct - self.last_spin # if time is greater than 150 ms if dur > 150e6 and time.time() - self.last_cal_log > 1: tid = self.main_thread_id frame = sys._current_frames()[tid] # specifically dont log pdb if frame.f_code is not cmd.Cmd.cmdloop.im_func.func_code: stack = _format_stack(frame) self.ctx.log.info('LONG_SPIN').failure(time=dur/1e6, slow_green=stack) self.last_cal_log = time.time()
def _thread_spin_monitor(self): while 1: time.sleep(0.05) if not self.ctx.tracing or self is not self.MAIN_INSTANCE: return if not self.last_spin: continue ct = faststat.nanotime() dur = ct - self.last_spin # if time is greater than 150 ms if dur > 150e6 and time.time() - self.last_cal_log > 1: tid = self.main_thread_id frame = sys._current_frames()[tid] # specifically dont log pdb if frame.f_code is not cmd.Cmd.cmdloop.im_func.func_code: stack = _format_stack(frame) self.ctx.log.info('LONG_SPIN').failure(time=dur / 1e6, slow_green=stack) self.last_cal_log = time.time()
def g(*a, **kw): s = faststat.nanotime() r = f(*a, **kw) context.get_context().stats[name].add((faststat.nanotime() - s) / 1e6) return r