d1990414c2fb759dd9de851306bf9db79c287fbe
[lttng-tools.git] / src / common / conditions / condition.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/condition/condition-internal.h>
9 #include <lttng/condition/buffer-usage-internal.h>
10 #include <lttng/condition/event-rule-internal.h>
11 #include <lttng/condition/session-consumed-size-internal.h>
12 #include <lttng/condition/session-rotation-internal.h>
13 #include <common/macros.h>
14 #include <common/error.h>
15 #include <common/dynamic-buffer.h>
16 #include <common/buffer-view.h>
17 #include <stdbool.h>
18 #include <assert.h>
19
20 enum lttng_condition_type lttng_condition_get_type(
21 const struct lttng_condition *condition)
22 {
23 return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN;
24 }
25
26 void lttng_condition_destroy(struct lttng_condition *condition)
27 {
28 lttng_condition_put(condition);
29 }
30
31 static void condition_destroy_ref(struct urcu_ref *ref)
32 {
33 struct lttng_condition *condition =
34 container_of(ref, struct lttng_condition, ref);
35
36 condition->destroy(condition);
37 }
38
39 LTTNG_HIDDEN
40 void lttng_condition_get(struct lttng_condition *condition)
41 {
42 urcu_ref_get(&condition->ref);
43 }
44
45 LTTNG_HIDDEN
46 void lttng_condition_put(struct lttng_condition *condition)
47 {
48 if (!condition) {
49 return;
50 }
51
52 assert(condition->destroy);
53 urcu_ref_put(&condition->ref, condition_destroy_ref);
54 }
55
56
57 LTTNG_HIDDEN
58 bool lttng_condition_validate(const struct lttng_condition *condition)
59 {
60 bool valid;
61
62 if (!condition) {
63 valid = false;
64 goto end;
65 }
66
67 if (!condition->validate) {
68 /* Sub-class guarantees that it can never be invalid. */
69 valid = true;
70 goto end;
71 }
72
73 valid = condition->validate(condition);
74 end:
75 return valid;
76 }
77
78 LTTNG_HIDDEN
79 int lttng_condition_serialize(const struct lttng_condition *condition,
80 struct lttng_payload *payload)
81 {
82 int ret;
83 struct lttng_condition_comm condition_comm = {};
84
85 if (!condition) {
86 ret = -1;
87 goto end;
88 }
89
90 condition_comm.condition_type = (int8_t) condition->type;
91
92 ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm,
93 sizeof(condition_comm));
94 if (ret) {
95 goto end;
96 }
97
98 ret = condition->serialize(condition, payload);
99 if (ret) {
100 goto end;
101 }
102 end:
103 return ret;
104 }
105
106 LTTNG_HIDDEN
107 bool lttng_condition_is_equal(const struct lttng_condition *a,
108 const struct lttng_condition *b)
109 {
110 bool is_equal = false;
111
112 if (!a || !b) {
113 goto end;
114 }
115
116 if (a->type != b->type) {
117 goto end;
118 }
119
120 if (a == b) {
121 is_equal = true;
122 goto end;
123 }
124
125 is_equal = a->equal ? a->equal(a, b) : true;
126 end:
127 return is_equal;
128 }
129
130 LTTNG_HIDDEN
131 ssize_t lttng_condition_create_from_payload(
132 struct lttng_payload_view *view,
133 struct lttng_condition **condition)
134 {
135 ssize_t ret, condition_size = 0;
136 condition_create_from_payload_cb create_from_payload = NULL;
137 const struct lttng_condition_comm *condition_comm;
138 const struct lttng_payload_view condition_comm_view =
139 lttng_payload_view_from_view(
140 view, 0, sizeof(*condition_comm));
141
142 if (!view || !condition) {
143 ret = -1;
144 goto end;
145 }
146
147 if (!lttng_payload_view_is_valid(&condition_comm_view)) {
148 /* Payload not large enough to contain the header. */
149 ret = -1;
150 goto end;
151 }
152
153 DBG("Deserializing condition from buffer");
154 condition_comm = (typeof(condition_comm)) condition_comm_view.buffer.data;
155 condition_size += sizeof(*condition_comm);
156
157 switch ((enum lttng_condition_type) condition_comm->condition_type) {
158 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
159 create_from_payload = lttng_condition_buffer_usage_low_create_from_payload;
160 break;
161 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
162 create_from_payload = lttng_condition_buffer_usage_high_create_from_payload;
163 break;
164 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
165 create_from_payload = lttng_condition_session_consumed_size_create_from_payload;
166 break;
167 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
168 create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload;
169 break;
170 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
171 create_from_payload = lttng_condition_session_rotation_completed_create_from_payload;
172 break;
173 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
174 create_from_payload = lttng_condition_event_rule_create_from_payload;
175 break;
176 default:
177 ERR("Attempted to create condition of unknown type (%i)",
178 (int) condition_comm->condition_type);
179 ret = -1;
180 goto end;
181 }
182
183 if (create_from_payload) {
184 struct lttng_payload_view condition_view =
185 lttng_payload_view_from_view(view,
186 sizeof(*condition_comm), -1);
187
188 ret = create_from_payload(&condition_view, condition);
189 if (ret < 0) {
190 goto end;
191 }
192 condition_size += ret;
193
194 } else {
195 abort();
196 }
197
198 ret = condition_size;
199 end:
200 return ret;
201 }
202
203 LTTNG_HIDDEN
204 void lttng_condition_init(struct lttng_condition *condition,
205 enum lttng_condition_type type)
206 {
207 condition->type = type;
208 urcu_ref_init(&condition->ref);
209 }
This page took 0.033751 seconds and 3 git commands to generate.