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 */
19 #include <lttng/align.h>
27 #include <ust-helper.h>
32 * Ensure we have the required amount of space available by writing 0
33 * into the entire buffer. Not doing so can trigger SIGBUS when going
34 * beyond the available shm space.
37 int zero_file(int fd
, size_t len
)
45 pagelen
= sysconf(_SC_PAGESIZE
);
48 zeropage
= calloc(pagelen
, 1);
52 while (len
> written
) {
54 retlen
= write(fd
, zeropage
,
55 min_t(size_t, pagelen
, len
- written
));
56 } while (retlen
== -1UL && errno
== EINTR
);
69 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
71 struct shm_object_table
*table
;
73 table
= zmalloc(sizeof(struct shm_object_table
) +
74 max_nb_obj
* sizeof(table
->objects
[0]));
77 table
->size
= max_nb_obj
;
82 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
83 size_t memory_map_size
,
86 int shmfd
, waitfd
[2], ret
, i
;
87 struct shm_object
*obj
;
92 if (table
->allocated_len
>= table
->size
)
94 obj
= &table
->objects
[table
->allocated_len
];
96 /* wait_fd: create pipe */
102 for (i
= 0; i
< 2; i
++) {
103 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
109 /* The write end of the pipe needs to be non-blocking */
110 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
115 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
118 * Set POSIX shared memory object size
120 * First, use ftruncate() to set its size, some implementations won't
121 * allow writes past the size set by ftruncate.
122 * Then, use write() to fill it with zeros, this allows us to fully
123 * allocate it and detect a shortage of shm space without dealing with
128 ret
= ftruncate(shmfd
, memory_map_size
);
131 goto error_ftruncate
;
133 ret
= zero_file(shmfd
, memory_map_size
);
136 goto error_zero_file
;
140 * Also ensure the file metadata is synced with the storage by using
141 * fsync(2). Some platforms don't allow fsync on POSIX shm fds, ignore
142 * EINVAL accordingly.
145 if (ret
&& errno
!= EINVAL
) {
149 obj
->shm_fd_ownership
= 0;
152 /* memory_map: mmap */
153 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
154 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
155 if (memory_map
== MAP_FAILED
) {
159 obj
->type
= SHM_OBJECT_SHM
;
160 obj
->memory_map
= memory_map
;
161 obj
->memory_map_size
= memory_map_size
;
162 obj
->allocated_len
= 0;
163 obj
->index
= table
->allocated_len
++;
172 for (i
= 0; i
< 2; i
++) {
173 ret
= close(waitfd
[i
]);
184 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
185 size_t memory_map_size
)
187 struct shm_object
*obj
;
189 int waitfd
[2], i
, ret
;
191 if (table
->allocated_len
>= table
->size
)
193 obj
= &table
->objects
[table
->allocated_len
];
195 memory_map
= zmalloc(memory_map_size
);
199 /* wait_fd: create pipe */
205 for (i
= 0; i
< 2; i
++) {
206 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
212 /* The write end of the pipe needs to be non-blocking */
213 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
218 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
222 obj
->shm_fd_ownership
= 0;
224 obj
->type
= SHM_OBJECT_MEM
;
225 obj
->memory_map
= memory_map
;
226 obj
->memory_map_size
= memory_map_size
;
227 obj
->allocated_len
= 0;
228 obj
->index
= table
->allocated_len
++;
233 for (i
= 0; i
< 2; i
++) {
234 ret
= close(waitfd
[i
]);
247 * libnuma prints errors on the console even for numa_available().
248 * Work-around this limitation by using get_mempolicy() directly to
249 * check whether the kernel supports mempolicy.
252 static bool lttng_is_numa_available(void)
256 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
257 if (ret
&& errno
== ENOSYS
) {
260 return numa_available() > 0;
264 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
265 size_t memory_map_size
,
266 enum shm_object_type type
,
270 struct shm_object
*shm_object
;
272 int oldnode
= 0, node
;
275 numa_avail
= lttng_is_numa_available();
277 oldnode
= numa_preferred();
279 node
= numa_node_of_cpu(cpu
);
281 numa_set_preferred(node
);
283 if (cpu
< 0 || node
< 0)
284 numa_set_localalloc();
286 #endif /* HAVE_LIBNUMA */
289 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
293 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
300 numa_set_preferred(oldnode
);
301 #endif /* HAVE_LIBNUMA */
305 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
306 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
307 size_t memory_map_size
)
309 struct shm_object
*obj
;
313 if (table
->allocated_len
>= table
->size
)
315 /* streams _must_ be received in sequential order, else fail. */
316 if (stream_nr
+ 1 != table
->allocated_len
)
319 obj
= &table
->objects
[table
->allocated_len
];
321 /* wait_fd: set write end of the pipe. */
322 obj
->wait_fd
[0] = -1; /* read end is unset */
323 obj
->wait_fd
[1] = wakeup_fd
;
324 obj
->shm_fd
= shm_fd
;
325 obj
->shm_fd_ownership
= 1;
327 /* The write end of the pipe needs to be non-blocking */
328 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
334 /* memory_map: mmap */
335 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
336 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
337 if (memory_map
== MAP_FAILED
) {
341 obj
->type
= SHM_OBJECT_SHM
;
342 obj
->memory_map
= memory_map
;
343 obj
->memory_map_size
= memory_map_size
;
344 obj
->allocated_len
= memory_map_size
;
345 obj
->index
= table
->allocated_len
++;
355 * Passing ownership of mem to object.
357 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
358 void *mem
, size_t memory_map_size
, int wakeup_fd
)
360 struct shm_object
*obj
;
363 if (table
->allocated_len
>= table
->size
)
365 obj
= &table
->objects
[table
->allocated_len
];
367 obj
->wait_fd
[0] = -1; /* read end is unset */
368 obj
->wait_fd
[1] = wakeup_fd
;
370 obj
->shm_fd_ownership
= 0;
372 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
377 /* The write end of the pipe needs to be non-blocking */
378 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
384 obj
->type
= SHM_OBJECT_MEM
;
385 obj
->memory_map
= mem
;
386 obj
->memory_map_size
= memory_map_size
;
387 obj
->allocated_len
= memory_map_size
;
388 obj
->index
= table
->allocated_len
++;
397 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
404 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
410 if (obj
->shm_fd_ownership
) {
411 /* Delete FDs only if called from app (not consumer). */
413 lttng_ust_lock_fd_tracker();
414 ret
= close(obj
->shm_fd
);
416 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
421 lttng_ust_unlock_fd_tracker();
423 ret
= close(obj
->shm_fd
);
430 for (i
= 0; i
< 2; i
++) {
431 if (obj
->wait_fd
[i
] < 0)
434 lttng_ust_lock_fd_tracker();
435 ret
= close(obj
->wait_fd
[i
]);
437 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
442 lttng_ust_unlock_fd_tracker();
444 ret
= close(obj
->wait_fd
[i
]);
457 for (i
= 0; i
< 2; i
++) {
458 if (obj
->wait_fd
[i
] < 0)
461 lttng_ust_lock_fd_tracker();
462 ret
= close(obj
->wait_fd
[i
]);
464 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
469 lttng_ust_unlock_fd_tracker();
471 ret
= close(obj
->wait_fd
[i
]);
478 free(obj
->memory_map
);
486 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
490 for (i
= 0; i
< table
->allocated_len
; i
++)
491 shmp_object_destroy(&table
->objects
[i
], consumer
);
496 * zalloc_shm - allocate memory within a shm object.
498 * Shared memory is already zeroed by shmget.
499 * *NOT* multithread-safe (should be protected by mutex).
500 * Returns a -1, -1 tuple on error.
502 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
505 struct shm_ref shm_ref_error
= { -1, -1 };
507 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
508 return shm_ref_error
;
509 ref
.index
= obj
->index
;
510 ref
.offset
= obj
->allocated_len
;
511 obj
->allocated_len
+= len
;
515 void align_shm(struct shm_object
*obj
, size_t align
)
517 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
518 obj
->allocated_len
+= offset_len
;
This page took 0.050186 seconds and 4 git commands to generate.