X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Fbuffer-usage.c;h=5f68608001a43d3559d527541c974e8edc0eef73;hp=49696a33f8e7883e9688df10807bada844778568;hb=fbc9f37df245d544a7705ba576297df791220b44;hpb=a58c490f0bff52a73717d31d04d1472629180de2 diff --git a/src/common/buffer-usage.c b/src/common/buffer-usage.c index 49696a33f..5f6860800 100644 --- a/src/common/buffer-usage.c +++ b/src/common/buffer-usage.c @@ -1,18 +1,8 @@ /* - * Copyright (C) 2017 - Jérémie Galarneau + * Copyright (C) 2017 Jérémie Galarneau * - * This library is free software; you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License, version 2.1 only, - * as published by the Free Software Foundation. + * SPDX-License-Identifier: LGPL-2.1-only * - * This library is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this library; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include @@ -88,6 +78,10 @@ bool lttng_condition_buffer_usage_validate( ERR("Invalid buffer condition: a threshold must be set."); goto end; } + if (!usage->domain.set) { + ERR("Invalid buffer usage condition: a domain must be set."); + goto end; + } valid = true; end: @@ -95,12 +89,14 @@ end: } static -ssize_t lttng_condition_buffer_usage_serialize( - const struct lttng_condition *condition, char *buf) +int lttng_condition_buffer_usage_serialize( + const struct lttng_condition *condition, + struct lttng_payload *payload) { + int ret; struct lttng_condition_buffer_usage *usage; - ssize_t ret, size; size_t session_name_len, channel_name_len; + struct lttng_condition_buffer_usage_comm usage_comm; if (!condition || !IS_USAGE_CONDITION(condition)) { ret = -1; @@ -110,7 +106,7 @@ ssize_t lttng_condition_buffer_usage_serialize( DBG("Serializing buffer usage condition"); usage = container_of(condition, struct lttng_condition_buffer_usage, parent); - size = sizeof(struct lttng_condition_buffer_usage_comm); + session_name_len = strlen(usage->session_name) + 1; channel_name_len = strlen(usage->channel_name) + 1; if (session_name_len > LTTNG_NAME_MAX || @@ -118,37 +114,43 @@ ssize_t lttng_condition_buffer_usage_serialize( ret = -1; goto end; } - size += session_name_len + channel_name_len; - if (buf) { - struct lttng_condition_buffer_usage_comm usage_comm = { - .threshold_set_in_bytes = usage->threshold_bytes.set ? 1 : 0, - .session_name_len = session_name_len, - .channel_name_len = channel_name_len, - .domain_type = (int8_t) usage->domain.type, - }; - - if (usage->threshold_bytes.set) { - usage_comm.threshold = 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_set_in_bytes = !!usage->threshold_bytes.set; + usage_comm.session_name_len = session_name_len; + usage_comm.channel_name_len = channel_name_len; + usage_comm.domain_type = (int8_t) usage->domain.type; + + if (usage->threshold_bytes.set) { + usage_comm.threshold = 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; + } + + ret = lttng_dynamic_buffer_append(&payload->buffer, &usage_comm, + sizeof(usage_comm)); + if (ret) { + goto end; + } - memcpy(buf, &usage_comm, sizeof(usage_comm)); - buf += sizeof(usage_comm); - memcpy(buf, usage->session_name, session_name_len); - buf += session_name_len; - memcpy(buf, usage->channel_name, channel_name_len); - buf += channel_name_len; + ret = lttng_dynamic_buffer_append(&payload->buffer, usage->session_name, + session_name_len); + if (ret) { + goto end; + } + + ret = lttng_dynamic_buffer_append(&payload->buffer, usage->channel_name, + channel_name_len); + if (ret) { + goto end; } - ret = size; end: return ret; } @@ -188,36 +190,24 @@ bool lttng_condition_buffer_usage_is_equal(const struct lttng_condition *_a, } } - if ((a->session_name && !b->session_name) || - (!a->session_name && b->session_name)) { + /* Condition is not valid if this is not true. */ + assert(a->session_name); + assert(b->session_name); + if (strcmp(a->session_name, b->session_name)) { goto end; } - if (a->channel_name && b->channel_name) { - if (strcmp(a->channel_name, b->channel_name)) { - goto end; - } - } if ((a->channel_name && !b->channel_name) || - (!a->channel_name && b->channel_name)) { + assert(a->channel_name); + assert(b->channel_name); + if (strcmp(a->channel_name, b->channel_name)) { goto end; } - if (a->channel_name && b->channel_name) { - if (strcmp(a->channel_name, b->channel_name)) { - goto end; - } - } - - if ((a->domain.set && !b->domain.set) || - (!a->domain.set && b->domain.set)) { + assert(a->domain.set); + assert(b->domain.set); + if (a->domain.type != b->domain.type) { goto end; } - - if (a->domain.set && b->domain.set) { - if (a->domain.type != b->domain.type) { - goto end; - } - } is_equal = true; end: return is_equal; @@ -231,7 +221,7 @@ struct lttng_condition *lttng_condition_buffer_usage_create( condition = zmalloc(sizeof(struct lttng_condition_buffer_usage)); if (!condition) { - goto end; + return NULL; } lttng_condition_init(&condition->parent, type); @@ -239,7 +229,6 @@ 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; -end: return &condition->parent; } @@ -256,24 +245,27 @@ struct lttng_condition *lttng_condition_buffer_usage_high_create(void) } static -ssize_t init_condition_from_buffer(struct lttng_condition *condition, - const struct lttng_buffer_view *src_view) +ssize_t init_condition_from_payload(struct lttng_condition *condition, + struct lttng_payload_view *src_view) { ssize_t ret, condition_size; enum lttng_condition_status status; enum lttng_domain_type domain_type; - const struct lttng_condition_buffer_usage_comm *condition_comm; const char *session_name, *channel_name; struct lttng_buffer_view names_view; + const struct lttng_condition_buffer_usage_comm *condition_comm; + const struct lttng_payload_view condition_comm_view = + lttng_payload_view_from_view( + src_view, 0, sizeof(*condition_comm)); - if (src_view->size < sizeof(*condition_comm)) { + if (!lttng_payload_view_is_valid(&condition_comm_view)) { ERR("Failed to initialize from malformed condition buffer: buffer too short to contain header"); ret = -1; goto end; } - condition_comm = (const struct lttng_condition_buffer_usage_comm *) src_view->data; - names_view = lttng_buffer_view_from_view(src_view, + condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data; + names_view = lttng_buffer_view_from_view(&src_view->buffer, sizeof(*condition_comm), -1); if (condition_comm->session_name_len > LTTNG_NAME_MAX || @@ -299,6 +291,7 @@ ssize_t init_condition_from_buffer(struct lttng_condition *condition, condition, fixed_to_double(condition_comm->threshold)); } + if (status != LTTNG_CONDITION_STATUS_OK) { ERR("Failed to initialize buffer usage condition threshold"); ret = -1; @@ -367,8 +360,8 @@ end: } LTTNG_HIDDEN -ssize_t lttng_condition_buffer_usage_low_create_from_buffer( - const struct lttng_buffer_view *view, +ssize_t lttng_condition_buffer_usage_low_create_from_payload( + struct lttng_payload_view *view, struct lttng_condition **_condition) { ssize_t ret; @@ -380,7 +373,7 @@ ssize_t lttng_condition_buffer_usage_low_create_from_buffer( goto error; } - ret = init_condition_from_buffer(condition, view); + ret = init_condition_from_payload(condition, view); if (ret < 0) { goto error; } @@ -393,8 +386,8 @@ error: } LTTNG_HIDDEN -ssize_t lttng_condition_buffer_usage_high_create_from_buffer( - const struct lttng_buffer_view *view, +ssize_t lttng_condition_buffer_usage_high_create_from_payload( + struct lttng_payload_view *view, struct lttng_condition **_condition) { ssize_t ret; @@ -406,7 +399,7 @@ ssize_t lttng_condition_buffer_usage_high_create_from_buffer( goto error; } - ret = init_condition_from_buffer(condition, view); + ret = init_condition_from_payload(condition, view); if (ret < 0) { goto error; } @@ -419,15 +412,15 @@ error: } static -struct lttng_evaluation *create_evaluation_from_buffer( +struct lttng_evaluation *create_evaluation_from_payload( enum lttng_condition_type type, - const struct lttng_buffer_view *view) + struct lttng_payload_view *view) { const struct lttng_evaluation_buffer_usage_comm *comm = - (const struct lttng_evaluation_buffer_usage_comm *) view->data; + (typeof(comm)) view->buffer.data; struct lttng_evaluation *evaluation = NULL; - if (view->size < sizeof(*comm)) { + if (view->buffer.size < sizeof(*comm)) { goto end; } @@ -438,8 +431,8 @@ end: } LTTNG_HIDDEN -ssize_t lttng_evaluation_buffer_usage_low_create_from_buffer( - const struct lttng_buffer_view *view, +ssize_t lttng_evaluation_buffer_usage_low_create_from_payload( + struct lttng_payload_view *view, struct lttng_evaluation **_evaluation) { ssize_t ret; @@ -450,7 +443,7 @@ ssize_t lttng_evaluation_buffer_usage_low_create_from_buffer( goto error; } - evaluation = create_evaluation_from_buffer( + evaluation = create_evaluation_from_payload( LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW, view); if (!evaluation) { ret = -1; @@ -466,8 +459,8 @@ error: } LTTNG_HIDDEN -ssize_t lttng_evaluation_buffer_usage_high_create_from_buffer( - const struct lttng_buffer_view *view, +ssize_t lttng_evaluation_buffer_usage_high_create_from_payload( + struct lttng_payload_view *view, struct lttng_evaluation **_evaluation) { ssize_t ret; @@ -478,7 +471,7 @@ ssize_t lttng_evaluation_buffer_usage_high_create_from_buffer( goto error; } - evaluation = create_evaluation_from_buffer( + evaluation = create_evaluation_from_payload( LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH, view); if (!evaluation) { ret = -1; @@ -741,25 +734,20 @@ end: } static -ssize_t lttng_evaluation_buffer_usage_serialize( - struct lttng_evaluation *evaluation, char *buf) +int lttng_evaluation_buffer_usage_serialize( + const struct lttng_evaluation *evaluation, + struct lttng_payload *payload) { - ssize_t ret; struct lttng_evaluation_buffer_usage *usage; + struct lttng_evaluation_buffer_usage_comm comm; usage = container_of(evaluation, struct lttng_evaluation_buffer_usage, parent); - if (buf) { - struct lttng_evaluation_buffer_usage_comm comm = { - .buffer_use = usage->buffer_use, - .buffer_capacity = usage->buffer_capacity, - }; - - memcpy(buf, &comm, sizeof(comm)); - } + comm.buffer_use = usage->buffer_use; + comm.buffer_capacity = usage->buffer_capacity; - ret = sizeof(struct lttng_evaluation_buffer_usage_comm); - return ret; + return lttng_dynamic_buffer_append( + &payload->buffer, &comm, sizeof(comm)); } static