libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Patch Set 2: Code-Review+2 -- To view, visit https://gerrit.osmocom.org/6961 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 Gerrit-PatchSet: 2 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Pau Espin Pedrol Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: No
[MERGED] libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Harald Welte has submitted this change and it was merged. Change subject: timer: Introduce osmo_clock_gettime to override clock_gettime .. timer: Introduce osmo_clock_gettime to override clock_gettime Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 --- M include/osmocom/core/timer.h M src/Makefile.am A src/timer_clockgettime.c M src/timer_gettimeofday.c M tests/Makefile.am M tests/testsuite.at A tests/timer/clk_override_test.c A tests/timer/clk_override_test.ok 8 files changed, 255 insertions(+), 4 deletions(-) Approvals: Harald Welte: Looks good to me, approved Jenkins Builder: Verified diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h index 4958efb..caf4c67 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -40,6 +40,7 @@ #pragma once #include +#include #include #include @@ -87,6 +88,7 @@ int osmo_timers_check(void); int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); +int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp); /* * timer override @@ -96,4 +98,8 @@ extern struct timeval osmo_gettimeofday_override_time; void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); +void osmo_clock_override_enable(clockid_t clk_id, bool enable); +void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs); +struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id); + /*! @} */ diff --git a/src/Makefile.am b/src/Makefile.am index 3d6e6f7..2641a97 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,8 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_LIBADD = $(BACKTRACE_LIB) $(TALLOC_LIBS) -libosmocore_la_SOURCES = timer.c timer_gettimeofday.c select.c signal.c msgb.c bits.c \ +libosmocore_la_SOURCES = timer.c timer_gettimeofday.c timer_clockgettime.c \ +select.c signal.c msgb.c bits.c \ bitvec.c bitcomp.c counter.c fsm.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c logging_gsmtap.c rate_ctr.c \ diff --git a/src/timer_clockgettime.c b/src/timer_clockgettime.c new file mode 100644 index 000..8d9760c --- /dev/null +++ b/src/timer_clockgettime.c @@ -0,0 +1,138 @@ +/* + * (C) 2016 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Authors: Pau Espin Pedrol + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/*! \addtogroup timer + * @{ + * \file timer_clockgettime.c + * Overriding Time: osmo_clock_gettime() + * - Useful to write and reproduce tests that depend on specific time + *factors. This API allows to fake the timespec provided by `clock_gettime()` + *by using a small shim osmo_clock_gettime(). + * - Choose the clock you want to override, for instance CLOCK_MONOTONIC. + * - If the clock override is disabled (default) for a given clock, + *osmo_clock_gettime() will do the same as regular `clock_gettime()`. + * - If you want osmo_clock_gettime() to provide a specific time, you must + *enable time override with osmo_clock_override_enable(), + *then set a pointer to the timespec storing the fake time for that + *specific clock (`struct timespec *ts = + *osmo_clock_override_gettimespec()`) and set it as + *desired. Next time osmo_clock_gettime() is called, it will return the + *values previously set through the ts pointer. + * - A helper osmo_clock_override_add() is provided to increment a given + *overriden clock with a specific amount of time. + */ + +/*! \file timer_clockgettime.c + */ + +#include +#include +#include +#include + +#include + +/*! An internal structure to handle overriden time for each clock type. */ +struct fakeclock { + bool override; + struct timespec time; +}; + +static struct fakeclock realtime; +static struct fakeclock realtime_coarse; +static struct fakeclock mono; +static struct fakeclock mono_coarse; +static struct fakeclock mono_raw; +static struct fakeclock boottime; +static struct fakeclock boottime; +static struct fakeclock proc_cputime_id; +static struct fakeclock th_cputime_id; + +static struct fakeclock* clkid_to_fakeclock(clock
[PATCH] libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Hello Harald Welte, Jenkins Builder, I'd like you to reexamine a change. Please visit https://gerrit.osmocom.org/6961 to look at the new patch set (#2). timer: Introduce osmo_clock_gettime to override clock_gettime Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 --- M include/osmocom/core/timer.h M src/Makefile.am A src/timer_clockgettime.c M src/timer_gettimeofday.c M tests/Makefile.am M tests/testsuite.at A tests/timer/clk_override_test.c A tests/timer/clk_override_test.ok 8 files changed, 255 insertions(+), 4 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/61/6961/2 diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h index 4958efb..caf4c67 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -40,6 +40,7 @@ #pragma once #include +#include #include #include @@ -87,6 +88,7 @@ int osmo_timers_check(void); int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); +int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp); /* * timer override @@ -96,4 +98,8 @@ extern struct timeval osmo_gettimeofday_override_time; void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); +void osmo_clock_override_enable(clockid_t clk_id, bool enable); +void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs); +struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id); + /*! @} */ diff --git a/src/Makefile.am b/src/Makefile.am index 3d6e6f7..2641a97 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,8 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_LIBADD = $(BACKTRACE_LIB) $(TALLOC_LIBS) -libosmocore_la_SOURCES = timer.c timer_gettimeofday.c select.c signal.c msgb.c bits.c \ +libosmocore_la_SOURCES = timer.c timer_gettimeofday.c timer_clockgettime.c \ +select.c signal.c msgb.c bits.c \ bitvec.c bitcomp.c counter.c fsm.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c logging_gsmtap.c rate_ctr.c \ diff --git a/src/timer_clockgettime.c b/src/timer_clockgettime.c new file mode 100644 index 000..8d9760c --- /dev/null +++ b/src/timer_clockgettime.c @@ -0,0 +1,138 @@ +/* + * (C) 2016 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Authors: Pau Espin Pedrol + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/*! \addtogroup timer + * @{ + * \file timer_clockgettime.c + * Overriding Time: osmo_clock_gettime() + * - Useful to write and reproduce tests that depend on specific time + *factors. This API allows to fake the timespec provided by `clock_gettime()` + *by using a small shim osmo_clock_gettime(). + * - Choose the clock you want to override, for instance CLOCK_MONOTONIC. + * - If the clock override is disabled (default) for a given clock, + *osmo_clock_gettime() will do the same as regular `clock_gettime()`. + * - If you want osmo_clock_gettime() to provide a specific time, you must + *enable time override with osmo_clock_override_enable(), + *then set a pointer to the timespec storing the fake time for that + *specific clock (`struct timespec *ts = + *osmo_clock_override_gettimespec()`) and set it as + *desired. Next time osmo_clock_gettime() is called, it will return the + *values previously set through the ts pointer. + * - A helper osmo_clock_override_add() is provided to increment a given + *overriden clock with a specific amount of time. + */ + +/*! \file timer_clockgettime.c + */ + +#include +#include +#include +#include + +#include + +/*! An internal structure to handle overriden time for each clock type. */ +struct fakeclock { + bool override; + struct timespec time; +}; + +static struct fakeclock realtime; +static struct fakeclock realtime_coarse; +static struct fakeclock mono; +static struct fakeclock mono_coarse; +static struct fakeclock mono_raw; +static struct fakeclock boottime; +static struct fakeclock boottime; +static struct fakeclock proc_cputime_id; +static struct fakeclock th_cputime_id; + +static struct fakeclock* clkid_to_fakeclock(clockid_t clk_id) +{ + switch(clk_id) { + case
libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Patch Set 1: Code-Review+1 (1 comment) https://gerrit.osmocom.org/#/c/6961/1/src/timer_clockgettime.c File src/timer_clockgettime.c: Line 23: /*! \addtogroup timer I think what's missing here is a nice description on why this API exists, what it is used for, and how one would use it - ending up with our doxygen documentation for the libosmocore API documentation. -- To view, visit https://gerrit.osmocom.org/6961 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Pau Espin Pedrol Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: Yes
libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Patch Set 1: another approach would have been to always use osmo_gettimeofday internally and provide a weak symbol inside the library, which a user could override with a strong symbol. But that ouf course would leave it to the user to implement his own logic for advancing time, without the infrastructure provided here. -- To view, visit https://gerrit.osmocom.org/6961 To unsubscribe, visit https://gerrit.osmocom.org/settings Gerrit-MessageType: comment Gerrit-Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 Gerrit-PatchSet: 1 Gerrit-Project: libosmocore Gerrit-Branch: master Gerrit-Owner: Pau Espin Pedrol Gerrit-Reviewer: Harald Welte Gerrit-Reviewer: Jenkins Builder Gerrit-HasComments: No
[PATCH] libosmocore[master]: timer: Introduce osmo_clock_gettime to override clock_gettime
Review at https://gerrit.osmocom.org/6961 timer: Introduce osmo_clock_gettime to override clock_gettime Change-Id: I5bebc6e01fc9d238065bc2517058f0ba85620349 --- M include/osmocom/core/timer.h M src/Makefile.am A src/timer_clockgettime.c M tests/Makefile.am M tests/testsuite.at A tests/timer/clk_override_test.c A tests/timer/clk_override_test.ok 7 files changed, 235 insertions(+), 3 deletions(-) git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/61/6961/1 diff --git a/include/osmocom/core/timer.h b/include/osmocom/core/timer.h index 4958efb..caf4c67 100644 --- a/include/osmocom/core/timer.h +++ b/include/osmocom/core/timer.h @@ -40,6 +40,7 @@ #pragma once #include +#include #include #include @@ -87,6 +88,7 @@ int osmo_timers_check(void); int osmo_gettimeofday(struct timeval *tv, struct timezone *tz); +int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp); /* * timer override @@ -96,4 +98,8 @@ extern struct timeval osmo_gettimeofday_override_time; void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs); +void osmo_clock_override_enable(clockid_t clk_id, bool enable); +void osmo_clock_override_add(clockid_t clk_id, time_t secs, long nsecs); +struct timespec *osmo_clock_override_gettimespec(clockid_t clk_id); + /*! @} */ diff --git a/src/Makefile.am b/src/Makefile.am index 3d6e6f7..2641a97 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,7 +13,8 @@ lib_LTLIBRARIES = libosmocore.la libosmocore_la_LIBADD = $(BACKTRACE_LIB) $(TALLOC_LIBS) -libosmocore_la_SOURCES = timer.c timer_gettimeofday.c select.c signal.c msgb.c bits.c \ +libosmocore_la_SOURCES = timer.c timer_gettimeofday.c timer_clockgettime.c \ +select.c signal.c msgb.c bits.c \ bitvec.c bitcomp.c counter.c fsm.c \ write_queue.c utils.c socket.c \ logging.c logging_syslog.c logging_gsmtap.c rate_ctr.c \ diff --git a/src/timer_clockgettime.c b/src/timer_clockgettime.c new file mode 100644 index 000..13454eb --- /dev/null +++ b/src/timer_clockgettime.c @@ -0,0 +1,119 @@ +/* + * (C) 2016 by sysmocom s.f.m.c. GmbH + * All Rights Reserved + * + * Authors: Pau Espin Pedrol + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + * + */ + +/*! \addtogroup timer + * @{ + */ + +/*! \file timer_clockgettime.c + */ + +#include +#include +#include +#include + +#include + + +struct fakeclock { + bool override; + struct timespec time; +}; + +static struct fakeclock realtime; +static struct fakeclock realtime_coarse; +static struct fakeclock mono; +static struct fakeclock mono_coarse; +static struct fakeclock mono_raw; +static struct fakeclock boottime; +static struct fakeclock boottime; +static struct fakeclock proc_cputime_id; +static struct fakeclock th_cputime_id; + +static struct fakeclock* clkid_to_fakeclock(clockid_t clk_id) +{ + switch(clk_id) { + case CLOCK_REALTIME: + return &realtime; + case CLOCK_REALTIME_COARSE: + return &realtime_coarse; + case CLOCK_MONOTONIC: + return &mono; + case CLOCK_MONOTONIC_COARSE: + return &mono_coarse; + case CLOCK_MONOTONIC_RAW: + return &mono_raw; + case CLOCK_BOOTTIME: + return &boottime; + case CLOCK_PROCESS_CPUTIME_ID: + return &proc_cputime_id; + case CLOCK_THREAD_CPUTIME_ID: + return &th_cputime_id; + default: + return NULL; + } +} + +/*! \brief shim around clock_gettime to be able to set the time manually. + * To override, use osmo_clock_override_enable and set the desired + * current time with osmo_clock_gettimespec. */ +int osmo_clock_gettime(clockid_t clk_id, struct timespec *tp) +{ + struct fakeclock* c = clkid_to_fakeclock(clk_id); + if (!c || !c->override) + return clock_gettime(clk_id, tp); + + *tp = c->time; + return 0; +} + +/*! \brief convenience function to enable or disable a specific clock fake time. + */ +void osmo_clock_override_enable(clockid_t clk_id, bool enable) +{ + struct fakeclock* c = clkid_to_fakeclock(clk_id); + if (c) + c->override = enable; +} + +/*! \brief convenience func