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>
39 * Ensure we have the required amount of space available by writing 0
40 * into the entire buffer. Not doing so can trigger SIGBUS when going
41 * beyond the available shm space.
44 int zero_file(int fd
, size_t len
)
52 pagelen
= sysconf(_SC_PAGESIZE
);
55 zeropage
= calloc(pagelen
, 1);
59 while (len
> written
) {
61 retlen
= write(fd
, zeropage
,
62 min_t(size_t, pagelen
, len
- written
));
63 } while (retlen
== -1UL && errno
== EINTR
);
76 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
78 struct shm_object_table
*table
;
80 table
= zmalloc(sizeof(struct shm_object_table
) +
81 max_nb_obj
* sizeof(table
->objects
[0]));
84 table
->size
= max_nb_obj
;
89 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
90 size_t memory_map_size
,
93 int shmfd
, waitfd
[2], ret
, i
;
94 struct shm_object
*obj
;
99 if (table
->allocated_len
>= table
->size
)
101 obj
= &table
->objects
[table
->allocated_len
];
103 /* wait_fd: create pipe */
109 for (i
= 0; i
< 2; i
++) {
110 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
116 /* The write end of the pipe needs to be non-blocking */
117 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
122 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
127 ret
= zero_file(shmfd
, memory_map_size
);
130 goto error_zero_file
;
132 ret
= ftruncate(shmfd
, memory_map_size
);
135 goto error_ftruncate
;
138 * Also ensure the file metadata is synced with the storage by using
146 obj
->shm_fd_ownership
= 0;
149 /* memory_map: mmap */
150 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
151 MAP_SHARED
, shmfd
, 0);
152 if (memory_map
== MAP_FAILED
) {
156 obj
->type
= SHM_OBJECT_SHM
;
157 obj
->memory_map
= memory_map
;
158 obj
->memory_map_size
= memory_map_size
;
159 obj
->allocated_len
= 0;
160 obj
->index
= table
->allocated_len
++;
169 for (i
= 0; i
< 2; i
++) {
170 ret
= close(waitfd
[i
]);
181 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
182 size_t memory_map_size
)
184 struct shm_object
*obj
;
186 int waitfd
[2], i
, ret
;
188 if (table
->allocated_len
>= table
->size
)
190 obj
= &table
->objects
[table
->allocated_len
];
192 memory_map
= zmalloc(memory_map_size
);
196 /* wait_fd: create pipe */
202 for (i
= 0; i
< 2; i
++) {
203 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
209 /* The write end of the pipe needs to be non-blocking */
210 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
215 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
219 obj
->shm_fd_ownership
= 0;
221 obj
->type
= SHM_OBJECT_MEM
;
222 obj
->memory_map
= memory_map
;
223 obj
->memory_map_size
= memory_map_size
;
224 obj
->allocated_len
= 0;
225 obj
->index
= table
->allocated_len
++;
230 for (i
= 0; i
< 2; i
++) {
231 ret
= close(waitfd
[i
]);
243 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
244 size_t memory_map_size
,
245 enum shm_object_type type
,
250 return _shm_object_table_alloc_shm(table
, memory_map_size
,
253 return _shm_object_table_alloc_mem(table
, memory_map_size
);
260 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
261 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
262 size_t memory_map_size
)
264 struct shm_object
*obj
;
268 if (table
->allocated_len
>= table
->size
)
270 /* streams _must_ be received in sequential order, else fail. */
271 if (stream_nr
+ 1 != table
->allocated_len
)
274 obj
= &table
->objects
[table
->allocated_len
];
276 /* wait_fd: set write end of the pipe. */
277 obj
->wait_fd
[0] = -1; /* read end is unset */
278 obj
->wait_fd
[1] = wakeup_fd
;
279 obj
->shm_fd
= shm_fd
;
280 obj
->shm_fd_ownership
= 1;
282 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
287 /* The write end of the pipe needs to be non-blocking */
288 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
294 /* memory_map: mmap */
295 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
296 MAP_SHARED
, shm_fd
, 0);
297 if (memory_map
== MAP_FAILED
) {
301 obj
->type
= SHM_OBJECT_SHM
;
302 obj
->memory_map
= memory_map
;
303 obj
->memory_map_size
= memory_map_size
;
304 obj
->allocated_len
= memory_map_size
;
305 obj
->index
= table
->allocated_len
++;
315 * Passing ownership of mem to object.
317 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
318 void *mem
, size_t memory_map_size
, int wakeup_fd
)
320 struct shm_object
*obj
;
323 if (table
->allocated_len
>= table
->size
)
325 obj
= &table
->objects
[table
->allocated_len
];
327 obj
->wait_fd
[0] = -1; /* read end is unset */
328 obj
->wait_fd
[1] = wakeup_fd
;
330 obj
->shm_fd_ownership
= 0;
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 obj
->type
= SHM_OBJECT_MEM
;
345 obj
->memory_map
= mem
;
346 obj
->memory_map_size
= memory_map_size
;
347 obj
->allocated_len
= memory_map_size
;
348 obj
->index
= table
->allocated_len
++;
357 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
364 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
370 if (obj
->shm_fd_ownership
) {
371 /* Delete FDs only if called from app (not consumer). */
373 lttng_ust_lock_fd_tracker();
374 ret
= close(obj
->shm_fd
);
376 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
381 lttng_ust_unlock_fd_tracker();
383 ret
= close(obj
->shm_fd
);
390 for (i
= 0; i
< 2; i
++) {
391 if (obj
->wait_fd
[i
] < 0)
394 lttng_ust_lock_fd_tracker();
395 ret
= close(obj
->wait_fd
[i
]);
397 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
402 lttng_ust_unlock_fd_tracker();
404 ret
= close(obj
->wait_fd
[i
]);
417 for (i
= 0; i
< 2; i
++) {
418 if (obj
->wait_fd
[i
] < 0)
421 lttng_ust_lock_fd_tracker();
422 ret
= close(obj
->wait_fd
[i
]);
424 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
429 lttng_ust_unlock_fd_tracker();
431 ret
= close(obj
->wait_fd
[i
]);
438 free(obj
->memory_map
);
446 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
450 for (i
= 0; i
< table
->allocated_len
; i
++)
451 shmp_object_destroy(&table
->objects
[i
], consumer
);
456 * zalloc_shm - allocate memory within a shm object.
458 * Shared memory is already zeroed by shmget.
459 * *NOT* multithread-safe (should be protected by mutex).
460 * Returns a -1, -1 tuple on error.
462 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
465 struct shm_ref shm_ref_error
= { -1, -1 };
467 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
468 return shm_ref_error
;
469 ref
.index
= obj
->index
;
470 ref
.offset
= obj
->allocated_len
;
471 obj
->allocated_len
+= len
;
475 void align_shm(struct shm_object
*obj
, size_t align
)
477 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
478 obj
->allocated_len
+= offset_len
;
This page took 0.06155 seconds and 4 git commands to generate.