2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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
18 #include <lttng/condition/condition-internal.h>
19 #include <lttng/condition/buffer-usage-internal.h>
20 #include <common/macros.h>
24 bool is_usage_condition(struct lttng_condition
*condition
)
26 enum lttng_condition_type type
= lttng_condition_get_type(condition
);
28 return type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
||
29 type
== LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
;
33 bool is_usage_condition(struct lttng_condition
*condition
)
35 return is_low_usage_condition(condition
) ||
36 is_high_usage_condition(condition
);
40 void lttng_condition_buffer_usage_destroy(struct lttng_condition
*condition
)
42 struct lttng_condition_buffer_usage
*usage
;
44 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
47 free(usage
->session_name
);
48 free(usage
->channel_name
);
53 bool lttng_condition_buffer_usage_validate(struct lttng_condition
*condition
)
56 struct lttng_condition_buffer_usage
*usage
;
62 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
64 if (!usage
->session_name
) {
67 if (!usage
->channel_name
) {
70 if (!usage
->threshold_percent
.set
&& !usage
->threshold_bytes
.set
) {
80 struct lttng_condition
*lttng_condition_buffer_usage_create(
81 enum lttng_condition_type type
)
83 struct lttng_condition_buffer_usage
*condition
;
85 condition
= zmalloc(sizeof(struct lttng_condition_buffer_usage
));
90 condition
->parent
.type
= type
;
91 condition
->parent
.validate
= lttng_condition_buffer_usage_validate
;
92 condition
->parent
.destroy
= lttng_condition_buffer_usage_destroy
;
94 return &condition
->parent
;
97 struct lttng_condition
*lttng_condition_buffer_usage_low_create(void)
99 return lttng_condition_buffer_usage_create(
100 LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW
);
103 struct lttng_condition
*lttng_condition_buffer_usage_high_create(void)
105 return lttng_condition_buffer_usage_create(
106 LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH
);
109 enum lttng_condition_status
110 lttng_condition_buffer_usage_get_threshold_percentage(
111 struct lttng_condition
*condition
, double *threshold_percent
)
113 struct lttng_condition_buffer_usage
*usage
;
114 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
116 if (!condition
|| !is_usage_condition(condition
) ||
117 !threshold_percent
) {
118 status
= LTTNG_CONDITION_STATUS_INVALID
;
122 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
124 if (!usage
->threshold_percent
.set
) {
125 status
= LTTNG_CONDITION_STATUS_UNSET
;
128 *threshold_percent
= usage
->threshold_percent
.value
;
133 /* threshold_percent expressed as [0.0, 1.0]. */
134 enum lttng_condition_status
135 lttng_condition_buffer_usage_set_threshold_percentage(
136 struct lttng_condition
*condition
, double threshold_percent
)
138 struct lttng_condition_buffer_usage
*usage
;
139 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
141 if (!condition
|| !is_usage_condition(condition
) ||
142 threshold_percent
< 0.0 || threshold_percent
> 1.0) {
143 status
= LTTNG_CONDITION_STATUS_INVALID
;
147 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
149 usage
->threshold_percent
.set
= true;
150 usage
->threshold_bytes
.set
= false;
151 usage
->threshold_percent
.value
= threshold_percent
;
156 enum lttng_condition_status
157 lttng_condition_buffer_usage_get_threshold(
158 struct lttng_condition
*condition
, uint64_t *threshold_bytes
)
160 struct lttng_condition_buffer_usage
*usage
;
161 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
163 if (!condition
|| !is_usage_condition(condition
) || !threshold_bytes
) {
164 status
= LTTNG_CONDITION_STATUS_INVALID
;
168 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
170 if (!usage
->threshold_bytes
.set
) {
171 status
= LTTNG_CONDITION_STATUS_UNSET
;
174 *threshold_bytes
= usage
->threshold_bytes
.value
;
179 enum lttng_condition_status
180 lttng_condition_buffer_usage_set_threshold(
181 struct lttng_condition
*condition
, uint64_t threshold_bytes
)
183 struct lttng_condition_buffer_usage
*usage
;
184 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
186 if (!condition
|| !is_usage_condition(condition
)) {
187 status
= LTTNG_CONDITION_STATUS_INVALID
;
191 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
193 usage
->threshold_percent
.set
= false;
194 usage
->threshold_bytes
.set
= true;
195 usage
->threshold_bytes
.value
= threshold_bytes
;
200 enum lttng_condition_status
201 lttng_condition_buffer_usage_get_session_name(
202 struct lttng_condition
*condition
, const char **session_name
)
204 struct lttng_condition_buffer_usage
*usage
;
205 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
207 if (!condition
|| !is_usage_condition(condition
) || !session_name
) {
208 status
= LTTNG_CONDITION_STATUS_INVALID
;
212 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
214 if (!usage
->session_name
) {
215 status
= LTTNG_CONDITION_STATUS_UNSET
;
218 *session_name
= usage
->session_name
;
223 extern enum lttng_condition_status
224 lttng_condition_buffer_usage_set_session_name(
225 struct lttng_condition
*condition
, const char *session_name
)
227 struct lttng_condition_buffer_usage
*usage
;
228 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
230 if (!condition
|| !is_usage_condition(condition
) || !session_name
||
231 strlen(session_name
) == 0) {
232 status
= LTTNG_CONDITION_STATUS_INVALID
;
236 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
238 usage
->session_name
= strdup(session_name
);
239 if (!usage
->session_name
) {
240 status
= LTTNG_CONDITION_STATUS_ERROR
;
247 enum lttng_condition_status
248 lttng_condition_buffer_usage_get_channel_name(
249 struct lttng_condition
*condition
, const char **channel_name
)
251 struct lttng_condition_buffer_usage
*usage
;
252 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
254 if (!condition
|| !is_usage_condition(condition
) || !channel_name
) {
255 status
= LTTNG_CONDITION_STATUS_INVALID
;
259 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
261 if (!usage
->channel_name
) {
262 status
= LTTNG_CONDITION_STATUS_UNSET
;
265 *channel_name
= usage
->channel_name
;
270 extern enum lttng_condition_status
271 lttng_condition_buffer_usage_set_channel_name(
272 struct lttng_condition
*condition
, const char *channel_name
)
274 struct lttng_condition_buffer_usage
*usage
;
275 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
277 if (!condition
|| !is_usage_condition(condition
) || !channel_name
||
278 strlen(channel_name
) == 0) {
279 status
= LTTNG_CONDITION_STATUS_INVALID
;
283 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
285 usage
->channel_name
= strdup(channel_name
);
286 if (!usage
->channel_name
) {
287 status
= LTTNG_CONDITION_STATUS_ERROR
;
294 extern enum lttng_condition_status
295 lttng_condition_buffer_usage_get_domain_type(
296 struct lttng_condition
*condition
, enum lttng_domain_type
*type
)
298 struct lttng_condition_buffer_usage
*usage
;
299 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
301 if (!condition
|| !is_usage_condition(condition
) || !type
) {
302 status
= LTTNG_CONDITION_STATUS_INVALID
;
306 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
308 if (!usage
->domain
.set
) {
309 status
= LTTNG_CONDITION_STATUS_UNSET
;
312 *type
= usage
->domain
.type
;
317 extern enum lttng_condition_status
318 lttng_condition_buffer_usage_set_domain_type(
319 struct lttng_condition
*condition
, enum lttng_domain_type type
)
321 struct lttng_condition_buffer_usage
*usage
;
322 enum lttng_condition_status status
= LTTNG_CONDITION_STATUS_OK
;
324 if (!condition
|| !is_usage_condition(condition
) ||
325 type
== LTTNG_DOMAIN_NONE
) {
326 status
= LTTNG_CONDITION_STATUS_INVALID
;
330 usage
= container_of(condition
, struct lttng_condition_buffer_usage
,
332 usage
->domain
.set
= true;
333 usage
->domain
.type
= type
;