Implement clock override plugin support
[lttng-ust.git] / doc / examples / clock-override / lttng-ust-clock-override-example.c
1 /*
2 * lttng-clock-override-example.c
3 *
4 * Copyright (c) 2014 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <stdlib.h>
26 #include <time.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <lttng/ust-clock.h>
30
31 static
32 uint64_t plugin_read64(void)
33 {
34 struct timespec ts;
35
36 clock_gettime(CLOCK_MONOTONIC, &ts);
37 /*
38 * This is a rather dumb example, but it illustrates the plugin
39 * mechanism: we take the monotonic clock, and transform it into
40 * a very coarse clock, which increment only at 1KHz frequency.
41 */
42 return ((uint64_t) ts.tv_sec * 1000ULL) + (ts.tv_nsec / 1000000ULL);
43 }
44
45 static
46 uint64_t plugin_freq(void)
47 {
48 return 1000; /* 1KHz clock (very coarse!) */
49 }
50
51 static
52 int plugin_uuid(char *uuid)
53 {
54 const char myuuid[] = "123456789012345678901234567890123456";
55
56 /*
57 * Should read some unique identifier for this clock shared
58 * across all components of the system using this clock for
59 * tracing.
60 */
61 memcpy(uuid, myuuid, LTTNG_UST_UUID_STR_LEN);
62 return 0;
63 }
64
65 static
66 const char *plugin_name(void)
67 {
68 return "my_example_clock";
69 }
70
71 static
72 const char *plugin_description(void)
73 {
74 return "Coarse monotonic clock at 1KHz";
75 }
76
77 void lttng_ust_clock_plugin_init(void)
78 {
79 int ret;
80
81 ret = lttng_ust_trace_clock_set_read64_cb(plugin_read64);
82 if (ret) {
83 fprintf(stderr, "Error setting clock override read64 callback: %s\n",
84 strerror(-ret));
85 goto error;
86 }
87 ret = lttng_ust_trace_clock_set_freq_cb(plugin_freq);
88 if (ret) {
89 fprintf(stderr, "Error setting clock override freq callback: %s\n",
90 strerror(-ret));
91 goto error;
92 }
93 ret = lttng_ust_trace_clock_set_uuid_cb(plugin_uuid);
94 if (ret) {
95 fprintf(stderr, "Error setting clock override uuid callback: %s\n",
96 strerror(-ret));
97 goto error;
98 }
99
100 ret = lttng_ust_trace_clock_set_name_cb(plugin_name);
101 if (ret) {
102 fprintf(stderr, "Error setting clock override name callback: %s\n",
103 strerror(-ret));
104 goto error;
105 }
106
107 ret = lttng_ust_trace_clock_set_description_cb(plugin_description);
108 if (ret) {
109 fprintf(stderr, "Error setting clock override description callback: %s\n",
110 strerror(-ret));
111 goto error;
112 }
113
114 ret = lttng_ust_enable_trace_clock_override();
115 if (ret) {
116 fprintf(stderr, "Error enabling clock override: %s\n",
117 strerror(-ret));
118 goto error;
119 }
120
121 return;
122
123 error:
124 exit(EXIT_FAILURE);
125 }
This page took 0.033043 seconds and 5 git commands to generate.