Commit | Line | Data |
---|---|---|
f9364363 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: MIT |
f9364363 | 3 | * |
c0c0989a | 4 | * Copyright (C) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
f9364363 MD |
5 | */ |
6 | ||
7 | #include <stdlib.h> | |
fb31eb73 | 8 | #include <stdint.h> |
f9364363 MD |
9 | #include <time.h> |
10 | #include <string.h> | |
11 | #include <stdio.h> | |
12 | #include <lttng/ust-clock.h> | |
13 | ||
2ea7bae5 MD |
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 | ||
f9364363 MD |
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 | */ | |
2ea7bae5 MD |
46 | return ((uint64_t) ts.tv_sec * 1000ULL) |
47 | + ((DIV_CLOCK_MUL * ((uint64_t) ts.tv_nsec >> DIV_CLOCK_SHIFT1)) | |
48 | >> DIV_CLOCK_SHIFT2); | |
f9364363 MD |
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 | ||
4b4a1337 MJ |
83 | /* |
84 | * Entry-point called by liblttng-ust through dlsym(); | |
85 | */ | |
86 | void lttng_ust_clock_plugin_init(void); | |
f9364363 MD |
87 | void lttng_ust_clock_plugin_init(void) |
88 | { | |
89 | int ret; | |
90 | ||
91 | ret = lttng_ust_trace_clock_set_read64_cb(plugin_read64); | |
92 | if (ret) { | |
93 | fprintf(stderr, "Error setting clock override read64 callback: %s\n", | |
94 | strerror(-ret)); | |
95 | goto error; | |
96 | } | |
97 | ret = lttng_ust_trace_clock_set_freq_cb(plugin_freq); | |
98 | if (ret) { | |
99 | fprintf(stderr, "Error setting clock override freq callback: %s\n", | |
100 | strerror(-ret)); | |
101 | goto error; | |
102 | } | |
103 | ret = lttng_ust_trace_clock_set_uuid_cb(plugin_uuid); | |
104 | if (ret) { | |
105 | fprintf(stderr, "Error setting clock override uuid callback: %s\n", | |
106 | strerror(-ret)); | |
107 | goto error; | |
108 | } | |
109 | ||
110 | ret = lttng_ust_trace_clock_set_name_cb(plugin_name); | |
111 | if (ret) { | |
112 | fprintf(stderr, "Error setting clock override name callback: %s\n", | |
113 | strerror(-ret)); | |
114 | goto error; | |
115 | } | |
116 | ||
117 | ret = lttng_ust_trace_clock_set_description_cb(plugin_description); | |
118 | if (ret) { | |
119 | fprintf(stderr, "Error setting clock override description callback: %s\n", | |
120 | strerror(-ret)); | |
121 | goto error; | |
122 | } | |
123 | ||
124 | ret = lttng_ust_enable_trace_clock_override(); | |
125 | if (ret) { | |
126 | fprintf(stderr, "Error enabling clock override: %s\n", | |
127 | strerror(-ret)); | |
128 | goto error; | |
129 | } | |
130 | ||
131 | return; | |
132 | ||
133 | error: | |
134 | exit(EXIT_FAILURE); | |
135 | } |