2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
12 #include <sys/types.h>
13 #include <sys/stat.h> /* For mode constants */
14 #include <fcntl.h> /* For O_* constants */
28 #include <lttng/ust-utils.h>
30 #include "common/macros.h"
31 #include "common/ust-fd.h"
32 #include "common/compat/mmap.h"
35 * Ensure we have the required amount of space available by writing 0
36 * into the entire buffer. Not doing so can trigger SIGBUS when going
37 * beyond the available shm space.
40 int zero_file(int fd
, size_t len
)
48 pagelen
= sysconf(_SC_PAGESIZE
);
51 zeropage
= calloc(pagelen
, 1);
55 while (len
> written
) {
57 retlen
= write(fd
, zeropage
,
58 min_t(size_t, pagelen
, len
- written
));
59 } while (retlen
== -1UL && errno
== EINTR
);
72 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
74 struct shm_object_table
*table
;
76 table
= zmalloc(sizeof(struct shm_object_table
) +
77 max_nb_obj
* sizeof(table
->objects
[0]));
80 table
->size
= max_nb_obj
;
85 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
86 size_t memory_map_size
,
89 int shmfd
, waitfd
[2], ret
, i
;
90 struct shm_object
*obj
;
95 if (table
->allocated_len
>= table
->size
)
97 obj
= &table
->objects
[table
->allocated_len
];
99 /* wait_fd: create pipe */
100 ret
= pipe2(waitfd
, O_CLOEXEC
);
105 /* The write end of the pipe needs to be non-blocking */
106 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
111 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
114 * Set POSIX shared memory object size
116 * First, use ftruncate() to set its size, some implementations won't
117 * allow writes past the size set by ftruncate.
118 * Then, use write() to fill it with zeros, this allows us to fully
119 * allocate it and detect a shortage of shm space without dealing with
124 ret
= ftruncate(shmfd
, memory_map_size
);
127 goto error_ftruncate
;
129 ret
= zero_file(shmfd
, memory_map_size
);
132 goto error_zero_file
;
136 * Also ensure the file metadata is synced with the storage by using
137 * fsync(2). Some platforms don't allow fsync on POSIX shm fds, ignore
138 * EINVAL accordingly.
141 if (ret
&& errno
!= EINVAL
) {
145 obj
->shm_fd_ownership
= 0;
148 /* memory_map: mmap */
149 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
150 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
151 if (memory_map
== MAP_FAILED
) {
155 obj
->type
= SHM_OBJECT_SHM
;
156 obj
->memory_map
= memory_map
;
157 obj
->memory_map_size
= memory_map_size
;
158 obj
->allocated_len
= 0;
159 obj
->index
= table
->allocated_len
++;
168 for (i
= 0; i
< 2; i
++) {
169 ret
= close(waitfd
[i
]);
180 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
181 size_t memory_map_size
)
183 struct shm_object
*obj
;
185 int waitfd
[2], i
, ret
;
187 if (table
->allocated_len
>= table
->size
)
189 obj
= &table
->objects
[table
->allocated_len
];
191 memory_map
= zmalloc(memory_map_size
);
195 /* wait_fd: create pipe */
196 ret
= pipe2(waitfd
, O_CLOEXEC
);
201 /* The write end of the pipe needs to be non-blocking */
202 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
207 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
211 obj
->shm_fd_ownership
= 0;
213 obj
->type
= SHM_OBJECT_MEM
;
214 obj
->memory_map
= memory_map
;
215 obj
->memory_map_size
= memory_map_size
;
216 obj
->allocated_len
= 0;
217 obj
->index
= table
->allocated_len
++;
222 for (i
= 0; i
< 2; i
++) {
223 ret
= close(waitfd
[i
]);
236 * libnuma prints errors on the console even for numa_available().
237 * Work-around this limitation by using get_mempolicy() directly to
238 * check whether the kernel supports mempolicy.
241 static bool lttng_is_numa_available(void)
245 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
246 if (ret
&& errno
== ENOSYS
) {
249 return numa_available() > 0;
254 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
255 size_t memory_map_size
,
256 enum shm_object_type type
,
260 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
261 size_t memory_map_size
,
262 enum shm_object_type type
,
264 int cpu
__attribute__((unused
)))
267 struct shm_object
*shm_object
;
269 int oldnode
= 0, node
;
272 numa_avail
= lttng_is_numa_available();
274 oldnode
= numa_preferred();
276 node
= numa_node_of_cpu(cpu
);
278 numa_set_preferred(node
);
280 if (cpu
< 0 || node
< 0)
281 numa_set_localalloc();
283 #endif /* HAVE_LIBNUMA */
286 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
290 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
297 numa_set_preferred(oldnode
);
298 #endif /* HAVE_LIBNUMA */
302 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
303 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
304 size_t memory_map_size
)
306 struct shm_object
*obj
;
310 if (table
->allocated_len
>= table
->size
)
312 /* streams _must_ be received in sequential order, else fail. */
313 if (stream_nr
+ 1 != table
->allocated_len
)
316 obj
= &table
->objects
[table
->allocated_len
];
318 /* wait_fd: set write end of the pipe. */
319 obj
->wait_fd
[0] = -1; /* read end is unset */
320 obj
->wait_fd
[1] = wakeup_fd
;
321 obj
->shm_fd
= shm_fd
;
322 obj
->shm_fd_ownership
= 1;
324 /* The write end of the pipe needs to be non-blocking */
325 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
331 /* memory_map: mmap */
332 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
333 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
334 if (memory_map
== MAP_FAILED
) {
338 obj
->type
= SHM_OBJECT_SHM
;
339 obj
->memory_map
= memory_map
;
340 obj
->memory_map_size
= memory_map_size
;
341 obj
->allocated_len
= memory_map_size
;
342 obj
->index
= table
->allocated_len
++;
352 * Passing ownership of mem to object.
354 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
355 void *mem
, size_t memory_map_size
, int wakeup_fd
)
357 struct shm_object
*obj
;
360 if (table
->allocated_len
>= table
->size
)
362 obj
= &table
->objects
[table
->allocated_len
];
364 obj
->wait_fd
[0] = -1; /* read end is unset */
365 obj
->wait_fd
[1] = wakeup_fd
;
367 obj
->shm_fd_ownership
= 0;
369 /* The write end of the pipe needs to be non-blocking */
370 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
376 obj
->type
= SHM_OBJECT_MEM
;
377 obj
->memory_map
= mem
;
378 obj
->memory_map_size
= memory_map_size
;
379 obj
->allocated_len
= memory_map_size
;
380 obj
->index
= table
->allocated_len
++;
389 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
396 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
402 if (obj
->shm_fd_ownership
) {
403 /* Delete FDs only if called from app (not consumer). */
405 lttng_ust_lock_fd_tracker();
406 ret
= close(obj
->shm_fd
);
408 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
413 lttng_ust_unlock_fd_tracker();
415 ret
= close(obj
->shm_fd
);
422 for (i
= 0; i
< 2; i
++) {
423 if (obj
->wait_fd
[i
] < 0)
426 lttng_ust_lock_fd_tracker();
427 ret
= close(obj
->wait_fd
[i
]);
429 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
434 lttng_ust_unlock_fd_tracker();
436 ret
= close(obj
->wait_fd
[i
]);
449 for (i
= 0; i
< 2; i
++) {
450 if (obj
->wait_fd
[i
] < 0)
453 lttng_ust_lock_fd_tracker();
454 ret
= close(obj
->wait_fd
[i
]);
456 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
461 lttng_ust_unlock_fd_tracker();
463 ret
= close(obj
->wait_fd
[i
]);
470 free(obj
->memory_map
);
478 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
482 for (i
= 0; i
< table
->allocated_len
; i
++)
483 shmp_object_destroy(&table
->objects
[i
], consumer
);
488 * zalloc_shm - allocate memory within a shm object.
490 * Shared memory is already zeroed by shmget.
491 * *NOT* multithread-safe (should be protected by mutex).
492 * Returns a -1, -1 tuple on error.
494 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
497 struct shm_ref shm_ref_error
= { -1, -1 };
499 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
500 return shm_ref_error
;
501 ref
.index
= obj
->index
;
502 ref
.offset
= obj
->allocated_len
;
503 obj
->allocated_len
+= len
;
507 void align_shm(struct shm_object
*obj
, size_t align
)
509 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
510 obj
->allocated_len
+= offset_len
;
This page took 0.041707 seconds and 4 git commands to generate.