diff options
author | 2019-09-30 15:47:46 +0300 | |
---|---|---|
committer | 2019-09-30 15:47:46 +0300 | |
commit | ed9fa7dafb9407aeaa044d835daabf26a934b7df (patch) | |
tree | 945bf2cb6c1e9b0d945d256eb2d8690b9b669c8e | |
parent | 5d29cc15a5871ab3ee4b78bc1b803818e8bce812 (diff) | |
download | librin-ed9fa7dafb9407aeaa044d835daabf26a934b7df.tar.gz librin-ed9fa7dafb9407aeaa044d835daabf26a934b7df.tar.bz2 librin-ed9fa7dafb9407aeaa044d835daabf26a934b7df.zip |
time: add benchmarking capabilities.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
-rw-r--r-- | include/rin/time.h | 14 | ||||
-rw-r--r-- | src/time/benchmark.c | 76 | ||||
-rw-r--r-- | src/time/meson.build | 1 |
3 files changed, 91 insertions, 0 deletions
diff --git a/include/rin/time.h b/include/rin/time.h index 9c8200f..d335e1f 100644 --- a/include/rin/time.h +++ b/include/rin/time.h @@ -23,6 +23,20 @@ #include <sys/time.h> +/* benchmarking related */ + +struct rin_bench_result { + struct timespec wall; + struct timeval system; + struct timeval user; + struct timeval total; +}; + +int rin_bench_start(void); +int rin_bench_stop(struct rin_bench_result *res); + +/* time data types related */ + struct timespec rin_time_add(const struct timespec * const a, const struct timespec * const b); struct timespec rin_time_sub(const struct timespec * const a, const struct timespec * const b); struct timespec rin_time_normalize(const struct timespec * const t); diff --git a/src/time/benchmark.c b/src/time/benchmark.c new file mode 100644 index 0000000..543275a --- /dev/null +++ b/src/time/benchmark.c @@ -0,0 +1,76 @@ +/* + * The Rin Library – time module, benchmarking section + * + * Copyright (C) 2019 Gediminas Jakutis + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; version 2.1 + * of the License. + * + * 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#include <time.h> +#include <errno.h> +#include <sys/resource.h> +#include "rin/time.h" +#include "time_private.h" + +#if defined CLOCK_MONOTONIC +# define RIN_CLOCK_WALL_COUNTER CLOCK_MONOTONIC +#else +# define RIN_CLOCK_WALL_COUNTER CLOCK_REALTIME +#endif /* defined CLOCK_MONOTONIC */ + +static struct bench { + int status; + struct rusage runtime; + struct timespec wall; +} bench = {0}; + + +int rin_bench_start(void) +{ + if (bench.status) { + return EAGAIN; + } + + bench.status = 1; + clock_gettime(RIN_CLOCK_WALL_COUNTER, &bench.wall); + getrusage(RUSAGE_SELF, &bench.runtime); + + return 0; +} + +int rin_bench_stop(struct rin_bench_result *res) +{ + struct timespec wall; + struct rusage run; + + if (!bench.status) { + return EAGAIN; + } + + if (!res) { + return EINVAL; + } + + getrusage(RUSAGE_SELF, &run); + clock_gettime(RIN_CLOCK_WALL_COUNTER, &wall); + + res->wall = rin_time_sub(&wall, &bench.wall); + res->system = rin_timeval_sub(&run.ru_stime, &bench.runtime.ru_stime); + res->user = rin_timeval_sub(&run.ru_utime, &bench.runtime.ru_utime); + res->total = rin_timeval_add(&res->system, &res->user); + + bench.status = 0; + return 0; +} diff --git a/src/time/meson.build b/src/time/meson.build index 93c1a4d..5a6d9cc 100644 --- a/src/time/meson.build +++ b/src/time/meson.build @@ -1,5 +1,6 @@ if get_option('time') time_filenames = [ + 'benchmark.c', 'time.c', 'time_private.h', ] |