Module Oclock


module Oclock: sig .. end
Oclock: precise POSIX clock for OCaml

This module give access to the clock_gettime (2) family of functions to Ocaml programs.

If this module allows to access time of real- or CPU-time clocks in nanoseconds, the actual precision of the clocks might be much coarser. Also, the resolution of a clock, Oclock.getres should indicate the period of the timer used for this clock, but the actual precision of the clock greatly depends on the CPU (watch out for frequency scaling!) and its time source. You can estimate the precision available on your platform with the shipped example examples/realtime.


type clockid = int 
Clock identifier type

Clock access


val getres : clockid -> int64
Gets the clock's resolution in nanoseconds.
val gettime : clockid -> int64
Gets the clock's time in nanoseconds.
val settime : clockid -> int64 -> unit
Sets the clock's time in nanoseconds.

The three above functions raise Invalid_argument if the clock identifier is not supported, and a Failure if the call fails for any ohter reason (including permission problems).

Clock identifiers



Current process/thread's clock identifiers


val realtime : clockid
Realtime (always valid)
val monotonic : clockid
Monotonic (not subject to system time change)
val process_cputime : clockid
Current process CPU-time clock
val thread_cputime : clockid
Current thread CPU-time clock
val monotonic_raw : clockid
Another monotonic clock (not always present, since Linux 2.6.28; Linux-specific), not subject to NTP adjustements

Remote clock identifier


val getcpuclockid : int -> clockid
Gets the CPU-time clock identifier of a process (given its PID).

Raises an Invalid_argument exception if the provided integer is not a valid PID, and a Failure if the calls fails for any other reason (including permission problems).

val pthread_getcpuclockid : int -> clockid
Gets the CPU-time clock identifier of a thread given its pthread identifier, as returned by Thread.id (but only if you use real POSIX threads -thread and not VM threads -vmthread).

Raises an Invalid_argument exception if the provided integer is not a valid thread identifier, and a Failure if the calls fails for any other reason (including permission problems).