/* * ***************************************************************************** * * SPDX-License-Identifier: BSD-2-Clause * * Copyright (c) 2018-2020 Gavin D. Howard and contributors. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * * ***************************************************************************** */ #include #include typedef struct timespec TimeSpec; unsigned long pow10(unsigned long p) { unsigned ret = 1; size_t i; for (i = 0; i < p; ++i) { ret *= 10; } return ret; } TimeSpec ts_get(void) { TimeSpec ts; clock_gettime(CLOCK_MONOTONIC, &ts); return ts; } TimeSpec ts_diff(TimeSpec end, TimeSpec start) { TimeSpec temp; if ((end.tv_nsec - start.tv_nsec) < 0) { temp.tv_sec = end.tv_sec - start.tv_sec - 1; temp.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec; } else { temp.tv_sec = end.tv_sec - start.tv_sec; temp.tv_nsec = end.tv_nsec - start.tv_nsec; } return temp; } TimeSpec ts_add(TimeSpec ts1, TimeSpec ts2) { TimeSpec temp; if ((ts1.tv_nsec + ts2.tv_nsec) > 1000000000) { temp.tv_sec = ts1.tv_sec + ts2.tv_sec + 1; temp.tv_nsec = ts1.tv_nsec + ts2.tv_nsec - 1000000000; } else { temp.tv_sec = ts1.tv_sec + ts2.tv_sec; temp.tv_nsec = ts1.tv_nsec + ts2.tv_nsec; } return temp; } unsigned long ts_div(TimeSpec *ts, unsigned long denom) { TimeSpec temp; unsigned long ns; temp.tv_sec = ts->tv_sec / denom; ns = (ts->tv_sec % denom) * 1000000000; ns = ns + ts->tv_nsec; temp.tv_nsec = ns / denom; ns %= denom; *ts = temp; return ns; } void ts_time(TimeSpec *ts) { clock_gettime(CLOCK_MONOTONIC, ts); } void ts_print(TimeSpec ts, size_t iters) { unsigned long mod = ts_div(&ts, iters); printf("%lu.%09lu%09zu", (unsigned long) ts.tv_sec, ts.tv_nsec, mod); }