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>
45 * Ensure we have the required amount of space available by writing 0
46 * into the entire buffer. Not doing so can trigger SIGBUS when going
47 * beyond the available shm space.
50 int zero_file(int fd
, size_t len
)
58 pagelen
= sysconf(_SC_PAGESIZE
);
61 zeropage
= calloc(pagelen
, 1);
65 while (len
> written
) {
67 retlen
= write(fd
, zeropage
,
68 min_t(size_t, pagelen
, len
- written
));
69 } while (retlen
== -1UL && errno
== EINTR
);
82 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
84 struct shm_object_table
*table
;
86 table
= zmalloc(sizeof(struct shm_object_table
) +
87 max_nb_obj
* sizeof(table
->objects
[0]));
90 table
->size
= max_nb_obj
;
95 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
96 size_t memory_map_size
,
99 int shmfd
, waitfd
[2], ret
, i
;
100 struct shm_object
*obj
;
105 if (table
->allocated_len
>= table
->size
)
107 obj
= &table
->objects
[table
->allocated_len
];
109 /* wait_fd: create pipe */
115 for (i
= 0; i
< 2; i
++) {
116 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
122 /* The write end of the pipe needs to be non-blocking */
123 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
128 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
133 ret
= zero_file(shmfd
, memory_map_size
);
136 goto error_zero_file
;
138 ret
= ftruncate(shmfd
, memory_map_size
);
141 goto error_ftruncate
;
144 * Also ensure the file metadata is synced with the storage by using
152 obj
->shm_fd_ownership
= 0;
155 /* memory_map: mmap */
156 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
157 MAP_SHARED
, shmfd
, 0);
158 if (memory_map
== MAP_FAILED
) {
162 obj
->type
= SHM_OBJECT_SHM
;
163 obj
->memory_map
= memory_map
;
164 obj
->memory_map_size
= memory_map_size
;
165 obj
->allocated_len
= 0;
166 obj
->index
= table
->allocated_len
++;
175 for (i
= 0; i
< 2; i
++) {
176 ret
= close(waitfd
[i
]);
187 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
188 size_t memory_map_size
)
190 struct shm_object
*obj
;
192 int waitfd
[2], i
, ret
;
194 if (table
->allocated_len
>= table
->size
)
196 obj
= &table
->objects
[table
->allocated_len
];
198 memory_map
= zmalloc(memory_map_size
);
202 /* wait_fd: create pipe */
208 for (i
= 0; i
< 2; i
++) {
209 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
215 /* The write end of the pipe needs to be non-blocking */
216 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
221 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
225 obj
->shm_fd_ownership
= 0;
227 obj
->type
= SHM_OBJECT_MEM
;
228 obj
->memory_map
= memory_map
;
229 obj
->memory_map_size
= memory_map_size
;
230 obj
->allocated_len
= 0;
231 obj
->index
= table
->allocated_len
++;
236 for (i
= 0; i
< 2; i
++) {
237 ret
= close(waitfd
[i
]);
250 * libnuma prints errors on the console even for numa_available().
251 * Work-around this limitation by using get_mempolicy() directly to
252 * check whether the kernel supports mempolicy.
255 static bool lttng_is_numa_available(void)
259 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
260 if (ret
&& errno
== ENOSYS
) {
263 return numa_available() > 0;
267 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
268 size_t memory_map_size
,
269 enum shm_object_type type
,
273 struct shm_object
*shm_object
;
275 int oldnode
= 0, node
;
278 numa_avail
= lttng_is_numa_available();
280 oldnode
= numa_preferred();
282 node
= numa_node_of_cpu(cpu
);
284 numa_set_preferred(node
);
286 if (cpu
< 0 || node
< 0)
287 numa_set_localalloc();
289 #endif /* HAVE_LIBNUMA */
292 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
296 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
303 numa_set_preferred(oldnode
);
304 #endif /* HAVE_LIBNUMA */
308 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
309 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
310 size_t memory_map_size
)
312 struct shm_object
*obj
;
316 if (table
->allocated_len
>= table
->size
)
318 /* streams _must_ be received in sequential order, else fail. */
319 if (stream_nr
+ 1 != table
->allocated_len
)
322 obj
= &table
->objects
[table
->allocated_len
];
324 /* wait_fd: set write end of the pipe. */
325 obj
->wait_fd
[0] = -1; /* read end is unset */
326 obj
->wait_fd
[1] = wakeup_fd
;
327 obj
->shm_fd
= shm_fd
;
328 obj
->shm_fd_ownership
= 1;
330 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
335 /* The write end of the pipe needs to be non-blocking */
336 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
342 /* memory_map: mmap */
343 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
344 MAP_SHARED
, shm_fd
, 0);
345 if (memory_map
== MAP_FAILED
) {
349 obj
->type
= SHM_OBJECT_SHM
;
350 obj
->memory_map
= memory_map
;
351 obj
->memory_map_size
= memory_map_size
;
352 obj
->allocated_len
= memory_map_size
;
353 obj
->index
= table
->allocated_len
++;
363 * Passing ownership of mem to object.
365 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
366 void *mem
, size_t memory_map_size
, int wakeup_fd
)
368 struct shm_object
*obj
;
371 if (table
->allocated_len
>= table
->size
)
373 obj
= &table
->objects
[table
->allocated_len
];
375 obj
->wait_fd
[0] = -1; /* read end is unset */
376 obj
->wait_fd
[1] = wakeup_fd
;
378 obj
->shm_fd_ownership
= 0;
380 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
385 /* The write end of the pipe needs to be non-blocking */
386 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
392 obj
->type
= SHM_OBJECT_MEM
;
393 obj
->memory_map
= mem
;
394 obj
->memory_map_size
= memory_map_size
;
395 obj
->allocated_len
= memory_map_size
;
396 obj
->index
= table
->allocated_len
++;
405 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
412 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
418 if (obj
->shm_fd_ownership
) {
419 /* Delete FDs only if called from app (not consumer). */
421 lttng_ust_lock_fd_tracker();
422 ret
= close(obj
->shm_fd
);
424 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
429 lttng_ust_unlock_fd_tracker();
431 ret
= close(obj
->shm_fd
);
438 for (i
= 0; i
< 2; i
++) {
439 if (obj
->wait_fd
[i
] < 0)
442 lttng_ust_lock_fd_tracker();
443 ret
= close(obj
->wait_fd
[i
]);
445 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
450 lttng_ust_unlock_fd_tracker();
452 ret
= close(obj
->wait_fd
[i
]);
465 for (i
= 0; i
< 2; i
++) {
466 if (obj
->wait_fd
[i
] < 0)
469 lttng_ust_lock_fd_tracker();
470 ret
= close(obj
->wait_fd
[i
]);
472 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
477 lttng_ust_unlock_fd_tracker();
479 ret
= close(obj
->wait_fd
[i
]);
486 free(obj
->memory_map
);
494 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
498 for (i
= 0; i
< table
->allocated_len
; i
++)
499 shmp_object_destroy(&table
->objects
[i
], consumer
);
504 * zalloc_shm - allocate memory within a shm object.
506 * Shared memory is already zeroed by shmget.
507 * *NOT* multithread-safe (should be protected by mutex).
508 * Returns a -1, -1 tuple on error.
510 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
513 struct shm_ref shm_ref_error
= { -1, -1 };
515 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
516 return shm_ref_error
;
517 ref
.index
= obj
->index
;
518 ref
.offset
= obj
->allocated_len
;
519 obj
->allocated_len
+= len
;
523 void align_shm(struct shm_object
*obj
, size_t align
)
525 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
526 obj
->allocated_len
+= offset_len
;
This page took 0.049169 seconds and 4 git commands to generate.