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>
36 void bt_clock_class_priority_map_destroy(struct bt_object
*obj
)
38 struct bt_clock_class_priority_map
*cc_prio_map
= (void *) obj
;
44 if (cc_prio_map
->entries
) {
45 g_ptr_array_free(cc_prio_map
->entries
, TRUE
);
48 if (cc_prio_map
->prios
) {
49 g_hash_table_destroy(cc_prio_map
->prios
);
55 struct bt_clock_class_priority_map
*bt_clock_class_priority_map_create()
57 struct bt_clock_class_priority_map
*cc_prio_map
= NULL
;
59 cc_prio_map
= g_new0(struct bt_clock_class_priority_map
, 1);
64 bt_object_init(cc_prio_map
, bt_clock_class_priority_map_destroy
);
65 cc_prio_map
->entries
= g_ptr_array_new_with_free_func(
66 (GDestroyNotify
) bt_put
);
67 if (!cc_prio_map
->entries
) {
71 cc_prio_map
->prios
= g_hash_table_new_full(g_direct_hash
,
72 g_direct_equal
, NULL
, (GDestroyNotify
) g_free
);
73 if (!cc_prio_map
->entries
) {
86 int64_t bt_clock_class_priority_map_get_clock_class_count(
87 struct bt_clock_class_priority_map
*cc_prio_map
)
89 int64_t ret
= (int64_t) -1;
95 ret
= (int64_t) cc_prio_map
->entries
->len
;
101 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class_by_index(
102 struct bt_clock_class_priority_map
*cc_prio_map
,
105 struct bt_ctf_clock_class
*clock_class
= NULL
;
107 if (!cc_prio_map
|| index
>= cc_prio_map
->entries
->len
) {
111 clock_class
= g_ptr_array_index(cc_prio_map
->entries
, index
);
118 struct bt_ctf_clock_class
*bt_clock_class_priority_map_get_clock_class_by_name(
119 struct bt_clock_class_priority_map
*cc_prio_map
,
123 struct bt_ctf_clock_class
*clock_class
= NULL
;
125 if (!cc_prio_map
|| !name
) {
129 for (i
= 0; i
< cc_prio_map
->entries
->len
; i
++) {
130 struct bt_ctf_clock_class
*cur_cc
=
131 g_ptr_array_index(cc_prio_map
->entries
, i
);
132 // FIXME when available: use bt_ctf_clock_class_get_name()
133 const char *cur_cc_name
=
134 cur_cc
->name
? cur_cc
->name
->str
: NULL
;
140 if (strcmp(cur_cc_name
, name
) == 0) {
141 clock_class
= bt_get(cur_cc
);
151 struct clock_class_prio
{
153 struct bt_ctf_clock_class
*clock_class
;
157 void current_highest_prio_gh_func(gpointer key
, gpointer value
,
160 struct clock_class_prio
*func_data
= user_data
;
161 uint64_t *prio
= value
;
163 if (*prio
<= func_data
->prio
) {
164 func_data
->prio
= *prio
;
165 func_data
->clock_class
= key
;
170 struct clock_class_prio
bt_ctf_clock_class_priority_map_current_highest_prio(
171 struct bt_clock_class_priority_map
*cc_prio_map
)
173 struct clock_class_prio func_data
= {
178 g_hash_table_foreach(cc_prio_map
->prios
, current_highest_prio_gh_func
,
183 struct bt_ctf_clock_class
*
184 bt_clock_class_priority_map_get_highest_priority_clock_class(
185 struct bt_clock_class_priority_map
*cc_prio_map
)
187 struct bt_ctf_clock_class
*clock_class
= NULL
;
193 clock_class
= bt_get(cc_prio_map
->highest_prio_cc
);
199 int bt_clock_class_priority_map_get_clock_class_priority(
200 struct bt_clock_class_priority_map
*cc_prio_map
,
201 struct bt_ctf_clock_class
*clock_class
, uint64_t *priority
)
206 if (!cc_prio_map
|| !clock_class
|| !priority
) {
211 prio
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
223 int bt_clock_class_priority_map_add_clock_class(
224 struct bt_clock_class_priority_map
*cc_prio_map
,
225 struct bt_ctf_clock_class
*clock_class
, uint64_t priority
)
228 uint64_t *prio_ptr
= NULL
;
229 struct clock_class_prio cc_prio
;
231 // FIXME when available: check
232 // bt_ctf_clock_class_is_valid(clock_class)
233 if (!cc_prio_map
|| !clock_class
|| cc_prio_map
->frozen
) {
238 /* Check for existing clock class */
239 prio_ptr
= g_hash_table_lookup(cc_prio_map
->prios
, clock_class
);
241 *prio_ptr
= priority
;
243 goto set_highest_prio
;
246 prio_ptr
= g_new(uint64_t, 1);
252 *prio_ptr
= priority
;
254 g_ptr_array_add(cc_prio_map
->entries
, clock_class
);
255 g_hash_table_insert(cc_prio_map
->prios
, clock_class
, prio_ptr
);
259 cc_prio
= bt_ctf_clock_class_priority_map_current_highest_prio(
261 assert(cc_prio
.clock_class
);
262 cc_prio_map
->highest_prio_cc
= cc_prio
.clock_class
;
272 struct bt_clock_class_priority_map
*bt_clock_class_priority_map_copy(
273 struct bt_clock_class_priority_map
*orig_cc_prio_map
)
275 struct bt_clock_class_priority_map
*cc_prio_map
;
278 cc_prio_map
= bt_clock_class_priority_map_create();
283 for (i
= 0; i
< orig_cc_prio_map
->entries
->len
; i
++) {
284 struct bt_ctf_clock_class
*clock_class
=
285 g_ptr_array_index(orig_cc_prio_map
->entries
, i
);
286 uint64_t *prio
= g_hash_table_lookup(orig_cc_prio_map
->prios
,
288 int ret
= bt_clock_class_priority_map_add_clock_class(
289 cc_prio_map
, clock_class
, *prio
);
296 cc_prio_map
->highest_prio_cc
= orig_cc_prio_map
->highest_prio_cc
;