4 * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; only
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
28 #include <sys/stat.h> /* For mode constants */
29 #include <fcntl.h> /* For O_* constants */
34 #include <lttng/align.h>
44 #include "../libringbuffer/mmap.h"
47 * Ensure we have the required amount of space available by writing 0
48 * into the entire buffer. Not doing so can trigger SIGBUS when going
49 * beyond the available shm space.
52 int zero_file(int fd
, size_t len
)
60 pagelen
= sysconf(_SC_PAGESIZE
);
63 zeropage
= calloc(pagelen
, 1);
67 while (len
> written
) {
69 retlen
= write(fd
, zeropage
,
70 min_t(size_t, pagelen
, len
- written
));
71 } while (retlen
== -1UL && errno
== EINTR
);
84 struct lttng_counter_shm_object_table
*lttng_counter_shm_object_table_create(size_t max_nb_obj
)
86 struct lttng_counter_shm_object_table
*table
;
88 table
= zmalloc(sizeof(struct lttng_counter_shm_object_table
) +
89 max_nb_obj
* sizeof(table
->objects
[0]));
92 table
->size
= max_nb_obj
;
97 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_shm(struct lttng_counter_shm_object_table
*table
,
98 size_t memory_map_size
,
102 struct lttng_counter_shm_object
*obj
;
107 if (table
->allocated_len
>= table
->size
)
109 obj
= &table
->objects
[table
->allocated_len
];
114 ret
= zero_file(shmfd
, memory_map_size
);
117 goto error_zero_file
;
119 ret
= ftruncate(shmfd
, memory_map_size
);
122 goto error_ftruncate
;
125 * Also ensure the file metadata is synced with the storage by using
133 obj
->shm_fd_ownership
= 0;
136 /* memory_map: mmap */
137 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
138 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
139 if (memory_map
== MAP_FAILED
) {
143 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
144 obj
->memory_map
= memory_map
;
145 obj
->memory_map_size
= memory_map_size
;
146 obj
->allocated_len
= 0;
147 obj
->index
= table
->allocated_len
++;
159 struct lttng_counter_shm_object
*_lttng_counter_shm_object_table_alloc_mem(struct lttng_counter_shm_object_table
*table
,
160 size_t memory_map_size
)
162 struct lttng_counter_shm_object
*obj
;
165 if (table
->allocated_len
>= table
->size
)
167 obj
= &table
->objects
[table
->allocated_len
];
169 memory_map
= zmalloc(memory_map_size
);
175 obj
->shm_fd_ownership
= 0;
177 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
178 obj
->memory_map
= memory_map
;
179 obj
->memory_map_size
= memory_map_size
;
180 obj
->allocated_len
= 0;
181 obj
->index
= table
->allocated_len
++;
190 * libnuma prints errors on the console even for numa_available().
191 * Work-around this limitation by using get_mempolicy() directly to
192 * check whether the kernel supports mempolicy.
195 static bool lttng_is_numa_available(void)
199 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
200 if (ret
&& errno
== ENOSYS
) {
203 return numa_available() > 0;
207 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table
*table
,
208 size_t memory_map_size
,
209 enum lttng_counter_shm_object_type type
,
213 struct lttng_counter_shm_object
*shm_object
;
215 int oldnode
= 0, node
;
218 numa_avail
= lttng_is_numa_available();
220 oldnode
= numa_preferred();
222 node
= numa_node_of_cpu(cpu
);
224 numa_set_preferred(node
);
226 if (cpu
< 0 || node
< 0)
227 numa_set_localalloc();
229 #endif /* HAVE_LIBNUMA */
231 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
232 shm_object
= _lttng_counter_shm_object_table_alloc_shm(table
, memory_map_size
,
235 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
236 shm_object
= _lttng_counter_shm_object_table_alloc_mem(table
, memory_map_size
);
243 numa_set_preferred(oldnode
);
244 #endif /* HAVE_LIBNUMA */
248 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table
*table
,
250 size_t memory_map_size
)
252 struct lttng_counter_shm_object
*obj
;
255 if (table
->allocated_len
>= table
->size
)
258 obj
= &table
->objects
[table
->allocated_len
];
260 obj
->shm_fd
= shm_fd
;
261 obj
->shm_fd_ownership
= 1;
263 /* memory_map: mmap */
264 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
265 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
266 if (memory_map
== MAP_FAILED
) {
270 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_SHM
;
271 obj
->memory_map
= memory_map
;
272 obj
->memory_map_size
= memory_map_size
;
273 obj
->allocated_len
= memory_map_size
;
274 obj
->index
= table
->allocated_len
++;
283 * Passing ownership of mem to object.
285 struct lttng_counter_shm_object
*lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table
*table
,
286 void *mem
, size_t memory_map_size
)
288 struct lttng_counter_shm_object
*obj
;
290 if (table
->allocated_len
>= table
->size
)
292 obj
= &table
->objects
[table
->allocated_len
];
295 obj
->shm_fd_ownership
= 0;
297 obj
->type
= LTTNG_COUNTER_SHM_OBJECT_MEM
;
298 obj
->memory_map
= mem
;
299 obj
->memory_map_size
= memory_map_size
;
300 obj
->allocated_len
= memory_map_size
;
301 obj
->index
= table
->allocated_len
++;
309 void lttng_counter_shmp_object_destroy(struct lttng_counter_shm_object
*obj
, int consumer
)
312 case LTTNG_COUNTER_SHM_OBJECT_SHM
:
316 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
322 if (obj
->shm_fd_ownership
) {
323 /* Delete FDs only if called from app (not consumer). */
325 lttng_ust_lock_fd_tracker();
326 ret
= close(obj
->shm_fd
);
328 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
333 lttng_ust_unlock_fd_tracker();
335 ret
= close(obj
->shm_fd
);
344 case LTTNG_COUNTER_SHM_OBJECT_MEM
:
346 free(obj
->memory_map
);
354 void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table
*table
, int consumer
)
358 for (i
= 0; i
< table
->allocated_len
; i
++)
359 lttng_counter_shmp_object_destroy(&table
->objects
[i
], consumer
);
364 * lttng_counter_zalloc_shm - allocate memory within a shm object.
366 * Shared memory is already zeroed by shmget.
367 * *NOT* multithread-safe (should be protected by mutex).
368 * Returns a -1, -1 tuple on error.
370 struct lttng_counter_shm_ref
lttng_counter_zalloc_shm(struct lttng_counter_shm_object
*obj
, size_t len
)
372 struct lttng_counter_shm_ref ref
;
373 struct lttng_counter_shm_ref shm_ref_error
= { -1, -1 };
375 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
376 return shm_ref_error
;
377 ref
.index
= obj
->index
;
378 ref
.offset
= obj
->allocated_len
;
379 obj
->allocated_len
+= len
;
383 void lttng_counter_align_shm(struct lttng_counter_shm_object
*obj
, size_t align
)
385 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
386 obj
->allocated_len
+= offset_len
;