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 lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
, bool populate
)
74 struct lttng_counter_shm_object_table
*table
;
76 table
= zmalloc_populate(sizeof(struct lttng_counter_shm_object_table
) +
77 max_nb_obj
* sizeof(table
->objects
[0]), populate
);
80 table
->size
= max_nb_obj
;
85 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_shm(struct lttng_counter_shm_object_table
*table
,
86 size_t memory_map_size
,
87 int cpu_fd
, bool populate
)
89 struct lttng_counter_shm_object
*obj
;
90 int flags
= MAP_SHARED
;
96 if (table
->allocated_len
>= table
->size
)
98 obj
= &table
->objects
[table
->allocated_len
];
103 ret
= zero_file(shmfd
, memory_map_size
);
106 goto error_zero_file
;
108 ret
= ftruncate(shmfd
, memory_map_size
);
111 goto error_ftruncate
;
114 * Also ensure the file metadata is synced with the storage by using
122 obj
->shm_fd_ownership
= 0;
126 flags
|= LTTNG_MAP_POPULATE
;
127 /* memory_map: mmap */
128 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
130 if (memory_map
== MAP_FAILED
) {
134 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
135 obj
->memory_map
= memory_map
;
136 obj
->memory_map_size
= memory_map_size
;
137 obj
->allocated_len
= 0;
138 obj
->index
= table
->allocated_len
++;
150 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_mem(struct lttng_counter_shm_object_table
*table
,
151 size_t memory_map_size
, bool populate
)
153 struct lttng_counter_shm_object
*obj
;
156 if (table
->allocated_len
>= table
->size
)
158 obj
= &table
->objects
[table
->allocated_len
];
160 memory_map
= zmalloc_populate(memory_map_size
, populate
);
166 obj
->shm_fd_ownership
= 0;
168 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
169 obj
->memory_map
= memory_map
;
170 obj
->memory_map_size
= memory_map_size
;
171 obj
->allocated_len
= 0;
172 obj
->index
= table
->allocated_len
++;
181 * libnuma prints errors on the console even for numa_available().
182 * Work-around this limitation by using get_mempolicy() directly to
183 * check whether the kernel supports mempolicy.
186 static bool lttng_is_numa_available(void)
190 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
191 if (ret
&& errno
== ENOSYS
) {
194 return numa_available() > 0;
199 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
200 size_t memory_map_size
,
201 enum lttng_counter_shm_object_type type
,
206 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
207 size_t memory_map_size
,
208 enum lttng_counter_shm_object_type type
,
210 int cpu
__attribute__((unused
)),
214 struct lttng_counter_shm_object
*shm_object
;
216 int oldnode
= 0, node
;
219 numa_avail
= lttng_is_numa_available();
221 oldnode
= numa_preferred();
223 node
= numa_node_of_cpu(cpu
);
225 numa_set_preferred(node
);
227 if (cpu
< 0 || node
< 0)
228 numa_set_localalloc();
230 #endif /* HAVE_LIBNUMA */
232 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
233 shm_object
= _lttng_counter_shm_object_table_alloc_shm(table
, memory_map_size
,
236 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
237 shm_object
= _lttng_counter_shm_object_table_alloc_mem(table
, memory_map_size
,
245 numa_set_preferred(oldnode
);
246 #endif /* HAVE_LIBNUMA */
250 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
251 int shm_fd
, size_t memory_map_size
, bool populate
)
253 struct lttng_counter_shm_object
*obj
;
254 int flags
= MAP_SHARED
;
257 if (table
->allocated_len
>= table
->size
)
260 obj
= &table
->objects
[table
->allocated_len
];
262 obj
->shm_fd
= shm_fd
;
263 obj
->shm_fd_ownership
= 1;
266 flags
|= LTTNG_MAP_POPULATE
;
267 /* memory_map: mmap */
268 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
270 if (memory_map
== MAP_FAILED
) {
274 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
275 obj
->memory_map
= memory_map
;
276 obj
->memory_map_size
= memory_map_size
;
277 obj
->allocated_len
= memory_map_size
;
278 obj
->index
= table
->allocated_len
++;
287 * Passing ownership of mem to object.
289 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
290 void *mem
, size_t memory_map_size
)
292 struct lttng_counter_shm_object
*obj
;
294 if (table
->allocated_len
>= table
->size
)
296 obj
= &table
->objects
[table
->allocated_len
];
299 obj
->shm_fd_ownership
= 0;
301 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
302 obj
->memory_map
= mem
;
303 obj
->memory_map_size
= memory_map_size
;
304 obj
->allocated_len
= memory_map_size
;
305 obj
->index
= table
->allocated_len
++;
313 void lttng_counter_shmp_object_destroy(struct lttng_counter_shm_object
*obj
, int consumer
)
316 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
320 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
326 if (obj
->shm_fd_ownership
) {
327 /* Delete FDs only if called from app (not consumer). */
329 lttng_ust_lock_fd_tracker();
330 ret
= close(obj
->shm_fd
);
332 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
337 lttng_ust_unlock_fd_tracker();
339 ret
= close(obj
->shm_fd
);
348 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
350 free(obj
->memory_map
);
358 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
362 for (i
= 0; i
< table
->allocated_len
; i
++)
363 lttng_counter_shmp_object_destroy(&table
->objects
[i
], consumer
);
368 * lttng_counter_zalloc_shm - allocate memory within a shm object.
370 * Shared memory is already zeroed by shmget.
371 * *NOT* multithread-safe (should be protected by mutex).
372 * Returns a -1, -1 tuple on error.
374 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
376 struct lttng_counter_shm_ref ref
;
377 struct lttng_counter_shm_ref shm_ref_error
= { -1, -1 };
379 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
380 return shm_ref_error
;
381 ref
.index
= obj
->index
;
382 ref
.offset
= obj
->allocated_len
;
383 obj
->allocated_len
+= len
;
387 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
389 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
390 obj
->allocated_len
+= offset_len
;