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
26 #include <sys/types.h>
27 #include <sys/stat.h> /* For mode constants */
28 #include <fcntl.h> /* For O_* constants */
33 #include <lttng/align.h>
46 * Ensure we have the required amount of space available by writing 0
47 * into the entire buffer. Not doing so can trigger SIGBUS when going
48 * beyond the available shm space.
51 int zero_file(int fd
, size_t len
)
59 pagelen
= sysconf(_SC_PAGESIZE
);
62 zeropage
= calloc(pagelen
, 1);
66 while (len
> written
) {
68 retlen
= write(fd
, zeropage
,
69 min_t(size_t, pagelen
, len
- written
));
70 } while (retlen
== -1UL && errno
== EINTR
);
83 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
85 struct shm_object_table
*table
;
87 table
= zmalloc(sizeof(struct shm_object_table
) +
88 max_nb_obj
* sizeof(table
->objects
[0]));
91 table
->size
= max_nb_obj
;
96 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
97 size_t memory_map_size
,
100 int shmfd
, waitfd
[2], ret
, i
;
101 struct shm_object
*obj
;
106 if (table
->allocated_len
>= table
->size
)
108 obj
= &table
->objects
[table
->allocated_len
];
110 /* wait_fd: create pipe */
116 for (i
= 0; i
< 2; i
++) {
117 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
123 /* The write end of the pipe needs to be non-blocking */
124 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
129 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
132 * Set POSIX shared memory object size
134 * First, use ftruncate() to set its size, some implementations won't
135 * allow writes past the size set by ftruncate.
136 * Then, use write() to fill it with zeros, this allows us to fully
137 * allocate it and detect a shortage of shm space without dealing with
142 ret
= ftruncate(shmfd
, memory_map_size
);
145 goto error_ftruncate
;
147 ret
= zero_file(shmfd
, memory_map_size
);
150 goto error_zero_file
;
154 * Also ensure the file metadata is synced with the storage by using
155 * fsync(2). Some platforms don't allow fsync on POSIX shm fds, ignore
156 * EINVAL accordingly.
159 if (ret
&& errno
!= EINVAL
) {
163 obj
->shm_fd_ownership
= 0;
166 /* memory_map: mmap */
167 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
168 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
169 if (memory_map
== MAP_FAILED
) {
173 obj
->type
= SHM_OBJECT_SHM
;
174 obj
->memory_map
= memory_map
;
175 obj
->memory_map_size
= memory_map_size
;
176 obj
->allocated_len
= 0;
177 obj
->index
= table
->allocated_len
++;
186 for (i
= 0; i
< 2; i
++) {
187 ret
= close(waitfd
[i
]);
198 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
199 size_t memory_map_size
)
201 struct shm_object
*obj
;
203 int waitfd
[2], i
, ret
;
205 if (table
->allocated_len
>= table
->size
)
207 obj
= &table
->objects
[table
->allocated_len
];
209 memory_map
= zmalloc(memory_map_size
);
213 /* wait_fd: create pipe */
219 for (i
= 0; i
< 2; i
++) {
220 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
226 /* The write end of the pipe needs to be non-blocking */
227 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
232 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
236 obj
->shm_fd_ownership
= 0;
238 obj
->type
= SHM_OBJECT_MEM
;
239 obj
->memory_map
= memory_map
;
240 obj
->memory_map_size
= memory_map_size
;
241 obj
->allocated_len
= 0;
242 obj
->index
= table
->allocated_len
++;
247 for (i
= 0; i
< 2; i
++) {
248 ret
= close(waitfd
[i
]);
261 * libnuma prints errors on the console even for numa_available().
262 * Work-around this limitation by using get_mempolicy() directly to
263 * check whether the kernel supports mempolicy.
266 static bool lttng_is_numa_available(void)
270 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
271 if (ret
&& errno
== ENOSYS
) {
274 return numa_available() > 0;
278 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
279 size_t memory_map_size
,
280 enum shm_object_type type
,
284 struct shm_object
*shm_object
;
286 int oldnode
= 0, node
;
289 numa_avail
= lttng_is_numa_available();
291 oldnode
= numa_preferred();
293 node
= numa_node_of_cpu(cpu
);
295 numa_set_preferred(node
);
297 if (cpu
< 0 || node
< 0)
298 numa_set_localalloc();
300 #endif /* HAVE_LIBNUMA */
303 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
307 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
314 numa_set_preferred(oldnode
);
315 #endif /* HAVE_LIBNUMA */
319 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
320 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
321 size_t memory_map_size
)
323 struct shm_object
*obj
;
327 if (table
->allocated_len
>= table
->size
)
329 /* streams _must_ be received in sequential order, else fail. */
330 if (stream_nr
+ 1 != table
->allocated_len
)
333 obj
= &table
->objects
[table
->allocated_len
];
335 /* wait_fd: set write end of the pipe. */
336 obj
->wait_fd
[0] = -1; /* read end is unset */
337 obj
->wait_fd
[1] = wakeup_fd
;
338 obj
->shm_fd
= shm_fd
;
339 obj
->shm_fd_ownership
= 1;
341 /* The write end of the pipe needs to be non-blocking */
342 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
348 /* memory_map: mmap */
349 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
350 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
351 if (memory_map
== MAP_FAILED
) {
355 obj
->type
= SHM_OBJECT_SHM
;
356 obj
->memory_map
= memory_map
;
357 obj
->memory_map_size
= memory_map_size
;
358 obj
->allocated_len
= memory_map_size
;
359 obj
->index
= table
->allocated_len
++;
369 * Passing ownership of mem to object.
371 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
372 void *mem
, size_t memory_map_size
, int wakeup_fd
)
374 struct shm_object
*obj
;
377 if (table
->allocated_len
>= table
->size
)
379 obj
= &table
->objects
[table
->allocated_len
];
381 obj
->wait_fd
[0] = -1; /* read end is unset */
382 obj
->wait_fd
[1] = wakeup_fd
;
384 obj
->shm_fd_ownership
= 0;
386 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
391 /* The write end of the pipe needs to be non-blocking */
392 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
398 obj
->type
= SHM_OBJECT_MEM
;
399 obj
->memory_map
= mem
;
400 obj
->memory_map_size
= memory_map_size
;
401 obj
->allocated_len
= memory_map_size
;
402 obj
->index
= table
->allocated_len
++;
411 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
418 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
424 if (obj
->shm_fd_ownership
) {
425 /* Delete FDs only if called from app (not consumer). */
427 lttng_ust_lock_fd_tracker();
428 ret
= close(obj
->shm_fd
);
430 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
435 lttng_ust_unlock_fd_tracker();
437 ret
= close(obj
->shm_fd
);
444 for (i
= 0; i
< 2; i
++) {
445 if (obj
->wait_fd
[i
] < 0)
448 lttng_ust_lock_fd_tracker();
449 ret
= close(obj
->wait_fd
[i
]);
451 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
456 lttng_ust_unlock_fd_tracker();
458 ret
= close(obj
->wait_fd
[i
]);
471 for (i
= 0; i
< 2; i
++) {
472 if (obj
->wait_fd
[i
] < 0)
475 lttng_ust_lock_fd_tracker();
476 ret
= close(obj
->wait_fd
[i
]);
478 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
483 lttng_ust_unlock_fd_tracker();
485 ret
= close(obj
->wait_fd
[i
]);
492 free(obj
->memory_map
);
500 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
504 for (i
= 0; i
< table
->allocated_len
; i
++)
505 shmp_object_destroy(&table
->objects
[i
], consumer
);
510 * zalloc_shm - allocate memory within a shm object.
512 * Shared memory is already zeroed by shmget.
513 * *NOT* multithread-safe (should be protected by mutex).
514 * Returns a -1, -1 tuple on error.
516 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
519 struct shm_ref shm_ref_error
= { -1, -1 };
521 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
522 return shm_ref_error
;
523 ref
.index
= obj
->index
;
524 ref
.offset
= obj
->allocated_len
;
525 obj
->allocated_len
+= len
;
529 void align_shm(struct shm_object
*obj
, size_t align
)
531 size_t offset_len
= lttng_ust_offset_align(obj
->allocated_len
, align
);
532 obj
->allocated_len
+= offset_len
;
This page took 0.041567 seconds and 4 git commands to generate.