X-Git-Url: https://git.lttng.org/?a=blobdiff_plain;f=src%2Fcommon%2Fconditions%2Fbuffer-usage.c;h=714ba582c412394e6e8bfbf691f6df81ef904f26;hb=ca806b0b247f89c62ac628a7779ae84049a8c2d7;hp=5f68608001a43d3559d527541c974e8edc0eef73;hpb=ae20d1bd206b317393846accdc45082d7c37b571;p=lttng-tools.git diff --git a/src/common/conditions/buffer-usage.c b/src/common/conditions/buffer-usage.c index 5f6860800..714ba582c 100644 --- a/src/common/conditions/buffer-usage.c +++ b/src/common/conditions/buffer-usage.c @@ -5,13 +5,13 @@ * */ -#include -#include -#include #include -#include -#include +#include +#include #include +#include +#include +#include #include #define IS_USAGE_CONDITION(condition) ( \ @@ -19,18 +19,6 @@ lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH \ ) -static -double fixed_to_double(uint32_t val) -{ - return (double) val / (double) UINT32_MAX; -} - -static -uint64_t double_to_fixed(double val) -{ - return (val * (double) UINT32_MAX); -} - static bool is_usage_evaluation(const struct lttng_evaluation *evaluation) { @@ -74,8 +62,8 @@ bool lttng_condition_buffer_usage_validate( ERR("Invalid buffer condition: a target channel name must be set."); goto end; } - if (!usage->threshold_ratio.set && !usage->threshold_bytes.set) { - ERR("Invalid buffer condition: a threshold must be set."); + if (usage->threshold_ratio.set == usage->threshold_bytes.set) { + ERR("Invalid buffer condition: a threshold must be set or both type cannot be used simultaneously."); goto end; } if (!usage->domain.set) { @@ -96,7 +84,7 @@ int lttng_condition_buffer_usage_serialize( int ret; struct lttng_condition_buffer_usage *usage; size_t session_name_len, channel_name_len; - struct lttng_condition_buffer_usage_comm usage_comm; + struct lttng_condition_buffer_usage_comm usage_comm = {}; if (!condition || !IS_USAGE_CONDITION(condition)) { ret = -1; @@ -121,17 +109,9 @@ int lttng_condition_buffer_usage_serialize( usage_comm.domain_type = (int8_t) usage->domain.type; if (usage->threshold_bytes.set) { - usage_comm.threshold = usage->threshold_bytes.value; + usage_comm.threshold_bytes = usage->threshold_bytes.value; } else { - uint64_t val = double_to_fixed( - usage->threshold_ratio.value); - - if (val > UINT32_MAX) { - /* overflow. */ - ret = -1; - goto end; - } - usage_comm.threshold = val; + usage_comm.threshold_ratio = usage->threshold_ratio.value; } ret = lttng_dynamic_buffer_append(&payload->buffer, &usage_comm, @@ -191,20 +171,20 @@ bool lttng_condition_buffer_usage_is_equal(const struct lttng_condition *_a, } /* Condition is not valid if this is not true. */ - assert(a->session_name); - assert(b->session_name); + LTTNG_ASSERT(a->session_name); + LTTNG_ASSERT(b->session_name); if (strcmp(a->session_name, b->session_name)) { goto end; } - assert(a->channel_name); - assert(b->channel_name); + LTTNG_ASSERT(a->channel_name); + LTTNG_ASSERT(b->channel_name); if (strcmp(a->channel_name, b->channel_name)) { goto end; } - assert(a->domain.set); - assert(b->domain.set); + LTTNG_ASSERT(a->domain.set); + LTTNG_ASSERT(b->domain.set); if (a->domain.type != b->domain.type) { goto end; } @@ -213,6 +193,128 @@ end: return is_equal; } +static enum lttng_error_code lttng_condition_buffer_usage_mi_serialize( + const struct lttng_condition *condition, + struct mi_writer *writer) +{ + int ret; + enum lttng_error_code ret_code; + enum lttng_condition_status status; + const char *session_name = NULL, *channel_name = NULL; + enum lttng_domain_type domain_type; + bool is_threshold_bytes = false; + double threshold_ratio; + uint64_t threshold_bytes; + const char *condition_type_str = NULL; + + LTTNG_ASSERT(condition); + LTTNG_ASSERT(IS_USAGE_CONDITION(condition)); + + status = lttng_condition_buffer_usage_get_session_name( + condition, &session_name); + LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); + LTTNG_ASSERT(session_name); + + status = lttng_condition_buffer_usage_get_channel_name( + condition, &channel_name); + LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); + LTTNG_ASSERT(session_name); + + status = lttng_condition_buffer_usage_get_domain_type( + condition, &domain_type); + LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); + + status = lttng_condition_buffer_usage_get_threshold( + condition, &threshold_bytes); + if (status == LTTNG_CONDITION_STATUS_OK) { + is_threshold_bytes = true; + } else if (status != LTTNG_CONDITION_STATUS_UNSET) { + /* Unexpected at this stage. */ + ret_code = LTTNG_ERR_INVALID; + goto end; + } + + if (!is_threshold_bytes) { + status = lttng_condition_buffer_usage_get_threshold_ratio( + condition, &threshold_ratio); + LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK); + } + + switch (lttng_condition_get_type(condition)) { + case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: + condition_type_str = + mi_lttng_element_condition_buffer_usage_high; + break; + case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: + condition_type_str = + mi_lttng_element_condition_buffer_usage_low; + break; + default: + abort(); + break; + } + + /* Open the sub type condition element. */ + ret = mi_lttng_writer_open_element(writer, condition_type_str); + if (ret) { + goto mi_error; + } + + /* Session name. */ + ret = mi_lttng_writer_write_element_string( + writer, mi_lttng_element_session_name, session_name); + if (ret) { + goto mi_error; + } + + /* Channel name. */ + ret = mi_lttng_writer_write_element_string(writer, + mi_lttng_element_condition_channel_name, channel_name); + if (ret) { + goto mi_error; + } + + /* Domain. */ + ret = mi_lttng_writer_write_element_string(writer, + config_element_domain, + mi_lttng_domaintype_string(domain_type)); + if (ret) { + goto mi_error; + } + + if (is_threshold_bytes) { + /* Usage in bytes. */ + ret = mi_lttng_writer_write_element_unsigned_int(writer, + mi_lttng_element_condition_threshold_bytes, + threshold_bytes); + if (ret) { + goto mi_error; + } + } else { + /* Ratio. */ + ret = mi_lttng_writer_write_element_double(writer, + mi_lttng_element_condition_threshold_ratio, + threshold_ratio); + if (ret) { + goto mi_error; + } + } + + /* Closing sub type condition 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; +} + static struct lttng_condition *lttng_condition_buffer_usage_create( enum lttng_condition_type type) @@ -229,6 +331,7 @@ struct lttng_condition *lttng_condition_buffer_usage_create( condition->parent.serialize = lttng_condition_buffer_usage_serialize; condition->parent.equal = lttng_condition_buffer_usage_is_equal; condition->parent.destroy = lttng_condition_buffer_usage_destroy; + condition->parent.mi_serialize = lttng_condition_buffer_usage_mi_serialize; return &condition->parent; } @@ -285,11 +388,10 @@ ssize_t init_condition_from_payload(struct lttng_condition *condition, if (condition_comm->threshold_set_in_bytes) { status = lttng_condition_buffer_usage_set_threshold(condition, - condition_comm->threshold); + condition_comm->threshold_bytes); } else { status = lttng_condition_buffer_usage_set_threshold_ratio( - condition, - fixed_to_double(condition_comm->threshold)); + condition, condition_comm->threshold_ratio); } if (status != LTTNG_CONDITION_STATUS_OK) { @@ -359,7 +461,6 @@ end: return ret; } -LTTNG_HIDDEN ssize_t lttng_condition_buffer_usage_low_create_from_payload( struct lttng_payload_view *view, struct lttng_condition **_condition) @@ -385,7 +486,6 @@ error: return ret; } -LTTNG_HIDDEN ssize_t lttng_condition_buffer_usage_high_create_from_payload( struct lttng_payload_view *view, struct lttng_condition **_condition) @@ -430,7 +530,6 @@ end: return evaluation; } -LTTNG_HIDDEN ssize_t lttng_evaluation_buffer_usage_low_create_from_payload( struct lttng_payload_view *view, struct lttng_evaluation **_evaluation) @@ -458,7 +557,6 @@ error: return ret; } -LTTNG_HIDDEN ssize_t lttng_evaluation_buffer_usage_high_create_from_payload( struct lttng_payload_view *view, struct lttng_evaluation **_evaluation) @@ -761,7 +859,6 @@ void lttng_evaluation_buffer_usage_destroy( free(usage); } -LTTNG_HIDDEN struct lttng_evaluation *lttng_evaluation_buffer_usage_create( enum lttng_condition_type type, uint64_t use, uint64_t capacity) {