X-Git-Url: http://git.lttng.org/?a=blobdiff_plain;f=doc%2Fexamples%2Fclock-override%2Flttng-ust-clock-override-example.c;h=7b8201b2d52617f75dd9139ce34ebb6a7400c727;hb=HEAD;hp=766e66c9a13a612862aaa23b09112caf6ccbc524;hpb=f9364363c8982127d66c2a29cb1fa21760bd00e5;p=lttng-ust.git diff --git a/doc/examples/clock-override/lttng-ust-clock-override-example.c b/doc/examples/clock-override/lttng-ust-clock-override-example.c index 766e66c9..7b8201b2 100644 --- a/doc/examples/clock-override/lttng-ust-clock-override-example.c +++ b/doc/examples/clock-override/lttng-ust-clock-override-example.c @@ -1,33 +1,37 @@ /* - * lttng-clock-override-example.c + * SPDX-License-Identifier: MIT * - * Copyright (c) 2014 Mathieu Desnoyers - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * Copyright (C) 2014 Mathieu Desnoyers */ #include +#include #include #include #include #include +/* + * For sake of example, transform time into a coarse clock (freq: 1KHz). + * Note that division can be slow on some architectures. Should be + * avoided. Use shift and multiplication instead. e.g.: + * + * Operation: / 1000000ULL + * 1/1000000ULL = .000001 + * 2^19 < 1000000 < 2^20 + * Add a 10 bits shift to increase accuracy: + * 2^(19+10) = 536870912 + * x * 1 / 2^(19+10) ~= .000001 + * 537 * 1 / 2^29 = .00000100024044513702 + * 537 (multiplication factor) is between 2^9 and 2^10. + * + * In order not to overflow, first right shift by 10, multiply, and right + * shift by 19. + */ +#define DIV_CLOCK_SHIFT1 10 +#define DIV_CLOCK_MUL 537 +#define DIV_CLOCK_SHIFT2 19 + static uint64_t plugin_read64(void) { @@ -39,7 +43,9 @@ uint64_t plugin_read64(void) * mechanism: we take the monotonic clock, and transform it into * a very coarse clock, which increment only at 1KHz frequency. */ - return ((uint64_t) ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL); + return ((uint64_t) ts.tv_sec * 1000ULL) + + ((DIV_CLOCK_MUL * ((uint64_t) ts.tv_nsec >> DIV_CLOCK_SHIFT1)) + >> DIV_CLOCK_SHIFT2); } static @@ -74,6 +80,10 @@ const char *plugin_description(void) return "Coarse monotonic clock at 1KHz"; } +/* + * Entry-point called by liblttng-ust through dlsym(); + */ +void lttng_ust_clock_plugin_init(void); void lttng_ust_clock_plugin_init(void) { int ret;