bt_notification_event_create(): create an empty CC priority map when NULL
[babeltrace.git] / lib / graph / notification / inactivity.c
CommitLineData
ece3fb0f
PP
1/*
2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
fe30b20b
PP
23#define BT_LOG_TAG "NOTIF-INACTIVITY"
24#include <babeltrace/lib-logging-internal.h>
25
ece3fb0f 26#include <babeltrace/object-internal.h>
3d9990ac 27#include <babeltrace/compiler-internal.h>
ece3fb0f
PP
28#include <babeltrace/ctf-ir/clock-class.h>
29#include <babeltrace/graph/clock-class-priority-map.h>
fe650ea4 30#include <babeltrace/graph/clock-class-priority-map-internal.h>
ece3fb0f
PP
31#include <babeltrace/graph/notification-internal.h>
32#include <babeltrace/graph/notification-inactivity-internal.h>
33
34static
35void bt_notification_inactivity_destroy(struct bt_object *obj)
36{
37 struct bt_notification_inactivity *notification =
38 (struct bt_notification_inactivity *) obj;
39
fe30b20b
PP
40 BT_LOGD("Destroying inactivity notification: addr=%p", notification);
41 BT_LOGD_STR("Putting clock class priority map.");
ece3fb0f
PP
42 bt_put(notification->cc_prio_map);
43
44 if (notification->clock_values) {
fe30b20b 45 BT_LOGD_STR("Putting clock values.");
ece3fb0f
PP
46 g_hash_table_destroy(notification->clock_values);
47 }
48
49 g_free(notification);
50}
51
52struct bt_notification *bt_notification_inactivity_create(
53 struct bt_clock_class_priority_map *cc_prio_map)
54{
55 struct bt_notification_inactivity *notification;
56 struct bt_notification *ret_notif = NULL;
57
58 if (!cc_prio_map) {
fe30b20b 59 BT_LOGW_STR("Invalid parameter: clock class priority map is NULL.");
ece3fb0f
PP
60 goto error;
61 }
62
fe30b20b
PP
63 BT_LOGD("Creating inactivity notification object: "
64 "cc-prio-map-addr=%p",
65 cc_prio_map);
ece3fb0f
PP
66 notification = g_new0(struct bt_notification_inactivity, 1);
67 if (!notification) {
fe30b20b 68 BT_LOGE_STR("Failed to allocate one inactivity notification.");
ece3fb0f
PP
69 goto error;
70 }
71 bt_notification_init(&notification->parent,
fe30b20b
PP
72 BT_NOTIFICATION_TYPE_INACTIVITY,
73 bt_notification_inactivity_destroy);
ece3fb0f
PP
74 ret_notif = &notification->parent;
75 notification->clock_values = g_hash_table_new_full(g_direct_hash,
fe30b20b 76 g_direct_equal, bt_put, bt_put);
ece3fb0f 77 if (!notification->clock_values) {
fe30b20b 78 BT_LOGE_STR("Failed to allocate a GHashTable.");
ece3fb0f
PP
79 goto error;
80 }
81
82 notification->cc_prio_map = bt_get(cc_prio_map);
fe30b20b 83 BT_LOGD_STR("Freezing inactivity notification's clock class priority map.");
fe650ea4 84 bt_clock_class_priority_map_freeze(cc_prio_map);
fe30b20b
PP
85 BT_LOGD("Created inactivity notification object: "
86 "cc-prio-map-addr=%p, notif-addr=%p",
87 cc_prio_map, ret_notif);
ece3fb0f
PP
88 goto end;
89
90error:
91 BT_PUT(ret_notif);
92
93end:
94 return ret_notif;
95}
96
97extern struct bt_clock_class_priority_map *
98bt_notification_inactivity_get_clock_class_priority_map(
99 struct bt_notification *notification)
100{
101 struct bt_clock_class_priority_map *cc_prio_map = NULL;
102 struct bt_notification_inactivity *inactivity_notification;
103
fe30b20b
PP
104 if (!notification) {
105 BT_LOGW_STR("Invalid parameter: notification is NULL.");
106 goto end;
107 }
108
ece3fb0f
PP
109 if (bt_notification_get_type(notification) !=
110 BT_NOTIFICATION_TYPE_INACTIVITY) {
fe30b20b
PP
111 BT_LOGW("Invalid parameter: notification is not an inactivity notification: "
112 "addr%p, notif-type=%s",
113 notification, bt_notification_type_string(
114 bt_notification_get_type(notification)));
ece3fb0f
PP
115 goto end;
116 }
117
118 inactivity_notification = container_of(notification,
119 struct bt_notification_inactivity, parent);
120 cc_prio_map = bt_get(inactivity_notification->cc_prio_map);
121end:
122 return cc_prio_map;
123}
124
125struct bt_ctf_clock_value *bt_notification_inactivity_get_clock_value(
126 struct bt_notification *notification,
127 struct bt_ctf_clock_class *clock_class)
128{
129 struct bt_ctf_clock_value *clock_value = NULL;
130 struct bt_notification_inactivity *inactivity_notification;
131
fe30b20b
PP
132 if (!notification) {
133 BT_LOGW_STR("Invalid parameter: notification is NULL.");
134 goto end;
135 }
136
137 if (!clock_class) {
138 BT_LOGW_STR("Invalid parameter: clock class is NULL.");
ece3fb0f
PP
139 goto end;
140 }
141
142 if (bt_notification_get_type(notification) !=
143 BT_NOTIFICATION_TYPE_INACTIVITY) {
fe30b20b
PP
144 BT_LOGW("Invalid parameter: notification is not an inactivity notification: "
145 "addr%p, notif-type=%s",
146 notification, bt_notification_type_string(
147 bt_notification_get_type(notification)));
ece3fb0f
PP
148 goto end;
149 }
150
151 inactivity_notification = container_of(notification,
fe30b20b 152 struct bt_notification_inactivity, parent);
ece3fb0f
PP
153 clock_value = g_hash_table_lookup(inactivity_notification->clock_values,
154 clock_class);
155 bt_get(clock_value);
156
157end:
158 return clock_value;
159}
160
161int bt_notification_inactivity_set_clock_value(
162 struct bt_notification *notification,
163 struct bt_ctf_clock_value *clock_value)
164{
165 int ret = 0;
166 uint64_t prio;
167 struct bt_ctf_clock_class *clock_class = NULL;
168 struct bt_notification_inactivity *inactivity_notification;
169
fe30b20b
PP
170 if (!notification) {
171 BT_LOGW_STR("Invalid parameter: notification is NULL.");
172 ret = -1;
173 goto end;
174 }
175
176 if (!clock_value) {
177 BT_LOGW_STR("Invalid parameter: clock value is NULL.");
178 ret = -1;
179 goto end;
180 }
181
182 if (notification->frozen) {
183 BT_LOGW_STR("Invalid parameter: notification is frozen.");
ece3fb0f
PP
184 ret = -1;
185 goto end;
186 }
187
188 if (bt_notification_get_type(notification) !=
189 BT_NOTIFICATION_TYPE_INACTIVITY) {
fe30b20b
PP
190 BT_LOGW("Invalid parameter: notification is not an inactivity notification: "
191 "addr%p, notif-type=%s",
192 notification, bt_notification_type_string(
193 bt_notification_get_type(notification)));
7bd556d8 194 ret = -1;
ece3fb0f
PP
195 goto end;
196 }
197
198 inactivity_notification = container_of(notification,
199 struct bt_notification_inactivity, parent);
200 clock_class = bt_ctf_clock_value_get_class(clock_value);
201 ret = bt_clock_class_priority_map_get_clock_class_priority(
202 inactivity_notification->cc_prio_map, clock_class, &prio);
203 if (ret) {
fe30b20b
PP
204 BT_LOGW("Clock value's class is not mapped to a priority within the scope of the inactivity notification: "
205 "notif-addr=%p, cc-prio-map-addr=%p, "
206 "clock-class-addr=%p, clock-class-name=\"%s\", "
207 "clock-value-addr=%p",
208 inactivity_notification,
209 inactivity_notification->cc_prio_map,
210 clock_class, bt_ctf_clock_class_get_name(clock_class),
211 clock_value);
7bd556d8 212 ret = -1;
ece3fb0f
PP
213 goto end;
214 }
215
216 g_hash_table_insert(inactivity_notification->clock_values,
217 clock_class, bt_get(clock_value));
218 clock_class = NULL;
fe30b20b
PP
219 BT_LOGV("Set inactivity notification's clock value: "
220 "notif-addr=%p, cc-prio-map-addr=%p, "
221 "clock-class-addr=%p, clock-class-name=\"%s\", "
222 "clock-value-addr=%p",
223 inactivity_notification,
224 inactivity_notification->cc_prio_map,
225 clock_class, bt_ctf_clock_class_get_name(clock_class),
226 clock_value);
ece3fb0f
PP
227
228end:
229 bt_put(clock_class);
230 return ret;
231}
This page took 0.039004 seconds and 4 git commands to generate.