From 7f4d5b07cf7be895b38b69073389a4fcc318ec29 Mon Sep 17 00:00:00 2001 From: Jonathan Rajotte Date: Wed, 14 Apr 2021 23:18:25 -0400 Subject: [PATCH] Rename firing policy to rate policy MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is necessary since that now that the policy is moved to the actions it is a bit weird to use the term "firing". CLI change is done in a later patch. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: Id6940a49cd0640a8fa7145dab5b805fcfa515f40 --- .gitignore | 2 +- configure.ac | 2 +- include/Makefile.am | 4 +- include/lttng/action/action-internal.h | 10 +- include/lttng/action/notify-internal.h | 2 +- include/lttng/action/notify.h | 16 +- include/lttng/action/rate-policy-internal.h | 40 + include/lttng/action/rate-policy.h | 99 +++ include/lttng/action/rotate-session.h | 14 +- include/lttng/action/snapshot-session.h | 14 +- include/lttng/action/start-session.h | 14 +- include/lttng/action/stop-session.h | 14 +- include/lttng/lttng.h | 2 +- src/bin/lttng/commands/add_trigger.c | 56 +- src/bin/lttng/commands/list_triggers.c | 48 +- src/common/Makefile.am | 2 +- src/common/actions/action.c | 14 +- src/common/actions/notify.c | 53 +- src/common/actions/rate-policy.c | 683 ++++++++++++++++++ src/common/actions/rotate-session.c | 60 +- src/common/actions/snapshot-session.c | 95 ++- src/common/actions/start-session.c | 60 +- src/common/actions/stop-session.c | 61 +- tests/regression/Makefile.am | 2 +- tests/regression/tools/trigger/Makefile.am | 2 +- .../Makefile.am | 4 +- .../test_ust_rate_policy} | 14 +- .../tools/trigger/test_list_triggers_cli | 8 +- tests/unit/Makefile.am | 10 +- tests/unit/test_action.c | 134 ++-- tests/unit/test_firing_policy.c | 250 ------- tests/unit/test_rate_policy.c | 253 +++++++ 32 files changed, 1432 insertions(+), 610 deletions(-) create mode 100644 include/lttng/action/rate-policy-internal.h create mode 100644 include/lttng/action/rate-policy.h create mode 100644 src/common/actions/rate-policy.c rename tests/regression/tools/trigger/{firing-policy => rate-policy}/Makefile.am (81%) rename tests/regression/tools/trigger/{firing-policy/test_ust_firing_policy => rate-policy/test_ust_rate_policy} (96%) delete mode 100644 tests/unit/test_firing_policy.c create mode 100644 tests/unit/test_rate_policy.c diff --git a/.gitignore b/.gitignore index fb4c02062..4ee9dde72 100644 --- a/.gitignore +++ b/.gitignore @@ -147,7 +147,7 @@ compile_commands.json /tests/unit/test_buffer_view /tests/unit/test_kernel_probe /tests/unit/test_event_expr_to_bytecode -/tests/unit/test_firing_policy +/tests/unit/test_rate_policy /tests/unit/test_log_level_rule /tests/utils/testapp/gen-ust-nevents-str/gen-ust-nevents-str /tests/utils/testapp/userspace-probe-elf-binary/userspace-probe-elf-binary diff --git a/configure.ac b/configure.ac index 5a75c1efa..883e89f5c 100644 --- a/configure.ac +++ b/configure.ac @@ -1162,7 +1162,7 @@ AC_CONFIG_FILES([ tests/regression/tools/relayd-grouping/Makefile tests/regression/tools/clear/Makefile tests/regression/tools/trigger/Makefile - tests/regression/tools/trigger/firing-policy/Makefile + tests/regression/tools/trigger/rate-policy/Makefile tests/regression/tools/trigger/start-stop/Makefile tests/regression/tools/trigger/utils/Makefile tests/regression/ust/Makefile diff --git a/include/Makefile.am b/include/Makefile.am index a75f6c3d2..94a421b0a 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -131,7 +131,7 @@ lttngactioninclude_HEADERS= \ lttng/action/snapshot-session.h \ lttng/action/start-session.h \ lttng/action/stop-session.h \ - lttng/action/firing-policy.h + lttng/action/rate-policy.h lttngconditioninclude_HEADERS= \ lttng/condition/condition.h \ @@ -163,7 +163,7 @@ noinst_HEADERS = \ lttng/action/snapshot-session-internal.h \ lttng/action/start-session-internal.h \ lttng/action/stop-session-internal.h \ - lttng/action/firing-policy-internal.h \ + lttng/action/rate-policy-internal.h \ lttng/channel-internal.h \ lttng/condition/buffer-usage-internal.h \ lttng/condition/condition-internal.h \ diff --git a/include/lttng/action/action-internal.h b/include/lttng/action/action-internal.h index 1422a6dce..12029e42f 100644 --- a/include/lttng/action/action-internal.h +++ b/include/lttng/action/action-internal.h @@ -18,7 +18,7 @@ #include #include -struct lttng_firing_policy; +struct lttng_rate_policy; typedef bool (*action_validate_cb)(struct lttng_action *action); typedef void (*action_destroy_cb)(struct lttng_action *action); @@ -29,7 +29,7 @@ typedef bool (*action_equal_cb)(const struct lttng_action *a, typedef ssize_t (*action_create_from_payload_cb)( struct lttng_payload_view *view, struct lttng_action **action); -typedef const struct lttng_firing_policy *(*action_get_firing_policy_cb)( +typedef const struct lttng_rate_policy *(*action_get_rate_policy_cb)( const struct lttng_action *action); struct lttng_action { @@ -39,7 +39,7 @@ struct lttng_action { action_serialize_cb serialize; action_equal_cb equal; action_destroy_cb destroy; - action_get_firing_policy_cb get_firing_policy; + action_get_rate_policy_cb get_rate_policy; /* Internal use only. */ @@ -47,7 +47,7 @@ struct lttng_action { uint64_t execution_request_counter; /* * The number of time the action was actually executed. - * Action firing policy can impact on this number. + * Action rate policy can impact on this number. * */ uint64_t execution_counter; /* @@ -68,7 +68,7 @@ void lttng_action_init(struct lttng_action *action, action_serialize_cb serialize, action_equal_cb equal, action_destroy_cb destroy, - action_get_firing_policy_cb get_firing_policy); + action_get_rate_policy_cb get_rate_policy); LTTNG_HIDDEN bool lttng_action_validate(struct lttng_action *action); diff --git a/include/lttng/action/notify-internal.h b/include/lttng/action/notify-internal.h index d727e6860..d15532a04 100644 --- a/include/lttng/action/notify-internal.h +++ b/include/lttng/action/notify-internal.h @@ -13,7 +13,7 @@ struct lttng_action_notify { struct lttng_action parent; - struct lttng_firing_policy *policy; + struct lttng_rate_policy *policy; }; LTTNG_HIDDEN diff --git a/include/lttng/action/notify.h b/include/lttng/action/notify.h index b41ba0378..08574753f 100644 --- a/include/lttng/action/notify.h +++ b/include/lttng/action/notify.h @@ -9,7 +9,7 @@ #define LTTNG_ACTION_NOTIFY_H struct lttng_action; -struct lttng_firing_policy; +struct lttng_rate_policy; #ifdef __cplusplus extern "C" { @@ -23,7 +23,7 @@ extern "C" { * must have subscribed to a condition equivalent to the one paired to this * notify action in a trigger. * - * The default firing policy for a notify action is a "every 1" firing policy. + * The default rate policy for a notify action is a "every 1" rate policy. * * Returns a new action on success, NULL on failure. This action must be * destroyed using lttng_action_destroy(). @@ -31,25 +31,25 @@ extern "C" { extern struct lttng_action *lttng_action_notify_create(void); /* - * Set the firing policy of a notify action. + * Set the rate policy of a notify action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_ERROR on internal error, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_notify_set_firing_policy( +extern enum lttng_action_status lttng_action_notify_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy); + const struct lttng_rate_policy *policy); /* - * Get the firing policy of a notify action. + * Get the rate policy of a notify action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_notify_get_firing_policy( +extern enum lttng_action_status lttng_action_notify_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy); + const struct lttng_rate_policy **policy); #ifdef __cplusplus } diff --git a/include/lttng/action/rate-policy-internal.h b/include/lttng/action/rate-policy-internal.h new file mode 100644 index 000000000..03f6d9736 --- /dev/null +++ b/include/lttng/action/rate-policy-internal.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_RATE_POLICY_INTERNAL_H +#define LTTNG_RATE_POLICY_INTERNAL_H + +#include +#include +#include +#include + +LTTNG_HIDDEN +int lttng_rate_policy_serialize(struct lttng_rate_policy *rate_policy, + struct lttng_payload *buf); + +LTTNG_HIDDEN +ssize_t lttng_rate_policy_create_from_payload(struct lttng_payload_view *view, + struct lttng_rate_policy **rate_policy); + +LTTNG_HIDDEN +bool lttng_rate_policy_is_equal(const struct lttng_rate_policy *a, + const struct lttng_rate_policy *b); + +LTTNG_HIDDEN +const char *lttng_rate_policy_type_string( + enum lttng_rate_policy_type rate_policy_type); + +LTTNG_HIDDEN +struct lttng_rate_policy *lttng_rate_policy_copy( + const struct lttng_rate_policy *source); + +LTTNG_HIDDEN +bool lttng_rate_policy_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter); + +#endif /* LTTNG_RATE_POLICY */ diff --git a/include/lttng/action/rate-policy.h b/include/lttng/action/rate-policy.h new file mode 100644 index 000000000..a3a288a28 --- /dev/null +++ b/include/lttng/action/rate-policy.h @@ -0,0 +1,99 @@ +/* + * Copyright (C) 2021 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#ifndef LTTNG_RATE_POLICY_H +#define LTTNG_RATE_POLICY_H + +#include +#include + +struct lttng_rate_policy; + +#ifdef __cplusplus +extern "C" { +#endif + +enum lttng_rate_policy_status { + LTTNG_RATE_POLICY_STATUS_OK = 0, + LTTNG_RATE_POLICY_STATUS_ERROR = -1, + LTTNG_RATE_POLICY_STATUS_UNKNOWN = -2, + LTTNG_RATE_POLICY_STATUS_INVALID = -3, + LTTNG_RATE_POLICY_STATUS_UNSET = -4, + LTTNG_RATE_POLICY_STATUS_UNSUPPORTED = -5, + LTTNG_RATE_POLICY_STATUS_PERMISSION_DENIED = -6, +}; + +enum lttng_rate_policy_type { + LTTNG_RATE_POLICY_TYPE_UNKNOWN = -1, + LTTNG_RATE_POLICY_TYPE_EVERY_N = 0, + LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N = 1, +}; + +/* + * Get the type of a rate policy. + */ +extern enum lttng_rate_policy_type lttng_rate_policy_get_type( + const struct lttng_rate_policy *policy); + +/* + * Create a rate_policy of type `every n`. + * + * A `every n` rate policy will carry the execution of an action only when the + * action was ready for execution for a multiple of N. + * + * Returns a rate_policy object on success, NULL on error. + * rate_policy objects must be destroyed using the lttng_rate_policy_destroy() + * function. + */ +extern struct lttng_rate_policy *lttng_rate_policy_every_n_create( + uint64_t interval); + +/* + * Get the interval of a every N rate policy. + * + * Returns LTTNG_RATE_POLICY_STATUS_OK and a sets the interval. + * on success, LTTNG_RATE_FIRING_POLICY_STATUS_INVALID if an invalid + * parameter is passed. + */ +extern enum lttng_rate_policy_status lttng_rate_policy_every_n_get_interval( + const struct lttng_rate_policy *policy, uint64_t *interval); + +/* + * Create a rate_policy of type `once after N`. + * + * A `once after N` rate policy will carry the execution of an action only when + * the action was ready for execution at least N times and will only be carried + * one time. + * + * Returns a rate_policy object on success, NULL on error. + * rate_policy objects must be destroyed using the lttng_rate_policy_destroy() + * function. + */ +extern struct lttng_rate_policy *lttng_rate_policy_once_after_n_create( + uint64_t threshold); + +/* + * Get the threshold of a once after N rate policy. + * + * Returns LTTNG_RATE_POLICY_STATUS_OK and sets the threshold. + * on success, LTTNG_RATE_POLICY_STATUS_INVALID if an invalid + * parameter is passed. + */ +extern enum lttng_rate_policy_status +lttng_rate_policy_once_after_n_get_threshold( + const struct lttng_rate_policy *policy, uint64_t *threshold); + +/* + * Destroy (frees) a rate policy object. + */ +extern void lttng_rate_policy_destroy(struct lttng_rate_policy *policy); + +#ifdef __cplusplus +} +#endif + +#endif /* LTTNG_rate_policy_H */ diff --git a/include/lttng/action/rotate-session.h b/include/lttng/action/rotate-session.h index b05cd8426..7fbcce98c 100644 --- a/include/lttng/action/rotate-session.h +++ b/include/lttng/action/rotate-session.h @@ -9,7 +9,7 @@ #define LTTNG_ACTION_ROTATE_SESSION_H struct lttng_action; -struct lttng_firing_policy; +struct lttng_rate_policy; #ifdef __cplusplus extern "C" { @@ -42,25 +42,25 @@ extern enum lttng_action_status lttng_action_rotate_session_get_session_name( const struct lttng_action *action, const char **session_name); /* - * Set the firing policy of a rotate session action. + * Set the rate policy of a rotate session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_ERROR on internal error, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_rotate_session_set_firing_policy( +extern enum lttng_action_status lttng_action_rotate_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy); + const struct lttng_rate_policy *policy); /* - * Get the firing policy of a rotate session action. + * Get the rate policy of a rotate session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_rotate_session_get_firing_policy( +extern enum lttng_action_status lttng_action_rotate_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy); + const struct lttng_rate_policy **policy); #ifdef __cplusplus } diff --git a/include/lttng/action/snapshot-session.h b/include/lttng/action/snapshot-session.h index 71af0563a..faa9b9fab 100644 --- a/include/lttng/action/snapshot-session.h +++ b/include/lttng/action/snapshot-session.h @@ -14,7 +14,7 @@ extern "C" { struct lttng_action; struct lttng_snapshot_output; -struct lttng_firing_policy; +struct lttng_rate_policy; /* * Create a newly allocated snapshot-session action object. @@ -62,25 +62,25 @@ extern enum lttng_action_status lttng_action_snapshot_session_get_output( const struct lttng_snapshot_output **output); /* - * Set the firing policy of a snapshot session action. + * Set the rate policy of a snapshot session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_ERROR on internal error, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_snapshot_session_set_firing_policy( +extern enum lttng_action_status lttng_action_snapshot_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy); + const struct lttng_rate_policy *policy); /* - * Get the firing policy of a snapshot session action. + * Get the rate policy of a snapshot session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_snapshot_session_get_firing_policy( +extern enum lttng_action_status lttng_action_snapshot_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy); + const struct lttng_rate_policy **policy); #ifdef __cplusplus } diff --git a/include/lttng/action/start-session.h b/include/lttng/action/start-session.h index dc54219ae..c63a7d278 100644 --- a/include/lttng/action/start-session.h +++ b/include/lttng/action/start-session.h @@ -9,7 +9,7 @@ #define LTTNG_ACTION_START_SESSION_H struct lttng_action; -struct lttng_firing_policy; +struct lttng_rate_policy; #ifdef __cplusplus extern "C" { @@ -42,25 +42,25 @@ extern enum lttng_action_status lttng_action_start_session_get_session_name( const struct lttng_action *action, const char **session_name); /* - * Set the firing policy of a start session action. + * Set the rate policy of a start session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_ERROR on internal error, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_start_session_set_firing_policy( +extern enum lttng_action_status lttng_action_start_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy); + const struct lttng_rate_policy *policy); /* - * Get the firing policy of a start session action. + * Get the rate policy of a start session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_start_session_get_firing_policy( +extern enum lttng_action_status lttng_action_start_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy); + const struct lttng_rate_policy **policy); #ifdef __cplusplus } diff --git a/include/lttng/action/stop-session.h b/include/lttng/action/stop-session.h index f4f598f2d..dbe6819e6 100644 --- a/include/lttng/action/stop-session.h +++ b/include/lttng/action/stop-session.h @@ -9,7 +9,7 @@ #define LTTNG_ACTION_STOP_SESSION_H struct lttng_action; -struct lttng_firing_policy; +struct lttng_rate_policy; #ifdef __cplusplus extern "C" { @@ -42,25 +42,25 @@ extern enum lttng_action_status lttng_action_stop_session_get_session_name( const struct lttng_action *action, const char **session_name); /* - * Set the firing policy of a stop session action. + * Set the rate policy of a stop session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_ERROR on internal error, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_stop_session_set_firing_policy( +extern enum lttng_action_status lttng_action_stop_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy); + const struct lttng_rate_policy *policy); /* - * Get the firing policy of a stop session action. + * Get the rate policy of a stop session action. * * Returns LTTNG_ACTION_STATUS_OK on success, * LTTNG_ACTION_STATUS_INVALID if invalid parameters are passed. */ -extern enum lttng_action_status lttng_action_stop_session_get_firing_policy( +extern enum lttng_action_status lttng_action_stop_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy); + const struct lttng_rate_policy **policy); #ifdef __cplusplus } diff --git a/include/lttng/lttng.h b/include/lttng/lttng.h index 9d1592177..345d2b707 100644 --- a/include/lttng/lttng.h +++ b/include/lttng/lttng.h @@ -17,9 +17,9 @@ /* Include every LTTng ABI/API available. */ #include -#include #include #include +#include #include #include #include diff --git a/src/bin/lttng/commands/add_trigger.c b/src/bin/lttng/commands/add_trigger.c index 17e06445b..27a666c5d 100644 --- a/src/bin/lttng/commands/add_trigger.c +++ b/src/bin/lttng/commands/add_trigger.c @@ -1351,7 +1351,7 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv) char *error = NULL; char *fire_once_after_str = NULL; char *fire_every_str = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; state = argpar_state_create(*argc, *argv, notify_action_opt_descrs); if (!state) { @@ -1444,7 +1444,7 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv) goto error; } - policy = lttng_firing_policy_once_after_n_create(threshold); + policy = lttng_rate_policy_once_after_n_create(threshold); if (!policy) { ERR("Failed to create policy once after `%s`.", fire_once_after_str); @@ -1465,7 +1465,7 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv) goto error; } - policy = lttng_firing_policy_every_n_create(interval); + policy = lttng_rate_policy_every_n_create(interval); if (!policy) { ERR("Failed to create policy every `%s`.", fire_every_str); @@ -1481,9 +1481,9 @@ struct lttng_action *handle_action_notify(int *argc, const char ***argv) if (policy) { enum lttng_action_status status; - status = lttng_action_notify_set_firing_policy(action, policy); + status = lttng_action_notify_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to set firing policy"); + ERR("Failed to set rate policy"); goto error; } } @@ -1497,7 +1497,7 @@ error: end: free(fire_once_after_str); free(fire_every_str); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); argpar_state_destroy(state); argpar_item_destroy(item); return action; @@ -1505,7 +1505,7 @@ end: /* * Generic handler for a kind of action that takes a session name and an - * optional firing policy. + * optional rate policy. */ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, @@ -1513,9 +1513,9 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, struct lttng_action *(*create_action_cb)(void), enum lttng_action_status (*set_session_name_cb)( struct lttng_action *, const char *), - enum lttng_action_status (*set_firing_policy_cb)( + enum lttng_action_status (*set_rate_policy_cb)( struct lttng_action *, - const struct lttng_firing_policy *), + const struct lttng_rate_policy *), const char *action_name) { struct lttng_action *action = NULL; @@ -1526,18 +1526,18 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, char *fire_every_str = NULL; char *error = NULL; enum lttng_action_status action_status; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; assert(set_session_name_cb); - assert(set_firing_policy_cb); + assert(set_rate_policy_cb); - const struct argpar_opt_descr firing_policy_opt_descrs[] = { + const struct argpar_opt_descr rate_policy_opt_descrs[] = { { OPT_FIRE_ONCE_AFTER, '\0', "fire-once-after", true }, { OPT_FIRE_EVERY, '\0', "fire-every", true }, ARGPAR_OPT_DESCR_SENTINEL }; - state = argpar_state_create(*argc, *argv, firing_policy_opt_descrs); + state = argpar_state_create(*argc, *argv, rate_policy_opt_descrs); if (!state) { ERR("Failed to allocate an argpar state."); goto error; @@ -1633,7 +1633,7 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, goto error; } - policy = lttng_firing_policy_once_after_n_create(threshold); + policy = lttng_rate_policy_once_after_n_create(threshold); if (!policy) { ERR("Failed to create policy once after `%s`.", fire_once_after_str); @@ -1654,7 +1654,7 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, goto error; } - policy = lttng_firing_policy_every_n_create(interval); + policy = lttng_rate_policy_every_n_create(interval); if (!policy) { ERR("Failed to create policy every `%s`.", fire_every_str); @@ -1676,9 +1676,9 @@ static struct lttng_action *handle_action_simple_session_with_policy(int *argc, } if (policy) { - action_status = set_firing_policy_cb(action, policy); + action_status = set_rate_policy_cb(action, policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to set firing policy"); + ERR("Failed to set rate policy"); goto error; } } @@ -1690,7 +1690,7 @@ error: action = NULL; argpar_item_destroy(item); end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); free(error); argpar_state_destroy(state); return action; @@ -1703,7 +1703,7 @@ struct lttng_action *handle_action_start_session(int *argc, return handle_action_simple_session_with_policy(argc, argv, lttng_action_start_session_create, lttng_action_start_session_set_session_name, - lttng_action_start_session_set_firing_policy, "start"); + lttng_action_start_session_set_rate_policy, "start"); } static @@ -1713,7 +1713,7 @@ struct lttng_action *handle_action_stop_session(int *argc, return handle_action_simple_session_with_policy(argc, argv, lttng_action_stop_session_create, lttng_action_stop_session_set_session_name, - lttng_action_stop_session_set_firing_policy, "stop"); + lttng_action_stop_session_set_rate_policy, "stop"); } static @@ -1723,7 +1723,7 @@ struct lttng_action *handle_action_rotate_session(int *argc, return handle_action_simple_session_with_policy(argc, argv, lttng_action_rotate_session_create, lttng_action_rotate_session_set_session_name, - lttng_action_rotate_session_set_firing_policy, + lttng_action_rotate_session_set_rate_policy, "rotate"); } @@ -1758,7 +1758,7 @@ struct lttng_action *handle_action_snapshot_session(int *argc, char *fire_every_str = NULL; enum lttng_action_status action_status; struct lttng_snapshot_output *snapshot_output = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; int ret; unsigned int locations_specified = 0; @@ -1910,7 +1910,7 @@ struct lttng_action *handle_action_snapshot_session(int *argc, } } - /* Any firing policy ? */ + /* Any rate policy ? */ if (fire_once_after_str && fire_every_str) { ERR("--fire-once and --fire-every are mutually exclusive."); goto error; @@ -1931,7 +1931,7 @@ struct lttng_action *handle_action_snapshot_session(int *argc, goto error; } - policy = lttng_firing_policy_once_after_n_create(threshold); + policy = lttng_rate_policy_once_after_n_create(threshold); if (!policy) { ERR("Failed to create policy once after `%s`.", fire_once_after_str); @@ -1952,7 +1952,7 @@ struct lttng_action *handle_action_snapshot_session(int *argc, goto error; } - policy = lttng_firing_policy_every_n_create(interval); + policy = lttng_rate_policy_every_n_create(interval); if (!policy) { ERR("Failed to create policy every `%s`.", fire_every_str); @@ -2083,10 +2083,10 @@ struct lttng_action *handle_action_snapshot_session(int *argc, if (policy) { enum lttng_action_status status; - status = lttng_action_snapshot_session_set_firing_policy( + status = lttng_action_snapshot_session_set_rate_policy( action, policy); if (status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to set firing policy"); + ERR("Failed to set rate policy"); goto error; } } @@ -2105,7 +2105,7 @@ end: free(data_url_arg); free(snapshot_output); free(max_size_arg); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); argpar_state_destroy(state); argpar_item_destroy(item); return action; diff --git a/src/bin/lttng/commands/list_triggers.c b/src/bin/lttng/commands/list_triggers.c index 3321aacd3..d208f4ff6 100644 --- a/src/bin/lttng/commands/list_triggers.c +++ b/src/bin/lttng/commands/list_triggers.c @@ -454,7 +454,7 @@ void print_one_action(const struct lttng_action *action) { enum lttng_action_type action_type; enum lttng_action_status action_status; - const struct lttng_firing_policy *policy = NULL; + const struct lttng_rate_policy *policy = NULL; const char *value; action_type = lttng_action_get_type(action); @@ -464,10 +464,10 @@ void print_one_action(const struct lttng_action *action) case LTTNG_ACTION_TYPE_NOTIFY: _MSG("notify"); - action_status = lttng_action_notify_get_firing_policy( + action_status = lttng_action_notify_get_rate_policy( action, &policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to retrieve firing policy."); + ERR("Failed to retrieve rate policy."); goto end; } break; @@ -477,10 +477,10 @@ void print_one_action(const struct lttng_action *action) assert(action_status == LTTNG_ACTION_STATUS_OK); _MSG("start session `%s`", value); - action_status = lttng_action_start_session_get_firing_policy( + action_status = lttng_action_start_session_get_rate_policy( action, &policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to retrieve firing policy."); + ERR("Failed to retrieve rate policy."); goto end; } break; @@ -490,10 +490,10 @@ void print_one_action(const struct lttng_action *action) assert(action_status == LTTNG_ACTION_STATUS_OK); _MSG("stop session `%s`", value); - action_status = lttng_action_stop_session_get_firing_policy( + action_status = lttng_action_stop_session_get_rate_policy( action, &policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to retrieve firing policy."); + ERR("Failed to retrieve rate policy."); goto end; } break; @@ -503,10 +503,10 @@ void print_one_action(const struct lttng_action *action) assert(action_status == LTTNG_ACTION_STATUS_OK); _MSG("rotate session `%s`", value); - action_status = lttng_action_rotate_session_get_firing_policy( + action_status = lttng_action_rotate_session_get_rate_policy( action, &policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to retrieve firing policy."); + ERR("Failed to retrieve rate policy."); goto end; } break; @@ -563,10 +563,10 @@ void print_one_action(const struct lttng_action *action) } } - action_status = lttng_action_snapshot_session_get_firing_policy( + action_status = lttng_action_snapshot_session_get_rate_policy( action, &policy); if (action_status != LTTNG_ACTION_STATUS_OK) { - ERR("Failed to retrieve firing policy."); + ERR("Failed to retrieve rate policy."); goto end; } break; @@ -576,37 +576,37 @@ void print_one_action(const struct lttng_action *action) } if (policy) { - enum lttng_firing_policy_type policy_type; - enum lttng_firing_policy_status policy_status; + enum lttng_rate_policy_type policy_type; + enum lttng_rate_policy_status policy_status; uint64_t policy_value = 0; - policy_type = lttng_firing_policy_get_type(policy); + policy_type = lttng_rate_policy_get_type(policy); switch (policy_type) { - case LTTNG_FIRING_POLICY_TYPE_EVERY_N: - policy_status = lttng_firing_policy_every_n_get_interval( + case LTTNG_RATE_POLICY_TYPE_EVERY_N: + policy_status = lttng_rate_policy_every_n_get_interval( policy, &policy_value); - if (policy_status != LTTNG_FIRING_POLICY_STATUS_OK) { - ERR("Failed to get action firing policy interval"); + if (policy_status != LTTNG_RATE_POLICY_STATUS_OK) { + ERR("Failed to get action rate policy interval"); goto end; } if (policy_value > 1) { /* The default is 1 so print only when it is a * special case. */ - _MSG(", firing policy: after every %" PRIu64 + _MSG(", rate policy: after every %" PRIu64 " occurrences", policy_value); } break; - case LTTNG_FIRING_POLICY_TYPE_ONCE_AFTER_N: - policy_status = lttng_firing_policy_once_after_n_get_threshold( + case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: + policy_status = lttng_rate_policy_once_after_n_get_threshold( policy, &policy_value); - if (policy_status != LTTNG_FIRING_POLICY_STATUS_OK) { - ERR("Failed to get action firing policy interval"); + if (policy_status != LTTNG_RATE_POLICY_STATUS_OK) { + ERR("Failed to get action rate policy interval"); goto end; } - _MSG(", firing policy: once after %" PRIu64 + _MSG(", rate policy: once after %" PRIu64 " occurrences", policy_value); break; diff --git a/src/common/Makefile.am b/src/common/Makefile.am index f564d3835..b90b8854c 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -41,7 +41,7 @@ libcommon_la_SOURCES = \ actions/snapshot-session.c \ actions/start-session.c \ actions/stop-session.c \ - actions/firing-policy.c \ + actions/rate-policy.c \ buffer-view.h buffer-view.c \ common.h \ conditions/buffer-usage.c \ diff --git a/src/common/actions/action.c b/src/common/actions/action.c index 07eefeeb0..64864a627 100644 --- a/src/common/actions/action.c +++ b/src/common/actions/action.c @@ -8,9 +8,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -51,7 +51,7 @@ void lttng_action_init(struct lttng_action *action, action_serialize_cb serialize, action_equal_cb equal, action_destroy_cb destroy, - action_get_firing_policy_cb get_firing_policy) + action_get_rate_policy_cb get_rate_policy) { urcu_ref_init(&action->ref); action->type = type; @@ -59,7 +59,7 @@ void lttng_action_init(struct lttng_action *action, action->serialize = serialize; action->equal = equal; action->destroy = destroy; - action->get_firing_policy = get_firing_policy; + action->get_rate_policy = get_rate_policy; action->execution_request_counter = 0; action->execution_counter = 0; @@ -271,21 +271,21 @@ void lttng_action_increase_execution_failure_count(struct lttng_action *action) LTTNG_HIDDEN bool lttng_action_should_execute(const struct lttng_action *action) { - const struct lttng_firing_policy *policy = NULL; + const struct lttng_rate_policy *policy = NULL; bool execute = false; - if (action->get_firing_policy == NULL) { + if (action->get_rate_policy == NULL) { execute = true; goto end; } - policy = action->get_firing_policy(action); + policy = action->get_rate_policy(action); if (policy == NULL) { execute = true; goto end; } - execute = lttng_firing_policy_should_execute( + execute = lttng_rate_policy_should_execute( policy, action->execution_request_counter); end: return execute; diff --git a/src/common/actions/notify.c b/src/common/actions/notify.c index 3ce1b00f9..0ab88be44 100644 --- a/src/common/actions/notify.c +++ b/src/common/actions/notify.c @@ -9,8 +9,8 @@ #include #include #include -#include #include +#include #define IS_NOTIFY_ACTION(action) \ (lttng_action_get_type(action) == LTTNG_ACTION_TYPE_NOTIFY) @@ -36,7 +36,7 @@ void lttng_action_notify_destroy(struct lttng_action *action) { struct lttng_action_notify *notify_action; notify_action = action_notify_from_action(action); - lttng_firing_policy_destroy(notify_action->policy); + lttng_rate_policy_destroy(notify_action->policy); free(notify_action); } @@ -55,8 +55,8 @@ int lttng_action_notify_serialize(struct lttng_action *action, DBG("Serializing notify action"); notify_action = action_notify_from_action(action); - DBG("Serializing notify action firing policy"); - ret = lttng_firing_policy_serialize(notify_action->policy, payload); + DBG("Serializing notify action rate policy"); + ret = lttng_rate_policy_serialize(notify_action->policy, payload); end: return ret; @@ -70,12 +70,11 @@ bool lttng_action_notify_is_equal(const struct lttng_action *a, _a = action_notify_from_action_const(a); _b = action_notify_from_action_const(b); - return lttng_firing_policy_is_equal(_a->policy, _b->policy); + return lttng_rate_policy_is_equal(_a->policy, _b->policy); } -static const struct lttng_firing_policy * -lttng_action_notify_internal_get_firing_policy( - const struct lttng_action *action) +static const struct lttng_rate_policy * +lttng_action_notify_internal_get_rate_policy(const struct lttng_action *action) { const struct lttng_action_notify *_action; _action = action_notify_from_action_const(action); @@ -85,7 +84,7 @@ lttng_action_notify_internal_get_firing_policy( struct lttng_action *lttng_action_notify_create(void) { - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; struct lttng_action_notify *notify = NULL; struct lttng_action *action = NULL; @@ -95,7 +94,7 @@ struct lttng_action *lttng_action_notify_create(void) } /* Default policy. */ - policy = lttng_firing_policy_every_n_create(1); + policy = lttng_rate_policy_every_n_create(1); if (!policy) { goto end; } @@ -104,7 +103,7 @@ struct lttng_action *lttng_action_notify_create(void) lttng_action_notify_serialize, lttng_action_notify_is_equal, lttng_action_notify_destroy, - lttng_action_notify_internal_get_firing_policy); + lttng_action_notify_internal_get_rate_policy); notify->policy = policy; policy = NULL; @@ -114,7 +113,7 @@ struct lttng_action *lttng_action_notify_create(void) end: free(notify); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); return action; } @@ -124,12 +123,12 @@ ssize_t lttng_action_notify_create_from_payload( { enum lttng_action_status status; ssize_t consumed_length; - struct lttng_firing_policy *firing_policy = NULL; + struct lttng_rate_policy *rate_policy = NULL; struct lttng_action *_action = NULL; - consumed_length = lttng_firing_policy_create_from_payload( - view, &firing_policy); - if (!firing_policy) { + consumed_length = lttng_rate_policy_create_from_payload( + view, &rate_policy); + if (!rate_policy) { consumed_length = -1; goto end; } @@ -140,7 +139,7 @@ ssize_t lttng_action_notify_create_from_payload( goto end; } - status = lttng_action_notify_set_firing_policy(_action, firing_policy); + status = lttng_action_notify_set_rate_policy(_action, rate_policy); if (status != LTTNG_ACTION_STATUS_OK) { consumed_length = -1; goto end; @@ -150,25 +149,25 @@ ssize_t lttng_action_notify_create_from_payload( _action = NULL; end: - lttng_firing_policy_destroy(firing_policy); + lttng_rate_policy_destroy(rate_policy); lttng_action_destroy(_action); return consumed_length; } -enum lttng_action_status lttng_action_notify_set_firing_policy( +enum lttng_action_status lttng_action_notify_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy) + const struct lttng_rate_policy *policy) { enum lttng_action_status status; struct lttng_action_notify *notify_action; - struct lttng_firing_policy *copy = NULL; + struct lttng_rate_policy *copy = NULL; if (!action || !policy || !IS_NOTIFY_ACTION(action)) { status = LTTNG_ACTION_STATUS_INVALID; goto end; } - copy = lttng_firing_policy_copy(policy); + copy = lttng_rate_policy_copy(policy); if (!copy) { status = LTTNG_ACTION_STATUS_ERROR; goto end; @@ -176,8 +175,8 @@ enum lttng_action_status lttng_action_notify_set_firing_policy( notify_action = action_notify_from_action(action); - /* Free the previous firing policy .*/ - lttng_firing_policy_destroy(notify_action->policy); + /* Free the previous rate policy .*/ + lttng_rate_policy_destroy(notify_action->policy); /* Assign the policy. */ notify_action->policy = copy; @@ -185,13 +184,13 @@ enum lttng_action_status lttng_action_notify_set_firing_policy( copy = NULL; end: - lttng_firing_policy_destroy(copy); + lttng_rate_policy_destroy(copy); return status; } -enum lttng_action_status lttng_action_notify_get_firing_policy( +enum lttng_action_status lttng_action_notify_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy) + const struct lttng_rate_policy **policy) { enum lttng_action_status status; const struct lttng_action_notify *notify_action; diff --git a/src/common/actions/rate-policy.c b/src/common/actions/rate-policy.c new file mode 100644 index 000000000..244fb70e3 --- /dev/null +++ b/src/common/actions/rate-policy.c @@ -0,0 +1,683 @@ +/* + * Copyright (C) 2021 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define IS_EVERY_N_RATE_POLICY(policy) \ + (lttng_rate_policy_get_type(policy) == LTTNG_RATE_POLICY_TYPE_EVERY_N) + +#define IS_ONCE_AFTER_N_RATE_POLICY(policy) \ + (lttng_rate_policy_get_type(policy) == \ + LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N) + +typedef void (*rate_policy_destroy_cb)(struct lttng_rate_policy *rate_policy); +typedef int (*rate_policy_serialize_cb)(struct lttng_rate_policy *rate_policy, + struct lttng_payload *payload); +typedef bool (*rate_policy_equal_cb)(const struct lttng_rate_policy *a, + const struct lttng_rate_policy *b); +typedef ssize_t (*rate_policy_create_from_payload_cb)( + struct lttng_payload_view *view, + struct lttng_rate_policy **rate_policy); +typedef struct lttng_rate_policy *(*rate_policy_copy_cb)( + const struct lttng_rate_policy *source); + +struct lttng_rate_policy { + enum lttng_rate_policy_type type; + rate_policy_serialize_cb serialize; + rate_policy_equal_cb equal; + rate_policy_destroy_cb destroy; + rate_policy_copy_cb copy; +}; + +struct lttng_rate_policy_every_n { + struct lttng_rate_policy parent; + uint64_t interval; +}; + +struct lttng_rate_policy_once_after_n { + struct lttng_rate_policy parent; + uint64_t threshold; +}; + +struct lttng_rate_policy_comm { + /* enum lttng_rate_policy_type */ + int8_t rate_policy_type; +} LTTNG_PACKED; + +struct lttng_rate_policy_once_after_n_comm { + uint64_t threshold; +} LTTNG_PACKED; + +struct lttng_rate_policy_every_n_comm { + uint64_t interval; +} LTTNG_PACKED; + +/* Forward declaration. */ +static void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy, + enum lttng_rate_policy_type type, + rate_policy_serialize_cb serialize, + rate_policy_equal_cb equal, + rate_policy_destroy_cb destroy, + rate_policy_copy_cb copy); + +/* Forward declaration. Every n */ +static bool lttng_rate_policy_every_n_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter); + +/* Forward declaration. Once after N */ +static bool lttng_rate_policy_once_after_n_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter); + +LTTNG_HIDDEN +const char *lttng_rate_policy_type_string( + enum lttng_rate_policy_type rate_policy_type) +{ + switch (rate_policy_type) { + case LTTNG_RATE_POLICY_TYPE_EVERY_N: + return "EVERY-N"; + case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: + return "ONCE-AFTER-N"; + default: + return "???"; + } +} + +enum lttng_rate_policy_type lttng_rate_policy_get_type( + const struct lttng_rate_policy *policy) +{ + return policy ? policy->type : LTTNG_RATE_POLICY_TYPE_UNKNOWN; +} + +LTTNG_HIDDEN +void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy, + enum lttng_rate_policy_type type, + rate_policy_serialize_cb serialize, + rate_policy_equal_cb equal, + rate_policy_destroy_cb destroy, + rate_policy_copy_cb copy) +{ + rate_policy->type = type; + rate_policy->serialize = serialize; + rate_policy->equal = equal; + rate_policy->destroy = destroy; + rate_policy->copy = copy; +} + +void lttng_rate_policy_destroy(struct lttng_rate_policy *rate_policy) +{ + if (!rate_policy) { + return; + } + + rate_policy->destroy(rate_policy); +} + +LTTNG_HIDDEN +int lttng_rate_policy_serialize(struct lttng_rate_policy *rate_policy, + struct lttng_payload *payload) +{ + int ret; + struct lttng_rate_policy_comm rate_policy_comm = { + .rate_policy_type = (int8_t) rate_policy->type, + }; + + ret = lttng_dynamic_buffer_append(&payload->buffer, &rate_policy_comm, + sizeof(rate_policy_comm)); + if (ret) { + goto end; + } + + ret = rate_policy->serialize(rate_policy, payload); + if (ret) { + goto end; + } +end: + return ret; +} + +static ssize_t lttng_rate_policy_once_after_n_create_from_payload( + struct lttng_payload_view *view, + struct lttng_rate_policy **rate_policy) +{ + ssize_t consumed_len = -1; + struct lttng_rate_policy *policy = NULL; + const struct lttng_rate_policy_once_after_n_comm *comm; + const struct lttng_payload_view comm_view = + lttng_payload_view_from_view(view, 0, sizeof(*comm)); + + if (!view || !rate_policy) { + consumed_len = -1; + goto end; + } + + if (!lttng_payload_view_is_valid(&comm_view)) { + /* Payload not large enough to contain the header. */ + consumed_len = -1; + goto end; + } + + comm = (const struct lttng_rate_policy_once_after_n_comm *) + comm_view.buffer.data; + + policy = lttng_rate_policy_once_after_n_create(comm->threshold); + if (policy == NULL) { + consumed_len = -1; + goto end; + } + + *rate_policy = policy; + consumed_len = sizeof(*comm); + +end: + return consumed_len; +} + +static ssize_t lttng_rate_policy_every_n_create_from_payload( + struct lttng_payload_view *view, + struct lttng_rate_policy **rate_policy) +{ + ssize_t consumed_len = -1; + struct lttng_rate_policy *policy = NULL; + const struct lttng_rate_policy_every_n_comm *comm; + const struct lttng_payload_view comm_view = + lttng_payload_view_from_view(view, 0, sizeof(*comm)); + + if (!view || !rate_policy) { + consumed_len = -1; + goto end; + } + + if (!lttng_payload_view_is_valid(&comm_view)) { + /* Payload not large enough to contain the header. */ + consumed_len = -1; + goto end; + } + + comm = (const struct lttng_rate_policy_every_n_comm *) + comm_view.buffer.data; + + policy = lttng_rate_policy_every_n_create(comm->interval); + if (policy == NULL) { + consumed_len = -1; + goto end; + } + + *rate_policy = policy; + consumed_len = sizeof(*comm); + +end: + return consumed_len; +} + +LTTNG_HIDDEN +ssize_t lttng_rate_policy_create_from_payload(struct lttng_payload_view *view, + struct lttng_rate_policy **rate_policy) +{ + ssize_t consumed_len, specific_rate_policy_consumed_len; + rate_policy_create_from_payload_cb create_from_payload_cb; + const struct lttng_rate_policy_comm *rate_policy_comm; + const struct lttng_payload_view rate_policy_comm_view = + lttng_payload_view_from_view( + view, 0, sizeof(*rate_policy_comm)); + + if (!view || !rate_policy) { + consumed_len = -1; + goto end; + } + + if (!lttng_payload_view_is_valid(&rate_policy_comm_view)) { + /* Payload not large enough to contain the header. */ + consumed_len = -1; + goto end; + } + + rate_policy_comm = (const struct lttng_rate_policy_comm *) + rate_policy_comm_view.buffer.data; + + DBG("Create rate_policy from payload: rate-policy-type=%s", + lttng_rate_policy_type_string( + rate_policy_comm->rate_policy_type)); + + switch (rate_policy_comm->rate_policy_type) { + case LTTNG_RATE_POLICY_TYPE_EVERY_N: + create_from_payload_cb = + lttng_rate_policy_every_n_create_from_payload; + break; + case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: + create_from_payload_cb = + lttng_rate_policy_once_after_n_create_from_payload; + break; + default: + ERR("Failed to create rate-policy from payload, unhandled rate-policy type: rate-policy-type=%u (%s)", + rate_policy_comm->rate_policy_type, + lttng_rate_policy_type_string( + rate_policy_comm->rate_policy_type)); + consumed_len = -1; + goto end; + } + + { + /* Create buffer view for the rate_policy-type-specific data. + */ + struct lttng_payload_view specific_rate_policy_view = + lttng_payload_view_from_view(view, + sizeof(struct lttng_rate_policy_comm), + -1); + + specific_rate_policy_consumed_len = create_from_payload_cb( + &specific_rate_policy_view, rate_policy); + } + if (specific_rate_policy_consumed_len < 0) { + ERR("Failed to create specific rate_policy from buffer."); + consumed_len = -1; + goto end; + } + + assert(*rate_policy); + + consumed_len = sizeof(struct lttng_rate_policy_comm) + + specific_rate_policy_consumed_len; + +end: + return consumed_len; +} + +LTTNG_HIDDEN +bool lttng_rate_policy_is_equal(const struct lttng_rate_policy *a, + const struct lttng_rate_policy *b) +{ + bool is_equal = false; + + if (!a || !b) { + goto end; + } + + if (a->type != b->type) { + goto end; + } + + if (a == b) { + is_equal = true; + goto end; + } + + assert(a->equal); + is_equal = a->equal(a, b); +end: + return is_equal; +} + +LTTNG_HIDDEN +bool lttng_rate_policy_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter) +{ + switch (policy->type) { + case LTTNG_RATE_POLICY_TYPE_EVERY_N: + return lttng_rate_policy_every_n_should_execute( + policy, counter); + case LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N: + return lttng_rate_policy_once_after_n_should_execute( + policy, counter); + default: + abort(); + break; + } +} + +/* Every N */ +static struct lttng_rate_policy_every_n *rate_policy_every_n_from_rate_policy( + struct lttng_rate_policy *policy) +{ + assert(policy); + + return container_of(policy, struct lttng_rate_policy_every_n, parent); +} + +static const struct lttng_rate_policy_every_n * +rate_policy_every_n_from_rate_policy_const( + const struct lttng_rate_policy *policy) +{ + assert(policy); + + return container_of(policy, struct lttng_rate_policy_every_n, parent); +} + +static int lttng_rate_policy_every_n_serialize( + struct lttng_rate_policy *policy, struct lttng_payload *payload) +{ + int ret; + + struct lttng_rate_policy_every_n *every_n_policy; + struct lttng_rate_policy_every_n_comm comm = {}; + + assert(policy); + assert(payload); + + every_n_policy = rate_policy_every_n_from_rate_policy(policy); + comm.interval = every_n_policy->interval; + + ret = lttng_dynamic_buffer_append( + &payload->buffer, &comm, sizeof(comm)); + return ret; +} + +static bool lttng_rate_policy_every_n_is_equal( + const struct lttng_rate_policy *_a, + const struct lttng_rate_policy *_b) +{ + bool is_equal = false; + const struct lttng_rate_policy_every_n *a, *b; + + a = rate_policy_every_n_from_rate_policy_const(_a); + b = rate_policy_every_n_from_rate_policy_const(_b); + + if (a->interval != b->interval) { + goto end; + } + + is_equal = true; + +end: + return is_equal; +} + +static void lttng_rate_policy_every_n_destroy(struct lttng_rate_policy *policy) +{ + struct lttng_rate_policy_every_n *every_n_policy; + + if (!policy) { + goto end; + } + + every_n_policy = rate_policy_every_n_from_rate_policy(policy); + + free(every_n_policy); + +end: + return; +} + +static struct lttng_rate_policy *lttng_rate_policy_every_n_copy( + const struct lttng_rate_policy *source) +{ + struct lttng_rate_policy *copy = NULL; + const struct lttng_rate_policy_every_n *every_n_policy; + + if (!source) { + goto end; + } + + every_n_policy = rate_policy_every_n_from_rate_policy_const(source); + copy = lttng_rate_policy_every_n_create(every_n_policy->interval); + +end: + return copy; +} + +struct lttng_rate_policy *lttng_rate_policy_every_n_create(uint64_t interval) +{ + struct lttng_rate_policy_every_n *policy = NULL; + struct lttng_rate_policy *_policy = NULL; + + if (interval == 0) { + /* + * An interval of 0 is invalid since it would never be fired. + */ + goto end; + } + + policy = zmalloc(sizeof(struct lttng_rate_policy_every_n)); + if (!policy) { + goto end; + } + + lttng_rate_policy_init(&policy->parent, LTTNG_RATE_POLICY_TYPE_EVERY_N, + lttng_rate_policy_every_n_serialize, + lttng_rate_policy_every_n_is_equal, + lttng_rate_policy_every_n_destroy, + lttng_rate_policy_every_n_copy); + + policy->interval = interval; + + _policy = &policy->parent; + policy = NULL; + +end: + free(policy); + return _policy; +} + +LTTNG_HIDDEN +enum lttng_rate_policy_status lttng_rate_policy_every_n_get_interval( + const struct lttng_rate_policy *policy, uint64_t *interval) +{ + const struct lttng_rate_policy_every_n *every_n_policy; + enum lttng_rate_policy_status status; + + if (!policy || !IS_EVERY_N_RATE_POLICY(policy) || !interval) { + status = LTTNG_RATE_POLICY_STATUS_INVALID; + goto end; + } + + every_n_policy = rate_policy_every_n_from_rate_policy_const(policy); + *interval = every_n_policy->interval; + status = LTTNG_RATE_POLICY_STATUS_OK; +end: + + return status; +} + +static bool lttng_rate_policy_every_n_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter) +{ + const struct lttng_rate_policy_every_n *every_n_policy; + assert(policy); + bool execute = false; + + every_n_policy = rate_policy_every_n_from_rate_policy_const(policy); + + if (every_n_policy->interval == 0) { + abort(); + } + + execute = (counter % every_n_policy->interval) == 0; + + DBG("Policy every N = %" PRIu64 + ": execution %s. Execution count: %" PRIu64, + every_n_policy->interval, + execute ? "accepted" : "denied", counter); + + return execute; +} + +/* Once after N */ + +static struct lttng_rate_policy_once_after_n * +rate_policy_once_after_n_from_rate_policy(struct lttng_rate_policy *policy) +{ + assert(policy); + + return container_of( + policy, struct lttng_rate_policy_once_after_n, parent); +} + +static const struct lttng_rate_policy_once_after_n * +rate_policy_once_after_n_from_rate_policy_const( + const struct lttng_rate_policy *policy) +{ + assert(policy); + + return container_of( + policy, struct lttng_rate_policy_once_after_n, parent); +} +static int lttng_rate_policy_once_after_n_serialize( + struct lttng_rate_policy *policy, struct lttng_payload *payload) +{ + int ret; + + struct lttng_rate_policy_once_after_n *once_after_n_policy; + struct lttng_rate_policy_once_after_n_comm comm = {}; + + assert(policy); + assert(payload); + + once_after_n_policy = rate_policy_once_after_n_from_rate_policy(policy); + comm.threshold = once_after_n_policy->threshold; + + ret = lttng_dynamic_buffer_append( + &payload->buffer, &comm, sizeof(comm)); + return ret; +} + +static bool lttng_rate_policy_once_after_n_is_equal( + const struct lttng_rate_policy *_a, + const struct lttng_rate_policy *_b) +{ + bool is_equal = false; + const struct lttng_rate_policy_once_after_n *a, *b; + + a = rate_policy_once_after_n_from_rate_policy_const(_a); + b = rate_policy_once_after_n_from_rate_policy_const(_b); + + if (a->threshold != b->threshold) { + goto end; + } + + is_equal = true; + +end: + return is_equal; +} + +static void lttng_rate_policy_once_after_n_destroy( + struct lttng_rate_policy *policy) +{ + struct lttng_rate_policy_once_after_n *once_after_n_policy; + + if (!policy) { + goto end; + } + + once_after_n_policy = rate_policy_once_after_n_from_rate_policy(policy); + + free(once_after_n_policy); + +end: + return; +} + +static struct lttng_rate_policy *lttng_rate_policy_once_after_n_copy( + const struct lttng_rate_policy *source) +{ + struct lttng_rate_policy *copy = NULL; + const struct lttng_rate_policy_once_after_n *once_after_n_policy; + + if (!source) { + goto end; + } + + once_after_n_policy = + rate_policy_once_after_n_from_rate_policy_const(source); + copy = lttng_rate_policy_once_after_n_create( + once_after_n_policy->threshold); + +end: + return copy; +} + +struct lttng_rate_policy *lttng_rate_policy_once_after_n_create( + uint64_t threshold) +{ + struct lttng_rate_policy_once_after_n *policy = NULL; + struct lttng_rate_policy *_policy = NULL; + + if (threshold == 0) { + /* threshold is expected to be > 0 */ + goto end; + } + + policy = zmalloc(sizeof(struct lttng_rate_policy_once_after_n)); + if (!policy) { + goto end; + } + + lttng_rate_policy_init(&policy->parent, + LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N, + lttng_rate_policy_once_after_n_serialize, + lttng_rate_policy_once_after_n_is_equal, + lttng_rate_policy_once_after_n_destroy, + lttng_rate_policy_once_after_n_copy); + + policy->threshold = threshold; + + _policy = &policy->parent; + policy = NULL; + +end: + free(policy); + return _policy; +} + +LTTNG_HIDDEN +enum lttng_rate_policy_status lttng_rate_policy_once_after_n_get_threshold( + const struct lttng_rate_policy *policy, uint64_t *threshold) +{ + const struct lttng_rate_policy_once_after_n *once_after_n_policy; + enum lttng_rate_policy_status status; + + if (!policy || !IS_ONCE_AFTER_N_RATE_POLICY(policy) || !threshold) { + status = LTTNG_RATE_POLICY_STATUS_INVALID; + goto end; + } + + once_after_n_policy = + rate_policy_once_after_n_from_rate_policy_const(policy); + *threshold = once_after_n_policy->threshold; + status = LTTNG_RATE_POLICY_STATUS_OK; +end: + + return status; +} + +LTTNG_HIDDEN +struct lttng_rate_policy *lttng_rate_policy_copy( + const struct lttng_rate_policy *source) +{ + assert(source->copy); + return source->copy(source); +} + +static bool lttng_rate_policy_once_after_n_should_execute( + const struct lttng_rate_policy *policy, uint64_t counter) +{ + const struct lttng_rate_policy_once_after_n *once_after_n_policy; + bool execute = false; + assert(policy); + + once_after_n_policy = + rate_policy_once_after_n_from_rate_policy_const(policy); + + execute = counter == once_after_n_policy->threshold; + + DBG("Policy once after N = %" PRIu64 + ": execution %s. Execution count: %" PRIu64, + once_after_n_policy->threshold, + execute ? "accepted" : "denied", counter); + + return counter == once_after_n_policy->threshold; +} diff --git a/src/common/actions/rotate-session.c b/src/common/actions/rotate-session.c index f2ac2d04d..3d5a61709 100644 --- a/src/common/actions/rotate-session.c +++ b/src/common/actions/rotate-session.c @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -22,7 +22,7 @@ struct lttng_action_rotate_session { /* Owned by this. */ char *session_name; - struct lttng_firing_policy *policy; + struct lttng_rate_policy *policy; }; struct lttng_action_rotate_session_comm { @@ -38,8 +38,8 @@ struct lttng_action_rotate_session_comm { char data[]; } LTTNG_PACKED; -static const struct lttng_firing_policy * -lttng_action_rotate_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_rotate_session_internal_get_rate_policy( const struct lttng_action *action); static struct lttng_action_rotate_session *action_rotate_session_from_action( @@ -98,7 +98,7 @@ static bool lttng_action_rotate_session_is_equal( goto end; } - is_equal = lttng_firing_policy_is_equal(a->policy, b->policy); + is_equal = lttng_rate_policy_is_equal(a->policy, b->policy); end: return is_equal; } @@ -137,7 +137,7 @@ static int lttng_action_rotate_session_serialize( goto end; } - ret = lttng_firing_policy_serialize( + ret = lttng_rate_policy_serialize( action_rotate_session->policy, payload); if (ret) { ret = -1; @@ -157,7 +157,7 @@ static void lttng_action_rotate_session_destroy(struct lttng_action *action) action_rotate_session = action_rotate_session_from_action(action); - lttng_firing_policy_destroy(action_rotate_session->policy); + lttng_rate_policy_destroy(action_rotate_session->policy); free(action_rotate_session->session_name); free(action_rotate_session); @@ -174,7 +174,7 @@ ssize_t lttng_action_rotate_session_create_from_payload( const char *session_name; struct lttng_action *action; enum lttng_action_status status; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; action = lttng_action_rotate_session_create(); if (!action) { @@ -192,12 +192,12 @@ ssize_t lttng_action_rotate_session_create_from_payload( } consumed_len = sizeof(*comm) + comm->session_name_len; - /* Firing policy. */ + /* Rate policy. */ { struct lttng_payload_view policy_view = lttng_payload_view_from_view( view, consumed_len, -1); - ret = lttng_firing_policy_create_from_payload( + ret = lttng_rate_policy_create_from_payload( &policy_view, &policy); if (ret < 0) { consumed_len = -1; @@ -214,7 +214,7 @@ ssize_t lttng_action_rotate_session_create_from_payload( } assert(policy); - status = lttng_action_rotate_session_set_firing_policy(action, policy); + status = lttng_action_rotate_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { consumed_len = -1; goto end; @@ -224,7 +224,7 @@ ssize_t lttng_action_rotate_session_create_from_payload( action = NULL; end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); lttng_action_rotate_session_destroy(action); return consumed_len; @@ -233,11 +233,11 @@ end: struct lttng_action *lttng_action_rotate_session_create(void) { struct lttng_action *action = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; - /* Create a every N = 1 firing policy. */ - policy = lttng_firing_policy_every_n_create(1); + /* Create a every N = 1 rate policy. */ + policy = lttng_rate_policy_every_n_create(1); if (!policy) { goto end; } @@ -252,9 +252,9 @@ struct lttng_action *lttng_action_rotate_session_create(void) lttng_action_rotate_session_serialize, lttng_action_rotate_session_is_equal, lttng_action_rotate_session_destroy, - lttng_action_rotate_session_internal_get_firing_policy); + lttng_action_rotate_session_internal_get_rate_policy); - status = lttng_action_rotate_session_set_firing_policy(action, policy); + status = lttng_action_rotate_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { free(action); action = NULL; @@ -262,7 +262,7 @@ struct lttng_action *lttng_action_rotate_session_create(void) } end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); return action; } @@ -313,20 +313,20 @@ end: return status; } -enum lttng_action_status lttng_action_rotate_session_set_firing_policy( +enum lttng_action_status lttng_action_rotate_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy) + const struct lttng_rate_policy *policy) { enum lttng_action_status status; struct lttng_action_rotate_session *rotate_session_action; - struct lttng_firing_policy *copy = NULL; + struct lttng_rate_policy *copy = NULL; if (!action || !policy || !IS_ROTATE_SESSION_ACTION(action)) { status = LTTNG_ACTION_STATUS_INVALID; goto end; } - copy = lttng_firing_policy_copy(policy); + copy = lttng_rate_policy_copy(policy); if (!copy) { status = LTTNG_ACTION_STATUS_ERROR; goto end; @@ -334,8 +334,8 @@ enum lttng_action_status lttng_action_rotate_session_set_firing_policy( rotate_session_action = action_rotate_session_from_action(action); - /* Free the previous firing policy .*/ - lttng_firing_policy_destroy(rotate_session_action->policy); + /* Free the previous rate policy .*/ + lttng_rate_policy_destroy(rotate_session_action->policy); /* Assign the policy. */ rotate_session_action->policy = copy; @@ -343,13 +343,13 @@ enum lttng_action_status lttng_action_rotate_session_set_firing_policy( copy = NULL; end: - lttng_firing_policy_destroy(copy); + lttng_rate_policy_destroy(copy); return status; } -enum lttng_action_status lttng_action_rotate_session_get_firing_policy( +enum lttng_action_status lttng_action_rotate_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy) + const struct lttng_rate_policy **policy) { enum lttng_action_status status; const struct lttng_action_rotate_session *rotate_session_action; @@ -367,8 +367,8 @@ end: return status; } -static const struct lttng_firing_policy * -lttng_action_rotate_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_rotate_session_internal_get_rate_policy( const struct lttng_action *action) { const struct lttng_action_rotate_session *_action; diff --git a/src/common/actions/snapshot-session.c b/src/common/actions/snapshot-session.c index 5d0178495..239cf5e73 100644 --- a/src/common/actions/snapshot-session.c +++ b/src/common/actions/snapshot-session.c @@ -13,8 +13,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include @@ -36,14 +36,14 @@ struct lttng_action_snapshot_session { * Owned by this. */ struct lttng_snapshot_output *output; - struct lttng_firing_policy *policy; + struct lttng_rate_policy *policy; }; struct lttng_action_snapshot_session_comm { /* All string lengths include the trailing \0. */ uint32_t session_name_len; uint32_t snapshot_output_len; - uint32_t firing_policy_len; + uint32_t rate_policy_len; /* * Variable data (all strings are null-terminated): @@ -55,8 +55,8 @@ struct lttng_action_snapshot_session_comm { char data[]; } LTTNG_PACKED; -static const struct lttng_firing_policy * -lttng_action_snapshot_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_snapshot_session_internal_get_rate_policy( const struct lttng_action *action); static struct lttng_action_snapshot_session * @@ -127,7 +127,7 @@ static bool lttng_action_snapshot_session_is_equal( goto end; } - is_equal = lttng_firing_policy_is_equal(a->policy, b->policy); + is_equal = lttng_rate_policy_is_equal(a->policy, b->policy); end: return is_equal; } @@ -192,12 +192,12 @@ static int lttng_action_snapshot_session_serialize( payload->buffer.size - size_before_output; } - /* Serialize the firing policy. */ + /* Serialize the rate policy. */ { const size_t size_before_output = payload->buffer.size; struct lttng_action_snapshot_session_comm *comm_in_payload; - ret = lttng_firing_policy_serialize( + ret = lttng_rate_policy_serialize( action_snapshot_session->policy, payload); if (ret) { ret = -1; @@ -206,8 +206,8 @@ static int lttng_action_snapshot_session_serialize( comm_in_payload = (typeof(comm_in_payload))( payload->buffer.data + size_before_comm); - /* Adjust firing policy length in header. */ - comm_in_payload->firing_policy_len = + /* Adjust rate policy length in header. */ + comm_in_payload->rate_policy_len = payload->buffer.size - size_before_output; } @@ -227,7 +227,7 @@ static void lttng_action_snapshot_session_destroy(struct lttng_action *action) free(action_snapshot_session->session_name); lttng_snapshot_output_destroy(action_snapshot_session->output); - lttng_firing_policy_destroy(action_snapshot_session->policy); + lttng_rate_policy_destroy(action_snapshot_session->policy); free(action_snapshot_session); end: @@ -243,7 +243,7 @@ ssize_t lttng_action_snapshot_session_create_from_payload( struct lttng_action *action; enum lttng_action_status status; struct lttng_snapshot_output *snapshot_output = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; const struct lttng_action_snapshot_session_comm *comm; const struct lttng_payload_view snapshot_session_comm_view = lttng_payload_view_from_view( @@ -316,46 +316,46 @@ ssize_t lttng_action_snapshot_session_create_from_payload( variable_data += comm->snapshot_output_len; consumed_len += comm->snapshot_output_len; - /* Firing policy. */ - if (comm->firing_policy_len <= 0) { - ERR("Firing policy should be present."); + /* Rate policy. */ + if (comm->rate_policy_len <= 0) { + ERR("Rate policy should be present."); goto error; } { - ssize_t firing_policy_consumed_len; + ssize_t rate_policy_consumed_len; struct lttng_payload_view policy_view = lttng_payload_view_from_view(view, consumed_len, - comm->firing_policy_len); + comm->rate_policy_len); if (!lttng_payload_view_is_valid(&policy_view)) { - ERR("Failed to create buffer view for firing policy."); + ERR("Failed to create buffer view for rate policy."); goto error; } - firing_policy_consumed_len = - lttng_firing_policy_create_from_payload( + rate_policy_consumed_len = + lttng_rate_policy_create_from_payload( &policy_view, &policy); - if (firing_policy_consumed_len < 0) { + if (rate_policy_consumed_len < 0) { goto error; } - if (firing_policy_consumed_len != comm->firing_policy_len) { - ERR("Failed to deserialize firing policy object: " + if (rate_policy_consumed_len != comm->rate_policy_len) { + ERR("Failed to deserialize rate policy object: " "consumed-len: %zd, expected-len: %" PRIu32, - firing_policy_consumed_len, - comm->firing_policy_len); + rate_policy_consumed_len, + comm->rate_policy_len); goto error; } - status = lttng_action_snapshot_session_set_firing_policy( + status = lttng_action_snapshot_session_set_rate_policy( action, policy); if (status != LTTNG_ACTION_STATUS_OK) { goto error; } } - variable_data += comm->firing_policy_len; - consumed_len += comm->firing_policy_len; + variable_data += comm->rate_policy_len; + consumed_len += comm->rate_policy_len; *p_action = action; action = NULL; @@ -366,7 +366,7 @@ error: consumed_len = -1; end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); lttng_action_snapshot_session_destroy(action); lttng_snapshot_output_destroy(snapshot_output); @@ -376,11 +376,11 @@ end: struct lttng_action *lttng_action_snapshot_session_create(void) { struct lttng_action *action = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; - /* Create a every N = 1 firing policy. */ - policy = lttng_firing_policy_every_n_create(1); + /* Create a every N = 1 rate policy. */ + policy = lttng_rate_policy_every_n_create(1); if (!policy) { goto end; } @@ -395,10 +395,9 @@ struct lttng_action *lttng_action_snapshot_session_create(void) lttng_action_snapshot_session_serialize, lttng_action_snapshot_session_is_equal, lttng_action_snapshot_session_destroy, - lttng_action_snapshot_session_internal_get_firing_policy); + lttng_action_snapshot_session_internal_get_rate_policy); - status = lttng_action_snapshot_session_set_firing_policy( - action, policy); + status = lttng_action_snapshot_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { free(action); action = NULL; @@ -406,7 +405,7 @@ struct lttng_action *lttng_action_snapshot_session_create(void) } end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); return action; } @@ -510,20 +509,20 @@ end: return status; } -enum lttng_action_status lttng_action_snapshot_session_set_firing_policy( +enum lttng_action_status lttng_action_snapshot_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy) + const struct lttng_rate_policy *policy) { enum lttng_action_status status; struct lttng_action_snapshot_session *snapshot_session_action; - struct lttng_firing_policy *copy = NULL; + struct lttng_rate_policy *copy = NULL; if (!action || !policy || !IS_SNAPSHOT_SESSION_ACTION(action)) { status = LTTNG_ACTION_STATUS_INVALID; goto end; } - copy = lttng_firing_policy_copy(policy); + copy = lttng_rate_policy_copy(policy); if (!copy) { status = LTTNG_ACTION_STATUS_ERROR; goto end; @@ -531,8 +530,8 @@ enum lttng_action_status lttng_action_snapshot_session_set_firing_policy( snapshot_session_action = action_snapshot_session_from_action(action); - /* Free the previous firing policy .*/ - lttng_firing_policy_destroy(snapshot_session_action->policy); + /* Free the previous rate policy .*/ + lttng_rate_policy_destroy(snapshot_session_action->policy); /* Assign the policy. */ snapshot_session_action->policy = copy; @@ -540,13 +539,13 @@ enum lttng_action_status lttng_action_snapshot_session_set_firing_policy( copy = NULL; end: - lttng_firing_policy_destroy(copy); + lttng_rate_policy_destroy(copy); return status; } -enum lttng_action_status lttng_action_snapshot_session_get_firing_policy( +enum lttng_action_status lttng_action_snapshot_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy) + const struct lttng_rate_policy **policy) { enum lttng_action_status status; const struct lttng_action_snapshot_session *snapshot_session_action; @@ -565,8 +564,8 @@ end: return status; } -static const struct lttng_firing_policy * -lttng_action_snapshot_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_snapshot_session_internal_get_rate_policy( const struct lttng_action *action) { const struct lttng_action_snapshot_session *_action; diff --git a/src/common/actions/start-session.c b/src/common/actions/start-session.c index 07aeaf779..8ed8068dd 100644 --- a/src/common/actions/start-session.c +++ b/src/common/actions/start-session.c @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -22,7 +22,7 @@ struct lttng_action_start_session { /* Owned by this. */ char *session_name; - struct lttng_firing_policy *policy; + struct lttng_rate_policy *policy; }; struct lttng_action_start_session_comm { @@ -38,8 +38,8 @@ struct lttng_action_start_session_comm { char data[]; } LTTNG_PACKED; -static const struct lttng_firing_policy * -lttng_action_start_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_start_session_internal_get_rate_policy( const struct lttng_action *action); static struct lttng_action_start_session *action_start_session_from_action( @@ -98,7 +98,7 @@ static bool lttng_action_start_session_is_equal( goto end; } - is_equal = lttng_firing_policy_is_equal(a->policy, b->policy); + is_equal = lttng_rate_policy_is_equal(a->policy, b->policy); end: return is_equal; } @@ -137,7 +137,7 @@ static int lttng_action_start_session_serialize( goto end; } - ret = lttng_firing_policy_serialize( + ret = lttng_rate_policy_serialize( action_start_session->policy, payload); if (ret) { ret = -1; @@ -159,7 +159,7 @@ static void lttng_action_start_session_destroy(struct lttng_action *action) action_start_session = action_start_session_from_action(action); - lttng_firing_policy_destroy(action_start_session->policy); + lttng_rate_policy_destroy(action_start_session->policy); free(action_start_session->session_name); free(action_start_session); @@ -176,7 +176,7 @@ ssize_t lttng_action_start_session_create_from_payload( const char *session_name; struct lttng_action *action = NULL; enum lttng_action_status status; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; comm = (typeof(comm)) view->buffer.data; session_name = (const char *) &comm->data; @@ -189,12 +189,12 @@ ssize_t lttng_action_start_session_create_from_payload( } consumed_len = sizeof(*comm) + comm->session_name_len; - /* Firing policy. */ + /* Rate policy. */ { struct lttng_payload_view policy_view = lttng_payload_view_from_view( view, consumed_len, -1); - ret = lttng_firing_policy_create_from_payload( + ret = lttng_rate_policy_create_from_payload( &policy_view, &policy); if (ret < 0) { consumed_len = -1; @@ -217,7 +217,7 @@ ssize_t lttng_action_start_session_create_from_payload( } assert(policy); - status = lttng_action_start_session_set_firing_policy(action, policy); + status = lttng_action_start_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { consumed_len = -1; goto end; @@ -227,7 +227,7 @@ ssize_t lttng_action_start_session_create_from_payload( action = NULL; end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); lttng_action_start_session_destroy(action); return consumed_len; @@ -236,11 +236,11 @@ end: struct lttng_action *lttng_action_start_session_create(void) { struct lttng_action *action = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; - /* Create a every N = 1 firing policy. */ - policy = lttng_firing_policy_every_n_create(1); + /* Create a every N = 1 rate policy. */ + policy = lttng_rate_policy_every_n_create(1); if (!policy) { goto end; } @@ -255,9 +255,9 @@ struct lttng_action *lttng_action_start_session_create(void) lttng_action_start_session_serialize, lttng_action_start_session_is_equal, lttng_action_start_session_destroy, - lttng_action_start_session_internal_get_firing_policy); + lttng_action_start_session_internal_get_rate_policy); - status = lttng_action_start_session_set_firing_policy(action, policy); + status = lttng_action_start_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { free(action); action = NULL; @@ -265,7 +265,7 @@ struct lttng_action *lttng_action_start_session_create(void) } end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); return action; } @@ -316,20 +316,20 @@ end: return status; } -enum lttng_action_status lttng_action_start_session_set_firing_policy( +enum lttng_action_status lttng_action_start_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy) + const struct lttng_rate_policy *policy) { enum lttng_action_status status; struct lttng_action_start_session *start_session_action; - struct lttng_firing_policy *copy = NULL; + struct lttng_rate_policy *copy = NULL; if (!action || !policy || !IS_START_SESSION_ACTION(action)) { status = LTTNG_ACTION_STATUS_INVALID; goto end; } - copy = lttng_firing_policy_copy(policy); + copy = lttng_rate_policy_copy(policy); if (!copy) { status = LTTNG_ACTION_STATUS_ERROR; goto end; @@ -337,8 +337,8 @@ enum lttng_action_status lttng_action_start_session_set_firing_policy( start_session_action = action_start_session_from_action(action); - /* Release the previous firing policy .*/ - lttng_firing_policy_destroy(start_session_action->policy); + /* Release the previous rate policy .*/ + lttng_rate_policy_destroy(start_session_action->policy); /* Assign the policy. */ start_session_action->policy = copy; @@ -346,13 +346,13 @@ enum lttng_action_status lttng_action_start_session_set_firing_policy( copy = NULL; end: - lttng_firing_policy_destroy(copy); + lttng_rate_policy_destroy(copy); return status; } -enum lttng_action_status lttng_action_start_session_get_firing_policy( +enum lttng_action_status lttng_action_start_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy) + const struct lttng_rate_policy **policy) { enum lttng_action_status status; const struct lttng_action_start_session *start_session_action; @@ -370,8 +370,8 @@ end: return status; } -static const struct lttng_firing_policy * -lttng_action_start_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_start_session_internal_get_rate_policy( const struct lttng_action *action) { const struct lttng_action_start_session *_action; diff --git a/src/common/actions/stop-session.c b/src/common/actions/stop-session.c index b3ff63188..9f74bf884 100644 --- a/src/common/actions/stop-session.c +++ b/src/common/actions/stop-session.c @@ -9,8 +9,8 @@ #include #include #include -#include -#include +#include +#include #include #include @@ -22,7 +22,7 @@ struct lttng_action_stop_session { /* Owned by this. */ char *session_name; - struct lttng_firing_policy *policy; + struct lttng_rate_policy *policy; }; struct lttng_action_stop_session_comm { @@ -38,8 +38,8 @@ struct lttng_action_stop_session_comm { char data[]; } LTTNG_PACKED; -static const struct lttng_firing_policy * -lttng_action_stop_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_stop_session_internal_get_rate_policy( const struct lttng_action *action); static struct lttng_action_stop_session *action_stop_session_from_action( @@ -98,7 +98,7 @@ static bool lttng_action_stop_session_is_equal( goto end; } - is_equal = lttng_firing_policy_is_equal(a->policy, b->policy); + is_equal = lttng_rate_policy_is_equal(a->policy, b->policy); end: return is_equal; } @@ -138,8 +138,7 @@ static int lttng_action_stop_session_serialize( goto end; } - ret = lttng_firing_policy_serialize( - action_stop_session->policy, payload); + ret = lttng_rate_policy_serialize(action_stop_session->policy, payload); if (ret) { ret = -1; goto end; @@ -160,7 +159,7 @@ static void lttng_action_stop_session_destroy(struct lttng_action *action) action_stop_session = action_stop_session_from_action(action); - lttng_firing_policy_destroy(action_stop_session->policy); + lttng_rate_policy_destroy(action_stop_session->policy); free(action_stop_session->session_name); free(action_stop_session); @@ -177,7 +176,7 @@ ssize_t lttng_action_stop_session_create_from_payload( const char *session_name; struct lttng_action *action = NULL; enum lttng_action_status status; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; comm = (typeof(comm)) view->buffer.data; session_name = (const char *) &comm->data; @@ -190,12 +189,12 @@ ssize_t lttng_action_stop_session_create_from_payload( } consumed_len = sizeof(*comm) + comm->session_name_len; - /* Firing policy. */ + /* Rate policy. */ { struct lttng_payload_view policy_view = lttng_payload_view_from_view( view, consumed_len, -1); - ret = lttng_firing_policy_create_from_payload( + ret = lttng_rate_policy_create_from_payload( &policy_view, &policy); if (ret < 0) { consumed_len = -1; @@ -218,7 +217,7 @@ ssize_t lttng_action_stop_session_create_from_payload( } assert(policy); - status = lttng_action_stop_session_set_firing_policy(action, policy); + status = lttng_action_stop_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { consumed_len = -1; goto end; @@ -228,7 +227,7 @@ ssize_t lttng_action_stop_session_create_from_payload( action = NULL; end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); lttng_action_stop_session_destroy(action); return consumed_len; @@ -237,11 +236,11 @@ end: struct lttng_action *lttng_action_stop_session_create(void) { struct lttng_action *action = NULL; - struct lttng_firing_policy *policy = NULL; + struct lttng_rate_policy *policy = NULL; enum lttng_action_status status; - /* Create a every N = 1 firing policy. */ - policy = lttng_firing_policy_every_n_create(1); + /* Create a every N = 1 rate policy. */ + policy = lttng_rate_policy_every_n_create(1); if (!policy) { goto end; } @@ -256,9 +255,9 @@ struct lttng_action *lttng_action_stop_session_create(void) lttng_action_stop_session_serialize, lttng_action_stop_session_is_equal, lttng_action_stop_session_destroy, - lttng_action_stop_session_internal_get_firing_policy); + lttng_action_stop_session_internal_get_rate_policy); - status = lttng_action_stop_session_set_firing_policy(action, policy); + status = lttng_action_stop_session_set_rate_policy(action, policy); if (status != LTTNG_ACTION_STATUS_OK) { free(action); action = NULL; @@ -266,7 +265,7 @@ struct lttng_action *lttng_action_stop_session_create(void) } end: - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(policy); return action; } @@ -317,41 +316,41 @@ end: return status; } -enum lttng_action_status lttng_action_stop_session_set_firing_policy( +enum lttng_action_status lttng_action_stop_session_set_rate_policy( struct lttng_action *action, - const struct lttng_firing_policy *policy) + const struct lttng_rate_policy *policy) { enum lttng_action_status status; struct lttng_action_stop_session *stop_session_action; - struct lttng_firing_policy *copy = NULL; + struct lttng_rate_policy *copy = NULL; if (!action || !policy || !IS_STOP_SESSION_ACTION(action)) { status = LTTNG_ACTION_STATUS_INVALID; goto end; } - copy = lttng_firing_policy_copy(policy); + copy = lttng_rate_policy_copy(policy); if (!copy) { status = LTTNG_ACTION_STATUS_ERROR; goto end; } stop_session_action = action_stop_session_from_action(action); - /* Free the previous firing policy .*/ - lttng_firing_policy_destroy(stop_session_action->policy); + /* Free the previous rate policy .*/ + lttng_rate_policy_destroy(stop_session_action->policy); stop_session_action->policy = copy; status = LTTNG_ACTION_STATUS_OK; copy = NULL; end: - lttng_firing_policy_destroy(copy); + lttng_rate_policy_destroy(copy); return status; } -enum lttng_action_status lttng_action_stop_session_get_firing_policy( +enum lttng_action_status lttng_action_stop_session_get_rate_policy( const struct lttng_action *action, - const struct lttng_firing_policy **policy) + const struct lttng_rate_policy **policy) { enum lttng_action_status status; const struct lttng_action_stop_session *stop_session_action; @@ -369,8 +368,8 @@ end: return status; } -static const struct lttng_firing_policy * -lttng_action_stop_session_internal_get_firing_policy( +static const struct lttng_rate_policy * +lttng_action_stop_session_internal_get_rate_policy( const struct lttng_action *action) { const struct lttng_action_stop_session *_action; diff --git a/tests/regression/Makefile.am b/tests/regression/Makefile.am index c66f64d88..cc110506f 100644 --- a/tests/regression/Makefile.am +++ b/tests/regression/Makefile.am @@ -74,7 +74,7 @@ TESTS += ust/before-after/test_before_after \ ust/rotation-destroy-flush/test_rotation_destroy_flush \ tools/metadata/test_ust \ tools/relayd-grouping/test_ust \ - tools/trigger/firing-policy/test_ust_firing_policy + tools/trigger/rate-policy/test_ust_rate_policy if IS_LINUX TESTS += \ diff --git a/tests/regression/tools/trigger/Makefile.am b/tests/regression/tools/trigger/Makefile.am index 914634a04..6cd4ef660 100644 --- a/tests/regression/tools/trigger/Makefile.am +++ b/tests/regression/tools/trigger/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS=utils start-stop firing-policy +SUBDIRS=utils start-stop rate-policy noinst_SCRIPTS = test_add_trigger_cli \ test_list_triggers_cli \ diff --git a/tests/regression/tools/trigger/firing-policy/Makefile.am b/tests/regression/tools/trigger/rate-policy/Makefile.am similarity index 81% rename from tests/regression/tools/trigger/firing-policy/Makefile.am rename to tests/regression/tools/trigger/rate-policy/Makefile.am index 14d84cbd2..40ee3b7f3 100644 --- a/tests/regression/tools/trigger/firing-policy/Makefile.am +++ b/tests/regression/tools/trigger/rate-policy/Makefile.am @@ -1,8 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only -noinst_SCRIPTS = test_ust_firing_policy +noinst_SCRIPTS = test_ust_rate_policy -EXTRA_DIST = test_ust_firing_policy +EXTRA_DIST = test_ust_rate_policy all-local: @if [ x"$(srcdir)" != x"$(builddir)" ]; then \ diff --git a/tests/regression/tools/trigger/firing-policy/test_ust_firing_policy b/tests/regression/tools/trigger/rate-policy/test_ust_rate_policy similarity index 96% rename from tests/regression/tools/trigger/firing-policy/test_ust_firing_policy rename to tests/regression/tools/trigger/rate-policy/test_ust_rate_policy index d92dacc21..27d813048 100755 --- a/tests/regression/tools/trigger/firing-policy/test_ust_firing_policy +++ b/tests/regression/tools/trigger/rate-policy/test_ust_rate_policy @@ -4,7 +4,7 @@ # # SPDX-License-Identifier: LGPL-2.1-only -TEST_DESC="Triggers - Start and stop actions" +TEST_DESC="Triggers - rate policy notify" CURDIR=$(dirname "$0")/ TESTDIR=${CURDIR}/../../../.. @@ -21,7 +21,7 @@ NUM_TESTS=19 NR_ITER=10 NR_USEC_WAIT=5 -function test_firing_policy_every_n() +function test_rate_policy_every_n() { local SESSION_NAME="my_triggered_session" local TRIGGER_NAME="trigger1" @@ -30,7 +30,7 @@ function test_firing_policy_every_n() SYNC_AFTER_NOTIF_REGISTER_PATH=$(mktemp -u test-notif-register.XXXXXX) - diag "Every N firing policy" + diag "Every N rate policy" # Add a trigger with a notify action with a policy to fire it every 5 # time the condition is met. @@ -107,7 +107,7 @@ function test_firing_policy_every_n() rm -f "$SYNC_AFTER_NOTIF_REGISTER_PATH" } -function test_firing_policy_once_after_n() +function test_rate_policy_once_after_n() { local SESSION_NAME="my_triggered_session" local TRIGGER_NAME="trigger1" @@ -116,7 +116,7 @@ function test_firing_policy_once_after_n() SYNC_AFTER_NOTIF_REGISTER_PATH=$(mktemp -u test-notif-register.XXXXXX) - diag "Once after N firing policy" + diag "Once after N rate policy" # Add a trigger with a notify action with a policy to fire it every 5 # time the condition is met. @@ -220,7 +220,7 @@ print_test_banner "$TEST_DESC" start_lttng_sessiond_notap -test_firing_policy_every_n -test_firing_policy_once_after_n +test_rate_policy_every_n +test_rate_policy_once_after_n stop_lttng_sessiond_notap diff --git a/tests/regression/tools/trigger/test_list_triggers_cli b/tests/regression/tools/trigger/test_list_triggers_cli index ac7f09925..fa1af958d 100755 --- a/tests/regression/tools/trigger/test_list_triggers_cli +++ b/tests/regression/tools/trigger/test_list_triggers_cli @@ -363,14 +363,14 @@ test_snapshot_action () rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: - snapshot session \`ze-session\`, firing policy: after every 10 occurrences + snapshot session \`ze-session\`, rate policy: after every 10 occurrences - id: T9 user id: ${uid} condition: event rule hit rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: - snapshot session \`ze-session\`, firing policy: once after 10 occurrences + snapshot session \`ze-session\`, rate policy: once after 10 occurrences EOF list_triggers "snapshot action" "${tmp_expected_stdout}" @@ -393,14 +393,14 @@ test_notify_action () rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: - notify, firing policy: once after 5 occurrences + notify, rate policy: once after 5 occurrences - id: T1 user id: ${uid} condition: event rule hit rule: some-event (type: tracepoint, domain: ust) tracer notifications discarded: 0 actions: - notify, firing policy: after every 10 occurrences + notify, rate policy: after every 10 occurrences EOF list_triggers "snapshot action" "${tmp_expected_stdout}" diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 10dbd3a60..2a0f3c9fb 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -16,7 +16,7 @@ TESTS = \ test_event_expr_to_bytecode \ test_event_rule \ test_fd_tracker \ - test_firing_policy \ + test_rate_policy \ test_kernel_data \ test_kernel_probe \ test_log_level_rule \ @@ -53,7 +53,7 @@ noinst_PROGRAMS = \ test_event_expr_to_bytecode \ test_event_rule \ test_fd_tracker \ - test_firing_policy \ + test_rate_policy \ test_kernel_data \ test_kernel_probe \ test_log_level_rule \ @@ -234,9 +234,9 @@ test_relayd_backward_compat_group_by_session_SOURCES = test_relayd_backward_comp test_relayd_backward_compat_group_by_session_LDADD = $(LIBTAP) $(LIBCOMMON) $(RELAYD_OBJS) test_relayd_backward_compat_group_by_session_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src/bin/lttng-relayd -# firing policy object unit test -test_firing_policy_SOURCES = test_firing_policy.c -test_firing_policy_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBLTTNG_CTL) $(DL_LIBS) \ +# rate policy object unit test +test_rate_policy_SOURCES = test_rate_policy.c +test_rate_policy_LDADD = $(LIBTAP) $(LIBCOMMON) $(LIBLTTNG_CTL) $(DL_LIBS) \ $(top_builddir)/src/bin/lttng/lttng-loglevel.$(OBJEXT) # fd tracker unit test diff --git a/tests/unit/test_action.c b/tests/unit/test_action.c index 80e534036..95df311d0 100644 --- a/tests/unit/test_action.c +++ b/tests/unit/test_action.c @@ -21,9 +21,9 @@ #include #include #include -#include -#include #include +#include +#include #include #include #include @@ -42,15 +42,15 @@ static void test_action_notify(void) enum lttng_action_status status; struct lttng_action *notify_action = NULL, *notify_action_from_buffer = NULL; - struct lttng_firing_policy *policy = NULL, *default_policy; + struct lttng_rate_policy *policy = NULL, *default_policy; struct lttng_payload payload; lttng_payload_init(&payload); /* To set. */ - policy = lttng_firing_policy_every_n_create(100); + policy = lttng_rate_policy_every_n_create(100); /* For comparison. */ - default_policy = lttng_firing_policy_every_n_create(1); + default_policy = lttng_rate_policy_every_n_create(1); assert(policy && default_policy); @@ -61,27 +61,27 @@ static void test_action_notify(void) /* Validate the default policy for a notify action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_notify_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_notify_get_rate_policy( notify_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( default_policy, cur_policy), "Default policy is every n=1"); } /* Set a custom policy. */ - status = lttng_action_notify_set_firing_policy(notify_action, policy); - ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); + status = lttng_action_notify_set_rate_policy(notify_action, policy); + ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy"); /* Validate the custom policy for a notify action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_notify_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_notify_get_rate_policy( notify_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( policy, cur_policy), "Notify action policy get"); @@ -103,8 +103,8 @@ static void test_action_notify(void) ok(lttng_action_is_equal(notify_action, notify_action_from_buffer), "Serialized and de-serialized notify action are equal"); - lttng_firing_policy_destroy(default_policy); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(default_policy); + lttng_rate_policy_destroy(policy); lttng_action_destroy(notify_action); lttng_action_destroy(notify_action_from_buffer); lttng_payload_reset(&payload); @@ -116,7 +116,7 @@ static void test_action_rotate_session(void) enum lttng_action_status status; struct lttng_action *rotate_session_action = NULL, *rotate_session_action_from_buffer = NULL; - struct lttng_firing_policy *policy = NULL, *default_policy; + struct lttng_rate_policy *policy = NULL, *default_policy; struct lttng_payload payload; const char *session_name = "my_session_name"; const char *get_session_name; @@ -124,9 +124,9 @@ static void test_action_rotate_session(void) lttng_payload_init(&payload); /* To set. */ - policy = lttng_firing_policy_every_n_create(100); + policy = lttng_rate_policy_every_n_create(100); /* For comparison. */ - default_policy = lttng_firing_policy_every_n_create(1); + default_policy = lttng_rate_policy_every_n_create(1); assert(policy && default_policy); @@ -163,28 +163,28 @@ static void test_action_rotate_session(void) /* Validate the default policy for a rotate_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_rotate_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_rotate_session_get_rate_policy( rotate_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( default_policy, cur_policy), "Default policy is every n=1"); } /* Set a custom policy. */ - status = lttng_action_rotate_session_set_firing_policy( + status = lttng_action_rotate_session_set_rate_policy( rotate_session_action, policy); - ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); + ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy"); /* Validate the custom policy for a rotate_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_rotate_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_rotate_session_get_rate_policy( rotate_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( policy, cur_policy), "rotate_session action policy get"); @@ -208,8 +208,8 @@ static void test_action_rotate_session(void) rotate_session_action_from_buffer), "Serialized and de-serialized rotate_session action are equal"); - lttng_firing_policy_destroy(default_policy); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(default_policy); + lttng_rate_policy_destroy(policy); lttng_action_destroy(rotate_session_action); lttng_action_destroy(rotate_session_action_from_buffer); lttng_payload_reset(&payload); @@ -221,7 +221,7 @@ static void test_action_start_session(void) enum lttng_action_status status; struct lttng_action *start_session_action = NULL, *start_session_action_from_buffer = NULL; - struct lttng_firing_policy *policy = NULL, *default_policy; + struct lttng_rate_policy *policy = NULL, *default_policy; struct lttng_payload payload; const char *session_name = "my_session_name"; const char *get_session_name; @@ -229,9 +229,9 @@ static void test_action_start_session(void) lttng_payload_init(&payload); /* To set. */ - policy = lttng_firing_policy_every_n_create(100); + policy = lttng_rate_policy_every_n_create(100); /* For comparison. */ - default_policy = lttng_firing_policy_every_n_create(1); + default_policy = lttng_rate_policy_every_n_create(1); assert(policy && default_policy); @@ -268,28 +268,28 @@ static void test_action_start_session(void) /* Validate the default policy for a start_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_start_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_start_session_get_rate_policy( start_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( default_policy, cur_policy), "Default policy is every n=1"); } /* Set a custom policy. */ - status = lttng_action_start_session_set_firing_policy( + status = lttng_action_start_session_set_rate_policy( start_session_action, policy); - ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); + ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy"); /* Validate the custom policy for a start_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_start_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_start_session_get_rate_policy( start_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( policy, cur_policy), "start_session action policy get"); @@ -313,8 +313,8 @@ static void test_action_start_session(void) start_session_action_from_buffer), "Serialized and de-serialized start_session action are equal"); - lttng_firing_policy_destroy(default_policy); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(default_policy); + lttng_rate_policy_destroy(policy); lttng_action_destroy(start_session_action); lttng_action_destroy(start_session_action_from_buffer); lttng_payload_reset(&payload); @@ -326,7 +326,7 @@ static void test_action_stop_session(void) enum lttng_action_status status; struct lttng_action *stop_session_action = NULL, *stop_session_action_from_buffer = NULL; - struct lttng_firing_policy *policy = NULL, *default_policy; + struct lttng_rate_policy *policy = NULL, *default_policy; struct lttng_payload payload; const char *session_name = "my_session_name"; const char *get_session_name; @@ -334,9 +334,9 @@ static void test_action_stop_session(void) lttng_payload_init(&payload); /* To set. */ - policy = lttng_firing_policy_every_n_create(100); + policy = lttng_rate_policy_every_n_create(100); /* For comparison. */ - default_policy = lttng_firing_policy_every_n_create(1); + default_policy = lttng_rate_policy_every_n_create(1); assert(policy && default_policy); @@ -372,28 +372,28 @@ static void test_action_stop_session(void) /* Validate the default policy for a stop_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_stop_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_stop_session_get_rate_policy( stop_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( default_policy, cur_policy), "Default policy is every n=1"); } /* Set a custom policy. */ - status = lttng_action_stop_session_set_firing_policy( + status = lttng_action_stop_session_set_rate_policy( stop_session_action, policy); - ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); + ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy"); /* Validate the custom policy for a stop_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_stop_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_stop_session_get_rate_policy( stop_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( policy, cur_policy), "stop_session action policy get"); @@ -417,8 +417,8 @@ static void test_action_stop_session(void) stop_session_action_from_buffer), "Serialized and de-serialized stop_session action are equal"); - lttng_firing_policy_destroy(default_policy); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(default_policy); + lttng_rate_policy_destroy(policy); lttng_action_destroy(stop_session_action); lttng_action_destroy(stop_session_action_from_buffer); lttng_payload_reset(&payload); @@ -430,7 +430,7 @@ static void test_action_snapshot_session(void) enum lttng_action_status status; struct lttng_action *snapshot_session_action = NULL, *snapshot_session_action_from_buffer = NULL; - struct lttng_firing_policy *policy = NULL, *default_policy; + struct lttng_rate_policy *policy = NULL, *default_policy; struct lttng_payload payload; const char *session_name = "my_session_name"; const char *get_session_name; @@ -438,9 +438,9 @@ static void test_action_snapshot_session(void) lttng_payload_init(&payload); /* To set. */ - policy = lttng_firing_policy_every_n_create(100); + policy = lttng_rate_policy_every_n_create(100); /* For comparison. */ - default_policy = lttng_firing_policy_every_n_create(1); + default_policy = lttng_rate_policy_every_n_create(1); assert(policy && default_policy); @@ -477,28 +477,28 @@ static void test_action_snapshot_session(void) /* Validate the default policy for a snapshot_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_snapshot_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_snapshot_session_get_rate_policy( snapshot_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( default_policy, cur_policy), "Default policy is every n=1"); } /* Set a custom policy. */ - status = lttng_action_snapshot_session_set_firing_policy( + status = lttng_action_snapshot_session_set_rate_policy( snapshot_session_action, policy); - ok(status == LTTNG_ACTION_STATUS_OK, "Set firing policy"); + ok(status == LTTNG_ACTION_STATUS_OK, "Set rate policy"); /* Validate the custom policy for a snapshot_session action. */ { - const struct lttng_firing_policy *cur_policy = NULL; - status = lttng_action_snapshot_session_get_firing_policy( + const struct lttng_rate_policy *cur_policy = NULL; + status = lttng_action_snapshot_session_get_rate_policy( snapshot_session_action, &cur_policy); ok(status == LTTNG_ACTION_STATUS_OK && - lttng_firing_policy_is_equal( + lttng_rate_policy_is_equal( policy, cur_policy), "snapshot_session action policy get"); @@ -522,8 +522,8 @@ static void test_action_snapshot_session(void) snapshot_session_action_from_buffer), "Serialized and de-serialized snapshot_session action are equal"); - lttng_firing_policy_destroy(default_policy); - lttng_firing_policy_destroy(policy); + lttng_rate_policy_destroy(default_policy); + lttng_rate_policy_destroy(policy); lttng_action_destroy(snapshot_session_action); lttng_action_destroy(snapshot_session_action_from_buffer); lttng_payload_reset(&payload); diff --git a/tests/unit/test_firing_policy.c b/tests/unit/test_firing_policy.c deleted file mode 100644 index e7504c212..000000000 --- a/tests/unit/test_firing_policy.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * Unit tests for the firing policy object API. - * - * Copyright (C) 2019 Jonathan Rajotte - * - * SPDX-License-Identifier: LGPL-2.1-only - * - */ - -#include -#include -#include -#include -#include - -#include - -#include -#include -#include -#include - -/* For error.h. */ -int lttng_opt_quiet = 1; -int lttng_opt_verbose; -int lttng_opt_mi; - -#define NUM_TESTS 42 - -static void test_firing_policy_every_n(void) -{ - enum lttng_firing_policy_status status; - struct lttng_firing_policy *policy_a = NULL; /* Interval of 100. */ - struct lttng_firing_policy *policy_b = NULL; /* Interval of 100. */ - struct lttng_firing_policy *policy_c = NULL; /* Interval of 1. */ - struct lttng_firing_policy *policy_from_buffer = NULL; - uint64_t interval_a_b = 100; - uint64_t interval_c = 1; - uint64_t interval_query = 0; - struct lttng_payload payload; - - lttng_payload_init(&payload); - - policy_a = lttng_firing_policy_every_n_create(interval_a_b); - policy_b = lttng_firing_policy_every_n_create(interval_a_b); - policy_c = lttng_firing_policy_every_n_create(interval_c); - ok(policy_a != NULL, - "Firing policy 'every n' A created: interval: %" PRIu64, - interval_a_b); - ok(policy_b != NULL, - "Firing policy 'every n' B created: interval: %" PRIu64, - interval_a_b); - ok(policy_c != NULL, - "Firing policy 'every n' C created: interval: %" PRIu64, - interval_c); - - ok(LTTNG_FIRING_POLICY_TYPE_EVERY_N == - lttng_firing_policy_get_type(policy_a), - "Type is LTTNG_FIRING_POLICY_TYPE_EVERY_N"); - - /* Getter tests */ - status = lttng_firing_policy_every_n_get_interval(NULL, NULL); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get interval returns INVALID"); - - status = lttng_firing_policy_every_n_get_interval( - NULL, &interval_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get interval returns INVALID"); - - status = lttng_firing_policy_every_n_get_interval(policy_a, NULL); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get interval returns INVALID"); - - status = lttng_firing_policy_every_n_get_interval( - policy_a, &interval_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - interval_query == interval_a_b, - "Getting interval A"); - - status = lttng_firing_policy_every_n_get_interval( - policy_b, &interval_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - interval_query == interval_a_b, - "Getting interval B"); - - status = lttng_firing_policy_every_n_get_interval( - policy_c, &interval_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - interval_query == interval_c, - "Getting interval C"); - - /* is_equal tests */ - ok(false == lttng_firing_policy_is_equal(NULL, NULL), - "is equal (NULL,NULL)"); - ok(false == lttng_firing_policy_is_equal(policy_a, NULL), - "is equal (object, NULL)"); - ok(false == lttng_firing_policy_is_equal(NULL, policy_a), - "is equal (NULL, object)"); - ok(true == lttng_firing_policy_is_equal(policy_a, policy_a), - "is equal (object A, object A)"); - - ok(true == lttng_firing_policy_is_equal(policy_a, policy_b), - "is equal (object A, object B"); - ok(true == lttng_firing_policy_is_equal(policy_b, policy_a), - "is equal (object B, object A"); - - ok(false == lttng_firing_policy_is_equal(policy_a, policy_c), - "is equal (object A, object C)"); - ok(false == lttng_firing_policy_is_equal(policy_c, policy_a), - "is equal (object C, object A)"); - - /* Serialization and create_from buffer. */ - ok(lttng_firing_policy_serialize(policy_a, &payload) == 0, - "Serializing firing policy"); - { - struct lttng_payload_view view = - lttng_payload_view_from_payload( - &payload, 0, -1); - - ok(lttng_firing_policy_create_from_payload( - &view, &policy_from_buffer) > 0 && - policy_from_buffer != NULL, - "Deserializing firing policy"); - } - - ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer), - "Original and deserialized instances are equal"); - - lttng_firing_policy_destroy(policy_a); - lttng_firing_policy_destroy(policy_b); - lttng_firing_policy_destroy(policy_c); - lttng_firing_policy_destroy(policy_from_buffer); - lttng_payload_reset(&payload); -} - -static void test_firing_policy_once_after_n(void) -{ - enum lttng_firing_policy_status status; - struct lttng_firing_policy *policy_a = NULL; /* Threshold of 100. */ - struct lttng_firing_policy *policy_b = NULL; /* threshold of 100 */ - struct lttng_firing_policy *policy_c = NULL; /* threshold of 1 */ - struct lttng_firing_policy *policy_from_buffer = NULL; - uint64_t threshold_a_b = 100; - uint64_t threshold_c = 1; - uint64_t threshold_query = 0; - struct lttng_payload payload; - - lttng_payload_init(&payload); - - policy_a = lttng_firing_policy_once_after_n_create(threshold_a_b); - policy_b = lttng_firing_policy_once_after_n_create(threshold_a_b); - policy_c = lttng_firing_policy_once_after_n_create(threshold_c); - ok(policy_a != NULL, - "Firing policy every n A created: threshold: %" PRIu64, - threshold_a_b); - ok(policy_b != NULL, - "Firing policy every n B created: threshold: %" PRIu64, - threshold_a_b); - ok(policy_c != NULL, - "Firing policy every n C created: threshold: %" PRIu64, - threshold_c); - - ok(LTTNG_FIRING_POLICY_TYPE_ONCE_AFTER_N == - lttng_firing_policy_get_type(policy_a), - "Type is LTTNG_FIRING_POLICY_TYPE_once_after_n"); - - /* Getter tests */ - status = lttng_firing_policy_once_after_n_get_threshold(NULL, NULL); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get threshold returns INVALID"); - - status = lttng_firing_policy_once_after_n_get_threshold( - NULL, &threshold_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get threshold returns INVALID"); - - status = lttng_firing_policy_once_after_n_get_threshold(policy_a, NULL); - ok(status == LTTNG_FIRING_POLICY_STATUS_INVALID, - "Get threshold returns INVALID"); - - status = lttng_firing_policy_once_after_n_get_threshold( - policy_a, &threshold_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - threshold_query == threshold_a_b, - "Getting threshold A"); - - status = lttng_firing_policy_once_after_n_get_threshold( - policy_b, &threshold_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - threshold_query == threshold_a_b, - "Getting threshold B"); - - status = lttng_firing_policy_once_after_n_get_threshold( - policy_c, &threshold_query); - ok(status == LTTNG_FIRING_POLICY_STATUS_OK && - threshold_query == threshold_c, - "Getting threshold C"); - - /* is_equal tests */ - ok(false == lttng_firing_policy_is_equal(NULL, NULL), - "is equal (NULL,NULL)"); - ok(false == lttng_firing_policy_is_equal(policy_a, NULL), - "is equal (object, NULL)"); - ok(false == lttng_firing_policy_is_equal(NULL, policy_a), - "is equal (NULL, object)"); - ok(true == lttng_firing_policy_is_equal(policy_a, policy_a), - "is equal (object A, object A)"); - - ok(true == lttng_firing_policy_is_equal(policy_a, policy_b), - "is equal (object A, object B"); - ok(true == lttng_firing_policy_is_equal(policy_b, policy_a), - "is equal (object B, object A"); - - ok(false == lttng_firing_policy_is_equal(policy_a, policy_c), - "is equal (object A, object C)"); - ok(false == lttng_firing_policy_is_equal(policy_c, policy_a), - "is equal (object C, object A)"); - - /* Serialization and create_from buffer. */ - ok(lttng_firing_policy_serialize(policy_a, &payload) == 0, - "Serializing firing policy"); - { - struct lttng_payload_view view = - lttng_payload_view_from_payload( - &payload, 0, -1); - - ok(lttng_firing_policy_create_from_payload( - &view, &policy_from_buffer) > 0 && - policy_from_buffer != NULL, - "Deserializing firing policy"); - } - - ok(lttng_firing_policy_is_equal(policy_a, policy_from_buffer), - "Original and deserialized instances are equal"); - - lttng_firing_policy_destroy(policy_a); - lttng_firing_policy_destroy(policy_b); - lttng_firing_policy_destroy(policy_c); - lttng_firing_policy_destroy(policy_from_buffer); - lttng_payload_reset(&payload); -} - -int main(int argc, const char *argv[]) -{ - plan_tests(NUM_TESTS); - test_firing_policy_every_n(); - test_firing_policy_once_after_n(); - return exit_status(); -} diff --git a/tests/unit/test_rate_policy.c b/tests/unit/test_rate_policy.c new file mode 100644 index 000000000..fcb27733c --- /dev/null +++ b/tests/unit/test_rate_policy.c @@ -0,0 +1,253 @@ +/* + * Unit tests for the rate policy object API. + * + * Copyright (C) 2019 Jonathan Rajotte + * + * SPDX-License-Identifier: LGPL-2.1-only + * + */ + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +/* For error.h. */ +int lttng_opt_quiet = 1; +int lttng_opt_verbose; +int lttng_opt_mi; + +#define NUM_TESTS 42 + +static void test_rate_policy_every_n(void) +{ + enum lttng_rate_policy_status status; + struct lttng_rate_policy *policy_a = NULL; /* Interval of 100. */ + struct lttng_rate_policy *policy_b = NULL; /* Interval of 100 */ + struct lttng_rate_policy *policy_c = NULL; /* Interval of 1 */ + struct lttng_rate_policy *policy_from_buffer = NULL; + uint64_t interval_a_b = 100; + uint64_t interval_c = 1; + uint64_t interval_query = 0; + struct lttng_payload payload; + + lttng_payload_init(&payload); + + policy_a = lttng_rate_policy_every_n_create(interval_a_b); + policy_b = lttng_rate_policy_every_n_create(interval_a_b); + policy_c = lttng_rate_policy_every_n_create(interval_c); + ok(policy_a != NULL, + "Rate policy every n A created: interval: %" PRIu64, + interval_a_b); + ok(policy_b != NULL, + "Rate policy every n B created: interval: %" PRIu64, + interval_a_b); + ok(policy_c != NULL, + "Rate policy every n C created: interval: %" PRIu64, + interval_c); + + ok(LTTNG_RATE_POLICY_TYPE_EVERY_N == + lttng_rate_policy_get_type(policy_a), + "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N"); + + /* Getter tests */ + status = lttng_rate_policy_every_n_get_interval(NULL, NULL); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get interval returns INVALID"); + + status = lttng_rate_policy_every_n_get_interval(NULL, &interval_query); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get interval returns INVALID"); + + status = lttng_rate_policy_every_n_get_interval(policy_a, NULL); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get interval returns INVALID"); + + status = lttng_rate_policy_every_n_get_interval( + policy_a, &interval_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + interval_query == interval_a_b, + " Getting interval A"); + + status = lttng_rate_policy_every_n_get_interval( + policy_b, &interval_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + interval_query == interval_a_b, + " Getting interval B"); + + status = lttng_rate_policy_every_n_get_interval( + policy_c, &interval_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + interval_query == interval_c, + " Getting interval C"); + + /* is_equal tests */ + /* TODO: this is the behaviour introduced by the + * lttng_condition_is_equal back in 2017 do we want to fix this and + * return true if both are NULL? + */ + ok(false == lttng_rate_policy_is_equal(NULL, NULL), + "is equal (NULL,NULL)"); + ok(false == lttng_rate_policy_is_equal(policy_a, NULL), + "is equal (object, NULL)"); + ok(false == lttng_rate_policy_is_equal(NULL, policy_a), + " is equal (NULL, object)"); + ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), + "is equal (object A, object A)"); + + ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), + "is equal (object A, object B"); + ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), + "is equal (object B, object A"); + + ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), + "is equal (object A, object C)"); + ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), + "is equal (object C, object A)"); + + /* Serialization and create_from buffer. */ + ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); + { + struct lttng_payload_view view = + lttng_payload_view_from_payload( + &payload, 0, -1); + + ok(lttng_rate_policy_create_from_payload( + &view, &policy_from_buffer) > 0 && + policy_from_buffer != NULL, + "Deserializing"); + } + + ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), + "serialized and from buffer are equal"); + + lttng_rate_policy_destroy(policy_a); + lttng_rate_policy_destroy(policy_b); + lttng_rate_policy_destroy(policy_c); + lttng_payload_reset(&payload); +} + +static void test_rate_policy_once_after_n(void) +{ + enum lttng_rate_policy_status status; + struct lttng_rate_policy *policy_a = NULL; /* Threshold of 100. */ + struct lttng_rate_policy *policy_b = NULL; /* threshold of 100 */ + struct lttng_rate_policy *policy_c = NULL; /* threshold of 1 */ + struct lttng_rate_policy *policy_from_buffer = NULL; + uint64_t threshold_a_b = 100; + uint64_t threshold_c = 1; + uint64_t threshold_query = 0; + struct lttng_payload payload; + + lttng_payload_init(&payload); + + policy_a = lttng_rate_policy_once_after_n_create(threshold_a_b); + policy_b = lttng_rate_policy_once_after_n_create(threshold_a_b); + policy_c = lttng_rate_policy_once_after_n_create(threshold_c); + ok(policy_a != NULL, + "Rate policy every n A created: threshold: %" PRIu64, + threshold_a_b); + ok(policy_b != NULL, + "Rate policy every n B created: threshold: %" PRIu64, + threshold_a_b); + ok(policy_c != NULL, + "Rate policy every n C created: threshold: %" PRIu64, + threshold_c); + + ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N == + lttng_rate_policy_get_type(policy_a), + "Type is LTTNG_RATE_POLICY_TYPE_once_after_n"); + + /* Getter tests */ + status = lttng_rate_policy_once_after_n_get_threshold(NULL, NULL); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get threshold returns INVALID"); + + status = lttng_rate_policy_once_after_n_get_threshold( + NULL, &threshold_query); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get threshold returns INVALID"); + + status = lttng_rate_policy_once_after_n_get_threshold(policy_a, NULL); + ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, + "Get threshold returns INVALID"); + + status = lttng_rate_policy_once_after_n_get_threshold( + policy_a, &threshold_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + threshold_query == threshold_a_b, + " Getting threshold A"); + + status = lttng_rate_policy_once_after_n_get_threshold( + policy_b, &threshold_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + threshold_query == threshold_a_b, + " Getting threshold B"); + + status = lttng_rate_policy_once_after_n_get_threshold( + policy_c, &threshold_query); + ok(status == LTTNG_RATE_POLICY_STATUS_OK && + threshold_query == threshold_c, + " Getting threshold C"); + + /* is_equal tests */ + /* TODO: this is the behaviour introduced by the + * lttng_condition_is_equal back in 2017 do we want to fix this and + * return true if both are NULL? + */ + ok(false == lttng_rate_policy_is_equal(NULL, NULL), + "is equal (NULL,NULL)"); + ok(false == lttng_rate_policy_is_equal(policy_a, NULL), + "is equal (object, NULL)"); + ok(false == lttng_rate_policy_is_equal(NULL, policy_a), + " is equal (NULL, object)"); + ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), + "is equal (object A, object A)"); + + ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), + "is equal (object A, object B"); + ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), + "is equal (object B, object A"); + + ok(false == lttng_rate_policy_is_equal(policy_a, policy_c), + "is equal (object A, object C)"); + ok(false == lttng_rate_policy_is_equal(policy_c, policy_a), + "is equal (object C, object A)"); + + /* Serialization and create_from buffer. */ + ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing"); + { + struct lttng_payload_view view = + lttng_payload_view_from_payload( + &payload, 0, -1); + + ok(lttng_rate_policy_create_from_payload( + &view, &policy_from_buffer) > 0 && + policy_from_buffer != NULL, + "Deserializing"); + } + + ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer), + "serialized and from buffer are equal"); + + lttng_rate_policy_destroy(policy_a); + lttng_rate_policy_destroy(policy_b); + lttng_rate_policy_destroy(policy_c); + lttng_payload_reset(&payload); +} + +int main(int argc, const char *argv[]) +{ + plan_tests(NUM_TESTS); + test_rate_policy_every_n(); + test_rate_policy_once_after_n(); + return exit_status(); +} -- 2.34.1