def _recoverExistingVms(self): start_time = utils.monotonic_time() try: self.log.debug('recovery: started') # Starting up libvirt might take long when host under high load, # we prefer running this code in external thread to avoid blocking # API response. mog = min(config.getint('vars', 'max_outgoing_migrations'), caps.CpuTopology().cores()) migration.SourceThread.setMaxOutgoingMigrations(mog) recovery.all_vms(self) # recover stage 3: waiting for domains to go up self._waitForDomainsUp() recovery.clean_vm_files(self) self._recovery = False # Now if we have VMs to restore we should wait pool connection # and then prepare all volumes. # Actually, we need it just to get the resources for future # volumes manipulations self._waitForStoragePool() self._preparePathsForRecoveredVMs() self.log.info('recovery: completed in %is', utils.monotonic_time() - start_time) except: self.log.exception("recovery: failed") raise
def _recoverExistingVms(self): start_time = utils.monotonic_time() try: self.log.debug('recovery: started') # Starting up libvirt might take long when host under high load, # we prefer running this code in external thread to avoid blocking # API response. mog = min(config.getint('vars', 'max_outgoing_migrations'), numa.cpu_topology().cores) migration.SourceThread.setMaxOutgoingMigrations(mog) recovery.all_vms(self) # recover stage 3: waiting for domains to go up self._waitForDomainsUp() recovery.clean_vm_files(self) self._recovery = False # Now if we have VMs to restore we should wait pool connection # and then prepare all volumes. # Actually, we need it just to get the resources for future # volumes manipulations self._waitForStoragePool() self._preparePathsForRecoveredVMs() self.log.info('recovery: completed in %is', utils.monotonic_time() - start_time) except: self.log.exception("recovery: failed") raise
def wait_for_removal(path, timeout, wait=0.1): deadline = utils.monotonic_time() + timeout while True: if not os.path.exists(path): return True if utils.monotonic_time() > deadline: return False time.sleep(wait)
def assertMaxDuration(self, maxtime): start = utils.monotonic_time() try: yield finally: elapsed = utils.monotonic_time() - start if maxtime < elapsed: self.fail("Operation was too slow %.2fs > %.2fs" % (elapsed, maxtime))
def wait_for_zombie(proc, timeout, interval=0.1): interval = min(interval, timeout) deadline = utils.monotonic_time() + timeout while True: time.sleep(interval) if is_zombie(proc): return if utils.monotonic_time() > deadline: raise RuntimeError("Timeout waiting for process")
def send(self): disp = self._disp timeout = self._timeout duration = 0 s = monotonic_time() while ((timeout is None or duration < timeout) and (disp.writable() or not self._connected.isSet())): td = timeout - duration if timeout is not None else None self.process(td) duration = monotonic_time() - s
def _work(): invokations[0] += 1 invokations[1] = monotonic_time() if invokations[0] == BLOCK_AT: # must be > (PERIOD * TIMES) ~= forever time.sleep(10 * PERIOD * TIMES) executions[0] += 1 executions[1] = monotonic_time() if invokations[0] == TIMES: done.set()
def test_timeout_not_triggered(self): time_start = monotonic_time() with monitor.Monitor(timeout=self.TIMEOUT) as mon: dummy_name = dummy.create() dummy.remove(dummy_name) for event in mon: break self.assertTrue((monotonic_time() - time_start) <= self.TIMEOUT) self.assertTrue(mon.is_stopped())
def _serveRequest(self, ctx, req): start_time = monotonic_time() response = self._handle_request(req, ctx.server_address) error = getattr(response, "error", None) if error is None: response_log = "succeeded" else: response_log = "failed (error %s)" % (error.code,) self.log.info("RPC call %s %s in %.2f seconds", req.method, response_log, monotonic_time() - start_time) if response is not None: ctx.requestDone(response)
def _serveRequest(self, ctx, req): start_time = monotonic_time() response = self._handle_request(req, ctx.server_address) error = getattr(response, "error", None) if error is None: response_log = "succeeded" else: response_log = "failed (error %s)" % (error.code, ) self.log.info("RPC call %s %s in %.2f seconds", req.method, response_log, monotonic_time() - start_time) if response is not None: ctx.requestDone(response)
def recv(self): timeout = self._timeout s = monotonic_time() duration = 0 while timeout is None or duration < timeout: try: return self._inbox.popleft() except IndexError: td = timeout - duration if timeout is not None else None self.process(td) duration = monotonic_time() - s return None
def __init__(self, bridge, timeout, threadFactory=None): self._bridge = bridge self._workQueue = Queue() self._threadFactory = threadFactory self._timeout = timeout self._next_report = monotonic_time() + self._timeout self._counter = 0
def _scan(self): with closing(select.epoll()) as epoll: with _monitoring_socket(self._queue, self._groups, epoll) as sock: with _pipetrick(epoll) as self._pipetrick: self._scanning_started.set() while True: if self._timeout: timeout = self._end_time - monotonic_time() # timeout expired if timeout <= 0: self._scanning_stopped.set() self._queue.put(_TIMEOUT_FLAG) break else: timeout = -1 events = NoIntrPoll(epoll.poll, timeout=timeout) # poll timeouted if len(events) == 0: self._scanning_stopped.set() self._queue.put(_TIMEOUT_FLAG) break # stopped by pipetrick elif (self._pipetrick[0], select.POLLIN) in events: NoIntrCall(os.read, self._pipetrick[0], 1) self._queue.put(_STOP_FLAG) break _nl_recvmsgs_default(sock)
def tick(self): now = monotonic_time() result = self._result(now) self._counter = (self._counter + 1) % self._interval if result: self._last_time = now return result
def nic(name, model, mac, start_sample, end_sample, interval): ifSpeed = [100, 1000][model in ('e1000', 'virtio')] ifStats = {'macAddr': mac, 'name': name, 'speed': str(ifSpeed), 'state': 'unknown'} ifStats['rxErrors'] = str(end_sample['rx.errs']) ifStats['rxDropped'] = str(end_sample['rx.drop']) ifStats['txErrors'] = str(end_sample['tx.errs']) ifStats['txDropped'] = str(end_sample['tx.drop']) rxDelta = ( end_sample['rx.bytes'] - start_sample['rx.bytes']) ifRxBytes = (100.0 * (rxDelta % 2 ** 32) / interval / ifSpeed / _MBPS_TO_BPS) txDelta = ( end_sample['tx.bytes'] - start_sample['tx.bytes']) ifTxBytes = (100.0 * (txDelta % 2 ** 32) / interval / ifSpeed / _MBPS_TO_BPS) ifStats['rxRate'] = '%.1f' % ifRxBytes ifStats['txRate'] = '%.1f' % ifTxBytes ifStats['rx'] = str(end_sample['rx.bytes']) ifStats['tx'] = str(end_sample['tx.bytes']) ifStats['sampleTime'] = monotonic_time() return ifStats
def nic_traffic(name, model, mac, start_sample, start_index, end_sample, end_index, interval): ifSpeed = [100, 1000][model in ('e1000', 'virtio')] ifStats = { 'macAddr': mac, 'name': name, 'speed': str(ifSpeed), 'state': 'unknown' } ifStats['rxErrors'] = str(end_sample['net.%d.rx.errs' % end_index]) ifStats['rxDropped'] = str(end_sample['net.%d.rx.drop' % end_index]) ifStats['txErrors'] = str(end_sample['net.%d.tx.errs' % end_index]) ifStats['txDropped'] = str(end_sample['net.%d.tx.drop' % end_index]) rxDelta = (end_sample['net.%d.rx.bytes' % end_index] - start_sample['net.%d.rx.bytes' % start_index]) ifRxBytes = (100.0 * (rxDelta % 2**32) / interval / ifSpeed / _MBPS_TO_BPS) txDelta = (end_sample['net.%d.tx.bytes' % end_index] - start_sample['net.%d.tx.bytes' % start_index]) ifTxBytes = (100.0 * (txDelta % 2**32) / interval / ifSpeed / _MBPS_TO_BPS) ifStats['rxRate'] = '%.1f' % ifRxBytes ifStats['txRate'] = '%.1f' % ifTxBytes ifStats['rx'] = str(end_sample['net.%d.rx.bytes' % end_index]) ifStats['tx'] = str(end_sample['net.%d.tx.bytes' % end_index]) ifStats['sampleTime'] = monotonic_time() return ifStats
def _attempt_log_stats(self): self._counter += 1 if monotonic_time() > self._next_report: self.log.info('%s requests processed during %s seconds', self._counter, self._timeout) self._next_report += self._timeout self._counter = 0
def __init__(self, sslctx, handshake_finished_handler, handshake_timeout=SSL_HANDSHAKE_TIMEOUT): self._give_up_at = monotonic_time() + handshake_timeout self._has_been_set_up = False self._is_handshaking = True self.want_read = True self.want_write = True self._sslctx = sslctx self._handshake_finished_handler = handshake_finished_handler
def _resize_map(name): """ Invoke multipathd to resize a device Must run as root Raises Error if multipathd failed to resize the map. """ log.debug("Resizing map %r", name) cmd = [_MULTIPATHD.cmd, "resize", "map", name] start = utils.monotonic_time() rc, out, err = utils.execCmd(cmd, raw=True, execCmdLogger=log) # multipathd reports some errors using non-zero exit code and stderr (need # to be root), but the command may return 0, and the result is reported # using stdout. if rc != 0 or out != "ok\n": raise Error("Resizing map %r failed: out=%r err=%r" % (name, out, err)) elapsed = utils.monotonic_time() - start log.debug("Resized map %r in %.2f seconds", name, elapsed)
def __init__( self, sslctx, handshake_finished_handler, handshake_timeout=SSL_HANDSHAKE_TIMEOUT, ): self._give_up_at = monotonic_time() + handshake_timeout self._has_been_set_up = False self._is_handshaking = True self._sslctx = sslctx self._handshake_finished_handler = handshake_finished_handler
def time(self): """ Return the time according to the event loop's clock. This is a float expressed in seconds since an epoch, but the epoch, precision, accuracy and drift are unspecified and may differ per event loop. Changes from Python 3: - Use Python 2 compatible monotonic time """ return utils.monotonic_time()
def testTimeoutNoTimeForSleep(self): # time action # 0 first attempt # 1 bail out (1 + 1 == deadline) with FakeMonotonicTime(0): def operation(): time.sleep(1) raise RuntimeError self.assertRaises(RuntimeError, utils.retry, operation, timeout=2, sleep=1) self.assertEqual(utils.monotonic_time(), 1)
def _nic_traffic(vm_obj, name, model, mac, start_sample, start_index, end_sample, end_index, interval): """ Return per-nic statistics packed into a dictionary - macAddr - name - speed - state - {rx,tx}Errors - {rx,tx}Dropped - {rx,tx}Rate - {rx,tx} - sampleTime Produce as many statistics as possible, skipping errors. Expect two samplings `start_sample' and `end_sample' which must be data in the format of the libvirt bulk stats. Expects the indexes of the nic whose statistics needs to be produced, for each sampling: `start_index' for `start_sample', `end_index' for `end_sample'. `interval' is the time between the two samplings, in seconds. `vm_obj' is the Vm instance to which the nic belongs. `name', `model' and `mac' are the attributes of the said nic. Those three value are reported in the output stats. Return None on error, if any needed data is missing or wrong. Return the `stats' dictionary on success. """ if_speed = 1000 if model in ('e1000', 'virtio') else 100 if_stats = { 'macAddr': mac, 'name': name, 'speed': str(if_speed), 'state': 'unknown', } with _skip_if_missing_stats(vm_obj): if_stats['rxErrors'] = str(end_sample['net.%d.rx.errs' % end_index]) if_stats['rxDropped'] = str(end_sample['net.%d.rx.drop' % end_index]) if_stats['txErrors'] = str(end_sample['net.%d.tx.errs' % end_index]) if_stats['txDropped'] = str(end_sample['net.%d.tx.drop' % end_index]) with _skip_if_missing_stats(vm_obj): if_stats['rx'] = str(end_sample['net.%d.rx.bytes' % end_index]) if_stats['tx'] = str(end_sample['net.%d.tx.bytes' % end_index]) if_stats['sampleTime'] = monotonic_time() return if_stats
def wait(self, timeout=None): if timeout is not None: deadline = monotonic_time() + timeout else: deadline = None for p in self._procs: if deadline is not None: # NOTE: CPopen doesn't support timeout argument. while monotonic_time() < deadline: p.poll() if p.returncode is not None: break time.sleep(1) else: p.wait() if deadline is not None: if deadline < monotonic_time() or self.returncode is None: # Timed out return False return True
def testTimeoutDeadlineReached(self): # time action # 0 first attempt # 1 sleep # 2 second attempt # 3 bail out (3 == deadline) with FakeMonotonicTime(0): def operation(): time.sleep(1) raise RuntimeError self.assertRaises(RuntimeError, utils.retry, operation, timeout=3, sleep=1) self.assertEqual(utils.monotonic_time(), 3)
def testTimeoutSleepOnce(self): # time action # 0 first attempt # 2 sleep # 3 second attempt # 5 bail out (5 > deadline) with FakeMonotonicTime(0): counter = [0] def operation(): time.sleep(2) counter[0] += 1 raise RuntimeError self.assertRaises(RuntimeError, utils.retry, operation, timeout=4, sleep=1) self.assertEqual(counter[0], 2) self.assertEqual(utils.monotonic_time(), 5)
class _ProtocolDetector(object): log = logging.getLogger("ProtocolDetector.Detector") def __init__(self, detectors, timeout=None): self._detectors = detectors self._required_size = max(h.REQUIRED_SIZE for h in self._detectors) self.log.debug("Using required_size=%d", self._required_size) self._give_up_at = monotonic_time() + timeout def readable(self, dispatcher): return True def next_check_interval(self): return min(self._give_up_at - monotonic_time(), 0) def handle_read(self, dispatcher): sock = dispatcher.socket try: data = sock.recv(self._required_size, socket.MSG_PEEK) except socket.error, why: if why.args[0] == socket.EWOULDBLOCK: return dispatcher.handle_error() return if len(data) < self._required_size: return if monotonic_time() > self._give_up_at: self.log.debug("Timed out while waiting for data") dispatcher.close() for detector in self._detectors: if detector.detect(data): host, port = sock.getpeername() self.log.info("Detected protocol %s from %s:%d", detector.NAME, host, port) detector.handle_dispatcher(dispatcher, (host, port)) break else: self.log.warning("Unrecognized protocol: %r", data) dispatcher.close()
def _nic_traffic(vm_obj, name, model, mac, start_sample, start_index, end_sample, end_index, interval): ifSpeed = [100, 1000][model in ('e1000', 'virtio')] ifStats = {'macAddr': mac, 'name': name, 'speed': str(ifSpeed), 'state': 'unknown'} with _skip_if_missing_stats(vm_obj): ifStats['rxErrors'] = str(end_sample['net.%d.rx.errs' % end_index]) ifStats['rxDropped'] = str(end_sample['net.%d.rx.drop' % end_index]) ifStats['txErrors'] = str(end_sample['net.%d.tx.errs' % end_index]) ifStats['txDropped'] = str(end_sample['net.%d.tx.drop' % end_index]) with _skip_if_missing_stats(vm_obj): rxDelta = ( end_sample['net.%d.rx.bytes' % end_index] - start_sample['net.%d.rx.bytes' % start_index] ) txDelta = ( end_sample['net.%d.tx.bytes' % end_index] - start_sample['net.%d.tx.bytes' % start_index] ) ifRxBytes = (100.0 * (rxDelta % 2 ** 32) / interval / ifSpeed / _MBPS_TO_BPS) ifTxBytes = (100.0 * (txDelta % 2 ** 32) / interval / ifSpeed / _MBPS_TO_BPS) ifStats['rxRate'] = '%.1f' % ifRxBytes ifStats['txRate'] = '%.1f' % ifTxBytes ifStats['rx'] = str(end_sample['net.%d.rx.bytes' % end_index]) ifStats['tx'] = str(end_sample['net.%d.tx.bytes' % end_index]) ifStats['sampleTime'] = monotonic_time() return ifStats
def test_stop(self): PERIOD = 0.1 invocations = [0] def _work(): invocations[0] = monotonic_time() op = periodic.Operation(_work, period=PERIOD, scheduler=self.sched, executor=self.exc) op.start() time.sleep(PERIOD * 2) # avoid pathological case on which nothing ever runs self.assertTrue(invocations[0] > 0) op.stop() # cooldown. Let's try to avoid scheduler mistakes. time.sleep(PERIOD) stop = monotonic_time() self.assertTrue(stop > invocations[0])
def _work(): invokations[0] += 1 invokations[1] = monotonic_time() if invokations[0] == TIMES: invoked.set()
def _add_notify_time(self, body): body['notify_time'] = int(monotonic_time() * 1000)
def start(self): if self._timeout: self._end_time = monotonic_time() + self._timeout self._scan_thread.start() self._scanning_started.wait()
def _update_outgoing_heartbeat(self): self._lastOutgoingTimeStamp = monotonic_time()
def _milis(self): return int(round(monotonic_time() * 1000))
def __init__(self, detectors, timeout=None): self._detectors = detectors self._required_size = max(h.REQUIRED_SIZE for h in self._detectors) self.log.debug("Using required_size=%d", self._required_size) self._give_up_at = monotonic_time() + timeout
def has_expired(self): return monotonic_time() >= self._give_up_at
def _work(): invokations[0] = monotonic_time()
def next_check_interval(self): return max(self._give_up_at - monotonic_time(), 0)
def _outgoing_heartbeat_expiration_interval(self): since_last_update = (monotonic_time() - self._lastOutgoingTimeStamp) return (self._outgoing_heartbeat_in_milis / 1000.0) - since_last_update