Commit | Line | Data |
---|---|---|
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> | |
e8360425 | 10 | #include <lttng/condition/session-consumed-size-internal.h> |
c19092cd | 11 | #include <lttng/condition/session-rotation-internal.h> |
a58c490f JG |
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 | if (!condition) { | |
28 | return; | |
29 | } | |
30 | ||
31 | assert(condition->destroy); | |
32 | condition->destroy(condition); | |
33 | } | |
34 | ||
35 | LTTNG_HIDDEN | |
36 | bool lttng_condition_validate(const struct lttng_condition *condition) | |
37 | { | |
38 | bool valid; | |
39 | ||
40 | if (!condition) { | |
41 | valid = false; | |
42 | goto end; | |
43 | } | |
44 | ||
45 | if (!condition->validate) { | |
46 | /* Sub-class guarantees that it can never be invalid. */ | |
47 | valid = true; | |
48 | goto end; | |
49 | } | |
50 | ||
51 | valid = condition->validate(condition); | |
52 | end: | |
53 | return valid; | |
54 | } | |
55 | ||
56 | LTTNG_HIDDEN | |
3647288f | 57 | int lttng_condition_serialize(const struct lttng_condition *condition, |
c0a66c84 | 58 | struct lttng_payload *payload) |
a58c490f | 59 | { |
3647288f | 60 | int ret; |
c0a66c84 | 61 | struct lttng_condition_comm condition_comm = {}; |
a58c490f JG |
62 | |
63 | if (!condition) { | |
64 | ret = -1; | |
65 | goto end; | |
66 | } | |
67 | ||
3647288f JG |
68 | condition_comm.condition_type = (int8_t) condition->type; |
69 | ||
c0a66c84 | 70 | ret = lttng_dynamic_buffer_append(&payload->buffer, &condition_comm, |
3647288f JG |
71 | sizeof(condition_comm)); |
72 | if (ret) { | |
73 | goto end; | |
a58c490f JG |
74 | } |
75 | ||
c0a66c84 | 76 | ret = condition->serialize(condition, payload); |
3647288f | 77 | if (ret) { |
a58c490f JG |
78 | goto end; |
79 | } | |
a58c490f JG |
80 | end: |
81 | return ret; | |
82 | } | |
83 | ||
84 | LTTNG_HIDDEN | |
85 | bool lttng_condition_is_equal(const struct lttng_condition *a, | |
86 | const struct lttng_condition *b) | |
87 | { | |
88 | bool is_equal = false; | |
89 | ||
90 | if (!a || !b) { | |
91 | goto end; | |
92 | } | |
93 | ||
94 | if (a->type != b->type) { | |
95 | goto end; | |
96 | } | |
97 | ||
6c2fe319 JG |
98 | if (a == b) { |
99 | is_equal = true; | |
100 | goto end; | |
101 | } | |
102 | ||
a58c490f JG |
103 | is_equal = a->equal ? a->equal(a, b) : true; |
104 | end: | |
105 | return is_equal; | |
106 | } | |
107 | ||
108 | LTTNG_HIDDEN | |
c0a66c84 JG |
109 | ssize_t lttng_condition_create_from_payload( |
110 | struct lttng_payload_view *view, | |
a58c490f JG |
111 | struct lttng_condition **condition) |
112 | { | |
113 | ssize_t ret, condition_size = 0; | |
114 | const struct lttng_condition_comm *condition_comm; | |
c0a66c84 | 115 | condition_create_from_payload_cb create_from_payload = NULL; |
a58c490f | 116 | |
c0a66c84 | 117 | if (!view || !condition) { |
a58c490f JG |
118 | ret = -1; |
119 | goto end; | |
120 | } | |
121 | ||
122 | DBG("Deserializing condition from buffer"); | |
c0a66c84 | 123 | condition_comm = (typeof(condition_comm)) view->buffer.data; |
a58c490f JG |
124 | condition_size += sizeof(*condition_comm); |
125 | ||
126 | switch ((enum lttng_condition_type) condition_comm->condition_type) { | |
127 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_LOW: | |
c0a66c84 | 128 | create_from_payload = lttng_condition_buffer_usage_low_create_from_payload; |
a58c490f JG |
129 | break; |
130 | case LTTNG_CONDITION_TYPE_BUFFER_USAGE_HIGH: | |
c0a66c84 | 131 | create_from_payload = lttng_condition_buffer_usage_high_create_from_payload; |
a58c490f | 132 | break; |
e8360425 | 133 | case LTTNG_CONDITION_TYPE_SESSION_CONSUMED_SIZE: |
c0a66c84 | 134 | create_from_payload = lttng_condition_session_consumed_size_create_from_payload; |
e8360425 | 135 | break; |
c19092cd | 136 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_ONGOING: |
c0a66c84 | 137 | create_from_payload = lttng_condition_session_rotation_ongoing_create_from_payload; |
c19092cd JG |
138 | break; |
139 | case LTTNG_CONDITION_TYPE_SESSION_ROTATION_COMPLETED: | |
c0a66c84 | 140 | create_from_payload = lttng_condition_session_rotation_completed_create_from_payload; |
c19092cd | 141 | break; |
a58c490f JG |
142 | default: |
143 | ERR("Attempted to create condition of unknown type (%i)", | |
144 | (int) condition_comm->condition_type); | |
145 | ret = -1; | |
146 | goto end; | |
147 | } | |
148 | ||
c0a66c84 JG |
149 | if (create_from_payload) { |
150 | struct lttng_payload_view condition_view = | |
151 | lttng_payload_view_from_view(view, | |
a58c490f JG |
152 | sizeof(*condition_comm), -1); |
153 | ||
c0a66c84 | 154 | ret = create_from_payload(&condition_view, condition); |
a58c490f JG |
155 | if (ret < 0) { |
156 | goto end; | |
157 | } | |
158 | condition_size += ret; | |
159 | ||
160 | } else { | |
161 | abort(); | |
162 | } | |
163 | ||
164 | ret = condition_size; | |
165 | end: | |
166 | return ret; | |
167 | } | |
168 | ||
169 | LTTNG_HIDDEN | |
170 | void lttng_condition_init(struct lttng_condition *condition, | |
171 | enum lttng_condition_type type) | |
172 | { | |
173 | condition->type = type; | |
174 | } |