Move to kernel style SPDX license identifiers
[lttng-ust.git] / doc / examples / clock-override / lttng-ust-clock-override-example.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <time.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <lttng/ust-clock.h>
13
14 /*
15 * For sake of example, transform time into a coarse clock (freq: 1KHz).
16 * Note that division can be slow on some architectures. Should be
17 * avoided. Use shift and multiplication instead. e.g.:
18 *
19 * Operation: / 1000000ULL
20 * 1/1000000ULL = .000001
21 * 2^19 < 1000000 < 2^20
22 * Add a 10 bits shift to increase accuracy:
23 * 2^(19+10) = 536870912
24 * x * 1 / 2^(19+10) ~= .000001
25 * 537 * 1 / 2^29 = .00000100024044513702
26 * 537 (multiplication factor) is between 2^9 and 2^10.
27 *
28 * In order not to overflow, first right shift by 10, multiply, and right
29 * shift by 19.
30 */
31 #define DIV_CLOCK_SHIFT1 10
32 #define DIV_CLOCK_MUL 537
33 #define DIV_CLOCK_SHIFT2 19
34
35 static
36 uint64_t plugin_read64(void)
37 {
38 struct timespec ts;
39
40 clock_gettime(CLOCK_MONOTONIC, &ts);
41 /*
42 * This is a rather dumb example, but it illustrates the plugin
43 * mechanism: we take the monotonic clock, and transform it into
44 * a very coarse clock, which increment only at 1KHz frequency.
45 */
46 return ((uint64_t) ts.tv_sec * 1000ULL)
47 + ((DIV_CLOCK_MUL * ((uint64_t) ts.tv_nsec >> DIV_CLOCK_SHIFT1))
48 >> DIV_CLOCK_SHIFT2);
49 }
50
51 static
52 uint64_t plugin_freq(void)
53 {
54 return 1000; /* 1KHz clock (very coarse!) */
55 }
56
57 static
58 int plugin_uuid(char *uuid)
59 {
60 const char myuuid[] = "123456789012345678901234567890123456";
61
62 /*
63 * Should read some unique identifier for this clock shared
64 * across all components of the system using this clock for
65 * tracing.
66 */
67 memcpy(uuid, myuuid, LTTNG_UST_UUID_STR_LEN);
68 return 0;
69 }
70
71 static
72 const char *plugin_name(void)
73 {
74 return "my_example_clock";
75 }
76
77 static
78 const char *plugin_description(void)
79 {
80 return "Coarse monotonic clock at 1KHz";
81 }
82
83 void lttng_ust_clock_plugin_init(void)
84 {
85 int ret;
86
87 ret = lttng_ust_trace_clock_set_read64_cb(plugin_read64);
88 if (ret) {
89 fprintf(stderr, "Error setting clock override read64 callback: %s\n",
90 strerror(-ret));
91 goto error;
92 }
93 ret = lttng_ust_trace_clock_set_freq_cb(plugin_freq);
94 if (ret) {
95 fprintf(stderr, "Error setting clock override freq callback: %s\n",
96 strerror(-ret));
97 goto error;
98 }
99 ret = lttng_ust_trace_clock_set_uuid_cb(plugin_uuid);
100 if (ret) {
101 fprintf(stderr, "Error setting clock override uuid callback: %s\n",
102 strerror(-ret));
103 goto error;
104 }
105
106 ret = lttng_ust_trace_clock_set_name_cb(plugin_name);
107 if (ret) {
108 fprintf(stderr, "Error setting clock override name callback: %s\n",
109 strerror(-ret));
110 goto error;
111 }
112
113 ret = lttng_ust_trace_clock_set_description_cb(plugin_description);
114 if (ret) {
115 fprintf(stderr, "Error setting clock override description callback: %s\n",
116 strerror(-ret));
117 goto error;
118 }
119
120 ret = lttng_ust_enable_trace_clock_override();
121 if (ret) {
122 fprintf(stderr, "Error enabling clock override: %s\n",
123 strerror(-ret));
124 goto error;
125 }
126
127 return;
128
129 error:
130 exit(EXIT_FAILURE);
131 }
This page took 0.031022 seconds and 4 git commands to generate.