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>
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
));
134 ret
= zero_file(shmfd
, memory_map_size
);
137 goto error_zero_file
;
139 ret
= ftruncate(shmfd
, memory_map_size
);
142 goto error_ftruncate
;
145 * Also ensure the file metadata is synced with the storage by using
153 obj
->shm_fd_ownership
= 0;
156 /* memory_map: mmap */
157 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
158 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
159 if (memory_map
== MAP_FAILED
) {
163 obj
->type
= SHM_OBJECT_SHM
;
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 for (i
= 0; i
< 2; i
++) {
177 ret
= close(waitfd
[i
]);
188 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
189 size_t memory_map_size
)
191 struct shm_object
*obj
;
193 int waitfd
[2], i
, ret
;
195 if (table
->allocated_len
>= table
->size
)
197 obj
= &table
->objects
[table
->allocated_len
];
199 memory_map
= zmalloc(memory_map_size
);
203 /* wait_fd: create pipe */
209 for (i
= 0; i
< 2; i
++) {
210 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
216 /* The write end of the pipe needs to be non-blocking */
217 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
222 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
226 obj
->shm_fd_ownership
= 0;
228 obj
->type
= SHM_OBJECT_MEM
;
229 obj
->memory_map
= memory_map
;
230 obj
->memory_map_size
= memory_map_size
;
231 obj
->allocated_len
= 0;
232 obj
->index
= table
->allocated_len
++;
237 for (i
= 0; i
< 2; i
++) {
238 ret
= close(waitfd
[i
]);
251 * libnuma prints errors on the console even for numa_available().
252 * Work-around this limitation by using get_mempolicy() directly to
253 * check whether the kernel supports mempolicy.
256 static bool lttng_is_numa_available(void)
260 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
261 if (ret
&& errno
== ENOSYS
) {
264 return numa_available() > 0;
268 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
269 size_t memory_map_size
,
270 enum shm_object_type type
,
274 struct shm_object
*shm_object
;
276 int oldnode
= 0, node
;
279 numa_avail
= lttng_is_numa_available();
281 oldnode
= numa_preferred();
283 node
= numa_node_of_cpu(cpu
);
285 numa_set_preferred(node
);
287 if (cpu
< 0 || node
< 0)
288 numa_set_localalloc();
290 #endif /* HAVE_LIBNUMA */
293 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
297 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
304 numa_set_preferred(oldnode
);
305 #endif /* HAVE_LIBNUMA */
309 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
310 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
311 size_t memory_map_size
)
313 struct shm_object
*obj
;
317 if (table
->allocated_len
>= table
->size
)
319 /* streams _must_ be received in sequential order, else fail. */
320 if (stream_nr
+ 1 != table
->allocated_len
)
323 obj
= &table
->objects
[table
->allocated_len
];
325 /* wait_fd: set write end of the pipe. */
326 obj
->wait_fd
[0] = -1; /* read end is unset */
327 obj
->wait_fd
[1] = wakeup_fd
;
328 obj
->shm_fd
= shm_fd
;
329 obj
->shm_fd_ownership
= 1;
331 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
336 /* The write end of the pipe needs to be non-blocking */
337 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
343 /* memory_map: mmap */
344 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
345 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
346 if (memory_map
== MAP_FAILED
) {
350 obj
->type
= SHM_OBJECT_SHM
;
351 obj
->memory_map
= memory_map
;
352 obj
->memory_map_size
= memory_map_size
;
353 obj
->allocated_len
= memory_map_size
;
354 obj
->index
= table
->allocated_len
++;
364 * Passing ownership of mem to object.
366 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
367 void *mem
, size_t memory_map_size
, int wakeup_fd
)
369 struct shm_object
*obj
;
372 if (table
->allocated_len
>= table
->size
)
374 obj
= &table
->objects
[table
->allocated_len
];
376 obj
->wait_fd
[0] = -1; /* read end is unset */
377 obj
->wait_fd
[1] = wakeup_fd
;
379 obj
->shm_fd_ownership
= 0;
381 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
386 /* The write end of the pipe needs to be non-blocking */
387 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
393 obj
->type
= SHM_OBJECT_MEM
;
394 obj
->memory_map
= mem
;
395 obj
->memory_map_size
= memory_map_size
;
396 obj
->allocated_len
= memory_map_size
;
397 obj
->index
= table
->allocated_len
++;
406 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
413 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
419 if (obj
->shm_fd_ownership
) {
420 /* Delete FDs only if called from app (not consumer). */
422 lttng_ust_lock_fd_tracker();
423 ret
= close(obj
->shm_fd
);
425 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
430 lttng_ust_unlock_fd_tracker();
432 ret
= close(obj
->shm_fd
);
439 for (i
= 0; i
< 2; i
++) {
440 if (obj
->wait_fd
[i
] < 0)
443 lttng_ust_lock_fd_tracker();
444 ret
= close(obj
->wait_fd
[i
]);
446 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
451 lttng_ust_unlock_fd_tracker();
453 ret
= close(obj
->wait_fd
[i
]);
466 for (i
= 0; i
< 2; i
++) {
467 if (obj
->wait_fd
[i
] < 0)
470 lttng_ust_lock_fd_tracker();
471 ret
= close(obj
->wait_fd
[i
]);
473 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
478 lttng_ust_unlock_fd_tracker();
480 ret
= close(obj
->wait_fd
[i
]);
487 free(obj
->memory_map
);
495 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
499 for (i
= 0; i
< table
->allocated_len
; i
++)
500 shmp_object_destroy(&table
->objects
[i
], consumer
);
505 * zalloc_shm - allocate memory within a shm object.
507 * Shared memory is already zeroed by shmget.
508 * *NOT* multithread-safe (should be protected by mutex).
509 * Returns a -1, -1 tuple on error.
511 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
514 struct shm_ref shm_ref_error
= { -1, -1 };
516 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
517 return shm_ref_error
;
518 ref
.index
= obj
->index
;
519 ref
.offset
= obj
->allocated_len
;
520 obj
->allocated_len
+= len
;
524 void align_shm(struct shm_object
*obj
, size_t align
)
526 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
527 obj
->allocated_len
+= offset_len
;
This page took 0.049017 seconds and 4 git commands to generate.