Fix: consumerd: consumed size miscomputed during statistics sampling
[lttng-tools.git] / src / bin / lttng-sessiond / tracker.cpp
1 /*
2 * Copyright (C) 2018 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #include "common/dynamic-array.hpp"
10 #include "common/macros.hpp"
11 #include "lttng/tracker.h"
12 #define _LGPL_SOURCE
13 #include "tracker.hpp"
14
15 #include <common/defaults.hpp>
16 #include <common/error.hpp>
17 #include <common/hashtable/hashtable.hpp>
18 #include <common/hashtable/utils.hpp>
19 #include <common/tracker.hpp>
20 #include <common/urcu.hpp>
21
22 #include <lttng/lttng-error.h>
23
24 #include <grp.h>
25 #include <pwd.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <urcu.h>
29 #include <urcu/list.h>
30 #include <urcu/rculfhash.h>
31
32 struct process_attr_tracker {
33 enum lttng_tracking_policy policy;
34 struct cds_lfht *inclusion_set_ht;
35 };
36
37 namespace {
38 struct process_attr_tracker_value_node {
39 struct process_attr_value *value;
40 struct cds_lfht_node inclusion_set_ht_node;
41 struct rcu_head rcu_head;
42 };
43 } /* namespace */
44
45 static void process_attr_tracker_value_node_rcu_free(struct rcu_head *rcu_head)
46 {
47 struct process_attr_tracker_value_node *node =
48 lttng::utils::container_of(rcu_head, &process_attr_tracker_value_node::rcu_head);
49
50 free(node);
51 }
52
53 struct process_attr_tracker *process_attr_tracker_create()
54 {
55 struct process_attr_tracker *tracker;
56
57 tracker = zmalloc<process_attr_tracker>();
58 if (!tracker) {
59 return nullptr;
60 }
61
62 (void) process_attr_tracker_set_tracking_policy(tracker, LTTNG_TRACKING_POLICY_INCLUDE_ALL);
63
64 tracker->inclusion_set_ht = cds_lfht_new(
65 DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
66 if (!tracker->inclusion_set_ht) {
67 goto error;
68 }
69
70 return tracker;
71 error:
72 process_attr_tracker_destroy(tracker);
73 return nullptr;
74 }
75
76 static void
77 process_attr_tracker_remove_value_node(struct process_attr_tracker *tracker,
78 struct process_attr_tracker_value_node *value_node)
79 {
80 cds_lfht_del(tracker->inclusion_set_ht, &value_node->inclusion_set_ht_node);
81 process_attr_value_destroy(value_node->value);
82 call_rcu(&value_node->rcu_head, process_attr_tracker_value_node_rcu_free);
83 }
84
85 static void process_attr_tracker_clear_inclusion_set(struct process_attr_tracker *tracker)
86 {
87 int ret;
88 struct lttng_ht_iter iter;
89 struct process_attr_tracker_value_node *value_node;
90
91 if (!tracker->inclusion_set_ht) {
92 return;
93 }
94
95 {
96 lttng::urcu::read_lock_guard read_lock;
97
98 cds_lfht_for_each_entry (
99 tracker->inclusion_set_ht, &iter.iter, value_node, inclusion_set_ht_node) {
100 process_attr_tracker_remove_value_node(tracker, value_node);
101 }
102 }
103
104 ret = cds_lfht_destroy(tracker->inclusion_set_ht, nullptr);
105 LTTNG_ASSERT(ret == 0);
106 tracker->inclusion_set_ht = nullptr;
107 }
108
109 static int process_attr_tracker_create_inclusion_set(struct process_attr_tracker *tracker)
110 {
111 LTTNG_ASSERT(!tracker->inclusion_set_ht);
112 tracker->inclusion_set_ht = cds_lfht_new(
113 DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
114 return tracker->inclusion_set_ht ? 0 : -1;
115 }
116
117 void process_attr_tracker_destroy(struct process_attr_tracker *tracker)
118 {
119 if (!tracker) {
120 return;
121 }
122
123 process_attr_tracker_clear_inclusion_set(tracker);
124 free(tracker);
125 }
126
127 enum lttng_tracking_policy
128 process_attr_tracker_get_tracking_policy(const struct process_attr_tracker *tracker)
129 {
130 return tracker->policy;
131 }
132
133 int process_attr_tracker_set_tracking_policy(struct process_attr_tracker *tracker,
134 enum lttng_tracking_policy tracking_policy)
135 {
136 int ret = 0;
137
138 if (tracker->policy == tracking_policy) {
139 goto end;
140 }
141
142 process_attr_tracker_clear_inclusion_set(tracker);
143 ret = process_attr_tracker_create_inclusion_set(tracker);
144 if (ret) {
145 goto end;
146 }
147 tracker->policy = tracking_policy;
148 end:
149 return ret;
150 }
151
152 static int match_inclusion_set_value(struct cds_lfht_node *node, const void *key)
153 {
154 const struct process_attr_value *value_key = (process_attr_value *) key;
155 const struct process_attr_tracker_value_node *value_node = caa_container_of(
156 node, struct process_attr_tracker_value_node, inclusion_set_ht_node);
157
158 return process_attr_tracker_value_equal(value_node->value, value_key);
159 }
160
161 static struct process_attr_tracker_value_node *
162 process_attr_tracker_lookup(const struct process_attr_tracker *tracker,
163 const struct process_attr_value *value)
164 {
165 struct cds_lfht_iter iter;
166 struct cds_lfht_node *node;
167
168 LTTNG_ASSERT(tracker->policy == LTTNG_TRACKING_POLICY_INCLUDE_SET);
169
170 lttng::urcu::read_lock_guard read_lock;
171 cds_lfht_lookup(tracker->inclusion_set_ht,
172 process_attr_value_hash(value),
173 match_inclusion_set_value,
174 value,
175 &iter);
176 node = cds_lfht_iter_get_node(&iter);
177
178 return node ? lttng::utils::container_of(
179 node, &process_attr_tracker_value_node::inclusion_set_ht_node) :
180 nullptr;
181 }
182
183 /* Protected by session mutex held by caller. */
184 enum process_attr_tracker_status
185 process_attr_tracker_inclusion_set_add_value(struct process_attr_tracker *tracker,
186 const struct process_attr_value *value)
187 {
188 enum process_attr_tracker_status status = PROCESS_ATTR_TRACKER_STATUS_OK;
189 struct process_attr_value *value_copy = nullptr;
190 struct process_attr_tracker_value_node *value_node = nullptr;
191
192 lttng::urcu::read_lock_guard read_lock;
193 if (tracker->policy != LTTNG_TRACKING_POLICY_INCLUDE_SET) {
194 status = PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY;
195 goto end;
196 }
197
198 if (process_attr_tracker_lookup(tracker, value)) {
199 status = PROCESS_ATTR_TRACKER_STATUS_EXISTS;
200 goto end;
201 }
202
203 value_node = zmalloc<process_attr_tracker_value_node>();
204 if (!value_node) {
205 status = PROCESS_ATTR_TRACKER_STATUS_ERROR;
206 goto end;
207 }
208
209 value_copy = process_attr_value_copy(value);
210 if (!value_copy) {
211 status = PROCESS_ATTR_TRACKER_STATUS_ERROR;
212 goto end;
213 }
214
215 value_node->value = value_copy;
216 cds_lfht_add(tracker->inclusion_set_ht,
217 process_attr_value_hash(value_copy),
218 &value_node->inclusion_set_ht_node);
219 value_copy = nullptr;
220 value_node = nullptr;
221 end:
222 if (value_copy) {
223 process_attr_value_destroy(value_copy);
224 }
225 if (value_node) {
226 free(value_node);
227 }
228 return status;
229 }
230
231 /* Protected by session mutex held by caller. */
232 enum process_attr_tracker_status
233 process_attr_tracker_inclusion_set_remove_value(struct process_attr_tracker *tracker,
234 const struct process_attr_value *value)
235 {
236 struct process_attr_tracker_value_node *value_node;
237 enum process_attr_tracker_status status = PROCESS_ATTR_TRACKER_STATUS_OK;
238
239 lttng::urcu::read_lock_guard read_lock;
240 if (tracker->policy != LTTNG_TRACKING_POLICY_INCLUDE_SET) {
241 status = PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY;
242 goto end;
243 }
244
245 value_node = process_attr_tracker_lookup(tracker, value);
246 if (!value_node) {
247 status = PROCESS_ATTR_TRACKER_STATUS_MISSING;
248 goto end;
249 }
250
251 process_attr_tracker_remove_value_node(tracker, value_node);
252 end:
253 return status;
254 }
255
256 enum process_attr_tracker_status
257 process_attr_tracker_get_inclusion_set(const struct process_attr_tracker *tracker,
258 struct lttng_process_attr_values **_values)
259 {
260 struct lttng_ht_iter iter;
261 struct process_attr_tracker_value_node *value_node;
262 enum process_attr_tracker_status status = PROCESS_ATTR_TRACKER_STATUS_OK;
263 struct lttng_process_attr_values *values;
264 struct process_attr_value *new_value = nullptr;
265
266 values = lttng_process_attr_values_create();
267 if (!values) {
268 status = PROCESS_ATTR_TRACKER_STATUS_ERROR;
269 goto error;
270 }
271
272 if (tracker->policy != LTTNG_TRACKING_POLICY_INCLUDE_SET) {
273 status = PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY;
274 goto error;
275 }
276
277 {
278 lttng::urcu::read_lock_guard read_lock;
279
280 cds_lfht_for_each_entry (
281 tracker->inclusion_set_ht, &iter.iter, value_node, inclusion_set_ht_node) {
282 int ret;
283
284 new_value = process_attr_value_copy(value_node->value);
285 if (!new_value) {
286 status = PROCESS_ATTR_TRACKER_STATUS_ERROR;
287 goto error_unlock;
288 }
289
290 ret = lttng_dynamic_pointer_array_add_pointer(&values->array, new_value);
291 if (ret) {
292 status = PROCESS_ATTR_TRACKER_STATUS_ERROR;
293 goto error_unlock;
294 }
295
296 new_value = nullptr;
297 }
298 }
299
300 *_values = values;
301 return status;
302 error_unlock:
303 error:
304 lttng_process_attr_values_destroy(values);
305 process_attr_value_destroy(new_value);
306 return status;
307 }
This page took 0.038322 seconds and 4 git commands to generate.