Merge "Use better timing mechanisms instead of time.time()"

This commit is contained in:
Jenkins 2015-06-24 11:53:31 +00:00 committed by Gerrit Code Review
commit c7bf8ee266
3 changed files with 18 additions and 12 deletions

View File

@ -22,11 +22,11 @@ import subprocess
import sys import sys
import tempfile import tempfile
import threading import threading
import time
import weakref import weakref
import fasteners import fasteners
from oslo_config import cfg from oslo_config import cfg
from oslo_utils import timeutils
import six import six
from oslo_concurrency._i18n import _, _LI from oslo_concurrency._i18n import _, _LI
@ -236,31 +236,32 @@ def synchronized(name, lock_file_prefix=None, external=False, lock_path=None,
""" """
def wrap(f): def wrap(f):
@six.wraps(f) @six.wraps(f)
def inner(*args, **kwargs): def inner(*args, **kwargs):
t1 = time.time() t1 = timeutils.now()
t2 = None t2 = None
try: try:
with lock(name, lock_file_prefix, external, lock_path, with lock(name, lock_file_prefix, external, lock_path,
do_log=False, semaphores=semaphores, delay=delay): do_log=False, semaphores=semaphores, delay=delay):
t2 = time.time() t2 = timeutils.now()
LOG.debug('Lock "%(name)s" acquired by "%(function)s" :: ' LOG.debug('Lock "%(name)s" acquired by "%(function)s" :: '
'waited %(wait_secs)0.3fs', 'waited %(wait_secs)0.3fs',
{'name': name, 'function': f.__name__, {'name': name, 'function': f.__name__,
'wait_secs': (t2 - t1)}) 'wait_secs': (t2 - t1)})
return f(*args, **kwargs) return f(*args, **kwargs)
finally: finally:
t3 = time.time() t3 = timeutils.now()
if t2 is None: if t2 is None:
held_secs = "N/A" held_secs = "N/A"
else: else:
held_secs = "%0.3fs" % (t3 - t2) held_secs = "%0.3fs" % (t3 - t2)
LOG.debug('Lock "%(name)s" released by "%(function)s" :: held ' LOG.debug('Lock "%(name)s" released by "%(function)s" :: held '
'%(held_secs)s', '%(held_secs)s',
{'name': name, 'function': f.__name__, {'name': name, 'function': f.__name__,
'held_secs': held_secs}) 'held_secs': held_secs})
return inner return inner
return wrap return wrap

View File

@ -27,6 +27,7 @@ import time
from oslo_utils import importutils from oslo_utils import importutils
from oslo_utils import strutils from oslo_utils import strutils
from oslo_utils import timeutils
import six import six
from oslo_concurrency._i18n import _ from oslo_concurrency._i18n import _
@ -209,10 +210,12 @@ def execute(*cmd, **kwargs):
cmd = [str(c) for c in cmd] cmd = [str(c) for c in cmd]
sanitized_cmd = strutils.mask_password(' '.join(cmd)) sanitized_cmd = strutils.mask_password(' '.join(cmd))
watch = timeutils.StopWatch()
while attempts > 0: while attempts > 0:
attempts -= 1 attempts -= 1
watch.restart()
try: try:
start_time = time.time()
LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd) LOG.log(loglevel, _('Running cmd (subprocess): %s'), sanitized_cmd)
_PIPE = subprocess.PIPE # pylint: disable=E1101 _PIPE = subprocess.PIPE # pylint: disable=E1101
@ -240,9 +243,8 @@ def execute(*cmd, **kwargs):
obj.stdin.close() # pylint: disable=E1101 obj.stdin.close() # pylint: disable=E1101
_returncode = obj.returncode # pylint: disable=E1101 _returncode = obj.returncode # pylint: disable=E1101
end_time = time.time() - start_time LOG.log(loglevel, 'CMD "%s" returned: %s in %0.3fs',
LOG.log(loglevel, 'CMD "%s" returned: %s in %0.3fs' % sanitized_cmd, _returncode, watch.elapsed())
(sanitized_cmd, _returncode, end_time))
if on_completion: if on_completion:
on_completion(obj) on_completion(obj)

View File

@ -15,7 +15,8 @@
import contextlib import contextlib
import logging import logging
import threading import threading
import time
from oslo_utils import timeutils
@contextlib.contextmanager @contextlib.contextmanager
@ -53,11 +54,13 @@ def watch(logger, action, level=logging.DEBUG, after=5.0):
print "done" print "done"
""" """
start = time.time() watch = timeutils.StopWatch()
watch.start()
def log(): def log():
msg = "%s not completed after %0.3fs" % (action, time.time() - start) msg = "%s not completed after %0.3fs" % (action, watch.elapsed())
logger.log(level, msg) logger.log(level, msg)
timer = threading.Timer(after, log) timer = threading.Timer(after, log)
timer.start() timer.start()
try: try: