2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
13 #include <sys/types.h>
14 #include <sys/stat.h> /* For mode constants */
15 #include <fcntl.h> /* For O_* constants */
29 #include <lttng/ust-align.h>
31 #include <ust-helper.h>
33 #include "../libringbuffer/mmap.h"
36 * Ensure we have the required amount of space available by writing 0
37 * into the entire buffer. Not doing so can trigger SIGBUS when going
38 * beyond the available shm space.
41 int zero_file(int fd
, size_t len
)
49 pagelen
= sysconf(_SC_PAGESIZE
);
52 zeropage
= calloc(pagelen
, 1);
56 while (len
> written
) {
58 retlen
= write(fd
, zeropage
,
59 min_t(size_t, pagelen
, len
- written
));
60 } while (retlen
== -1UL && errno
== EINTR
);
73 struct lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
)
75 struct lttng_counter_shm_object_table
*table
;
77 table
= zmalloc(sizeof(struct lttng_counter_shm_object_table
) +
78 max_nb_obj
* sizeof(table
->objects
[0]));
81 table
->size
= max_nb_obj
;
86 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_shm(struct lttng_counter_shm_object_table
*table
,
87 size_t memory_map_size
,
91 struct lttng_counter_shm_object
*obj
;
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;
125 /* memory_map: mmap */
126 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
127 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
128 if (memory_map
== MAP_FAILED
) {
132 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
133 obj
->memory_map
= memory_map
;
134 obj
->memory_map_size
= memory_map_size
;
135 obj
->allocated_len
= 0;
136 obj
->index
= table
->allocated_len
++;
148 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_mem(struct lttng_counter_shm_object_table
*table
,
149 size_t memory_map_size
)
151 struct lttng_counter_shm_object
*obj
;
154 if (table
->allocated_len
>= table
->size
)
156 obj
= &table
->objects
[table
->allocated_len
];
158 memory_map
= zmalloc(memory_map_size
);
164 obj
->shm_fd_ownership
= 0;
166 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
167 obj
->memory_map
= memory_map
;
168 obj
->memory_map_size
= memory_map_size
;
169 obj
->allocated_len
= 0;
170 obj
->index
= table
->allocated_len
++;
179 * libnuma prints errors on the console even for numa_available().
180 * Work-around this limitation by using get_mempolicy() directly to
181 * check whether the kernel supports mempolicy.
184 static bool lttng_is_numa_available(void)
188 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
189 if (ret
&& errno
== ENOSYS
) {
192 return numa_available() > 0;
196 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
197 size_t memory_map_size
,
198 enum lttng_counter_shm_object_type type
,
202 struct lttng_counter_shm_object
*shm_object
;
204 int oldnode
= 0, node
;
207 numa_avail
= lttng_is_numa_available();
209 oldnode
= numa_preferred();
211 node
= numa_node_of_cpu(cpu
);
213 numa_set_preferred(node
);
215 if (cpu
< 0 || node
< 0)
216 numa_set_localalloc();
218 #endif /* HAVE_LIBNUMA */
220 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
221 shm_object
= _lttng_counter_shm_object_table_alloc_shm(table
, memory_map_size
,
224 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
225 shm_object
= _lttng_counter_shm_object_table_alloc_mem(table
, memory_map_size
);
232 numa_set_preferred(oldnode
);
233 #endif /* HAVE_LIBNUMA */
237 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
239 size_t memory_map_size
)
241 struct lttng_counter_shm_object
*obj
;
244 if (table
->allocated_len
>= table
->size
)
247 obj
= &table
->objects
[table
->allocated_len
];
249 obj
->shm_fd
= shm_fd
;
250 obj
->shm_fd_ownership
= 1;
252 /* memory_map: mmap */
253 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
254 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
255 if (memory_map
== MAP_FAILED
) {
259 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
260 obj
->memory_map
= memory_map
;
261 obj
->memory_map_size
= memory_map_size
;
262 obj
->allocated_len
= memory_map_size
;
263 obj
->index
= table
->allocated_len
++;
272 * Passing ownership of mem to object.
274 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
275 void *mem
, size_t memory_map_size
)
277 struct lttng_counter_shm_object
*obj
;
279 if (table
->allocated_len
>= table
->size
)
281 obj
= &table
->objects
[table
->allocated_len
];
284 obj
->shm_fd_ownership
= 0;
286 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
287 obj
->memory_map
= mem
;
288 obj
->memory_map_size
= memory_map_size
;
289 obj
->allocated_len
= memory_map_size
;
290 obj
->index
= table
->allocated_len
++;
298 void lttng_counter_shmp_object_destroy(struct lttng_counter_shm_object
*obj
, int consumer
)
301 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
305 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
311 if (obj
->shm_fd_ownership
) {
312 /* Delete FDs only if called from app (not consumer). */
314 lttng_ust_lock_fd_tracker();
315 ret
= close(obj
->shm_fd
);
317 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
322 lttng_ust_unlock_fd_tracker();
324 ret
= close(obj
->shm_fd
);
333 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
335 free(obj
->memory_map
);
343 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
347 for (i
= 0; i
< table
->allocated_len
; i
++)
348 lttng_counter_shmp_object_destroy(&table
->objects
[i
], consumer
);
353 * lttng_counter_zalloc_shm - allocate memory within a shm object.
355 * Shared memory is already zeroed by shmget.
356 * *NOT* multithread-safe (should be protected by mutex).
357 * Returns a -1, -1 tuple on error.
359 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
361 struct lttng_counter_shm_ref ref
;
362 struct lttng_counter_shm_ref shm_ref_error
= { -1, -1 };
364 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
365 return shm_ref_error
;
366 ref
.index
= obj
->index
;
367 ref
.offset
= obj
->allocated_len
;
368 obj
->allocated_len
+= len
;
372 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
374 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
375 obj
->allocated_len
+= offset_len
;