Commit | Line | Data |
---|---|---|
a58c490f JG |
1 | /* |
2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
3 | * | |
4 | * This library is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
6 | * as published by the Free Software Foundation. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
11 | * for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public License | |
14 | * along with this library; if not, write to the Free Software Foundation, | |
15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
16 | */ | |
17 | ||
18 | #include <lttng/condition/condition-internal.h> | |
19 | #include <lttng/condition/buffer-usage-internal.h> | |
e8360425 | 20 | #include <lttng/condition/session-consumed-size-internal.h> |
c19092cd | 21 | #include <lttng/condition/session-rotation-internal.h> |
a58c490f JG |
22 | #include <common/macros.h> |
23 | #include <common/error.h> | |
24 | #include <common/dynamic-buffer.h> | |
25 | #include <common/buffer-view.h> | |
26 | #include <stdbool.h> | |
27 | #include <assert.h> | |
28 | ||
29 | enum lttng_condition_type lttng_condition_get_type( | |
30 | const struct lttng_condition *condition) | |
31 | { | |
32 | return condition ? condition->type : LTTNG_CONDITION_TYPE_UNKNOWN; | |
33 | } | |
34 | ||
35 | void lttng_condition_destroy(struct lttng_condition *condition) | |
36 | { | |
37 | if (!condition) { | |
38 | return; | |
39 | } | |
40 | ||
41 | assert(condition->destroy); | |
42 | condition->destroy(condition); | |
43 | } | |
44 | ||
45 | LTTNG_HIDDEN | |
46 | bool lttng_condition_validate(const struct lttng_condition *condition) | |
47 | { | |
48 | bool valid; | |
49 | ||
50 | if (!condition) { | |
51 | valid = false; | |
52 | goto end; | |
53 | } | |
54 | ||
55 | if (!condition->validate) { | |
56 | /* Sub-class guarantees that it can never be invalid. */ | |
57 | valid = true; | |
58 | goto end; | |
59 | } | |
60 | ||
61 | valid = condition->validate(condition); | |
62 | end: | |
63 | return valid; | |
64 | } | |
65 | ||
66 | LTTNG_HIDDEN | |
3647288f JG |
67 | int lttng_condition_serialize(const struct lttng_condition *condition, |
68 | struct lttng_dynamic_buffer *buf) | |
a58c490f | 69 | { |
3647288f JG |
70 | int ret; |
71 | struct lttng_condition_comm condition_comm = { 0 }; | |
a58c490f JG |
72 | |
73 | if (!condition) { | |
74 | ret = -1; | |
75 | goto end; | |
76 | } | |
77 | ||
3647288f JG |
78 | condition_comm.condition_type = (int8_t) condition->type; |
79 | ||
80 | ret = lttng_dynamic_buffer_append(buf, &condition_comm, | |
81 | sizeof(condition_comm)); | |
82 | if (ret) { | |
83 | goto end; | |
a58c490f JG |
84 | } |
85 | ||
3647288f JG |
86 | ret = condition->serialize(condition, buf); |
87 | if (ret) { | |
a58c490f JG |
88 | goto end; |
89 | } | |
a58c490f JG |
90 | end: |
91 | return ret; | |
92 | } | |
93 | ||
94 | LTTNG_HIDDEN | |
95 | bool lttng_condition_is_equal(const struct lttng_condition *a, | |
96 | const struct lttng_condition *b) | |
97 | { | |
98 | bool is_equal = false; | |
99 | ||
100 | if (!a || !b) { | |
101 | goto end; | |
102 | } | |
103 | ||
104 | if (a->type != b->type) { | |
105 | goto end; | |
106 | } | |
107 | ||
6c2fe319 JG |
108 | if (a == b) { |
109 | is_equal = true; | |
110 | goto end; | |
111 | } | |
112 | ||
a58c490f JG |
113 | is_equal = a->equal ? a->equal(a, b) : true; |
114 | end: | |
115 | return is_equal; | |
116 | } | |
117 | ||
118 | LTTNG_HIDDEN | |
119 | ssize_t lttng_condition_create_from_buffer( | |
120 | const struct lttng_buffer_view *buffer, | |
121 | struct lttng_condition **condition) | |
122 | { | |
123 | ssize_t ret, condition_size = 0; | |
124 | const struct lttng_condition_comm *condition_comm; | |
125 | condition_create_from_buffer_cb create_from_buffer = NULL; | |
126 | ||
127 | if (!buffer || !condition) { | |
128 | ret = -1; | |
129 | goto end; | |
130 | } | |
131 | ||
132 | DBG("Deserializing condition from buffer"); | |
133 | condition_comm = (const struct lttng_condition_comm *) buffer->data; | |
134 | condition_size += sizeof(*condition_comm); | |
135 | ||
136 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
137 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
138 | create_from_buffer = lttng_condition_buffer_usage_low_create_from_buffer; | |
139 | break; | |
140 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
141 | create_from_buffer = lttng_condition_buffer_usage_high_create_from_buffer; | |
142 | break; | |
e8360425 JD |
143 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
144 | create_from_buffer = lttng_condition_session_consumed_size_create_from_buffer; | |
145 | break; | |
c19092cd JG |
146 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
147 | create_from_buffer = lttng_condition_session_rotation_ongoing_create_from_buffer; | |
148 | break; | |
149 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
150 | create_from_buffer = lttng_condition_session_rotation_completed_create_from_buffer; | |
151 | break; | |
a58c490f JG |
152 | default: |
153 | ERR("Attempted to create condition of unknown type (%i)", | |
154 | (int) condition_comm->condition_type); | |
155 | ret = -1; | |
156 | goto end; | |
157 | } | |
158 | ||
159 | if (create_from_buffer) { | |
160 | const struct lttng_buffer_view view = | |
161 | lttng_buffer_view_from_view(buffer, | |
162 | sizeof(*condition_comm), -1); | |
163 | ||
164 | ret = create_from_buffer(&view, condition); | |
165 | if (ret < 0) { | |
166 | goto end; | |
167 | } | |
168 | condition_size += ret; | |
169 | ||
170 | } else { | |
171 | abort(); | |
172 | } | |
173 | ||
174 | ret = condition_size; | |
175 | end: | |
176 | return ret; | |
177 | } | |
178 | ||
179 | LTTNG_HIDDEN | |
180 | void lttng_condition_init(struct lttng_condition *condition, | |
181 | enum lttng_condition_type type) | |
182 | { | |
183 | condition->type = type; | |
184 | } |