| 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 <common/macros.h> |
| 21 | #include <common/error.h> |
| 22 | #include <stdbool.h> |
| 23 | #include <assert.h> |
| 24 | |
| 25 | LTTNG_HIDDEN |
| 26 | ssize_t lttng_evaluation_serialize(struct lttng_evaluation *evaluation, |
| 27 | char *buf) |
| 28 | { |
| 29 | ssize_t ret, offset = 0; |
| 30 | struct lttng_evaluation_comm evaluation_comm = { |
| 31 | .type = (int8_t) evaluation->type |
| 32 | }; |
| 33 | |
| 34 | if (buf) { |
| 35 | memcpy(buf, &evaluation_comm, sizeof(evaluation_comm)); |
| 36 | } |
| 37 | offset += sizeof(evaluation_comm); |
| 38 | |
| 39 | if (evaluation->serialize) { |
| 40 | ret = evaluation->serialize(evaluation, |
| 41 | buf ? (buf + offset) : NULL); |
| 42 | if (ret < 0) { |
| 43 | goto end; |
| 44 | } |
| 45 | offset += ret; |
| 46 | } |
| 47 | |
| 48 | ret = offset; |
| 49 | end: |
| 50 | return ret; |
| 51 | } |
| 52 | |
| 53 | LTTNG_HIDDEN |
| 54 | ssize_t lttng_evaluation_create_from_buffer( |
| 55 | const struct lttng_buffer_view *src_view, |
| 56 | struct lttng_evaluation **evaluation) |
| 57 | { |
| 58 | ssize_t ret, evaluation_size = 0; |
| 59 | const struct lttng_evaluation_comm *evaluation_comm; |
| 60 | const struct lttng_buffer_view evaluation_view = |
| 61 | lttng_buffer_view_from_view(src_view, |
| 62 | sizeof(*evaluation_comm), -1); |
| 63 | |
| 64 | if (!src_view || !evaluation) { |
| 65 | ret = -1; |
| 66 | goto end; |
| 67 | } |
| 68 | |
| 69 | evaluation_comm = (const struct lttng_evaluation_comm *) src_view->data; |
| 70 | evaluation_size += sizeof(*evaluation_comm); |
| 71 | |
| 72 | switch ((enum lttng_condition_type) evaluation_comm->type) { |
| 73 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: |
| 74 | ret = lttng_evaluation_buffer_usage_low_create_from_buffer( |
| 75 | &evaluation_view, evaluation); |
| 76 | if (ret < 0) { |
| 77 | goto end; |
| 78 | } |
| 79 | evaluation_size += ret; |
| 80 | break; |
| 81 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: |
| 82 | ret = lttng_evaluation_buffer_usage_high_create_from_buffer( |
| 83 | &evaluation_view, evaluation); |
| 84 | if (ret < 0) { |
| 85 | goto end; |
| 86 | } |
| 87 | evaluation_size += ret; |
| 88 | break; |
| 89 | default: |
| 90 | ERR("Attempted to create evaluation of unknown type (%i)", |
| 91 | (int) evaluation_comm->type); |
| 92 | ret = -1; |
| 93 | goto end; |
| 94 | } |
| 95 | ret = evaluation_size; |
| 96 | end: |
| 97 | return ret; |
| 98 | } |
| 99 | |
| 100 | enum lttng_condition_type lttng_evaluation_get_type( |
| 101 | const struct lttng_evaluation *evaluation) |
| 102 | { |
| 103 | return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN; |
| 104 | } |
| 105 | |
| 106 | void lttng_evaluation_destroy(struct lttng_evaluation *evaluation) |
| 107 | { |
| 108 | if (!evaluation) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | assert(evaluation->destroy); |
| 113 | evaluation->destroy(evaluation); |
| 114 | } |