Force usage of assert() condition when NDEBUG is defined
[lttng-tools.git] / src / common / actions / rate-policy.c
index 244fb70e3ff03723bd2463875781f59086a3b266..bb1366eab8f68b6e869e1da2306df676c89a1cbb 100644 (file)
@@ -5,11 +5,11 @@
  *
  */
 
-#include <assert.h>
 #include <common/buffer-view.h>
 #include <common/dynamic-buffer.h>
 #include <common/error.h>
 #include <common/macros.h>
+#include <common/mi-lttng.h>
 #include <common/payload-view.h>
 #include <common/payload.h>
 #include <limits.h>
@@ -35,6 +35,9 @@ typedef ssize_t (*rate_policy_create_from_payload_cb)(
                struct lttng_rate_policy **rate_policy);
 typedef struct lttng_rate_policy *(*rate_policy_copy_cb)(
                const struct lttng_rate_policy *source);
+typedef enum lttng_error_code (*rate_policy_mi_serialize_cb)(
+               const struct lttng_rate_policy *rate_policy,
+               struct mi_writer *writer);
 
 struct lttng_rate_policy {
        enum lttng_rate_policy_type type;
@@ -42,6 +45,7 @@ struct lttng_rate_policy {
        rate_policy_equal_cb equal;
        rate_policy_destroy_cb destroy;
        rate_policy_copy_cb copy;
+       rate_policy_mi_serialize_cb mi_serialize;
 };
 
 struct lttng_rate_policy_every_n {
@@ -73,7 +77,8 @@ static void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy,
                rate_policy_serialize_cb serialize,
                rate_policy_equal_cb equal,
                rate_policy_destroy_cb destroy,
-               rate_policy_copy_cb copy);
+               rate_policy_copy_cb copy,
+               rate_policy_mi_serialize_cb mi);
 
 /* Forward declaration. Every n */
 static bool lttng_rate_policy_every_n_should_execute(
@@ -109,13 +114,15 @@ void lttng_rate_policy_init(struct lttng_rate_policy *rate_policy,
                rate_policy_serialize_cb serialize,
                rate_policy_equal_cb equal,
                rate_policy_destroy_cb destroy,
-               rate_policy_copy_cb copy)
+               rate_policy_copy_cb copy,
+               rate_policy_mi_serialize_cb mi)
 {
        rate_policy->type = type;
        rate_policy->serialize = serialize;
        rate_policy->equal = equal;
        rate_policy->destroy = destroy;
        rate_policy->copy = copy;
+       rate_policy->mi_serialize = mi;
 }
 
 void lttng_rate_policy_destroy(struct lttng_rate_policy *rate_policy)
@@ -288,7 +295,7 @@ ssize_t lttng_rate_policy_create_from_payload(struct lttng_payload_view *view,
                goto end;
        }
 
-       assert(*rate_policy);
+       LTTNG_ASSERT(*rate_policy);
 
        consumed_len = sizeof(struct lttng_rate_policy_comm) +
                        specific_rate_policy_consumed_len;
@@ -316,7 +323,7 @@ bool lttng_rate_policy_is_equal(const struct lttng_rate_policy *a,
                goto end;
        }
 
-       assert(a->equal);
+       LTTNG_ASSERT(a->equal);
        is_equal = a->equal(a, b);
 end:
        return is_equal;
@@ -343,7 +350,7 @@ bool lttng_rate_policy_should_execute(
 static struct lttng_rate_policy_every_n *rate_policy_every_n_from_rate_policy(
                struct lttng_rate_policy *policy)
 {
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        return container_of(policy, struct lttng_rate_policy_every_n, parent);
 }
@@ -352,7 +359,7 @@ static const struct lttng_rate_policy_every_n *
 rate_policy_every_n_from_rate_policy_const(
                const struct lttng_rate_policy *policy)
 {
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        return container_of(policy, struct lttng_rate_policy_every_n, parent);
 }
@@ -365,8 +372,8 @@ static int lttng_rate_policy_every_n_serialize(
        struct lttng_rate_policy_every_n *every_n_policy;
        struct lttng_rate_policy_every_n_comm comm = {};
 
-       assert(policy);
-       assert(payload);
+       LTTNG_ASSERT(policy);
+       LTTNG_ASSERT(payload);
 
        every_n_policy = rate_policy_every_n_from_rate_policy(policy);
        comm.interval = every_n_policy->interval;
@@ -429,6 +436,51 @@ end:
        return copy;
 }
 
+static enum lttng_error_code lttng_rate_policy_every_n_mi_serialize(
+               const struct lttng_rate_policy *rate_policy,
+               struct mi_writer *writer)
+{
+       int ret;
+       enum lttng_error_code ret_code;
+       const struct lttng_rate_policy_every_n *every_n_policy = NULL;
+
+       LTTNG_ASSERT(rate_policy);
+       LTTNG_ASSERT(IS_EVERY_N_RATE_POLICY(rate_policy));
+       LTTNG_ASSERT(writer);
+
+       every_n_policy = rate_policy_every_n_from_rate_policy_const(
+                       rate_policy);
+
+       /* Open rate_policy_every_n element. */
+       ret = mi_lttng_writer_open_element(
+                       writer, mi_lttng_element_rate_policy_every_n);
+       if (ret) {
+               goto mi_error;
+       }
+
+       /* Interval. */
+       ret = mi_lttng_writer_write_element_unsigned_int(writer,
+                       mi_lttng_element_rate_policy_every_n_interval,
+                       every_n_policy->interval);
+       if (ret) {
+               goto mi_error;
+       }
+
+       /* Close rate_policy_every_n element. */
+       ret = mi_lttng_writer_close_element(writer);
+       if (ret) {
+               goto mi_error;
+       }
+
+       ret_code = LTTNG_OK;
+       goto end;
+
+mi_error:
+       ret_code = LTTNG_ERR_MI_IO_FAIL;
+end:
+       return ret_code;
+}
+
 struct lttng_rate_policy *lttng_rate_policy_every_n_create(uint64_t interval)
 {
        struct lttng_rate_policy_every_n *policy = NULL;
@@ -450,7 +502,8 @@ struct lttng_rate_policy *lttng_rate_policy_every_n_create(uint64_t interval)
                        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);
+                       lttng_rate_policy_every_n_copy,
+                       lttng_rate_policy_every_n_mi_serialize);
 
        policy->interval = interval;
 
@@ -462,7 +515,6 @@ end:
        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)
 {
@@ -486,7 +538,7 @@ 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);
+       LTTNG_ASSERT(policy);
        bool execute = false;
 
        every_n_policy = rate_policy_every_n_from_rate_policy_const(policy);
@@ -510,7 +562,7 @@ static bool lttng_rate_policy_every_n_should_execute(
 static struct lttng_rate_policy_once_after_n *
 rate_policy_once_after_n_from_rate_policy(struct lttng_rate_policy *policy)
 {
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        return container_of(
                        policy, struct lttng_rate_policy_once_after_n, parent);
@@ -520,7 +572,7 @@ 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);
+       LTTNG_ASSERT(policy);
 
        return container_of(
                        policy, struct lttng_rate_policy_once_after_n, parent);
@@ -533,8 +585,8 @@ static int lttng_rate_policy_once_after_n_serialize(
        struct lttng_rate_policy_once_after_n *once_after_n_policy;
        struct lttng_rate_policy_once_after_n_comm comm = {};
 
-       assert(policy);
-       assert(payload);
+       LTTNG_ASSERT(policy);
+       LTTNG_ASSERT(payload);
 
        once_after_n_policy = rate_policy_once_after_n_from_rate_policy(policy);
        comm.threshold = once_after_n_policy->threshold;
@@ -600,6 +652,51 @@ end:
        return copy;
 }
 
+static enum lttng_error_code lttng_rate_policy_once_after_n_mi_serialize(
+               const struct lttng_rate_policy *rate_policy,
+               struct mi_writer *writer)
+{
+       int ret;
+       enum lttng_error_code ret_code;
+       const struct lttng_rate_policy_once_after_n *once_after_n_policy = NULL;
+
+       LTTNG_ASSERT(rate_policy);
+       LTTNG_ASSERT(IS_ONCE_AFTER_N_RATE_POLICY(rate_policy));
+       LTTNG_ASSERT(writer);
+
+       once_after_n_policy = rate_policy_once_after_n_from_rate_policy_const(
+                       rate_policy);
+
+       /* Open rate_policy_once_after_n. */
+       ret = mi_lttng_writer_open_element(
+                       writer, mi_lttng_element_rate_policy_once_after_n);
+       if (ret) {
+               goto mi_error;
+       }
+
+       /* Threshold. */
+       ret = mi_lttng_writer_write_element_unsigned_int(writer,
+                       mi_lttng_element_rate_policy_once_after_n_threshold,
+                       once_after_n_policy->threshold);
+       if (ret) {
+               goto mi_error;
+       }
+
+       /* Close rate_policy_once_after_n element. */
+       ret = mi_lttng_writer_close_element(writer);
+       if (ret) {
+               goto mi_error;
+       }
+
+       ret_code = LTTNG_OK;
+       goto end;
+
+mi_error:
+       ret_code = LTTNG_ERR_MI_IO_FAIL;
+end:
+       return ret_code;
+}
+
 struct lttng_rate_policy *lttng_rate_policy_once_after_n_create(
                uint64_t threshold)
 {
@@ -621,7 +718,8 @@ struct lttng_rate_policy *lttng_rate_policy_once_after_n_create(
                        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);
+                       lttng_rate_policy_once_after_n_copy,
+                       lttng_rate_policy_once_after_n_mi_serialize);
 
        policy->threshold = threshold;
 
@@ -633,7 +731,6 @@ end:
        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)
 {
@@ -658,7 +755,7 @@ LTTNG_HIDDEN
 struct lttng_rate_policy *lttng_rate_policy_copy(
                const struct lttng_rate_policy *source)
 {
-       assert(source->copy);
+       LTTNG_ASSERT(source->copy);
        return source->copy(source);
 }
 
@@ -667,7 +764,7 @@ static bool lttng_rate_policy_once_after_n_should_execute(
 {
        const struct lttng_rate_policy_once_after_n *once_after_n_policy;
        bool execute = false;
-       assert(policy);
+       LTTNG_ASSERT(policy);
 
        once_after_n_policy =
                        rate_policy_once_after_n_from_rate_policy_const(policy);
@@ -681,3 +778,43 @@ static bool lttng_rate_policy_once_after_n_should_execute(
 
        return counter == once_after_n_policy->threshold;
 }
+
+LTTNG_HIDDEN
+enum lttng_error_code lttng_rate_policy_mi_serialize(
+               const struct lttng_rate_policy *rate_policy,
+               struct mi_writer *writer)
+{
+       int ret;
+       enum lttng_error_code ret_code;
+
+       LTTNG_ASSERT(rate_policy);
+       LTTNG_ASSERT(writer);
+       LTTNG_ASSERT(rate_policy->mi_serialize);
+
+       /* Open rate policy element. */
+       ret = mi_lttng_writer_open_element(
+                       writer, mi_lttng_element_rate_policy);
+       if (ret) {
+               goto mi_error;
+       }
+
+       /* Serialize underlying rate policy. */
+       ret_code = rate_policy->mi_serialize(rate_policy, writer);
+       if (ret_code != LTTNG_OK) {
+               goto end;
+       }
+
+       /* Close rate policy element. */
+       ret = mi_lttng_writer_close_element(writer);
+       if (ret) {
+               goto mi_error;
+       }
+
+       ret_code = LTTNG_OK;
+       goto end;
+
+mi_error:
+       ret_code = LTTNG_ERR_MI_IO_FAIL;
+end:
+       return ret_code;
+}
This page took 0.027351 seconds and 4 git commands to generate.