2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
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>
20 enum lttng_condition_type
lttng_condition_get_type(
21 const struct lttng_condition
*condition
)
23 return condition
? condition
->type
: LTTNG_CONDITION_TYPE_UNKNOWN
;
26 void lttng_condition_destroy(struct lttng_condition
*condition
)
32 assert(condition
->destroy
);
33 condition
->destroy(condition
);
37 bool lttng_condition_validate(const struct lttng_condition
*condition
)
46 if (!condition
->validate
) {
47 /* Sub-class guarantees that it can never be invalid. */
52 valid
= condition
->validate(condition
);
58 int lttng_condition_serialize(const struct lttng_condition
*condition
,
59 struct lttng_dynamic_buffer
*buf
,
63 struct lttng_condition_comm condition_comm
= { 0 };
70 condition_comm
.condition_type
= (int8_t) condition
->type
;
72 ret
= lttng_dynamic_buffer_append(buf
, &condition_comm
,
73 sizeof(condition_comm
));
78 ret
= condition
->serialize(condition
, buf
, fd_to_send
);
87 bool lttng_condition_is_equal(const struct lttng_condition
*a
,
88 const struct lttng_condition
*b
)
90 bool is_equal
= false;
96 if (a
->type
!= b
->type
) {
105 is_equal
= a
->equal
? a
->equal(a
, b
) : true;
111 ssize_t
lttng_condition_create_from_buffer(
112 const struct lttng_buffer_view
*buffer
,
113 struct lttng_condition
**condition
)
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
;
119 if (!buffer
|| !condition
) {
124 DBG("Deserializing condition from buffer");
125 condition_comm
= (const struct lttng_condition_comm
*) buffer
->data
;
126 condition_size
+= sizeof(*condition_comm
);
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
;
132 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
133 create_from_buffer
= lttng_condition_buffer_usage_high_create_from_buffer
;
135 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
136 create_from_buffer
= lttng_condition_session_consumed_size_create_from_buffer
;
138 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
139 create_from_buffer
= lttng_condition_session_rotation_ongoing_create_from_buffer
;
141 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
142 create_from_buffer
= lttng_condition_session_rotation_completed_create_from_buffer
;
144 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
145 create_from_buffer
= lttng_condition_event_rule_create_from_buffer
;
148 ERR("Attempted to create condition of unknown type (%i)",
149 (int) condition_comm
->condition_type
);
154 if (create_from_buffer
) {
155 const struct lttng_buffer_view view
=
156 lttng_buffer_view_from_view(buffer
,
157 sizeof(*condition_comm
), -1);
159 ret
= create_from_buffer(&view
, condition
);
163 condition_size
+= ret
;
169 ret
= condition_size
;
175 void lttng_condition_init(struct lttng_condition
*condition
,
176 enum lttng_condition_type type
)
178 condition
->type
= type
;
182 const char *lttng_condition_type_str(enum lttng_condition_type type
)
185 case LTTNG_CONDITION_TYPE_UNKNOWN
:
188 case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE
:
189 return "session consumed size";
191 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
:
192 return "buffer usage high";
194 case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
:
195 return "buffer usage low";
197 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING
:
198 return "session rotation ongoing";
200 case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED
:
201 return "session rotation completed";
203 case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
:
204 return "event rule hit";