Skip to content
Snippets Groups Projects
  • Nadav Har'El's avatar
    8dffa912
    clock: add monotonic uptime clock · 8dffa912
    Nadav Har'El authored
    
    This patch starts to solve both issue #142 ("Support MONOTONIC_CLOCK")
    and issue #81 (use <chrono> for time).
    
    First, it adds an uptime() function to the "clock" interface, and
    implements it for kvm/xen/hpet by returning the system time from which
    we subtract the system time at boot (but not adding any correction
    for wallclock).
    
    Second, it adds a new std::chrono-based interface to this clock, in
    a new header file <osv/clock.hh>. Instead of the old-style
    clock::get()->uptime(), one should prefer osv::clock::uptime::now().
    This returns a std::chrono::time_point which is type-safe, in the
    sense that: 1. It knows what its epoch is (i.e., that it belongs to
    osv::clock::uptime), and 2. It knows what its units are (nanoseconds).
    This allows the compiler to prevent a user from confusing measurements
    from this clock with those from other clocks, or making mistakes in
    its units.
    
    Third, this patch implements clock_gettime(MONOTONIC_CLOCK), using
    the new osv::clock::uptime::now().
    
    Note that though the new osv::clock::uptime is almost identical to
    std::chrono::steady_clock, they should not be confused. The former is
    actually OSv's implementation of the latter: steady_clock is implemented
    by the C++11 standard library using the Posix clock_gettime, and that
    is implemented (in this patch) using osv::clock::uptime.
    
    With this patch, we're *not* done with either issues #142 or #81.
    For issue #142, i.e., for supporting MONOTONIC_CLOCK in timerfd, we
    need OSv's timers to work on uptime(), not on clock::get()->time().
    For issue #81, we should add a osv::clock::wall type too (similar to
    what clock::get()->time() does today, but more correctly), and use either
    osv::clock::wall or osv::clock::uptime everywhere that
    clock::get()->time() is currently used in the code.
    clock::get()->time() should be removed.
    
    Reviewed-by: default avatarGlauber Costa <glommer@cloudius-systems.com>
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
    Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>
    8dffa912
    History
    clock: add monotonic uptime clock
    Nadav Har'El authored
    
    This patch starts to solve both issue #142 ("Support MONOTONIC_CLOCK")
    and issue #81 (use <chrono> for time).
    
    First, it adds an uptime() function to the "clock" interface, and
    implements it for kvm/xen/hpet by returning the system time from which
    we subtract the system time at boot (but not adding any correction
    for wallclock).
    
    Second, it adds a new std::chrono-based interface to this clock, in
    a new header file <osv/clock.hh>. Instead of the old-style
    clock::get()->uptime(), one should prefer osv::clock::uptime::now().
    This returns a std::chrono::time_point which is type-safe, in the
    sense that: 1. It knows what its epoch is (i.e., that it belongs to
    osv::clock::uptime), and 2. It knows what its units are (nanoseconds).
    This allows the compiler to prevent a user from confusing measurements
    from this clock with those from other clocks, or making mistakes in
    its units.
    
    Third, this patch implements clock_gettime(MONOTONIC_CLOCK), using
    the new osv::clock::uptime::now().
    
    Note that though the new osv::clock::uptime is almost identical to
    std::chrono::steady_clock, they should not be confused. The former is
    actually OSv's implementation of the latter: steady_clock is implemented
    by the C++11 standard library using the Posix clock_gettime, and that
    is implemented (in this patch) using osv::clock::uptime.
    
    With this patch, we're *not* done with either issues #142 or #81.
    For issue #142, i.e., for supporting MONOTONIC_CLOCK in timerfd, we
    need OSv's timers to work on uptime(), not on clock::get()->time().
    For issue #81, we should add a osv::clock::wall type too (similar to
    what clock::get()->time() does today, but more correctly), and use either
    osv::clock::wall or osv::clock::uptime everywhere that
    clock::get()->time() is currently used in the code.
    clock::get()->time() should be removed.
    
    Reviewed-by: default avatarGlauber Costa <glommer@cloudius-systems.com>
    Signed-off-by: default avatarNadav Har'El <nyh@cloudius-systems.com>
    Signed-off-by: default avatarPekka Enberg <penberg@cloudius-systems.com>