Rename on-event to event-rule-matches
[lttng-tools.git] / src / common / evaluation.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
7c920b63 8#include <lttng/condition/condition-internal.h>
a58c490f
JG
9#include <lttng/condition/evaluation-internal.h>
10#include <lttng/condition/buffer-usage-internal.h>
e8360425 11#include <lttng/condition/session-consumed-size-internal.h>
c19092cd 12#include <lttng/condition/session-rotation-internal.h>
670a26e4 13#include <lttng/condition/event-rule-matches-internal.h>
a58c490f
JG
14#include <common/macros.h>
15#include <common/error.h>
16#include <stdbool.h>
17#include <assert.h>
18
c19092cd
JG
19LTTNG_HIDDEN
20void lttng_evaluation_init(struct lttng_evaluation *evaluation,
21 enum lttng_condition_type type)
22{
23 evaluation->type = type;
24}
25
a58c490f 26LTTNG_HIDDEN
9b63a4aa 27int lttng_evaluation_serialize(const struct lttng_evaluation *evaluation,
c0a66c84 28 struct lttng_payload *payload)
a58c490f 29{
3647288f 30 int ret;
db315d75
JG
31 struct lttng_evaluation_comm evaluation_comm = {
32 .type = (int8_t) evaluation->type
33 };
a58c490f 34
c0a66c84 35 ret = lttng_dynamic_buffer_append(&payload->buffer, &evaluation_comm,
3647288f
JG
36 sizeof(evaluation_comm));
37 if (ret) {
38 goto end;
a58c490f 39 }
a58c490f
JG
40
41 if (evaluation->serialize) {
c0a66c84 42 ret = evaluation->serialize(evaluation, payload);
3647288f 43 if (ret) {
a58c490f
JG
44 goto end;
45 }
a58c490f 46 }
a58c490f
JG
47end:
48 return ret;
49}
50
51LTTNG_HIDDEN
c0a66c84 52ssize_t lttng_evaluation_create_from_payload(
7c920b63 53 const struct lttng_condition *condition,
c0a66c84 54 struct lttng_payload_view *src_view,
a58c490f
JG
55 struct lttng_evaluation **evaluation)
56{
57 ssize_t ret, evaluation_size = 0;
58 const struct lttng_evaluation_comm *evaluation_comm;
3e6e0df2
JG
59 struct lttng_payload_view evaluation_comm_view =
60 lttng_payload_view_from_view(
61 src_view, 0, sizeof(*evaluation_comm));
62 struct lttng_payload_view evaluation_view =
2f571d6f 63 lttng_payload_view_from_view(src_view,
3e6e0df2 64 sizeof(*evaluation_comm), -1);
a58c490f
JG
65
66 if (!src_view || !evaluation) {
67 ret = -1;
68 goto end;
69 }
70
3e6e0df2
JG
71 if (!lttng_payload_view_is_valid(&evaluation_comm_view)) {
72 ret = -1;
73 goto end;
74 }
75
76 evaluation_comm = (typeof(evaluation_comm)) evaluation_comm_view.buffer.data;
a58c490f
JG
77 evaluation_size += sizeof(*evaluation_comm);
78
79 switch ((enum lttng_condition_type) evaluation_comm->type) {
80 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
c0a66c84 81 ret = lttng_evaluation_buffer_usage_low_create_from_payload(
a58c490f
JG
82 &evaluation_view, evaluation);
83 if (ret < 0) {
84 goto end;
85 }
86 evaluation_size += ret;
87 break;
88 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
c0a66c84 89 ret = lttng_evaluation_buffer_usage_high_create_from_payload(
a58c490f
JG
90 &evaluation_view, evaluation);
91 if (ret < 0) {
92 goto end;
93 }
94 evaluation_size += ret;
95 break;
e8360425 96 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
c0a66c84 97 ret = lttng_evaluation_session_consumed_size_create_from_payload(
e8360425
JD
98 &evaluation_view, evaluation);
99 if (ret < 0) {
100 goto end;
101 }
102 evaluation_size += ret;
103 break;
c19092cd 104 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
c0a66c84 105 ret = lttng_evaluation_session_rotation_ongoing_create_from_payload(
c19092cd
JG
106 &evaluation_view, evaluation);
107 if (ret < 0) {
108 goto end;
109 }
110 evaluation_size += ret;
111 break;
112 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
c0a66c84 113 ret = lttng_evaluation_session_rotation_completed_create_from_payload(
c19092cd
JG
114 &evaluation_view, evaluation);
115 if (ret < 0) {
116 goto end;
117 }
118 evaluation_size += ret;
119 break;
8dbb86b8 120 case LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES:
7c920b63 121 assert(condition);
8dbb86b8
JR
122 assert(condition->type ==
123 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
124 ret = lttng_evaluation_event_rule_matches_create_from_payload(
7c920b63 125 container_of(condition,
8dbb86b8 126 const struct lttng_condition_event_rule_matches,
7c920b63
PP
127 parent),
128 &evaluation_view, evaluation);
f1446131
JR
129 if (ret < 0) {
130 goto end;
131 }
132 evaluation_size += ret;
133 break;
a58c490f
JG
134 default:
135 ERR("Attempted to create evaluation of unknown type (%i)",
136 (int) evaluation_comm->type);
137 ret = -1;
138 goto end;
139 }
c0a66c84 140
a58c490f
JG
141 ret = evaluation_size;
142end:
143 return ret;
144}
145
146enum lttng_condition_type lttng_evaluation_get_type(
147 const struct lttng_evaluation *evaluation)
148{
149 return evaluation ? evaluation->type : LTTNG_CONDITION_TYPE_UNKNOWN;
150}
151
152void lttng_evaluation_destroy(struct lttng_evaluation *evaluation)
153{
154 if (!evaluation) {
155 return;
156 }
157
158 assert(evaluation->destroy);
159 evaluation->destroy(evaluation);
160}
This page took 0.043407 seconds and 4 git commands to generate.