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