blob: 8d9760c5d6d86ca62c659c91c2ae68beac5c4606 [file] [log] [blame]
Pau Espin Pedrol87fade82018-02-26 19:42:22 +01001/*
2 * (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
3 * All Rights Reserved
4 *
5 * Authors: Pau Espin Pedrol <pespin@sysmocom.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23/*! \addtogroup timer
24 * @{
25 * \file timer_clockgettime.c
26 * Overriding Time: osmo_clock_gettime()
27 * - Useful to write and reproduce tests that depend on specific time
28 * factors. This API allows to fake the timespec provided by `clock_gettime()`
29 * by using a small shim osmo_clock_gettime().
30 * - Choose the clock you want to override, for instance CLOCK_MONOTONIC.
31 * - If the clock override is disabled (default) for a given clock,
32 * osmo_clock_gettime() will do the same as regular `clock_gettime()`.
33 * - If you want osmo_clock_gettime() to provide a specific time, you must
34 * enable time override with osmo_clock_override_enable(),
35 * then set a pointer to the timespec storing the fake time for that
36 * specific clock (`struct timespec *ts =
37 * osmo_clock_override_gettimespec()`) and set it as
38 * desired. Next time osmo_clock_gettime() is called, it will return the
39 * values previously set through the ts pointer.
40 * - A helper osmo_clock_override_add() is provided to increment a given
41 * overriden clock with a specific amount of time.
42 */
43
44/*! \file timer_clockgettime.c
45 */
46
47#include <stdlib.h>
48#include <stdbool.h>
49#include <sys/time.h>
50#include <time.h>
51
52#include <osmocom/core/timer_compat.h>
53
54/*! An internal structure to handle overriden time for each clock type. */
55struct fakeclock {
56 bool override;
57 struct timespec time;
58};
59
60static struct fakeclock realtime;
61static struct fakeclock realtime_coarse;
62static struct fakeclock mono;
63static struct fakeclock mono_coarse;
64static struct fakeclock mono_raw;
65static struct fakeclock boottime;
66static struct fakeclock boottime;
67static struct fakeclock proc_cputime_id;
68static struct fakeclock th_cputime_id;
69
70static struct fakeclock* clkid_to_fakeclock(clockid_t clk_id)
71{
72 switch(clk_id) {
73 case CLOCK_REALTIME:
74 return &realtime;
75 case CLOCK_REALTIME_COARSE:
76 return &realtime_coarse;
77 case CLOCK_MONOTONIC:
78 return &mono;
79 case CLOCK_MONOTONIC_COARSE:
80 return &mono_coarse;
81 case CLOCK_MONOTONIC_RAW:
82 return &mono_raw;
83 case CLOCK_BOOTTIME:
84 return &boottime;
85 case CLOCK_PROCESS_CPUTIME_ID:
86 return &proc_cputime_id;
87 case CLOCK_THREAD_CPUTIME_ID:
88 return &th_cputime_id;
89 default:
90 return NULL;
91 }
92}
93
94/*! Shim around clock_gettime to be able to set the time manually.
95 *
96 * To override, use osmo_clock_override_enable and set the desired
97 * current time with osmo_clock_gettimespec. */
98int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp)
99{
100 struct fakeclock* c = clkid_to_fakeclock(clk_id);
101 if (!c || !c->override)
102 return clock_gettime(clk_id, tp);
103
104 *tp = c->time;
105 return 0;
106}
107
108/*! Convenience function to enable or disable a specific clock fake time.
109 */
110void osmo_clock_override_enable(clockid_t clk_id, bool enable)
111{
112 struct fakeclock* c = clkid_to_fakeclock(clk_id);
113 if (c)
114 c->override = enable;
115}
116
117/*! Convenience function to return a pointer to the timespec handling the
118 * fake time for clock clk_id. */
119struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id)
120{
121 struct fakeclock* c = clkid_to_fakeclock(clk_id);
122 if (c)
123 return &c->time;
124 return NULL;
125}
126
127/*! Convenience function to advance the fake time.
128 *
129 * Adds the given values to the clock time. */
130void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs)
131{
132 struct timespec val = { secs, nsecs };
133 struct fakeclock* c = clkid_to_fakeclock(clk_id);
134 if (c)
135 timespecadd(&c->time, &val, &c->time);
136}
137
138/*! @} */