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