def auto_commit(self): timer_id = (yield) if timer_id == self._commit_timer_id: start_ms = timers.get_monotonic_timestamp_in_ms() self._session.commit() elapsed_ms = timers.get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data("database-commits (periodic)", elapsed_ms / 100, "decisecond") self._commit_timer_id = None
def commit(self): if self._commit_inline: start_ms = timers.get_monotonic_timestamp_in_ms() self._session.commit() elapsed_ms = timers.get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data("database-commits (inline)", elapsed_ms / 100, "decisecond") else: if self._commit_timer_id is None: self._commit_timer_id \ = timers.timers_create_timer('db-auto-commit', 1, 1, self.auto_commit)
def schedule(self): """ Schedule timers """ now_ms = get_monotonic_timestamp_in_ms() ms_expired = now_ms - self._scheduler_timestamp_ms if ms_expired < self._scheduler_interval_ms: DLOG.verbose("Not enough time has elapsed to schedule timers, " "ms_expired=%d ms." % ms_expired) return if 0 != self._scheduler_timestamp_ms: if ms_expired >= self._scheduler_max_delay_ms: if self._scheduling_on_time: self._scheduling_on_time = False DLOG.info("Not scheduling on time, elapsed=%d ms." % ms_expired) self._scheduler_delay_timestamp_ms \ = get_monotonic_timestamp_in_ms() else: if not self.scheduling_on_time: ms_expired = now_ms - self._scheduler_delay_timestamp_ms if ms_expired > self._scheduler_delay_debounce_ms: self._scheduling_on_time = True DLOG.info("Now scheduling on time.") self._scheduler_timestamp_ms = now_ms self._scheduling_timers = True overall_start_ms = get_monotonic_timestamp_in_ms() try: DLOG.verbose('Scheduling timers.') for timer in self._timers: start_ms = get_monotonic_timestamp_in_ms() rearm = timer.callback(now_ms) elapsed_ms = get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data("timer callback: " + timer.timer_name, elapsed_ms / 100, "decisecond") if not rearm and timer.timer_id not in self._timers_to_delete: self._timers_to_delete.append(timer.timer_id) finally: self._scheduling_timers = False # Cleanup pending timers to be deleted. self._timers[:] = [timer for timer in self._timers if timer.timer_id not in self._timers_to_delete] # Cleanup list of timers to delete del self._timers_to_delete[:] elapsed_ms = get_monotonic_timestamp_in_ms() - overall_start_ms histogram.add_histogram_data("timer overall time per dispatch: ", elapsed_ms / 100, "decisecond")
def get_task_work_result(self): """ Returns the result of task work completed """ result = self._worker.get_result() if hasattr(result.ancillary_result_data, 'execution_time'): histogram.add_histogram_data( result.name + ' [worker-execution-time]', result.ancillary_result_data.execution_time, 'secs') now_ms = timers.get_monotonic_timestamp_in_ms() elapsed_secs = (now_ms - result.create_timestamp_ms) / 1000 histogram.add_histogram_data(result.name + ' [execution-time]', elapsed_secs, 'secs') return result
def _schedule_task(self, task, reschedule=False): """ Schedule or Reschedule a task """ DLOG.verbose("Pool %s: Scheduling Task, name=%s." % (self._task_worker_pool.name, task.name)) self._tasks[task.id] = task for pri in TASK_PRIORITY: if task.id in self._ready_queue[pri]: break else: if reschedule: self._ready_queue[task.priority].append(task.id) else: self._ready_queue[task.priority].appendleft(task.id) histogram.add_histogram_data( self._name + ' [tasks-queue-p%i]' % task.priority, len(self._ready_queue[task.priority]), "ready-tasks") if not self._tasks_scheduled: self._schedule_next_task()
def selobj_dispatch(timeout_in_ms): """ Dispatch selection objects that have become readable or writeable within the given time period """ from nfv_common import histogram from nfv_common import timers global _read_callbacks, _write_callbacks, _error_callbacks read_objs = _read_callbacks.keys() write_objs = _write_callbacks.keys() try: readable, writeable, in_error = select.select(read_objs, write_objs, [], timeout_in_ms / 1000.0) for selobj in readable: callback = _read_callbacks.get(selobj, None) if callback is not None: start_ms = timers.get_monotonic_timestamp_in_ms() try: callback.send(selobj) except StopIteration: _read_callbacks.pop(selobj) elapsed_ms = timers.get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data( "selobj read: " + callback.__name__, elapsed_ms / 100, "decisecond") for selobj in writeable: callback = _write_callbacks.get(selobj, None) if callback is not None: start_ms = timers.get_monotonic_timestamp_in_ms() try: callback.send(selobj) except StopIteration: _write_callbacks.pop(selobj) elapsed_ms = timers.get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data( "selobj write: " + callback.__name__, elapsed_ms / 100, "decisecond") for selobj in in_error: callback = _error_callbacks.get(selobj, None) if callback is not None: start_ms = timers.get_monotonic_timestamp_in_ms() try: callback.send(selobj) except StopIteration: _error_callbacks.pop(selobj) elapsed_ms = timers.get_monotonic_timestamp_in_ms() - start_ms histogram.add_histogram_data( "selobj error: " + callback.__name__, elapsed_ms / 100, "decisecond") if selobj in _read_callbacks.keys(): _read_callbacks.pop(selobj) if selobj in _write_callbacks.keys(): _write_callbacks.pop(selobj) except (OSError, socket.error, select.error) as e: if errno.EINTR == e.args[0]: pass