2 * clock-class-priority-map.c
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #define BT_LOG_TAG "CC-PRIO-MAP"
26 #include <babeltrace/lib-logging-internal.h>
28 #include <babeltrace/graph/clock-class-priority-map.h>
29 #include <babeltrace/graph/clock-class-priority-map-internal.h>
30 #include <babeltrace/ctf-ir/clock-class.h>
31 #include <babeltrace/ctf-ir/clock-class-internal.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/compiler-internal.h>
34 #include <babeltrace/ref.h>
40 void bt_clock_class_priority_map_destroy(struct bt_object
*obj
)
42 struct bt_clock_class_priority_map
*cc_prio_map
= (void *) obj
;
48 BT_LOGD("Destroying component class priority map object: addr=%p",
51 if (cc_prio_map
->entries
) {
52 BT_LOGD("Putting clock classes.");
53 g_ptr_array_free(cc_prio_map
->entries
, TRUE
);
56 if (cc_prio_map
->prios
) {
57 g_hash_table_destroy(cc_prio_map
->prios
);
63 struct bt_clock_class_priority_map
*bt_clock_class_priority_map_create()
65 struct bt_clock_class_priority_map
*cc_prio_map
= NULL
;
67 BT_LOGD_STR("Creating clock class priority map object.");
69 cc_prio_map
= g_new0(struct bt_clock_class_priority_map
, 1);
71 BT_LOGE_STR("Failed to allocate one clock class priority map.");
75 bt_object_init(cc_prio_map
, bt_clock_class_priority_map_destroy
);
76 cc_prio_map
->entries
= g_ptr_array_new_with_free_func(
77 (GDestroyNotify
) bt_put
);
78 if (!cc_prio_map
->entries
) {
79 BT_LOGE_STR("Failed to allocate a GPtrArray.");
83 cc_prio_map
->prios
= g_hash_table_new_full(g_direct_hash
,
84 g_direct_equal
, NULL
, (GDestroyNotify
) g_free
);
85 if (!cc_prio_map
->entries
) {
86 BT_LOGE_STR("Failed to allocate a GHashTable.");
90 BT_LOGD("Created clock class priority map object: addr=%p",
101 int64_t bt_clock_class_priority_map_get_clock_class_count(
102 struct bt_clock_class_priority_map
*cc_prio_map
)
104 int64_t ret
= (int64_t) -1;
107 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
111 ret
= (int64_t) cc_prio_map
->entries
->len
;
117 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class_by_index(
118 struct bt_clock_class_priority_map
*cc_prio_map
,
121 struct bt_ctf_clock_class
*clock_class
= NULL
;
124 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
128 if (index
>= cc_prio_map
->entries
->len
) {
129 BT_LOGW("Invalid parameter: index is out of bounds: "
130 "addr=%p, index=%" PRIu64
", count=%u",
131 cc_prio_map
, index
, cc_prio_map
->entries
->len
);
135 clock_class
= g_ptr_array_index(cc_prio_map
->entries
, index
);
142 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class_by_name(
143 struct bt_clock_class_priority_map
*cc_prio_map
,
147 struct bt_ctf_clock_class
*clock_class
= NULL
;
150 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
155 BT_LOGW_STR("Invalid parameter: name is NULL.");
159 for (i
= 0; i
< cc_prio_map
->entries
->len
; i
++) {
160 struct bt_ctf_clock_class
*cur_cc
=
161 g_ptr_array_index(cc_prio_map
->entries
, i
);
162 const char *cur_cc_name
=
163 bt_ctf_clock_class_get_name(cur_cc
);
167 if (strcmp(cur_cc_name
, name
) == 0) {
168 clock_class
= bt_get(cur_cc
);
178 struct clock_class_prio
{
180 struct bt_ctf_clock_class
*clock_class
;
184 void current_highest_prio_gh_func(gpointer key
, gpointer value
,
187 struct clock_class_prio
*func_data
= user_data
;
188 uint64_t *prio
= value
;
190 if (*prio
<= func_data
->prio
) {
191 func_data
->prio
= *prio
;
192 func_data
->clock_class
= key
;
197 struct clock_class_prio
bt_ctf_clock_class_priority_map_current_highest_prio(
198 struct bt_clock_class_priority_map
*cc_prio_map
)
200 struct clock_class_prio func_data
= {
205 g_hash_table_foreach(cc_prio_map
->prios
, current_highest_prio_gh_func
,
210 struct bt_ctf_clock_class
*
211 bt_clock_class_priority_map_get_highest_priority_clock_class(
212 struct bt_clock_class_priority_map
*cc_prio_map
)
214 struct bt_ctf_clock_class
*clock_class
= NULL
;
217 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
221 clock_class
= bt_get(cc_prio_map
->highest_prio_cc
);
227 int bt_clock_class_priority_map_get_clock_class_priority(
228 struct bt_clock_class_priority_map
*cc_prio_map
,
229 struct bt_ctf_clock_class
*clock_class
, uint64_t *priority
)
235 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
241 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
247 BT_LOGW_STR("Invalid parameter: priority is NULL.");
252 prio
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
254 BT_LOGV("Clock class does not exist in clock class priority map: "
255 "cc-prio-map-addr=%p, clock-class-addr=%p, "
256 "clock-class-name=\"%s\"",
257 cc_prio_map
, clock_class
,
258 bt_ctf_clock_class_get_name(clock_class
));
269 int bt_clock_class_priority_map_add_clock_class(
270 struct bt_clock_class_priority_map
*cc_prio_map
,
271 struct bt_ctf_clock_class
*clock_class
, uint64_t priority
)
274 uint64_t *prio_ptr
= NULL
;
275 struct clock_class_prio cc_prio
;
277 // FIXME when available: check
278 // bt_ctf_clock_class_is_valid(clock_class)
280 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
286 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
291 if (cc_prio_map
->frozen
) {
292 BT_LOGW("Invalid parameter: clock class priority map is frozen: "
293 "addr=%p", cc_prio_map
);
298 /* Check for existing clock class */
299 prio_ptr
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
301 *prio_ptr
= priority
;
303 goto set_highest_prio
;
306 prio_ptr
= g_new(uint64_t, 1);
308 BT_LOGE_STR("Failed to allocate a uint64_t.");
313 *prio_ptr
= priority
;
315 g_ptr_array_add(cc_prio_map
->entries
, clock_class
);
316 g_hash_table_insert(cc_prio_map
->prios
, clock_class
, prio_ptr
);
320 cc_prio
= bt_ctf_clock_class_priority_map_current_highest_prio(
322 assert(cc_prio
.clock_class
);
323 cc_prio_map
->highest_prio_cc
= cc_prio
.clock_class
;
324 BT_LOGV("Added clock class to clock class priority map: "
325 "cc-prio-map-addr=%p, added-clock-class-addr=%p, "
326 "added-clock-class-name=\"%s\", "
327 "highest-prio-clock-class-addr=%p, "
328 "highest-prio-clock-class-name=\"%s\"",
329 cc_prio_map
, clock_class
,
330 bt_ctf_clock_class_get_name(clock_class
),
332 bt_ctf_clock_class_get_name(cc_prio
.clock_class
));
342 struct bt_clock_class_priority_map
*bt_clock_class_priority_map_copy(
343 struct bt_clock_class_priority_map
*orig_cc_prio_map
)
345 struct bt_clock_class_priority_map
*cc_prio_map
;
348 cc_prio_map
= bt_clock_class_priority_map_create();
350 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
354 for (i
= 0; i
< orig_cc_prio_map
->entries
->len
; i
++) {
355 struct bt_ctf_clock_class
*clock_class
=
356 g_ptr_array_index(orig_cc_prio_map
->entries
, i
);
357 uint64_t *prio
= g_hash_table_lookup(orig_cc_prio_map
->prios
,
359 int ret
= bt_clock_class_priority_map_add_clock_class(
360 cc_prio_map
, clock_class
, *prio
);
363 BT_LOGE("Cannot add clock class to clock class priority map copy: "
364 "cc-prio-map-copy-addr=%p, clock-class-addr=%p, "
365 "clock-class-name=\"%s\"",
366 cc_prio_map
, clock_class
,
367 bt_ctf_clock_class_get_name(clock_class
));
372 cc_prio_map
->highest_prio_cc
= orig_cc_prio_map
->highest_prio_cc
;
373 BT_LOGD("Copied clock class priority map: "
374 "original-addr=%p, copy-addr=%p",
375 orig_cc_prio_map
, cc_prio_map
);