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 */
20 #include <lttng/align.h>
28 #include <ust-helper.h>
30 #include "../libringbuffer/mmap.h"
33 * Ensure we have the required amount of space available by writing 0
34 * into the entire buffer. Not doing so can trigger SIGBUS when going
35 * beyond the available shm space.
38 int zero_file(int fd
, size_t len
)
46 pagelen
= sysconf(_SC_PAGESIZE
);
49 zeropage
= calloc(pagelen
, 1);
53 while (len
> written
) {
55 retlen
= write(fd
, zeropage
,
56 min_t(size_t, pagelen
, len
- written
));
57 } while (retlen
== -1UL && errno
== EINTR
);
70 struct lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
)
72 struct lttng_counter_shm_object_table
*table
;
74 table
= zmalloc(sizeof(struct lttng_counter_shm_object_table
) +
75 max_nb_obj
* sizeof(table
->objects
[0]));
78 table
->size
= max_nb_obj
;
83 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_shm(struct lttng_counter_shm_object_table
*table
,
84 size_t memory_map_size
,
88 struct lttng_counter_shm_object
*obj
;
93 if (table
->allocated_len
>= table
->size
)
95 obj
= &table
->objects
[table
->allocated_len
];
100 ret
= zero_file(shmfd
, memory_map_size
);
103 goto error_zero_file
;
105 ret
= ftruncate(shmfd
, memory_map_size
);
108 goto error_ftruncate
;
111 * Also ensure the file metadata is synced with the storage by using
119 obj
->shm_fd_ownership
= 0;
122 /* memory_map: mmap */
123 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
124 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
125 if (memory_map
== MAP_FAILED
) {
129 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
130 obj
->memory_map
= memory_map
;
131 obj
->memory_map_size
= memory_map_size
;
132 obj
->allocated_len
= 0;
133 obj
->index
= table
->allocated_len
++;
145 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_mem(struct lttng_counter_shm_object_table
*table
,
146 size_t memory_map_size
)
148 struct lttng_counter_shm_object
*obj
;
151 if (table
->allocated_len
>= table
->size
)
153 obj
= &table
->objects
[table
->allocated_len
];
155 memory_map
= zmalloc(memory_map_size
);
161 obj
->shm_fd_ownership
= 0;
163 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
164 obj
->memory_map
= memory_map
;
165 obj
->memory_map_size
= memory_map_size
;
166 obj
->allocated_len
= 0;
167 obj
->index
= table
->allocated_len
++;
176 * libnuma prints errors on the console even for numa_available().
177 * Work-around this limitation by using get_mempolicy() directly to
178 * check whether the kernel supports mempolicy.
181 static bool lttng_is_numa_available(void)
185 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
186 if (ret
&& errno
== ENOSYS
) {
189 return numa_available() > 0;
193 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
194 size_t memory_map_size
,
195 enum lttng_counter_shm_object_type type
,
199 struct lttng_counter_shm_object
*shm_object
;
201 int oldnode
= 0, node
;
204 numa_avail
= lttng_is_numa_available();
206 oldnode
= numa_preferred();
208 node
= numa_node_of_cpu(cpu
);
210 numa_set_preferred(node
);
212 if (cpu
< 0 || node
< 0)
213 numa_set_localalloc();
215 #endif /* HAVE_LIBNUMA */
217 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
218 shm_object
= _lttng_counter_shm_object_table_alloc_shm(table
, memory_map_size
,
221 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
222 shm_object
= _lttng_counter_shm_object_table_alloc_mem(table
, memory_map_size
);
229 numa_set_preferred(oldnode
);
230 #endif /* HAVE_LIBNUMA */
234 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
236 size_t memory_map_size
)
238 struct lttng_counter_shm_object
*obj
;
241 if (table
->allocated_len
>= table
->size
)
244 obj
= &table
->objects
[table
->allocated_len
];
246 obj
->shm_fd
= shm_fd
;
247 obj
->shm_fd_ownership
= 1;
249 /* memory_map: mmap */
250 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
251 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
252 if (memory_map
== MAP_FAILED
) {
256 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
257 obj
->memory_map
= memory_map
;
258 obj
->memory_map_size
= memory_map_size
;
259 obj
->allocated_len
= memory_map_size
;
260 obj
->index
= table
->allocated_len
++;
269 * Passing ownership of mem to object.
271 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
272 void *mem
, size_t memory_map_size
)
274 struct lttng_counter_shm_object
*obj
;
276 if (table
->allocated_len
>= table
->size
)
278 obj
= &table
->objects
[table
->allocated_len
];
281 obj
->shm_fd_ownership
= 0;
283 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
284 obj
->memory_map
= mem
;
285 obj
->memory_map_size
= memory_map_size
;
286 obj
->allocated_len
= memory_map_size
;
287 obj
->index
= table
->allocated_len
++;
295 void lttng_counter_shmp_object_destroy(struct lttng_counter_shm_object
*obj
, int consumer
)
298 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
302 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
308 if (obj
->shm_fd_ownership
) {
309 /* Delete FDs only if called from app (not consumer). */
311 lttng_ust_lock_fd_tracker();
312 ret
= close(obj
->shm_fd
);
314 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
319 lttng_ust_unlock_fd_tracker();
321 ret
= close(obj
->shm_fd
);
330 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
332 free(obj
->memory_map
);
340 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
344 for (i
= 0; i
< table
->allocated_len
; i
++)
345 lttng_counter_shmp_object_destroy(&table
->objects
[i
], consumer
);
350 * lttng_counter_zalloc_shm - allocate memory within a shm object.
352 * Shared memory is already zeroed by shmget.
353 * *NOT* multithread-safe (should be protected by mutex).
354 * Returns a -1, -1 tuple on error.
356 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
358 struct lttng_counter_shm_ref ref
;
359 struct lttng_counter_shm_ref shm_ref_error
= { -1, -1 };
361 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
362 return shm_ref_error
;
363 ref
.index
= obj
->index
;
364 ref
.offset
= obj
->allocated_len
;
365 obj
->allocated_len
+= len
;
369 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
371 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
372 obj
->allocated_len
+= offset_len
;