Commit | Line | Data |
---|---|---|
1d498196 MD |
1 | /* |
2 | * libringbuffer/shm.c | |
3 | * | |
e92f3e28 | 4 | * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
1d498196 | 5 | * |
e92f3e28 MD |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Lesser General Public | |
8 | * License as published by the Free Software Foundation; only | |
9 | * version 2.1 of the License. | |
10 | * | |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
1d498196 MD |
19 | */ |
20 | ||
3fbec7dc | 21 | #define _LGPL_SOURCE |
1d498196 MD |
22 | #include "shm.h" |
23 | #include <unistd.h> | |
24 | #include <fcntl.h> | |
25 | #include <sys/mman.h> | |
a9ff648c | 26 | #include <sys/types.h> |
1d498196 MD |
27 | #include <sys/stat.h> /* For mode constants */ |
28 | #include <fcntl.h> /* For O_* constants */ | |
29 | #include <assert.h> | |
8da6cd6d MD |
30 | #include <stdio.h> |
31 | #include <signal.h> | |
32 | #include <dirent.h> | |
4318ae1b | 33 | #include <lttng/align.h> |
96e80018 | 34 | #include <limits.h> |
8a208943 | 35 | #include <stdbool.h> |
fb31eb73 | 36 | #include <stdint.h> |
bfcda6ce | 37 | #ifdef HAVE_LIBNUMA |
4b68c31f | 38 | #include <numa.h> |
8a208943 | 39 | #include <numaif.h> |
bfcda6ce | 40 | #endif |
3a81f31d | 41 | #include <helper.h> |
6548fca4 | 42 | #include <ust-fd.h> |
4d4838ba | 43 | #include "mmap.h" |
3a81f31d MD |
44 | |
45 | /* | |
46 | * Ensure we have the required amount of space available by writing 0 | |
47 | * into the entire buffer. Not doing so can trigger SIGBUS when going | |
48 | * beyond the available shm space. | |
49 | */ | |
50 | static | |
51 | int zero_file(int fd, size_t len) | |
52 | { | |
53 | ssize_t retlen; | |
54 | size_t written = 0; | |
55 | char *zeropage; | |
56 | long pagelen; | |
57 | int ret; | |
58 | ||
59 | pagelen = sysconf(_SC_PAGESIZE); | |
60 | if (pagelen < 0) | |
61 | return (int) pagelen; | |
62 | zeropage = calloc(pagelen, 1); | |
63 | if (!zeropage) | |
64 | return -ENOMEM; | |
65 | ||
66 | while (len > written) { | |
67 | do { | |
68 | retlen = write(fd, zeropage, | |
69 | min_t(size_t, pagelen, len - written)); | |
70 | } while (retlen == -1UL && errno == EINTR); | |
71 | if (retlen < 0) { | |
72 | ret = (int) retlen; | |
73 | goto error; | |
74 | } | |
75 | written += retlen; | |
76 | } | |
77 | ret = 0; | |
78 | error: | |
79 | free(zeropage); | |
80 | return ret; | |
81 | } | |
1d498196 MD |
82 | |
83 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj) | |
84 | { | |
85 | struct shm_object_table *table; | |
86 | ||
87 | table = zmalloc(sizeof(struct shm_object_table) + | |
88 | max_nb_obj * sizeof(table->objects[0])); | |
74d48abe MD |
89 | if (!table) |
90 | return NULL; | |
1d498196 MD |
91 | table->size = max_nb_obj; |
92 | return table; | |
93 | } | |
94 | ||
74d81a6c MD |
95 | static |
96 | struct shm_object *_shm_object_table_alloc_shm(struct shm_object_table *table, | |
a9ff648c | 97 | size_t memory_map_size, |
5ea386c3 | 98 | int stream_fd) |
1d498196 | 99 | { |
5ea386c3 | 100 | int shmfd, waitfd[2], ret, i; |
1d498196 MD |
101 | struct shm_object *obj; |
102 | char *memory_map; | |
103 | ||
5ea386c3 MD |
104 | if (stream_fd < 0) |
105 | return NULL; | |
1d498196 MD |
106 | if (table->allocated_len >= table->size) |
107 | return NULL; | |
7a9c21bd | 108 | obj = &table->objects[table->allocated_len]; |
1d498196 MD |
109 | |
110 | /* wait_fd: create pipe */ | |
111 | ret = pipe(waitfd); | |
112 | if (ret < 0) { | |
113 | PERROR("pipe"); | |
114 | goto error_pipe; | |
115 | } | |
116 | for (i = 0; i < 2; i++) { | |
117 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); | |
118 | if (ret < 0) { | |
119 | PERROR("fcntl"); | |
120 | goto error_fcntl; | |
121 | } | |
122 | } | |
5d61a504 MD |
123 | /* The write end of the pipe needs to be non-blocking */ |
124 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); | |
125 | if (ret < 0) { | |
126 | PERROR("fcntl"); | |
127 | goto error_fcntl; | |
128 | } | |
7a9c21bd | 129 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); |
1d498196 | 130 | |
053e6e24 MJ |
131 | /* |
132 | * Set POSIX shared memory object size | |
133 | * | |
134 | * First, use ftruncate() to set its size, some implementations won't | |
135 | * allow writes past the size set by ftruncate. | |
136 | * Then, use write() to fill it with zeros, this allows us to fully | |
137 | * allocate it and detect a shortage of shm space without dealing with | |
138 | * a SIGBUS. | |
139 | */ | |
a9ff648c | 140 | |
5ea386c3 | 141 | shmfd = stream_fd; |
1d498196 MD |
142 | ret = ftruncate(shmfd, memory_map_size); |
143 | if (ret) { | |
144 | PERROR("ftruncate"); | |
145 | goto error_ftruncate; | |
146 | } | |
053e6e24 MJ |
147 | ret = zero_file(shmfd, memory_map_size); |
148 | if (ret) { | |
149 | PERROR("zero_file"); | |
150 | goto error_zero_file; | |
151 | } | |
d0f6cf57 MD |
152 | /* |
153 | * Also ensure the file metadata is synced with the storage by using | |
154 | * fsync(2). | |
155 | */ | |
156 | ret = fsync(shmfd); | |
157 | if (ret) { | |
158 | PERROR("fsync"); | |
159 | goto error_fsync; | |
160 | } | |
5ea386c3 | 161 | obj->shm_fd_ownership = 0; |
1d498196 MD |
162 | obj->shm_fd = shmfd; |
163 | ||
164 | /* memory_map: mmap */ | |
165 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
4d4838ba | 166 | MAP_SHARED | LTTNG_MAP_POPULATE, shmfd, 0); |
1d498196 MD |
167 | if (memory_map == MAP_FAILED) { |
168 | PERROR("mmap"); | |
169 | goto error_mmap; | |
170 | } | |
74d81a6c | 171 | obj->type = SHM_OBJECT_SHM; |
1d498196 MD |
172 | obj->memory_map = memory_map; |
173 | obj->memory_map_size = memory_map_size; | |
174 | obj->allocated_len = 0; | |
dc613eb9 | 175 | obj->index = table->allocated_len++; |
7a9c21bd | 176 | |
1d498196 MD |
177 | return obj; |
178 | ||
179 | error_mmap: | |
d0f6cf57 | 180 | error_fsync: |
1d498196 | 181 | error_ftruncate: |
3a81f31d | 182 | error_zero_file: |
1d498196 MD |
183 | error_fcntl: |
184 | for (i = 0; i < 2; i++) { | |
185 | ret = close(waitfd[i]); | |
186 | if (ret) { | |
187 | PERROR("close"); | |
188 | assert(0); | |
189 | } | |
190 | } | |
191 | error_pipe: | |
1d498196 | 192 | return NULL; |
1d498196 MD |
193 | } |
194 | ||
74d81a6c MD |
195 | static |
196 | struct shm_object *_shm_object_table_alloc_mem(struct shm_object_table *table, | |
197 | size_t memory_map_size) | |
198 | { | |
199 | struct shm_object *obj; | |
200 | void *memory_map; | |
ff0f5728 | 201 | int waitfd[2], i, ret; |
74d81a6c MD |
202 | |
203 | if (table->allocated_len >= table->size) | |
204 | return NULL; | |
205 | obj = &table->objects[table->allocated_len]; | |
206 | ||
207 | memory_map = zmalloc(memory_map_size); | |
208 | if (!memory_map) | |
209 | goto alloc_error; | |
210 | ||
ff0f5728 MD |
211 | /* wait_fd: create pipe */ |
212 | ret = pipe(waitfd); | |
213 | if (ret < 0) { | |
214 | PERROR("pipe"); | |
215 | goto error_pipe; | |
216 | } | |
217 | for (i = 0; i < 2; i++) { | |
218 | ret = fcntl(waitfd[i], F_SETFD, FD_CLOEXEC); | |
219 | if (ret < 0) { | |
220 | PERROR("fcntl"); | |
221 | goto error_fcntl; | |
222 | } | |
223 | } | |
224 | /* The write end of the pipe needs to be non-blocking */ | |
225 | ret = fcntl(waitfd[1], F_SETFL, O_NONBLOCK); | |
226 | if (ret < 0) { | |
227 | PERROR("fcntl"); | |
228 | goto error_fcntl; | |
229 | } | |
230 | memcpy(obj->wait_fd, waitfd, sizeof(waitfd)); | |
231 | ||
232 | /* no shm_fd */ | |
74d81a6c | 233 | obj->shm_fd = -1; |
5ea386c3 | 234 | obj->shm_fd_ownership = 0; |
74d81a6c MD |
235 | |
236 | obj->type = SHM_OBJECT_MEM; | |
237 | obj->memory_map = memory_map; | |
238 | obj->memory_map_size = memory_map_size; | |
239 | obj->allocated_len = 0; | |
240 | obj->index = table->allocated_len++; | |
241 | ||
242 | return obj; | |
243 | ||
ff0f5728 MD |
244 | error_fcntl: |
245 | for (i = 0; i < 2; i++) { | |
246 | ret = close(waitfd[i]); | |
247 | if (ret) { | |
248 | PERROR("close"); | |
249 | assert(0); | |
250 | } | |
251 | } | |
252 | error_pipe: | |
253 | free(memory_map); | |
74d81a6c MD |
254 | alloc_error: |
255 | return NULL; | |
256 | } | |
257 | ||
8a208943 MD |
258 | /* |
259 | * libnuma prints errors on the console even for numa_available(). | |
260 | * Work-around this limitation by using get_mempolicy() directly to | |
261 | * check whether the kernel supports mempolicy. | |
262 | */ | |
263 | #ifdef HAVE_LIBNUMA | |
264 | static bool lttng_is_numa_available(void) | |
265 | { | |
266 | int ret; | |
267 | ||
268 | ret = get_mempolicy(NULL, NULL, 0, NULL, 0); | |
269 | if (ret && errno == ENOSYS) { | |
270 | return false; | |
271 | } | |
272 | return numa_available() > 0; | |
273 | } | |
274 | #endif | |
275 | ||
74d81a6c MD |
276 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
277 | size_t memory_map_size, | |
a9ff648c | 278 | enum shm_object_type type, |
4b68c31f MD |
279 | int stream_fd, |
280 | int cpu) | |
74d81a6c | 281 | { |
4b68c31f | 282 | struct shm_object *shm_object; |
bfcda6ce | 283 | #ifdef HAVE_LIBNUMA |
8a208943 MD |
284 | int oldnode = 0, node; |
285 | bool numa_avail; | |
4b68c31f | 286 | |
8a208943 MD |
287 | numa_avail = lttng_is_numa_available(); |
288 | if (numa_avail) { | |
289 | oldnode = numa_preferred(); | |
290 | if (cpu >= 0) { | |
291 | node = numa_node_of_cpu(cpu); | |
292 | if (node >= 0) | |
293 | numa_set_preferred(node); | |
294 | } | |
295 | if (cpu < 0 || node < 0) | |
296 | numa_set_localalloc(); | |
4b68c31f | 297 | } |
bfcda6ce | 298 | #endif /* HAVE_LIBNUMA */ |
74d81a6c MD |
299 | switch (type) { |
300 | case SHM_OBJECT_SHM: | |
4b68c31f | 301 | shm_object = _shm_object_table_alloc_shm(table, memory_map_size, |
5ea386c3 | 302 | stream_fd); |
4b68c31f | 303 | break; |
74d81a6c | 304 | case SHM_OBJECT_MEM: |
4b68c31f MD |
305 | shm_object = _shm_object_table_alloc_mem(table, memory_map_size); |
306 | break; | |
74d81a6c MD |
307 | default: |
308 | assert(0); | |
309 | } | |
bfcda6ce | 310 | #ifdef HAVE_LIBNUMA |
8a208943 MD |
311 | if (numa_avail) |
312 | numa_set_preferred(oldnode); | |
bfcda6ce | 313 | #endif /* HAVE_LIBNUMA */ |
4b68c31f | 314 | return shm_object; |
74d81a6c MD |
315 | } |
316 | ||
317 | struct shm_object *shm_object_table_append_shm(struct shm_object_table *table, | |
318 | int shm_fd, int wakeup_fd, uint32_t stream_nr, | |
319 | size_t memory_map_size) | |
193183fb MD |
320 | { |
321 | struct shm_object *obj; | |
322 | char *memory_map; | |
74d81a6c | 323 | int ret; |
193183fb MD |
324 | |
325 | if (table->allocated_len >= table->size) | |
326 | return NULL; | |
74d81a6c MD |
327 | /* streams _must_ be received in sequential order, else fail. */ |
328 | if (stream_nr + 1 != table->allocated_len) | |
329 | return NULL; | |
330 | ||
193183fb MD |
331 | obj = &table->objects[table->allocated_len]; |
332 | ||
74d81a6c MD |
333 | /* wait_fd: set write end of the pipe. */ |
334 | obj->wait_fd[0] = -1; /* read end is unset */ | |
335 | obj->wait_fd[1] = wakeup_fd; | |
193183fb | 336 | obj->shm_fd = shm_fd; |
5ea386c3 | 337 | obj->shm_fd_ownership = 1; |
193183fb | 338 | |
74d81a6c MD |
339 | /* The write end of the pipe needs to be non-blocking */ |
340 | ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); | |
341 | if (ret < 0) { | |
342 | PERROR("fcntl"); | |
343 | goto error_fcntl; | |
344 | } | |
345 | ||
193183fb MD |
346 | /* memory_map: mmap */ |
347 | memory_map = mmap(NULL, memory_map_size, PROT_READ | PROT_WRITE, | |
4d4838ba | 348 | MAP_SHARED | LTTNG_MAP_POPULATE, shm_fd, 0); |
193183fb MD |
349 | if (memory_map == MAP_FAILED) { |
350 | PERROR("mmap"); | |
351 | goto error_mmap; | |
352 | } | |
74d81a6c | 353 | obj->type = SHM_OBJECT_SHM; |
193183fb MD |
354 | obj->memory_map = memory_map; |
355 | obj->memory_map_size = memory_map_size; | |
356 | obj->allocated_len = memory_map_size; | |
357 | obj->index = table->allocated_len++; | |
358 | ||
359 | return obj; | |
360 | ||
74d81a6c | 361 | error_fcntl: |
193183fb MD |
362 | error_mmap: |
363 | return NULL; | |
364 | } | |
365 | ||
74d81a6c MD |
366 | /* |
367 | * Passing ownership of mem to object. | |
368 | */ | |
369 | struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, | |
ff0f5728 | 370 | void *mem, size_t memory_map_size, int wakeup_fd) |
74d81a6c MD |
371 | { |
372 | struct shm_object *obj; | |
ff0f5728 | 373 | int ret; |
74d81a6c MD |
374 | |
375 | if (table->allocated_len >= table->size) | |
376 | return NULL; | |
377 | obj = &table->objects[table->allocated_len]; | |
378 | ||
ff0f5728 MD |
379 | obj->wait_fd[0] = -1; /* read end is unset */ |
380 | obj->wait_fd[1] = wakeup_fd; | |
74d81a6c | 381 | obj->shm_fd = -1; |
5ea386c3 | 382 | obj->shm_fd_ownership = 0; |
74d81a6c | 383 | |
ff0f5728 MD |
384 | ret = fcntl(obj->wait_fd[1], F_SETFD, FD_CLOEXEC); |
385 | if (ret < 0) { | |
386 | PERROR("fcntl"); | |
387 | goto error_fcntl; | |
388 | } | |
389 | /* The write end of the pipe needs to be non-blocking */ | |
390 | ret = fcntl(obj->wait_fd[1], F_SETFL, O_NONBLOCK); | |
391 | if (ret < 0) { | |
392 | PERROR("fcntl"); | |
393 | goto error_fcntl; | |
394 | } | |
395 | ||
74d81a6c MD |
396 | obj->type = SHM_OBJECT_MEM; |
397 | obj->memory_map = mem; | |
398 | obj->memory_map_size = memory_map_size; | |
399 | obj->allocated_len = memory_map_size; | |
400 | obj->index = table->allocated_len++; | |
401 | ||
402 | return obj; | |
ff0f5728 MD |
403 | |
404 | error_fcntl: | |
405 | return NULL; | |
74d81a6c MD |
406 | } |
407 | ||
1d498196 | 408 | static |
6548fca4 | 409 | void shmp_object_destroy(struct shm_object *obj, int consumer) |
1d498196 | 410 | { |
74d81a6c MD |
411 | switch (obj->type) { |
412 | case SHM_OBJECT_SHM: | |
413 | { | |
414 | int ret, i; | |
1d498196 | 415 | |
7a784989 MD |
416 | ret = munmap(obj->memory_map, obj->memory_map_size); |
417 | if (ret) { | |
418 | PERROR("umnmap"); | |
419 | assert(0); | |
420 | } | |
6548fca4 | 421 | |
5ea386c3 | 422 | if (obj->shm_fd_ownership) { |
6548fca4 MD |
423 | /* Delete FDs only if called from app (not consumer). */ |
424 | if (!consumer) { | |
425 | lttng_ust_lock_fd_tracker(); | |
426 | ret = close(obj->shm_fd); | |
427 | if (!ret) { | |
428 | lttng_ust_delete_fd_from_tracker(obj->shm_fd); | |
429 | } else { | |
430 | PERROR("close"); | |
431 | assert(0); | |
432 | } | |
433 | lttng_ust_unlock_fd_tracker(); | |
434 | } else { | |
435 | ret = close(obj->shm_fd); | |
436 | if (ret) { | |
437 | PERROR("close"); | |
438 | assert(0); | |
439 | } | |
a9ff648c MD |
440 | } |
441 | } | |
74d81a6c MD |
442 | for (i = 0; i < 2; i++) { |
443 | if (obj->wait_fd[i] < 0) | |
444 | continue; | |
6548fca4 MD |
445 | if (!consumer) { |
446 | lttng_ust_lock_fd_tracker(); | |
447 | ret = close(obj->wait_fd[i]); | |
448 | if (!ret) { | |
449 | lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]); | |
450 | } else { | |
451 | PERROR("close"); | |
452 | assert(0); | |
453 | } | |
454 | lttng_ust_unlock_fd_tracker(); | |
455 | } else { | |
456 | ret = close(obj->wait_fd[i]); | |
457 | if (ret) { | |
458 | PERROR("close"); | |
459 | assert(0); | |
460 | } | |
74d81a6c | 461 | } |
1d498196 | 462 | } |
74d81a6c MD |
463 | break; |
464 | } | |
465 | case SHM_OBJECT_MEM: | |
ff0f5728 MD |
466 | { |
467 | int ret, i; | |
468 | ||
469 | for (i = 0; i < 2; i++) { | |
470 | if (obj->wait_fd[i] < 0) | |
471 | continue; | |
6548fca4 MD |
472 | if (!consumer) { |
473 | lttng_ust_lock_fd_tracker(); | |
474 | ret = close(obj->wait_fd[i]); | |
475 | if (!ret) { | |
476 | lttng_ust_delete_fd_from_tracker(obj->wait_fd[i]); | |
477 | } else { | |
478 | PERROR("close"); | |
479 | assert(0); | |
480 | } | |
481 | lttng_ust_unlock_fd_tracker(); | |
482 | } else { | |
483 | ret = close(obj->wait_fd[i]); | |
484 | if (ret) { | |
485 | PERROR("close"); | |
486 | assert(0); | |
487 | } | |
ff0f5728 MD |
488 | } |
489 | } | |
74d81a6c MD |
490 | free(obj->memory_map); |
491 | break; | |
ff0f5728 | 492 | } |
74d81a6c MD |
493 | default: |
494 | assert(0); | |
1d498196 MD |
495 | } |
496 | } | |
497 | ||
6548fca4 | 498 | void shm_object_table_destroy(struct shm_object_table *table, int consumer) |
1d498196 MD |
499 | { |
500 | int i; | |
501 | ||
502 | for (i = 0; i < table->allocated_len; i++) | |
6548fca4 | 503 | shmp_object_destroy(&table->objects[i], consumer); |
1d498196 MD |
504 | free(table); |
505 | } | |
506 | ||
507 | /* | |
508 | * zalloc_shm - allocate memory within a shm object. | |
509 | * | |
510 | * Shared memory is already zeroed by shmget. | |
511 | * *NOT* multithread-safe (should be protected by mutex). | |
512 | * Returns a -1, -1 tuple on error. | |
513 | */ | |
514 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len) | |
515 | { | |
516 | struct shm_ref ref; | |
517 | struct shm_ref shm_ref_error = { -1, -1 }; | |
518 | ||
519 | if (obj->memory_map_size - obj->allocated_len < len) | |
520 | return shm_ref_error; | |
521 | ref.index = obj->index; | |
522 | ref.offset = obj->allocated_len; | |
523 | obj->allocated_len += len; | |
524 | return ref; | |
525 | } | |
526 | ||
527 | void align_shm(struct shm_object *obj, size_t align) | |
528 | { | |
b72687b8 | 529 | size_t offset_len = lttng_ust_offset_align(obj->allocated_len, align); |
1d498196 MD |
530 | obj->allocated_len += offset_len; |
531 | } |