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 #include <babeltrace/graph/clock-class-priority-map.h>
26 #include <babeltrace/graph/clock-class-priority-map-internal.h>
27 #include <babeltrace/ctf-ir/clock-class.h>
28 #include <babeltrace/ctf-ir/clock-class-internal.h>
29 #include <babeltrace/babeltrace-internal.h>
30 #include <babeltrace/compiler-internal.h>
31 #include <babeltrace/ref.h>
35 void bt_clock_class_priority_map_destroy(struct bt_object
*obj
)
37 struct bt_clock_class_priority_map
*cc_prio_map
= (void *) obj
;
43 if (cc_prio_map
->entries
) {
44 g_ptr_array_free(cc_prio_map
->entries
, TRUE
);
47 if (cc_prio_map
->prios
) {
48 g_hash_table_destroy(cc_prio_map
->prios
);
54 struct bt_clock_class_priority_map
*bt_clock_class_priority_map_create()
56 struct bt_clock_class_priority_map
*cc_prio_map
= NULL
;
58 cc_prio_map
= g_new0(struct bt_clock_class_priority_map
, 1);
63 bt_object_init(cc_prio_map
, bt_clock_class_priority_map_destroy
);
64 cc_prio_map
->entries
= g_ptr_array_new_with_free_func(
65 (GDestroyNotify
) bt_put
);
66 if (!cc_prio_map
->entries
) {
70 cc_prio_map
->prios
= g_hash_table_new_full(g_direct_hash
,
71 g_direct_equal
, NULL
, (GDestroyNotify
) g_free
);
72 if (!cc_prio_map
->entries
) {
85 int bt_clock_class_priority_map_get_clock_class_count(
86 struct bt_clock_class_priority_map
*cc_prio_map
)
94 ret
= (int) cc_prio_map
->entries
->len
;
100 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class(
101 struct bt_clock_class_priority_map
*cc_prio_map
,
104 struct bt_ctf_clock_class
*clock_class
= NULL
;
106 if (!cc_prio_map
|| index
>= cc_prio_map
->entries
->len
) {
110 clock_class
= g_ptr_array_index(cc_prio_map
->entries
, index
);
117 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class_by_name(
118 struct bt_clock_class_priority_map
*cc_prio_map
,
122 struct bt_ctf_clock_class
*clock_class
= NULL
;
124 if (!cc_prio_map
|| !name
) {
128 for (i
= 0; i
< cc_prio_map
->entries
->len
; i
++) {
129 struct bt_ctf_clock_class
*cur_cc
=
130 g_ptr_array_index(cc_prio_map
->entries
, i
);
131 // FIXME when available: use bt_ctf_clock_class_get_name()
132 const char *cur_cc_name
=
133 cur_cc
->name
? cur_cc
->name
->str
: NULL
;
139 if (strcmp(cur_cc_name
, name
) == 0) {
140 clock_class
= bt_get(cur_cc
);
150 struct clock_class_prio
{
152 struct bt_ctf_clock_class
*clock_class
;
156 void current_highest_prio_gh_func(gpointer key
, gpointer value
,
159 struct clock_class_prio
*func_data
= user_data
;
160 uint64_t *prio
= value
;
162 if (*prio
<= func_data
->prio
) {
163 func_data
->prio
= *prio
;
164 func_data
->clock_class
= key
;
169 struct clock_class_prio
bt_ctf_clock_class_priority_map_current_highest_prio(
170 struct bt_clock_class_priority_map
*cc_prio_map
)
172 struct clock_class_prio func_data
= {
177 g_hash_table_foreach(cc_prio_map
->prios
, current_highest_prio_gh_func
,
182 struct bt_ctf_clock_class
*
183 bt_clock_class_priority_map_get_highest_priority_clock_class(
184 struct bt_clock_class_priority_map
*cc_prio_map
)
186 struct bt_ctf_clock_class
*clock_class
= NULL
;
192 clock_class
= bt_get(cc_prio_map
->highest_prio_cc
);
198 int bt_clock_class_priority_map_get_clock_class_priority(
199 struct bt_clock_class_priority_map
*cc_prio_map
,
200 struct bt_ctf_clock_class
*clock_class
, uint64_t *priority
)
205 if (!cc_prio_map
|| !clock_class
|| !priority
) {
210 prio
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
222 int bt_clock_class_priority_map_add_clock_class(
223 struct bt_clock_class_priority_map
*cc_prio_map
,
224 struct bt_ctf_clock_class
*clock_class
, uint64_t priority
)
227 uint64_t *prio_ptr
= NULL
;
228 struct clock_class_prio cc_prio
;
230 // FIXME when available: check
231 // bt_ctf_clock_class_is_valid(clock_class)
237 /* Check for duplicate clock classes */
238 prio_ptr
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
245 prio_ptr
= g_new(uint64_t, 1);
251 *prio_ptr
= priority
;
253 g_ptr_array_add(cc_prio_map
->entries
, clock_class
);
254 g_hash_table_insert(cc_prio_map
->prios
, clock_class
, prio_ptr
);
256 cc_prio
= bt_ctf_clock_class_priority_map_current_highest_prio(
258 assert(cc_prio
.clock_class
);
259 cc_prio_map
->highest_prio_cc
= cc_prio
.clock_class
;