Commit | Line | Data |
---|---|---|
ecbb78c0 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> | |
28 | #include <babeltrace/trace-ir/clock-snapshot-internal.h> | |
29 | #include <babeltrace/trace-ir/clock-class-internal.h> | |
30 | #include <babeltrace/assert-internal.h> | |
31 | ||
32 | struct bt_clock_snapshot_set { | |
33 | /* Unique objects owned by this */ | |
34 | GPtrArray *clock_snapshots; | |
35 | ||
36 | /* Weak; points to one of the clock snapshots above */ | |
37 | struct bt_clock_snapshot *default_cs; | |
38 | }; | |
39 | ||
40 | static inline | |
41 | int bt_clock_snapshot_set_initialize(struct bt_clock_snapshot_set *cs_set) | |
42 | { | |
43 | int ret = 0; | |
44 | ||
45 | cs_set->clock_snapshots = g_ptr_array_sized_new(1); | |
46 | if (!cs_set->clock_snapshots) { | |
47 | #ifdef BT_LOGE_STR | |
48 | BT_LOGE_STR("Failed to allocate one GPtrArray."); | |
49 | #endif | |
50 | ||
51 | ret = -1; | |
52 | goto end; | |
53 | } | |
54 | ||
55 | cs_set->default_cs = NULL; | |
56 | ||
57 | end: | |
58 | return ret; | |
59 | } | |
60 | ||
61 | static inline | |
62 | void bt_clock_snapshot_set_reset(struct bt_clock_snapshot_set *cs_set) | |
63 | { | |
64 | uint64_t i; | |
65 | ||
66 | BT_ASSERT(cs_set); | |
67 | BT_ASSERT(cs_set->clock_snapshots); | |
68 | ||
69 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
70 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
71 | ||
72 | BT_ASSERT(cs); | |
73 | bt_clock_snapshot_reset(cs); | |
74 | } | |
75 | ||
76 | cs_set->default_cs = NULL; | |
77 | } | |
78 | ||
79 | static inline | |
80 | void bt_clock_snapshot_set_finalize(struct bt_clock_snapshot_set *cs_set) | |
81 | { | |
82 | uint64_t i; | |
83 | ||
84 | BT_ASSERT(cs_set); | |
85 | ||
86 | if (cs_set->clock_snapshots) { | |
87 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
88 | struct bt_clock_snapshot *cs = | |
89 | cs_set->clock_snapshots->pdata[i]; | |
90 | ||
91 | BT_ASSERT(cs); | |
92 | bt_clock_snapshot_recycle(cs); | |
93 | } | |
94 | ||
95 | g_ptr_array_free(cs_set->clock_snapshots, TRUE); | |
96 | } | |
97 | ||
98 | cs_set->default_cs = NULL; | |
99 | } | |
100 | ||
101 | static inline | |
102 | int bt_clock_snapshot_set_set_clock_snapshot(struct bt_clock_snapshot_set *cs_set, | |
103 | struct bt_clock_class *cc, uint64_t raw_value) | |
104 | { | |
105 | int ret = 0; | |
106 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
107 | uint64_t i; | |
108 | ||
109 | BT_ASSERT(cs_set); | |
110 | BT_ASSERT(cc); | |
111 | ||
112 | /* | |
113 | * Check if we already have a value for this clock class. | |
114 | * | |
115 | * TODO: When we have many clock classes, make this more | |
116 | * efficient. | |
117 | */ | |
118 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
119 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
120 | ||
121 | BT_ASSERT(cs); | |
122 | ||
123 | if (cs->clock_class == cc) { | |
124 | clock_snapshot = cs; | |
125 | break; | |
126 | } | |
127 | } | |
128 | ||
129 | if (!clock_snapshot) { | |
130 | clock_snapshot = bt_clock_snapshot_create(cc); | |
131 | if (!clock_snapshot) { | |
132 | #ifdef BT_LIB_LOGE | |
133 | BT_LIB_LOGE("Cannot create a clock snapshot from a clock class: " | |
134 | "%![cc-]+K", cc); | |
135 | #endif | |
136 | ||
137 | ret = -1; | |
138 | goto end; | |
139 | } | |
140 | ||
141 | g_ptr_array_add(cs_set->clock_snapshots, clock_snapshot); | |
142 | } | |
143 | ||
0dcc515f | 144 | bt_clock_snapshot_set_raw_value(clock_snapshot, raw_value); |
ecbb78c0 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); | |
0dcc515f | 156 | bt_clock_snapshot_set_raw_value(cs_set->default_cs, raw_value); |
ecbb78c0 PP |
157 | } |
158 | ||
159 | #endif /* BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H */ |