44a830af20446142f8e21177b5187e8dfd6d4af0
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>
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 shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
86 struct shm_object_table
*table
;
88 table
= zmalloc(sizeof(struct shm_object_table
) +
89 max_nb_obj
* sizeof(table
->objects
[0]));
92 table
->size
= max_nb_obj
;
97 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
98 size_t memory_map_size
,
101 int shmfd
, waitfd
[2], ret
, i
;
102 struct shm_object
*obj
;
107 if (table
->allocated_len
>= table
->size
)
109 obj
= &table
->objects
[table
->allocated_len
];
111 /* wait_fd: create pipe */
117 for (i
= 0; i
< 2; i
++) {
118 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
124 /* The write end of the pipe needs to be non-blocking */
125 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
130 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
135 ret
= zero_file(shmfd
, memory_map_size
);
138 goto error_zero_file
;
140 ret
= ftruncate(shmfd
, memory_map_size
);
143 goto error_ftruncate
;
146 * Also ensure the file metadata is synced with the storage by using
154 obj
->shm_fd_ownership
= 0;
157 /* memory_map: mmap */
158 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
159 MAP_SHARED
| LTTNG_MAP_POPULATE
, shmfd
, 0);
160 if (memory_map
== MAP_FAILED
) {
164 obj
->type
= SHM_OBJECT_SHM
;
165 obj
->memory_map
= memory_map
;
166 obj
->memory_map_size
= memory_map_size
;
167 obj
->allocated_len
= 0;
168 obj
->index
= table
->allocated_len
++;
177 for (i
= 0; i
< 2; i
++) {
178 ret
= close(waitfd
[i
]);
189 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
190 size_t memory_map_size
)
192 struct shm_object
*obj
;
194 int waitfd
[2], i
, ret
;
196 if (table
->allocated_len
>= table
->size
)
198 obj
= &table
->objects
[table
->allocated_len
];
200 memory_map
= zmalloc(memory_map_size
);
204 /* wait_fd: create pipe */
210 for (i
= 0; i
< 2; i
++) {
211 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
217 /* The write end of the pipe needs to be non-blocking */
218 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
223 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
227 obj
->shm_fd_ownership
= 0;
229 obj
->type
= SHM_OBJECT_MEM
;
230 obj
->memory_map
= memory_map
;
231 obj
->memory_map_size
= memory_map_size
;
232 obj
->allocated_len
= 0;
233 obj
->index
= table
->allocated_len
++;
238 for (i
= 0; i
< 2; i
++) {
239 ret
= close(waitfd
[i
]);
252 * libnuma prints errors on the console even for numa_available().
253 * Work-around this limitation by using get_mempolicy() directly to
254 * check whether the kernel supports mempolicy.
257 static bool lttng_is_numa_available(void)
261 ret
= get_mempolicy(NULL
, NULL
, 0, NULL
, 0);
262 if (ret
&& errno
== ENOSYS
) {
265 return numa_available() > 0;
269 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
270 size_t memory_map_size
,
271 enum shm_object_type type
,
275 struct shm_object
*shm_object
;
277 int oldnode
= 0, node
;
280 numa_avail
= lttng_is_numa_available();
282 oldnode
= numa_preferred();
284 node
= numa_node_of_cpu(cpu
);
286 numa_set_preferred(node
);
288 if (cpu
< 0 || node
< 0)
289 numa_set_localalloc();
291 #endif /* HAVE_LIBNUMA */
294 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
298 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
305 numa_set_preferred(oldnode
);
306 #endif /* HAVE_LIBNUMA */
310 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
311 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
312 size_t memory_map_size
)
314 struct shm_object
*obj
;
318 if (table
->allocated_len
>= table
->size
)
320 /* streams _must_ be received in sequential order, else fail. */
321 if (stream_nr
+ 1 != table
->allocated_len
)
324 obj
= &table
->objects
[table
->allocated_len
];
326 /* wait_fd: set write end of the pipe. */
327 obj
->wait_fd
[0] = -1; /* read end is unset */
328 obj
->wait_fd
[1] = wakeup_fd
;
329 obj
->shm_fd
= shm_fd
;
330 obj
->shm_fd_ownership
= 1;
332 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
337 /* The write end of the pipe needs to be non-blocking */
338 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
344 /* memory_map: mmap */
345 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
346 MAP_SHARED
| LTTNG_MAP_POPULATE
, shm_fd
, 0);
347 if (memory_map
== MAP_FAILED
) {
351 obj
->type
= SHM_OBJECT_SHM
;
352 obj
->memory_map
= memory_map
;
353 obj
->memory_map_size
= memory_map_size
;
354 obj
->allocated_len
= memory_map_size
;
355 obj
->index
= table
->allocated_len
++;
365 * Passing ownership of mem to object.
367 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
368 void *mem
, size_t memory_map_size
, int wakeup_fd
)
370 struct shm_object
*obj
;
373 if (table
->allocated_len
>= table
->size
)
375 obj
= &table
->objects
[table
->allocated_len
];
377 obj
->wait_fd
[0] = -1; /* read end is unset */
378 obj
->wait_fd
[1] = wakeup_fd
;
380 obj
->shm_fd_ownership
= 0;
382 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
387 /* The write end of the pipe needs to be non-blocking */
388 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
394 obj
->type
= SHM_OBJECT_MEM
;
395 obj
->memory_map
= mem
;
396 obj
->memory_map_size
= memory_map_size
;
397 obj
->allocated_len
= memory_map_size
;
398 obj
->index
= table
->allocated_len
++;
407 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
414 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
420 if (obj
->shm_fd_ownership
) {
421 /* Delete FDs only if called from app (not consumer). */
423 lttng_ust_lock_fd_tracker();
424 ret
= close(obj
->shm_fd
);
426 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
431 lttng_ust_unlock_fd_tracker();
433 ret
= close(obj
->shm_fd
);
440 for (i
= 0; i
< 2; i
++) {
441 if (obj
->wait_fd
[i
] < 0)
444 lttng_ust_lock_fd_tracker();
445 ret
= close(obj
->wait_fd
[i
]);
447 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
452 lttng_ust_unlock_fd_tracker();
454 ret
= close(obj
->wait_fd
[i
]);
467 for (i
= 0; i
< 2; i
++) {
468 if (obj
->wait_fd
[i
] < 0)
471 lttng_ust_lock_fd_tracker();
472 ret
= close(obj
->wait_fd
[i
]);
474 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
479 lttng_ust_unlock_fd_tracker();
481 ret
= close(obj
->wait_fd
[i
]);
488 free(obj
->memory_map
);
496 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
500 for (i
= 0; i
< table
->allocated_len
; i
++)
501 shmp_object_destroy(&table
->objects
[i
], consumer
);
506 * zalloc_shm - allocate memory within a shm object.
508 * Shared memory is already zeroed by shmget.
509 * *NOT* multithread-safe (should be protected by mutex).
510 * Returns a -1, -1 tuple on error.
512 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
515 struct shm_ref shm_ref_error
= { -1, -1 };
517 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
518 return shm_ref_error
;
519 ref
.index
= obj
->index
;
520 ref
.offset
= obj
->allocated_len
;
521 obj
->allocated_len
+= len
;
525 void align_shm(struct shm_object
*obj
, size_t align
)
527 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
528 obj
->allocated_len
+= offset_len
;
This page took 0.039641 seconds and 4 git commands to generate.