Esempio n. 1
0
    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
Esempio n. 2
0
    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
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
 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))
Esempio n. 6
0
 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))
Esempio n. 7
0
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")
Esempio n. 8
0
 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
Esempio n. 9
0
 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()
Esempio n. 10
0
 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()
Esempio n. 11
0
 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
Esempio n. 12
0
    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())
Esempio n. 13
0
    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())
Esempio n. 14
0
 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)
Esempio n. 15
0
 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)
Esempio n. 16
0
    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
Esempio n. 17
0
    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
Esempio n. 18
0
 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
Esempio n. 19
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)
Esempio n. 20
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)
Esempio n. 21
0
 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
Esempio n. 22
0
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
Esempio n. 23
0
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
Esempio n. 24
0
 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
Esempio n. 25
0
 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
Esempio n. 26
0
 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
Esempio n. 27
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
Esempio n. 28
0
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)
Esempio n. 29
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._sslctx = sslctx
     self._handshake_finished_handler = handshake_finished_handler
Esempio n. 30
0
    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()
Esempio n. 31
0
    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()
Esempio n. 32
0
    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)
Esempio n. 33
0
    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)
Esempio n. 34
0
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
Esempio n. 35
0
    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
Esempio n. 36
0
    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
Esempio n. 37
0
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
Esempio n. 38
0
    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)
Esempio n. 39
0
    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)
Esempio n. 40
0
    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)
Esempio n. 41
0
    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)
Esempio n. 42
0
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()
Esempio n. 43
0
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
Esempio n. 44
0
    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])
Esempio n. 45
0
    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])
Esempio n. 46
0
 def _work():
     invokations[0] += 1
     invokations[1] = monotonic_time()
     if invokations[0] == TIMES:
         invoked.set()
Esempio n. 47
0
 def _add_notify_time(self, body):
     body['notify_time'] = int(monotonic_time() * 1000)
Esempio n. 48
0
 def start(self):
     if self._timeout:
         self._end_time = monotonic_time() + self._timeout
     self._scan_thread.start()
     self._scanning_started.wait()
Esempio n. 49
0
 def _update_outgoing_heartbeat(self):
     self._lastOutgoingTimeStamp = monotonic_time()
Esempio n. 50
0
 def _work():
     invokations[0] += 1
     invokations[1] = monotonic_time()
     if invokations[0] == TIMES:
         invoked.set()
Esempio n. 51
0
 def _milis(self):
     return int(round(monotonic_time() * 1000))
Esempio n. 52
0
 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
Esempio n. 53
0
 def has_expired(self):
     return monotonic_time() >= self._give_up_at
Esempio n. 54
0
 def _work():
     invokations[0] = monotonic_time()
Esempio n. 55
0
 def _milis(self):
     return int(round(monotonic_time() * 1000))
Esempio n. 56
0
 def has_expired(self):
     return monotonic_time() >= self._give_up_at
Esempio n. 57
0
 def _work():
     invokations[0] = monotonic_time()
Esempio n. 58
0
 def next_check_interval(self):
     return max(self._give_up_at - monotonic_time(), 0)
Esempio n. 59
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