From b70d57a123c4e4a208b988539a7eb8fceb2b63a7 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 20 Jul 2019 18:20:05 -0400 Subject: [PATCH] lib: add interrupter API This patch adds an interrupter API to the library. An interrupter is a very simple shared object which you can set and reset. On the const side, you can get whether or not the interrupter is set. The goal of this object is to be used as an async-signal-safe way to interrupt a graph or a query executor. An interrupter object can eventually be shared by many message iterators, interrupting all of them at once when setting the interrupter. Moreover, a single message iterator can have many interrupters, making it possible to interrupt one or more specific message iterators from different interruption sources (signal, graphical user interface input, thread-specific, etc.). The user code is still responsible for checking the message iterator's or sink component's interrupter's state at regular interval when performing "long" operations. You can create an interrupter object with bt_interrupter_create(). The interrupter object is not set at creation time. You can set it with bt_interrupter_set() and reset it with bt_interrupter_reset(). You can get whether or not the interrupter is set with bt_interrupter_is_set(). This patch only adds the API: interrupter objects are not used anywhere yet. Signed-off-by: Philippe Proulx Change-Id: I610d48b70412d752b34b8d5760adf99a68d20704 Reviewed-on: https://review.lttng.org/c/babeltrace/+/1733 --- CONTRIBUTING.adoc | 4 + include/Makefile.am | 2 + include/babeltrace2/babeltrace.h | 2 + include/babeltrace2/graph/interrupter-const.h | 59 +++++++++++++ include/babeltrace2/graph/interrupter.h | 46 ++++++++++ include/babeltrace2/types.h | 3 +- src/lib/graph/Makefile.am | 2 + src/lib/graph/interrupter.c | 84 +++++++++++++++++++ src/lib/graph/interrupter.h | 36 ++++++++ src/lib/lib-logging.c | 10 +++ 10 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 include/babeltrace2/graph/interrupter-const.h create mode 100644 include/babeltrace2/graph/interrupter.h create mode 100644 src/lib/graph/interrupter.c create mode 100644 src/lib/graph/interrupter.h diff --git a/CONTRIBUTING.adoc b/CONTRIBUTING.adoc index 81e964f0..46984b34 100644 --- a/CONTRIBUTING.adoc +++ b/CONTRIBUTING.adoc @@ -713,6 +713,10 @@ The available format specifiers are: |Graph |`+const struct bt_graph *+` +|`z` +|Interrupter +|`+struct bt_interrupter *+` + |`l` |Plugin |`+const struct bt_plugin *+` diff --git a/include/Makefile.am b/include/Makefile.am index f0451220..4baed59b 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -87,6 +87,8 @@ babeltrace2graphinclude_HEADERS = \ babeltrace2/graph/connection-const.h \ babeltrace2/graph/graph-const.h \ babeltrace2/graph/graph.h \ + babeltrace2/graph/interrupter-const.h \ + babeltrace2/graph/interrupter.h \ babeltrace2/graph/message-const.h \ babeltrace2/graph/message-discarded-events-const.h \ babeltrace2/graph/message-discarded-events.h \ diff --git a/include/babeltrace2/babeltrace.h b/include/babeltrace2/babeltrace.h index 62a75bf5..1be65906 100644 --- a/include/babeltrace2/babeltrace.h +++ b/include/babeltrace2/babeltrace.h @@ -139,6 +139,8 @@ #include #include #include +#include +#include #include #include #include diff --git a/include/babeltrace2/graph/interrupter-const.h b/include/babeltrace2/graph/interrupter-const.h new file mode 100644 index 00000000..8a85dcca --- /dev/null +++ b/include/babeltrace2/graph/interrupter-const.h @@ -0,0 +1,59 @@ +#ifndef BABELTRACE2_GRAPH_INTERRUPTER_CONST_H +#define BABELTRACE2_GRAPH_INTERRUPTER_CONST_H + +/* + * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __BT_IN_BABELTRACE_H +# error "Please include instead." +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern bt_bool bt_interrupter_is_set(const bt_interrupter *interrupter); + +extern void bt_interrupter_get_ref(const bt_interrupter *interrupter); + +extern void bt_interrupter_put_ref(const bt_interrupter *interrupter); + +#define BT_INTERRUPTER_PUT_REF_AND_RESET(_var) \ + do { \ + bt_interrupter_put_ref(_var); \ + (_var) = NULL; \ + } while (0) + +#define BT_INTERRUPTER_MOVE_REF(_var_dst, _var_src) \ + do { \ + bt_interrupter_put_ref(_var_dst); \ + (_var_dst) = (_var_src); \ + (_var_src) = NULL; \ + } while (0) + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE2_GRAPH_INTERRUPTER_CONST_H */ diff --git a/include/babeltrace2/graph/interrupter.h b/include/babeltrace2/graph/interrupter.h new file mode 100644 index 00000000..95377532 --- /dev/null +++ b/include/babeltrace2/graph/interrupter.h @@ -0,0 +1,46 @@ +#ifndef BABELTRACE2_GRAPH_INTERRUPTER_H +#define BABELTRACE2_GRAPH_INTERRUPTER_H + +/* + * Copyright (c) 2010-2019 EfficiOS Inc. and Linux Foundation + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef __BT_IN_BABELTRACE_H +# error "Please include instead." +#endif + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +extern bt_interrupter *bt_interrupter_create(void); + +extern void bt_interrupter_set(bt_interrupter *interrupter); + +extern void bt_interrupter_reset(bt_interrupter *interrupter); + +#ifdef __cplusplus +} +#endif + +#endif /* BABELTRACE2_GRAPH_INTERRUPTER_H */ diff --git a/include/babeltrace2/types.h b/include/babeltrace2/types.h index afdbd454..5b49f50c 100644 --- a/include/babeltrace2/types.h +++ b/include/babeltrace2/types.h @@ -100,8 +100,8 @@ typedef struct bt_field bt_field; typedef struct bt_field_class bt_field_class; typedef struct bt_field_class_enumeration_mapping bt_field_class_enumeration_mapping; typedef struct bt_field_class_enumeration_signed_mapping bt_field_class_enumeration_signed_mapping; -typedef struct bt_field_class_structure_member bt_field_class_structure_member; typedef struct bt_field_class_enumeration_unsigned_mapping bt_field_class_enumeration_unsigned_mapping; +typedef struct bt_field_class_structure_member bt_field_class_structure_member; typedef struct bt_field_class_variant_option bt_field_class_variant_option; typedef struct bt_field_class_variant_with_selector_signed_option bt_field_class_variant_with_selector_signed_option; typedef struct bt_field_class_variant_with_selector_unsigned_option bt_field_class_variant_with_selector_unsigned_option; @@ -113,6 +113,7 @@ typedef struct bt_integer_range_set_signed bt_integer_range_set_signed; typedef struct bt_integer_range_set_unsigned bt_integer_range_set_unsigned; typedef struct bt_integer_range_signed bt_integer_range_signed; typedef struct bt_integer_range_unsigned bt_integer_range_unsigned; +typedef struct bt_interrupter bt_interrupter; typedef struct bt_message bt_message; typedef struct bt_message_iterator bt_message_iterator; typedef struct bt_object bt_object; diff --git a/src/lib/graph/Makefile.am b/src/lib/graph/Makefile.am index a7376fb2..a7a9c037 100644 --- a/src/lib/graph/Makefile.am +++ b/src/lib/graph/Makefile.am @@ -20,6 +20,8 @@ libgraph_la_SOURCES = \ connection.h \ graph.c \ graph.h \ + interrupter.c \ + interrupter.h \ iterator.c \ port.c \ port.h \ diff --git a/src/lib/graph/interrupter.c b/src/lib/graph/interrupter.c new file mode 100644 index 00000000..2a066ddd --- /dev/null +++ b/src/lib/graph/interrupter.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2019 Philippe Proulx + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#define BT_LOG_TAG "LIB/INTERRUPTER" +#include "lib/logging.h" + +#include +#include +#include + +#include "interrupter.h" +#include "lib/assert-pre.h" + +static +void destroy_interrupter(struct bt_object *obj) +{ + g_free(obj); +} + +extern struct bt_interrupter *bt_interrupter_create(void) +{ + struct bt_interrupter *intr = g_new0(struct bt_interrupter, 1); + + if (!intr) { + BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one interrupter."); + goto error; + } + + bt_object_init_shared(&intr->base, destroy_interrupter); + goto end; + +error: + BT_OBJECT_PUT_REF_AND_RESET(intr); + +end: + return intr; +} + +void bt_interrupter_set(struct bt_interrupter *intr) +{ + BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); + intr->is_set = true; +} + +void bt_interrupter_reset(struct bt_interrupter *intr) +{ + BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); + intr->is_set = false; +} + +bt_bool bt_interrupter_is_set(const struct bt_interrupter *intr) +{ + BT_ASSERT_PRE_NON_NULL(intr, "Interrupter"); + return (bt_bool) intr->is_set; +} + +void bt_interrupter_get_ref(const struct bt_interrupter *intr) +{ + bt_object_get_ref(intr); +} + +void bt_interrupter_put_ref(const struct bt_interrupter *intr) +{ + bt_object_put_ref(intr); +} diff --git a/src/lib/graph/interrupter.h b/src/lib/graph/interrupter.h new file mode 100644 index 00000000..46fd153b --- /dev/null +++ b/src/lib/graph/interrupter.h @@ -0,0 +1,36 @@ +#ifndef BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H +#define BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H + +/* + * Copyright (c) 2019 Philippe Proulx + * + * 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 + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include + +#include "lib/object.h" + +struct bt_interrupter { + struct bt_object base; + bool is_set; +}; + +#endif /* BABELTRACE_GRAPH_INTERRUPTER_INTERNAL_H */ diff --git a/src/lib/lib-logging.c b/src/lib/lib-logging.c index 33f9d2f5..91db4a31 100644 --- a/src/lib/lib-logging.c +++ b/src/lib/lib-logging.c @@ -41,6 +41,7 @@ #include "value.h" #include "integer-range-set.h" #include "object-pool.h" +#include "graph/interrupter.h" #include "graph/component-class.h" #include "graph/component-class-sink-colander.h" #include "graph/component-filter.h" @@ -807,6 +808,12 @@ static inline void format_clock_snapshot(char **buf_ch, bool extended, } } +static inline void format_interrupter(char **buf_ch, bool extended, + const char *prefix, const struct bt_interrupter *intr) +{ + BUF_APPEND(", %sis-set=%d", PRFIELD(intr->is_set)); +} + static inline void format_value(char **buf_ch, bool extended, const char *prefix, const struct bt_value *value) { @@ -1446,6 +1453,9 @@ static inline void handle_conversion_specifier_bt(void *priv_data, case 'g': format_graph(buf_ch, extended, prefix, obj); break; + case 'z': + format_interrupter(buf_ch, extended, prefix, obj); + break; case 'o': format_object_pool(buf_ch, extended, prefix, obj); break; -- 2.34.1