CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1// Copyright Microsoft and CHERIoT Contributors.
2// SPDX-License-Identifier: MIT
3#pragma once
4
5/**
6 * \file .
7 *
8 * Standard clock support.
9 */
10
11#include <platform-time.h>
12#include <stdint.h>
13#include <sys/time.h>
14#include <thread.h>
15
16// The names in this file come from C or POSIX and so do not correspond to our
17// naming scheme. This header is expected to be included in C, so should also
18// not provide warnings about void in function parameter lists.
19// NOLINTBEGIN(readability-identifier-naming,modernize-redundant-void-arg)
20
21/// ID for a clock. Only monotonic and 'realtime' (wall clock) are supported.
22typedef enum __clockid_t
23{
24 /**
25 * The monotonic clock. This is zero at system start and increments at a
26 * fixed rate.
27 */
29 /**
30 * The wall-clock time.
31 *
32 * This clock's value is meaningful only if `clock_update_wall_clock` has
33 * been called at least once and there is at least one working clock source
34 * in the system.
35 */
37 /**
38 * CPU time consumed by the current thread, since boot time. This clock is
39 * equivalent to `CLOCK_MONOTONIC` if the scheduler is not compiled with
40 * support for accounting. Add `--scheduler-accounting=y` to your build
41 * configuration line to enable this.
42 */
44 /**
45 * CPU time consumed by the current 'process'. This value is defined by
46 * POSIX, but CHERIoT RTOS does not have a direct equivalent of a process
47 * and so this value is equivalent to `CLOCK_THREAD_CPUTIME_ID`, with all of
48 * the attendant caveats.
49 */
52
53/**
54 * The number of ticks on the monotonic clock per second.
55 */
56#define CLOCKS_PER_SEC ((clock_t)CPU_TIMER_HZ)
57
58/**
59 * Flag to indicate that a timespec should be treated as an absolute, rather
60 * than relative, time.
61 */
62#define TIMER_ABSTIME 1
63
64/**
65 * Type for holding time. The `CLOCKS_PER_SEC` macro defines the value in this
66 * type that corresponds to one second. This rate is SoC-specific.
67 */
68typedef uint64_t clock_t;
69
70/**
71 * A time value with up to nanosecond precision.
72 */
74{
75 /// Seconds
76 time_t tv_sec;
77 /**
78 * Nanoseconds
79 *
80 * Note that POSIX and pre-C23 versions of C specify that this is `long`,
81 * but they require that the values be between 0 and 999,999,999
82 * (inclusive). C23 allows this to be any type capable of representing
83 * this range and so we use `uint32_t`.
84 */
85 uint32_t tv_nsec;
86};
87
88__BEGIN_DECLS
89
90/**
91 * Returns the amount of CPU time (in units defined by `CLOCKS_PER_SEC`) that
92 * are accounted to the current thread (POSIX specifies 'process' here, but
93 * CHERIoT RTOS does not have an directly analogous abstraction).
94 *
95 * Note: If scheduler accounting is not enabled, this API will return the
96 * total elapsed uptime instead. Add `--scheduler-accounting=y` to your build
97 * configuration line to enable this.
98 */
99static inline clock_t clock(void)
100{
101#if SCHEDULER_ACCOUNTING == true
103#else
104 return platform_monotonic_time_read();
105#endif
106}
107
108/**
109 * Retrieve the time from the specified clock as a `timespec`.
110 *
111 * If `clockID` is `CLOCK_REALTIME`, the returned value is meaningful only if
112 * `clock_update_wall_clock` has been called at least once and there is at least
113 * one working clock source in the system.
114 */
115__cheriot_libcall int clock_gettime(clockid_t clockID,
116 struct timespec *outTime);
117
118/**
119 * Update the wall-clock time from available time sources.
120 */
121__cheriot_compartment("wall_clock") int clock_update_wall_clock(
122 TimeoutArgument timeout);
123
124/**
125 * POSIX-compatible time() implementation. Returns the time in seconds since
126 * the UNIX epoch.
127 *
128 * This value is meaningful only if `clock_update_wall_clock` has been called at
129 * least once and there is at least one working clock source in the system.
130 *
131 */
132__cheriot_libcall time_t time(time_t *tloc);
133
134/**
135 * Structure representing a date in the Gregorian calendar.
136 *
137 * This is intended to be compatible with C/POSIX, not all fields are used by
138 * all APIs.
139 */
140struct tm
141{
142 /// Seconds, in the range 0--60 (to account for leap seconds).
144 /// Minutes, in the range 0--59.
146 /// Hours, in the range 0--23.
148 /**
149 * Day of the month, in the range 1--31, or less if the month has fewer
150 * than 31 days.
151 */
153 /**
154 * Month of the year, in the range 0--11. Note that this counts from 0,
155 * whereas days of the month count from 1.
156 */
158 /// Year, as an offset from 1900 (so, for example, 2023 is 123).
160 /**
161 * Day of the week, in the range 0--6. Sunday is 0, Saturday is 6.
162 */
164 /**
165 * Day of the year, in the range 0--365 (0--364 if this is not a leap year).
166 */
168 /**
169 * Daylight savings flag.
170 */
172};
173
174/**
175 * Convert a `struct tm` to a `time_t`. This is intended to be compatible with
176 * the BSD extension and is equivalent to the POSIX `mktime` with a UTC locale.
177 *
178 * The `tm_wday` and `tm_yday` fields are ignored as inputs. Other fields may
179 * be out of range, for example an hour of -1 means hour 22 in the previous day,
180 * a day of 40 in a month with 31 days means day 9 in the next month, and so on.
181 *
182 * The values of the `tm_wday` and `tm_yday` fields will be set on successful
183 * completion.
184 *
185 * NOTE: UNIX time stamps do not include leap seconds. If a leap second (the
186 * 60th second at the end of June or December in a year that contains one) is
187 * specified in `time`, it will be treated as an overflow and the result of
188 * this function will be off by one.
189 */
190time_t __cheriot_libcall timegm(struct tm *time);
191
192/**
193 * C standard function to calculate a human-readable UTC date and time in a
194 * `struct tm` from a UNIX timestamp passed indirectly as `timer`. The
195 * `result` argument is used to provide space for the output. The return value
196 * is `result`, or an untagged value if an error occurs.
197 */
198struct tm *__cheriot_libcall gmtime_r(const time_t *__restrict timer,
199 struct tm *__restrict result);
200
201/**
202 * C standard function to calculate a human-readable UTC date and time in a
203 * `struct tm` from a UNIX timestamp. This uses an internal buffer that is
204 * invalidated on each subsequent call and is not thread safe. `gmtime_r`
205 * should be used instead.
206 */
207static inline struct tm *gmtime(const time_t *timer)
208{
209 static struct tm result;
210 return gmtime_r(timer, &result);
211}
212
213__END_DECLS
214
215// NOLINTEND(readability-identifier-naming,modernize-redundant-void-arg)
` defines the interface for the timer that is used to implement a monotonic clock.
A time value with up to nanosecond precision.
Definition time.h:74
uint32_t tv_nsec
Nanoseconds.
Definition time.h:85
time_t tv_sec
Seconds.
Definition time.h:76
Structure representing a date in the Gregorian calendar.
Definition time.h:141
int tm_mon
Month of the year, in the range 0–11.
Definition time.h:157
int tm_year
Year, as an offset from 1900 (so, for example, 2023 is 123).
Definition time.h:159
int tm_hour
Hours, in the range 0–23.
Definition time.h:147
int tm_sec
Seconds, in the range 0–60 (to account for leap seconds).
Definition time.h:143
int tm_isdst
Daylight savings flag.
Definition time.h:171
int tm_yday
Day of the year, in the range 0–365 (0–364 if this is not a leap year).
Definition time.h:167
int tm_mday
Day of the month, in the range 1–31, or less if the month has fewer than 31 days.
Definition time.h:152
int tm_min
Minutes, in the range 0–59.
Definition time.h:145
int tm_wday
Day of the week, in the range 0–6.
Definition time.h:163
POSIX time definitions.
Functions and types used for thread management.
uint64_t thread_elapsed_cycles_current(void)
Returns the number of cycles accounted to the current thread.
__cheriot_libcall int clock_gettime(clockid_t clockID, struct timespec *outTime)
Retrieve the time from the specified clock as a timespec.
struct tm *__cheriot_libcall gmtime_r(const time_t *__restrict timer, struct tm *__restrict result)
C standard function to calculate a human-readable UTC date and time in a struct tm from a UNIX timest...
__cheriot_compartment("wall_clock") int clock_update_wall_clock(TimeoutArgument timeout)
Update the wall-clock time from available time sources.
time_t __cheriot_libcall timegm(struct tm *time)
Convert a struct tm to a time_t.
static struct tm * gmtime(const time_t *timer)
C standard function to calculate a human-readable UTC date and time in a struct tm from a UNIX timest...
Definition time.h:207
uint64_t clock_t
Type for holding time.
Definition time.h:68
__clockid_t
ID for a clock. Only monotonic and 'realtime' (wall clock) are supported.
Definition time.h:23
@ CLOCK_MONOTONIC
The monotonic clock.
Definition time.h:28
@ CLOCK_REALTIME
The wall-clock time.
Definition time.h:36
@ CLOCK_THREAD_CPUTIME_ID
CPU time consumed by the current thread, since boot time.
Definition time.h:43
@ CLOCK_PROCESS_CPUTIME_ID
CPU time consumed by the current 'process'.
Definition time.h:50
__cheriot_libcall time_t time(time_t *tloc)
POSIX-compatible time() implementation.
enum __clockid_t clockid_t
ID for a clock. Only monotonic and 'realtime' (wall clock) are supported.
static clock_t clock(void)
Returns the amount of CPU time (in units defined by CLOCKS_PER_SEC) that are accounted to the current...
Definition time.h:99