VirtualBox

Changeset 61464 in vbox


Ignore:
Timestamp:
Jun 4, 2016 2:37:02 AM (9 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
107791
Message:

common/utils.py: Corrected the timestamp implementation on windows to produce the same timestamps in two different processes. (time.clock() doesn't do this, as per it's documentation.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/utils.py

    r61172 r61464  
    4646
    4747if sys.platform == 'win32':
     48    import ctypes;
    4849    import win32api;            # pylint: disable=F0401
    4950    import win32con;            # pylint: disable=F0401
     
    330331    Returns process exit code (see subprocess.poll).
    331332    """
    332     assert dKeywordArgs.get('stdout') == None;
    333     assert dKeywordArgs.get('stderr') == None;
     333    assert dKeywordArgs.get('stdout') is None;
     334    assert dKeywordArgs.get('stderr') is None;
    334335    _processFixPythonInterpreter(aPositionalArgs, dKeywordArgs);
    335336    oProcess = subprocess.Popen(*aPositionalArgs, **dKeywordArgs);
     
    451452    if sys.platform == 'win32':
    452453        try:
    453             win32console.GenerateConsoleCtrlEvent(win32con.CTRL_BREAK_EVENT, uPid); # pylint
     454            win32console.GenerateConsoleCtrlEvent(win32con.CTRL_BREAK_EVENT, uPid);             # pylint: disable=no-member
    454455            fRc = True;
    455456        except:
     
    490491    if sys.platform == 'win32':
    491492        try:
    492             hProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, uPid);
     493            hProcess = win32api.OpenProcess(win32con.PROCESS_TERMINATE, False, uPid);           # pylint: disable=no-member
    493494        except:
    494495            pass;
    495496        else:
    496497            try:
    497                 win32process.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS
     498                win32process.TerminateProcess(hProcess, 0x40010004); # DBG_TERMINATE_PROCESS    # pylint: disable=no-member
    498499                fRc = True;
    499500            except:
    500501                pass;
    501             win32api.CloseHandle(hProcess)
     502            win32api.CloseHandle(hProcess)                                                      # pylint: disable=no-member
    502503    else:
    503504        try:
     
    546547        fRc = False;
    547548        try:
    548             hProcess = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, uPid);
     549            hProcess = win32api.OpenProcess(win32con.PROCESS_QUERY_INFORMATION, False, uPid);   # pylint: disable=no-member
    549550        except:
    550551            pass;
    551552        else:
    552             win32api.CloseHandle(hProcess)
     553            win32api.CloseHandle(hProcess);                                                     # pylint: disable=no-member
    553554            fRc = True;
    554555    else:
     
    888889#
    889890
     891#
     892# The following test case shows how time.time() only have ~ms resolution
     893# on Windows (tested W10) and why it therefore makes sense to try use
     894# performance counters.
     895#
     896# Note! We cannot use time.clock() as the timestamp must be portable across
     897#       processes.  See timeout testcase problem on win hosts (no logs).
     898#
     899#import sys;
     900#import time;
     901#from common import utils;
     902#
     903#atSeries = [];
     904#for i in xrange(1,160):
     905#    if i == 159: time.sleep(10);
     906#    atSeries.append((utils.timestampNano(), long(time.clock() * 1000000000), long(time.time() * 1000000000)));
     907#
     908#tPrev = atSeries[0]
     909#for tCur in atSeries:
     910#    print 't1=%+22u, %u' % (tCur[0], tCur[0] - tPrev[0]);
     911#    print 't2=%+22u, %u' % (tCur[1], tCur[1] - tPrev[1]);
     912#    print 't3=%+22u, %u' % (tCur[2], tCur[2] - tPrev[2]);
     913#    print '';
     914#    tPrev = tCur
     915#
     916#print 't1=%u' % (atSeries[-1][0] - atSeries[0][0]);
     917#print 't2=%u' % (atSeries[-1][1] - atSeries[0][1]);
     918#print 't3=%u' % (atSeries[-1][2] - atSeries[0][2]);
     919
     920g_fWinUseWinPerfCounter           = sys.platform == 'win32';
     921g_fpWinPerfCounterFreq            = None;
     922g_oFuncwinQueryPerformanceCounter = None;
     923
     924def _winInitPerfCounter():
     925    """ Initializes the use of performance counters. """
     926    global g_fWinUseWinPerfCounter, g_fpWinPerfCounterFreq, g_oFuncwinQueryPerformanceCounter
     927
     928    uFrequency = ctypes.c_ulonglong(0);
     929    if ctypes.windll.kernel32.QueryPerformanceFrequency(ctypes.byref(uFrequency)):
     930        if uFrequency.value >= 1000:
     931            print 'uFrequency = %s' % (uFrequency,);
     932            print 'type(uFrequency) = %s' % (type(uFrequency),);
     933            g_fpWinPerfCounterFreq = float(uFrequency.value);
     934
     935            # Check that querying the counter works too.
     936            global g_oFuncwinQueryPerformanceCounter
     937            g_oFuncwinQueryPerformanceCounter = ctypes.windll.kernel32.QueryPerformanceCounter;
     938            uCurValue = ctypes.c_ulonglong(0);
     939            if g_oFuncwinQueryPerformanceCounter(ctypes.byref(uCurValue)):
     940                if uCurValue.value > 0:
     941                    return True;
     942    g_fWinUseWinPerfCounter = False;
     943    return False;
     944
     945def _winFloatTime():
     946    """ Gets floating point time on windows. """
     947    if g_fpWinPerfCounterFreq is not None or _winInitPerfCounter():
     948        uCurValue = ctypes.c_ulonglong(0);
     949        if g_oFuncwinQueryPerformanceCounter(ctypes.byref(uCurValue)):
     950            return float(uCurValue.value) / g_fpWinPerfCounterFreq;
     951    return time.time();
     952
     953
    890954def timestampNano():
    891955    """
    892956    Gets a nanosecond timestamp.
    893957    """
    894     if sys.platform == 'win32':
    895         return long(time.clock() * 1000000000);
     958    if g_fWinUseWinPerfCounter is True:
     959        return long(_winFloatTime() * 1000000000);
    896960    return long(time.time() * 1000000000);
    897961
     
    900964    Gets a millisecond timestamp.
    901965    """
    902     if sys.platform == 'win32':
    903         return long(time.clock() * 1000);
     966    if g_fWinUseWinPerfCounter is True:
     967        return long(_winFloatTime() * 1000);
    904968    return long(time.time() * 1000);
    905969
     
    908972    Gets a second timestamp.
    909973    """
    910     if sys.platform == 'win32':
    911         return long(time.clock());
     974    if g_fWinUseWinPerfCounter is True:
     975        return long(_winFloatTime());
    912976    return long(time.time());
    913977
     
    14381502    """
    14391503    if platform.system() == 'Windows':
    1440         import ctypes
    14411504        oCTypeFreeSpace = ctypes.c_ulonglong(0);
    14421505        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(sPath), None, None,
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette