~ubuntu-branches/ubuntu/wily/openvswitch/wily

« back to all changes in this revision

Viewing changes to python/ovs/timeval.py

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2015-08-10 11:35:15 UTC
  • mfrom: (1.1.30)
  • Revision ID: package-import@ubuntu.com-20150810113515-575vj06oq29emxsn
Tags: 2.4.0~git20150810.97bab95-0ubuntu1
* New upstream snapshot from 2.4 branch:
  - d/*: Align any relevant packaging changes with upstream.
* d/*: wrap-and-sort.
* d/openvswitch-{common,vswitch}.install: Correct install location for
  bash completion files.
* d/tests/openflow.py: Explicitly use ovs-testcontroller as provided
  by 2.4.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
 
 
15
import sys
15
16
import time
16
17
 
 
18
try:
 
19
    import ctypes
 
20
 
 
21
    LIBRT = 'librt.so.1'
 
22
    clock_gettime_name = 'clock_gettime'
 
23
 
 
24
    if sys.platform.startswith("linux"):
 
25
        CLOCK_MONOTONIC = 1
 
26
        time_t = ctypes.c_long
 
27
    elif sys.platform.startswith("netbsd"):
 
28
        # NetBSD uses function renaming for ABI versioning.  While the proper
 
29
        # way to get the appropriate version is of course "#include <time.h>",
 
30
        # it is difficult with ctypes.  The following is appropriate for
 
31
        # recent versions of NetBSD, including NetBSD-6.
 
32
        LIBRT = 'libc.so.12'
 
33
        clock_gettime_name = '__clock_gettime50'
 
34
        CLOCK_MONOTONIC = 3
 
35
        time_t = ctypes.c_int64
 
36
    elif sys.platform.startswith("freebsd"):
 
37
        CLOCK_MONOTONIC = 4
 
38
        time_t = ctypes.c_int64
 
39
    else:
 
40
        raise Exception
 
41
 
 
42
    class timespec(ctypes.Structure):
 
43
        _fields_ = [
 
44
            ('tv_sec', time_t),
 
45
            ('tv_nsec', ctypes.c_long),
 
46
        ]
 
47
 
 
48
    librt = ctypes.CDLL(LIBRT)
 
49
    clock_gettime = getattr(librt, clock_gettime_name)
 
50
    clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
 
51
except:
 
52
    # Librt shared library could not be loaded
 
53
    librt = None
 
54
 
 
55
def monotonic():
 
56
    if not librt:
 
57
        return time.time()
 
58
 
 
59
    t = timespec()
 
60
    if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) == 0:
 
61
        return t.tv_sec + t.tv_nsec * 1e-9
 
62
    # Kernel does not support CLOCK_MONOTONIC
 
63
    return time.time()
 
64
 
 
65
# Use time.monotonic() if Python version >= 3.3
 
66
if not hasattr(time, 'monotonic'):
 
67
    time.monotonic = monotonic
17
68
 
18
69
def msec():
19
 
    """Returns the current time, as the amount of time since the epoch, in
20
 
    milliseconds, as a float."""
21
 
    return time.time() * 1000.0
 
70
    """ Returns the system's monotonic time if possible, otherwise returns the
 
71
    current time as the amount of time since the epoch, in milliseconds, as a
 
72
    float."""
 
73
    return time.monotonic() * 1000.0
22
74
 
23
75
 
24
76
def postfork():