Commit | Line | Data |
---|---|---|
605e1019 PP |
1 | #ifndef BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H |
2 | #define BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H | |
3 | ||
4 | /* | |
5 | * Copyright 2018 Philippe Proulx <pproulx@efficios.com> | |
6 | * | |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
20 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
23 | * SOFTWARE. | |
24 | */ | |
25 | ||
26 | #include <stdint.h> | |
27 | #include <glib.h> | |
578e048b MJ |
28 | #include "common/assert.h" |
29 | ||
30 | #include "clock-snapshot.h" | |
31 | #include "clock-class.h" | |
605e1019 | 32 | |
197bad6d PP |
33 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
34 | #ifndef BT_LIB_LOG_SUPPORTED | |
35 | # error Please include "lib/logging.h" before including this file. | |
36 | #endif | |
37 | ||
605e1019 PP |
38 | struct bt_clock_snapshot_set { |
39 | /* Unique objects owned by this */ | |
40 | GPtrArray *clock_snapshots; | |
41 | ||
42 | /* Weak; points to one of the clock snapshots above */ | |
43 | struct bt_clock_snapshot *default_cs; | |
44 | }; | |
45 | ||
46 | static inline | |
47 | int bt_clock_snapshot_set_initialize(struct bt_clock_snapshot_set *cs_set) | |
48 | { | |
49 | int ret = 0; | |
50 | ||
51 | cs_set->clock_snapshots = g_ptr_array_sized_new(1); | |
52 | if (!cs_set->clock_snapshots) { | |
605e1019 | 53 | BT_LOGE_STR("Failed to allocate one GPtrArray."); |
605e1019 PP |
54 | ret = -1; |
55 | goto end; | |
56 | } | |
57 | ||
58 | cs_set->default_cs = NULL; | |
59 | ||
60 | end: | |
61 | return ret; | |
62 | } | |
63 | ||
64 | static inline | |
65 | void bt_clock_snapshot_set_reset(struct bt_clock_snapshot_set *cs_set) | |
66 | { | |
67 | uint64_t i; | |
68 | ||
69 | BT_ASSERT(cs_set); | |
70 | BT_ASSERT(cs_set->clock_snapshots); | |
71 | ||
72 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
73 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
74 | ||
75 | BT_ASSERT(cs); | |
76 | bt_clock_snapshot_reset(cs); | |
77 | } | |
78 | ||
79 | cs_set->default_cs = NULL; | |
80 | } | |
81 | ||
82 | static inline | |
83 | void bt_clock_snapshot_set_finalize(struct bt_clock_snapshot_set *cs_set) | |
84 | { | |
85 | uint64_t i; | |
86 | ||
87 | BT_ASSERT(cs_set); | |
88 | ||
89 | if (cs_set->clock_snapshots) { | |
90 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
91 | struct bt_clock_snapshot *cs = | |
92 | cs_set->clock_snapshots->pdata[i]; | |
93 | ||
94 | BT_ASSERT(cs); | |
95 | bt_clock_snapshot_recycle(cs); | |
96 | } | |
97 | ||
98 | g_ptr_array_free(cs_set->clock_snapshots, TRUE); | |
99 | } | |
100 | ||
101 | cs_set->default_cs = NULL; | |
102 | } | |
103 | ||
104 | static inline | |
105 | int bt_clock_snapshot_set_set_clock_snapshot(struct bt_clock_snapshot_set *cs_set, | |
106 | struct bt_clock_class *cc, uint64_t raw_value) | |
107 | { | |
108 | int ret = 0; | |
109 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
110 | uint64_t i; | |
111 | ||
112 | BT_ASSERT(cs_set); | |
113 | BT_ASSERT(cc); | |
114 | ||
115 | /* | |
116 | * Check if we already have a value for this clock class. | |
117 | * | |
118 | * TODO: When we have many clock classes, make this more | |
119 | * efficient. | |
120 | */ | |
121 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
122 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
123 | ||
124 | BT_ASSERT(cs); | |
125 | ||
126 | if (cs->clock_class == cc) { | |
127 | clock_snapshot = cs; | |
128 | break; | |
129 | } | |
130 | } | |
131 | ||
132 | if (!clock_snapshot) { | |
133 | clock_snapshot = bt_clock_snapshot_create(cc); | |
134 | if (!clock_snapshot) { | |
605e1019 PP |
135 | BT_LIB_LOGE("Cannot create a clock snapshot from a clock class: " |
136 | "%![cc-]+K", cc); | |
605e1019 PP |
137 | ret = -1; |
138 | goto end; | |
139 | } | |
140 | ||
141 | g_ptr_array_add(cs_set->clock_snapshots, clock_snapshot); | |
142 | } | |
143 | ||
f0ebeace | 144 | bt_clock_snapshot_set_raw_value(clock_snapshot, raw_value); |
605e1019 PP |
145 | |
146 | end: | |
147 | return ret; | |
148 | } | |
149 | ||
150 | static inline | |
151 | void bt_clock_snapshot_set_set_default_clock_snapshot( | |
152 | struct bt_clock_snapshot_set *cs_set, uint64_t raw_value) | |
153 | { | |
154 | BT_ASSERT(cs_set); | |
155 | BT_ASSERT(cs_set->default_cs); | |
f0ebeace | 156 | bt_clock_snapshot_set_raw_value(cs_set->default_cs, raw_value); |
605e1019 PP |
157 | } |
158 | ||
159 | #endif /* BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H */ |