X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=lib%2Fgraph%2Fnotification%2Fevent.c;h=3321af220abee0717ffedb50b8f83e5446d905e9;hb=c8db321922f3730c70628a5e452baf8a046e70fd;hp=a9fbf48bd56a6c9c8183f3380e0d100db92fe05a;hpb=c5504f7322f93b32b4bcc65a950ed61efadd694a;p=babeltrace.git diff --git a/lib/graph/notification/event.c b/lib/graph/notification/event.c index a9fbf48b..3321af22 100644 --- a/lib/graph/notification/event.c +++ b/lib/graph/notification/event.c @@ -1,10 +1,7 @@ /* - * Babeltrace Plug-in Event Notification - * + * Copyright 2017-2018 Philippe Proulx * Copyright 2016 Jérémie Galarneau * - * Author: Jérémie Galarneau - * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -24,139 +21,181 @@ * SOFTWARE. */ +#define BT_LOG_TAG "NOTIF-EVENT" +#include + +#include +#include #include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include +#include +#include +#include -static -void bt_notification_event_destroy(struct bt_object *obj) +BT_ASSERT_PRE_FUNC +static inline bool event_class_has_trace(struct bt_event_class *event_class) { - struct bt_notification_event *notification = - (struct bt_notification_event *) obj; + struct bt_stream_class *stream_class; - BT_PUT(notification->event); - BT_PUT(notification->cc_prio_map); - g_free(notification); + stream_class = bt_event_class_borrow_stream_class(event_class); + BT_ASSERT(stream_class); + return bt_stream_class_borrow_trace_class(stream_class) != NULL; } -static -bool validate_clock_classes(struct bt_notification_event *notif) +BT_HIDDEN +struct bt_notification *bt_notification_event_new( + struct bt_graph *graph) { - /* - * For each clock class found in the event's trace, get the - * event's clock value for this clock class, and if it exists, - * make sure that this clock class has a priority in the - * notification's clock class priority map. - */ - bool is_valid = true; - int ret; - int count; - size_t i; - struct bt_ctf_event_class *event_class = NULL; - struct bt_ctf_stream_class *stream_class = NULL; - struct bt_ctf_trace *trace = NULL; - uint64_t prio; - - event_class = bt_ctf_event_get_class(notif->event); - assert(event_class); - stream_class = bt_ctf_event_class_get_stream_class(event_class); - assert(stream_class); - trace = bt_ctf_stream_class_get_trace(stream_class); - assert(trace); - count = bt_ctf_trace_get_clock_class_count(trace); - assert(count >= 0); - - for (i = 0; i < count; i++) { - struct bt_ctf_clock_class *clock_class = - bt_ctf_trace_get_clock_class(trace, i); - - assert(clock_class); - ret = bt_clock_class_priority_map_get_clock_class_priority( - notif->cc_prio_map, clock_class, &prio); - bt_put(clock_class); - if (ret) { - is_valid = false; - goto end; - } + struct bt_notification_event *notification = NULL; + + notification = g_new0(struct bt_notification_event, 1); + if (!notification) { + BT_LOGE_STR("Failed to allocate one event notification."); + goto error; } + bt_notification_init(¬ification->parent, BT_NOTIFICATION_TYPE_EVENT, + (bt_object_release_func) bt_notification_event_recycle, graph); + goto end; + +error: + BT_OBJECT_PUT_REF_AND_RESET(notification); + end: - bt_put(trace); - bt_put(stream_class); - bt_put(event_class); - return is_valid; + return (void *) notification; } -struct bt_notification *bt_notification_event_create(struct bt_ctf_event *event, - struct bt_clock_class_priority_map *cc_prio_map) +struct bt_notification *bt_notification_event_create( + struct bt_self_notification_iterator *self_notif_iter, + struct bt_event_class *event_class, + struct bt_packet *packet) { + struct bt_self_component_port_input_notification_iterator *notif_iter = + (void *) self_notif_iter; struct bt_notification_event *notification = NULL; - - if (!event || !cc_prio_map) { + struct bt_event *event; + + BT_ASSERT_PRE_NON_NULL(notif_iter, "Notification iterator"); + BT_ASSERT_PRE_NON_NULL(event_class, "Event class"); + BT_ASSERT_PRE_NON_NULL(packet, "Packet"); + BT_ASSERT_PRE(event_class_has_trace(event_class), + "Event class is not part of a trace: %!+E", event_class); + BT_LIB_LOGD("Creating event notification object: %![ec-]+E", + event_class); + event = bt_event_create(event_class, packet); + if (unlikely(!event)) { + BT_LIB_LOGE("Cannot create event from event class: " + "%![ec-]+E", event_class); goto error; } - if (!bt_ctf_event_borrow_packet(event)) { + /* + * Create notification from pool _after_ we have everything + * (in this case, a valid event object) so that we never have an + * error condition with a non-NULL notification object. + * Otherwise: + * + * * We cannot recycle the notification on error because + * bt_notification_event_recycle() expects a complete + * notification (and the event or clock class priority map + * object could be unset). + * + * * We cannot destroy the notification because we would need + * to notify the graph (pool owner) so that it removes the + * notification from its notification array. + */ + notification = (void *) bt_notification_create_from_pool( + ¬if_iter->graph->event_notif_pool, notif_iter->graph); + if (unlikely(!notification)) { + /* bt_notification_create_from_pool() logs errors */ goto error; } - notification = g_new0(struct bt_notification_event, 1); - if (!notification) { - goto error; - } - bt_notification_init(¬ification->parent, - BT_NOTIFICATION_TYPE_EVENT, - bt_notification_event_destroy); - notification->event = bt_get(event); - notification->cc_prio_map = bt_get(cc_prio_map); - if (!validate_clock_classes(notification)) { - goto error; - } + BT_ASSERT(!notification->event); + notification->event = event; + bt_packet_set_is_frozen(packet, true); + bt_event_class_freeze(event_class); + BT_LIB_LOGD("Created event notification object: " + "%![notif-]+n, %![event-]+e", notification, event); + goto end; - bt_ctf_event_freeze(notification->event); - return ¬ification->parent; error: - bt_put(notification); - return NULL; + BT_ASSERT(!notification); + bt_event_destroy(event); + +end: + return (void *) notification; } -struct bt_ctf_event *bt_notification_event_get_event( - struct bt_notification *notification) +BT_HIDDEN +void bt_notification_event_destroy(struct bt_notification *notif) { - struct bt_ctf_event *event = NULL; - struct bt_notification_event *event_notification; + struct bt_notification_event *event_notif = (void *) notif; + + BT_LIB_LOGD("Destroying event notification: %!+n", notif); - if (bt_notification_get_type(notification) != - BT_NOTIFICATION_TYPE_EVENT) { - goto end; + if (event_notif->event) { + BT_LIB_LOGD("Recycling event: %!+e", event_notif->event); + bt_event_recycle(event_notif->event); + event_notif->event = NULL; } - event_notification = container_of(notification, - struct bt_notification_event, parent); - event = bt_get(event_notification->event); -end: - return event; + + g_free(notif); } -extern struct bt_clock_class_priority_map * -bt_notification_event_get_clock_class_priority_map( - struct bt_notification *notification) +BT_HIDDEN +void bt_notification_event_recycle(struct bt_notification *notif) { - struct bt_clock_class_priority_map *cc_prio_map = NULL; - struct bt_notification_event *event_notification; + struct bt_notification_event *event_notif = (void *) notif; + struct bt_graph *graph; - if (bt_notification_get_type(notification) != - BT_NOTIFICATION_TYPE_EVENT) { - goto end; + BT_ASSERT(event_notif); + + if (unlikely(!notif->graph)) { + bt_notification_event_destroy(notif); + return; } + BT_LIB_LOGD("Recycling event notification: %![notif-]+n, %![event-]+e", + notif, event_notif->event); + bt_notification_reset(notif); + BT_ASSERT(event_notif->event); + bt_event_recycle(event_notif->event); + event_notif->event = NULL; + graph = notif->graph; + notif->graph = NULL; + bt_object_pool_recycle_object(&graph->event_notif_pool, notif); +} + +static inline +struct bt_event *borrow_event(struct bt_notification *notification) +{ + struct bt_notification_event *event_notification; + + BT_ASSERT_PRE_NON_NULL(notification, "Notification"); + BT_ASSERT_PRE_NOTIF_IS_TYPE(notification, BT_NOTIFICATION_TYPE_EVENT); event_notification = container_of(notification, struct bt_notification_event, parent); - cc_prio_map = bt_get(event_notification->cc_prio_map); -end: - return cc_prio_map; + return event_notification->event; +} + +struct bt_event *bt_notification_event_borrow_event( + struct bt_notification *notification) +{ + return borrow_event(notification); +} + +const struct bt_event *bt_notification_event_borrow_event_const( + const struct bt_notification *notification) +{ + return borrow_event((void *) notification); }