| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <lttng/condition/evaluation-internal.h> |
| 19 | #include <lttng/condition/buffer-usage-internal.h> |
| 20 | #include <lttng/condition/session-consumed-size-internal.h> |
| 21 | #include <common/macros.h> |
| 22 | #include <common/error.h> |
| 23 | #include <stdbool.h> |
| 24 | #include <assert.h> |
| 25 | |
| 26 | LTTNG_HIDDEN |
| 27 | ssize_t lttng_evaluation_serialize(struct lttng_evaluation *evaluation, |
| 28 | char *buf) |
| 29 | { |
| 30 | ssize_t ret, offset = 0; |
| 31 | struct lttng_evaluation_comm evaluation_comm = { |
| 32 | .type = (int8_t) evaluation->type |
| 33 | }; |
| 34 | |
| 35 | if (buf) { |
| 36 | memcpy(buf, &evaluation_comm, sizeof(evaluation_comm)); |
| 37 | } |
| 38 | offset += sizeof(evaluation_comm); |
| 39 | |
| 40 | if (evaluation->serialize) { |
| 41 | ret = evaluation->serialize(evaluation, |
| 42 | buf ? (buf + offset) : NULL); |
| 43 | if (ret < 0) { |
| 44 | goto end; |
| 45 | } |
| 46 | offset += ret; |
| 47 | } |
| 48 | |
| 49 | ret = offset; |
| 50 | end: |
| 51 | return ret; |
| 52 | } |
| 53 | |
| 54 | LTTNG_HIDDEN |
| 55 | ssize_t lttng_evaluation_create_from_buffer( |
| 56 | const struct lttng_buffer_view *src_view, |
| 57 | struct lttng_evaluation **evaluation) |
| 58 | { |
| 59 | ssize_t ret, evaluation_size = 0; |
| 60 | const struct lttng_evaluation_comm *evaluation_comm; |
| 61 | const struct lttng_buffer_view evaluation_view = |
| 62 | lttng_buffer_view_from_view(src_view, |
| 63 | sizeof(*evaluation_comm), -1); |
| 64 | |
| 65 | if (!src_view || !evaluation) { |
| 66 | ret = -1; |
| 67 | goto end; |
| 68 | } |
| 69 | |
| 70 | evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data; |
| 71 | evaluation_size += sizeof(*evaluation_comm); |
| 72 | |
| 73 | switch ((enum lttng_condition_type) evaluation_comm->type) { |
| 74 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 75 | ret = lttng_evaluation_buffer_usage_low_create_from_buffer( |
| 76 | &evaluation_view, evaluation); |
| 77 | if (ret < 0) { |
| 78 | goto end; |
| 79 | } |
| 80 | evaluation_size += ret; |
| 81 | break; |
| 82 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 83 | ret = lttng_evaluation_buffer_usage_high_create_from_buffer( |
| 84 | &evaluation_view, evaluation); |
| 85 | if (ret < 0) { |
| 86 | goto end; |
| 87 | } |
| 88 | evaluation_size += ret; |
| 89 | break; |
| 90 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
| 91 | ret = lttng_evaluation_session_consumed_size_create_from_buffer( |
| 92 | &evaluation_view, evaluation); |
| 93 | if (ret < 0) { |
| 94 | goto end; |
| 95 | } |
| 96 | evaluation_size += ret; |
| 97 | break; |
| 98 | default: |
| 99 | ERR("Attempted to create evaluation of unknown type (%i)", |
| 100 | (int) evaluation_comm->type); |
| 101 | ret = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | ret = evaluation_size; |
| 105 | end: |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | enum lttng_condition_type lttng_evaluation_get_type( |
| 110 | const struct lttng_evaluation *evaluation) |
| 111 | { |
| 112 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 113 | } |
| 114 | |
| 115 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) |
| 116 | { |
| 117 | if (!evaluation) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | assert(evaluation->destroy); |
| 122 | evaluation->destroy(evaluation); |
| 123 | } |