Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP raw clock #315

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions kano_updater/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
from collections import defaultdict
import sys
from raw_clock import raw_clock
import time
import signal
import os
Expand Down Expand Up @@ -103,12 +104,14 @@ def monitor(watchproc, timeout):

spoll = SignalPoll(signal.SIGUSR1)

lastEvent = time.time()
raw_clk = raw_clock()

lastEvent = raw_clk.monotonic_time()

pids = MonitorPids(watchpid)

while True:
now = time.time()
now = raw_clk.monotonic_time()
# check for child events
changed = pids.is_changed()
if watchproc.poll() is not None:
Expand Down
36 changes: 36 additions & 0 deletions kano_updater/raw_clock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# raw_clock.py
#
# Copyright (C) 2018 Kano Computing Ltd.
# License: http://www.gnu.org/licenses/gpl-2.0.txt GNU GPL v2
#
# Module to obtain a clock value which is not disturbed by the boot process
# when it obtains time from ntp.
#
# From http://stackoverflow.com/questions/1205722/how-do-i-get-monotonic-time-durations-in-python


import ctypes
import os


class timespec(ctypes.Structure):
_fields_ = [
('tv_sec', ctypes.c_long),
('tv_nsec', ctypes.c_long)
]


class raw_clock:
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>
def __init__(self):
self.librt = ctypes.CDLL('librt.so.1', use_errno=True)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed it's the same libname on debian stretch, could perhaps be useful to have it as a default parameter, as in init(libname) or so.

self.clock_gettime = self.librt.clock_gettime
self.clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]


def monotonic_time(self):
t = timespec()
if self.clock_gettime(self.CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0:
errno_ = ctypes.get_errno()
raise OSError(errno_, os.strerror(errno_))
return t.tv_sec + t.tv_nsec * 1e-9