Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / actions / firing-policy.c
index 6152d61d8066735aafc5fc914396653e0497f67f..59d8279385a3677d9e8f5e902b348a90f1e31b22 100644 (file)
@@ -5,7 +5,6 @@
  *
  */
 
-#include <assert.h>
 #include <common/buffer-view.h>
 #include <common/dynamic-buffer.h>
 #include <common/error.h>
@@ -78,6 +77,14 @@ static void lttng_firing_policy_init(struct lttng_firing_policy *firing_policy,
                firing_policy_destroy_cb destroy,
                firing_policy_copy_cb copy);
 
+/* Forward declaration. Every n */
+static bool lttng_firing_policy_every_n_should_execute(
+               const struct lttng_firing_policy *policy, uint64_t counter);
+
+/* Forward declaration. Once after N */
+static bool lttng_firing_policy_once_after_n_should_execute(
+               const struct lttng_firing_policy *policy, uint64_t counter);
+
 LTTNG_HIDDEN
 const char *lttng_firing_policy_type_string(
                enum lttng_firing_policy_type firing_policy_type)
@@ -285,7 +292,7 @@ ssize_t lttng_firing_policy_create_from_payload(struct lttng_payload_view *view,
                goto end;
        }
 
-       assert(*firing_policy);
+       LTTNG_ASSERT(*firing_policy);
 
        consumed_len = sizeof(struct lttng_firing_policy_comm) +
                        specific_firing_policy_consumed_len;
@@ -313,18 +320,35 @@ bool lttng_firing_policy_is_equal(const struct lttng_firing_policy *a,
                goto end;
        }
 
-       assert(a->equal);
+       LTTNG_ASSERT(a->equal);
        is_equal = a->equal(a, b);
 end:
        return is_equal;
 }
 
+LTTNG_HIDDEN
+bool lttng_firing_policy_should_execute(
+               const struct lttng_firing_policy *policy, uint64_t counter)
+{
+       switch (policy->type) {
+       case LTTNG_FIRING_POLICY_TYPE_EVERY_N:
+               return lttng_firing_policy_every_n_should_execute(
+                               policy, counter);
+       case LTTNG_FIRING_POLICY_TYPE_ONCE_AFTER_N:
+               return lttng_firing_policy_once_after_n_should_execute(
+                               policy, counter);
+       default:
+               abort();
+               break;
+       }
+}
+
 /* Every N */
 static const struct lttng_firing_policy_every_n *
 firing_policy_every_n_from_firing_policy_const(
                const struct lttng_firing_policy *policy)
 {
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        return container_of(policy, const struct lttng_firing_policy_every_n,
                        parent);
@@ -338,8 +362,8 @@ static int lttng_firing_policy_every_n_serialize(
        const struct lttng_firing_policy_every_n *every_n_policy;
        struct lttng_firing_policy_every_n_comm comm = {};
 
-       assert(policy);
-       assert(payload);
+       LTTNG_ASSERT(policy);
+       LTTNG_ASSERT(payload);
 
        every_n_policy = firing_policy_every_n_from_firing_policy_const(policy);
        comm.interval = every_n_policy->interval;
@@ -400,6 +424,13 @@ struct lttng_firing_policy *lttng_firing_policy_every_n_create(
 {
        struct lttng_firing_policy_every_n *policy = NULL;
 
+       if (interval == 0) {
+               /*
+                * An interval of 0 is invalid since it would never be fired.
+                */
+               goto end;
+       }
+
        policy = zmalloc(sizeof(struct lttng_firing_policy_every_n));
        if (!policy) {
                goto end;
@@ -438,13 +469,36 @@ end:
        return status;
 }
 
+static bool lttng_firing_policy_every_n_should_execute(
+               const struct lttng_firing_policy *policy, uint64_t counter)
+{
+       const struct lttng_firing_policy_every_n *every_n_policy;
+       LTTNG_ASSERT(policy);
+       bool execute = false;
+
+       every_n_policy = firing_policy_every_n_from_firing_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 const struct lttng_firing_policy_once_after_n *
 firing_policy_once_after_n_from_firing_policy_const(
                const struct lttng_firing_policy *policy)
 {
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        return container_of(policy, struct lttng_firing_policy_once_after_n,
                        parent);
@@ -458,8 +512,8 @@ static int lttng_firing_policy_once_after_n_serialize(
        const struct lttng_firing_policy_once_after_n *once_after_n_policy;
        struct lttng_firing_policy_once_after_n_comm comm = {};
 
-       assert(policy);
-       assert(payload);
+       LTTNG_ASSERT(policy);
+       LTTNG_ASSERT(payload);
 
        once_after_n_policy =
                        firing_policy_once_after_n_from_firing_policy_const(
@@ -524,6 +578,11 @@ struct lttng_firing_policy *lttng_firing_policy_once_after_n_create(
 {
        struct lttng_firing_policy_once_after_n *policy = NULL;
 
+       if (threshold == 0) {
+               /* threshold is expected to be > 0 */
+               goto end;
+       }
+
        policy = zmalloc(sizeof(struct lttng_firing_policy_once_after_n));
        if (!policy) {
                goto end;
@@ -568,6 +627,27 @@ LTTNG_HIDDEN
 struct lttng_firing_policy *lttng_firing_policy_copy(
                const struct lttng_firing_policy *source)
 {
-       assert(source->copy);
+       LTTNG_ASSERT(source->copy);
        return source->copy(source);
 }
+
+static bool lttng_firing_policy_once_after_n_should_execute(
+               const struct lttng_firing_policy *policy, uint64_t counter)
+{
+       const struct lttng_firing_policy_once_after_n *once_after_n_policy;
+       bool execute = false;
+       LTTNG_ASSERT(policy);
+
+       once_after_n_policy =
+                       firing_policy_once_after_n_from_firing_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;
+}
This page took 0.025226 seconds and 4 git commands to generate.