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 | ||
870631a2 PP |
26 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
27 | #ifndef BT_LIB_LOG_SUPPORTED | |
28 | # error Please include "lib/logging.h" before including this file. | |
29 | #endif | |
30 | ||
605e1019 PP |
31 | #include <stdint.h> |
32 | #include <glib.h> | |
578e048b MJ |
33 | #include "common/assert.h" |
34 | ||
35 | #include "clock-snapshot.h" | |
36 | #include "clock-class.h" | |
605e1019 | 37 | |
197bad6d PP |
38 | /* Protection: this file uses BT_LIB_LOG*() macros directly */ |
39 | #ifndef BT_LIB_LOG_SUPPORTED | |
40 | # error Please include "lib/logging.h" before including this file. | |
41 | #endif | |
42 | ||
605e1019 PP |
43 | struct bt_clock_snapshot_set { |
44 | /* Unique objects owned by this */ | |
45 | GPtrArray *clock_snapshots; | |
46 | ||
47 | /* Weak; points to one of the clock snapshots above */ | |
48 | struct bt_clock_snapshot *default_cs; | |
49 | }; | |
50 | ||
51 | static inline | |
52 | int bt_clock_snapshot_set_initialize(struct bt_clock_snapshot_set *cs_set) | |
53 | { | |
54 | int ret = 0; | |
55 | ||
56 | cs_set->clock_snapshots = g_ptr_array_sized_new(1); | |
57 | if (!cs_set->clock_snapshots) { | |
870631a2 | 58 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GPtrArray."); |
605e1019 PP |
59 | ret = -1; |
60 | goto end; | |
61 | } | |
62 | ||
63 | cs_set->default_cs = NULL; | |
64 | ||
65 | end: | |
66 | return ret; | |
67 | } | |
68 | ||
69 | static inline | |
70 | void bt_clock_snapshot_set_reset(struct bt_clock_snapshot_set *cs_set) | |
71 | { | |
72 | uint64_t i; | |
73 | ||
98b15851 PP |
74 | BT_ASSERT_DBG(cs_set); |
75 | BT_ASSERT_DBG(cs_set->clock_snapshots); | |
605e1019 PP |
76 | |
77 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
78 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
79 | ||
98b15851 | 80 | BT_ASSERT_DBG(cs); |
605e1019 PP |
81 | bt_clock_snapshot_reset(cs); |
82 | } | |
83 | ||
84 | cs_set->default_cs = NULL; | |
85 | } | |
86 | ||
87 | static inline | |
88 | void bt_clock_snapshot_set_finalize(struct bt_clock_snapshot_set *cs_set) | |
89 | { | |
90 | uint64_t i; | |
91 | ||
92 | BT_ASSERT(cs_set); | |
93 | ||
94 | if (cs_set->clock_snapshots) { | |
95 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
96 | struct bt_clock_snapshot *cs = | |
97 | cs_set->clock_snapshots->pdata[i]; | |
98 | ||
99 | BT_ASSERT(cs); | |
100 | bt_clock_snapshot_recycle(cs); | |
101 | } | |
102 | ||
103 | g_ptr_array_free(cs_set->clock_snapshots, TRUE); | |
104 | } | |
105 | ||
106 | cs_set->default_cs = NULL; | |
107 | } | |
108 | ||
109 | static inline | |
110 | int bt_clock_snapshot_set_set_clock_snapshot(struct bt_clock_snapshot_set *cs_set, | |
111 | struct bt_clock_class *cc, uint64_t raw_value) | |
112 | { | |
113 | int ret = 0; | |
114 | struct bt_clock_snapshot *clock_snapshot = NULL; | |
115 | uint64_t i; | |
116 | ||
98b15851 PP |
117 | BT_ASSERT_DBG(cs_set); |
118 | BT_ASSERT_DBG(cc); | |
605e1019 PP |
119 | |
120 | /* | |
121 | * Check if we already have a value for this clock class. | |
122 | * | |
123 | * TODO: When we have many clock classes, make this more | |
124 | * efficient. | |
125 | */ | |
126 | for (i = 0; i < cs_set->clock_snapshots->len; i++) { | |
127 | struct bt_clock_snapshot *cs = cs_set->clock_snapshots->pdata[i]; | |
128 | ||
98b15851 | 129 | BT_ASSERT_DBG(cs); |
605e1019 PP |
130 | |
131 | if (cs->clock_class == cc) { | |
132 | clock_snapshot = cs; | |
133 | break; | |
134 | } | |
135 | } | |
136 | ||
137 | if (!clock_snapshot) { | |
138 | clock_snapshot = bt_clock_snapshot_create(cc); | |
139 | if (!clock_snapshot) { | |
870631a2 PP |
140 | BT_LIB_LOGE_APPEND_CAUSE( |
141 | "Cannot create a clock snapshot from a clock class: " | |
605e1019 | 142 | "%![cc-]+K", cc); |
605e1019 PP |
143 | ret = -1; |
144 | goto end; | |
145 | } | |
146 | ||
147 | g_ptr_array_add(cs_set->clock_snapshots, clock_snapshot); | |
148 | } | |
149 | ||
f0ebeace | 150 | bt_clock_snapshot_set_raw_value(clock_snapshot, raw_value); |
605e1019 PP |
151 | |
152 | end: | |
153 | return ret; | |
154 | } | |
155 | ||
156 | static inline | |
157 | void bt_clock_snapshot_set_set_default_clock_snapshot( | |
158 | struct bt_clock_snapshot_set *cs_set, uint64_t raw_value) | |
159 | { | |
98b15851 PP |
160 | BT_ASSERT_DBG(cs_set); |
161 | BT_ASSERT_DBG(cs_set->default_cs); | |
f0ebeace | 162 | bt_clock_snapshot_set_raw_value(cs_set->default_cs, raw_value); |
605e1019 PP |
163 | } |
164 | ||
165 | #endif /* BABELTRACE_GRAPH_CLOCK_SNAPSHOT_SET_H */ |