SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / common / conditions / condition.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/condition-internal.h>
9#include <lttng/condition/buffer-usage-internal.h>
1831ae68 10#include <lttng/condition/event-rule-internal.h>
e8360425 11#include <lttng/condition/session-consumed-size-internal.h>
c19092cd 12#include <lttng/condition/session-rotation-internal.h>
a58c490f
JG
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
20enum 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
26void lttng_condition_destroy(struct lttng_condition *condition)
27{
28 if (!condition) {
29 return;
30 }
31
32 assert(condition->destroy);
33 condition->destroy(condition);
34}
35
36LTTNG_HIDDEN
37bool lttng_condition_validate(const struct lttng_condition *condition)
38{
39 bool valid;
40
41 if (!condition) {
42 valid = false;
43 goto end;
44 }
45
46 if (!condition->validate) {
47 /* Sub-class guarantees that it can never be invalid. */
48 valid = true;
49 goto end;
50 }
51
52 valid = condition->validate(condition);
53end:
54 return valid;
55}
56
57LTTNG_HIDDEN
3647288f 58int lttng_condition_serialize(const struct lttng_condition *condition,
1831ae68
FD
59 struct lttng_dynamic_buffer *buf,
60 int *fd_to_send)
a58c490f 61{
3647288f
JG
62 int ret;
63 struct lttng_condition_comm condition_comm = { 0 };
a58c490f
JG
64
65 if (!condition) {
66 ret = -1;
67 goto end;
68 }
69
3647288f
JG
70 condition_comm.condition_type = (int8_t) condition->type;
71
72 ret = lttng_dynamic_buffer_append(buf, &condition_comm,
73 sizeof(condition_comm));
74 if (ret) {
75 goto end;
a58c490f
JG
76 }
77
1831ae68 78 ret = condition->serialize(condition, buf, fd_to_send);
3647288f 79 if (ret) {
a58c490f
JG
80 goto end;
81 }
a58c490f
JG
82end:
83 return ret;
84}
85
86LTTNG_HIDDEN
87bool lttng_condition_is_equal(const struct lttng_condition *a,
88 const struct lttng_condition *b)
89{
90 bool is_equal = false;
91
92 if (!a || !b) {
93 goto end;
94 }
95
96 if (a->type != b->type) {
97 goto end;
98 }
99
6c2fe319
JG
100 if (a == b) {
101 is_equal = true;
102 goto end;
103 }
104
a58c490f
JG
105 is_equal = a->equal ? a->equal(a, b) : true;
106end:
107 return is_equal;
108}
109
110LTTNG_HIDDEN
111ssize_t lttng_condition_create_from_buffer(
112 const struct lttng_buffer_view *buffer,
113 struct lttng_condition **condition)
114{
115 ssize_t ret, condition_size = 0;
116 const struct lttng_condition_comm *condition_comm;
117 condition_create_from_buffer_cb create_from_buffer = NULL;
118
119 if (!buffer || !condition) {
120 ret = -1;
121 goto end;
122 }
123
124 DBG("Deserializing condition from buffer");
125 condition_comm = (const struct lttng_condition_comm *) buffer->data;
126 condition_size += sizeof(*condition_comm);
127
128 switch ((enum lttng_condition_type) condition_comm->condition_type) {
129 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
130 create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer;
131 break;
132 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
133 create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer;
134 break;
e8360425
JD
135 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
136 create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer;
137 break;
c19092cd
JG
138 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
139 create_from_buffer = lttng_condition_session_rotation_ongoing_create_from_buffer;
140 break;
141 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
142 create_from_buffer = lttng_condition_session_rotation_completed_create_from_buffer;
143 break;
1831ae68
FD
144 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
145 create_from_buffer = lttng_condition_event_rule_create_from_buffer;
146 break;
a58c490f
JG
147 default:
148 ERR("Attempted to create condition of unknown type (%i)",
149 (int) condition_comm->condition_type);
150 ret = -1;
151 goto end;
152 }
153
154 if (create_from_buffer) {
155 const struct lttng_buffer_view view =
156 lttng_buffer_view_from_view(buffer,
157 sizeof(*condition_comm), -1);
158
159 ret = create_from_buffer(&view, condition);
160 if (ret < 0) {
161 goto end;
162 }
163 condition_size += ret;
164
165 } else {
166 abort();
167 }
168
169 ret = condition_size;
170end:
171 return ret;
172}
173
174LTTNG_HIDDEN
175void lttng_condition_init(struct lttng_condition *condition,
176 enum lttng_condition_type type)
177{
178 condition->type = type;
179}
1831ae68
FD
180
181LTTNG_HIDDEN
182const char *lttng_condition_type_str(enum lttng_condition_type type)
183{
184 switch (type) {
185 case LTTNG_CONDITION_TYPE_UNKNOWN:
186 return "unknown";
187
188 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE:
189 return "session consumed size";
190
191 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH:
192 return "buffer usage high";
193
194 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW:
195 return "buffer usage low";
196
197 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING:
198 return "session rotation ongoing";
199
200 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED:
201 return "session rotation completed";
202
203 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT:
204 return "event rule hit";
205
206 default:
207 return "???";
208 }
209}
This page took 0.0485 seconds and 5 git commands to generate.