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.
43 * Also ensure the file metadata is synced with the storage by using
47 int zero_file(int fd
, size_t len
)
55 pagelen
= sysconf(_SC_PAGESIZE
);
58 zeropage
= calloc(pagelen
, 1);
62 while (len
> written
) {
64 retlen
= write(fd
, zeropage
,
65 min_t(size_t, pagelen
, len
- written
));
66 } 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
;
145 obj
->shm_fd_ownership
= 0;
148 /* memory_map: mmap */
149 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
150 MAP_SHARED
, shmfd
, 0);
151 if (memory_map
== MAP_FAILED
) {
155 obj
->type
= SHM_OBJECT_SHM
;
156 obj
->memory_map
= memory_map
;
157 obj
->memory_map_size
= memory_map_size
;
158 obj
->allocated_len
= 0;
159 obj
->index
= table
->allocated_len
++;
167 for (i
= 0; i
< 2; i
++) {
168 ret
= close(waitfd
[i
]);
179 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
180 size_t memory_map_size
)
182 struct shm_object
*obj
;
184 int waitfd
[2], i
, ret
;
186 if (table
->allocated_len
>= table
->size
)
188 obj
= &table
->objects
[table
->allocated_len
];
190 memory_map
= zmalloc(memory_map_size
);
194 /* wait_fd: create pipe */
200 for (i
= 0; i
< 2; i
++) {
201 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
207 /* The write end of the pipe needs to be non-blocking */
208 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
213 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
217 obj
->shm_fd_ownership
= 0;
219 obj
->type
= SHM_OBJECT_MEM
;
220 obj
->memory_map
= memory_map
;
221 obj
->memory_map_size
= memory_map_size
;
222 obj
->allocated_len
= 0;
223 obj
->index
= table
->allocated_len
++;
228 for (i
= 0; i
< 2; i
++) {
229 ret
= close(waitfd
[i
]);
241 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
242 size_t memory_map_size
,
243 enum shm_object_type type
,
248 return _shm_object_table_alloc_shm(table
, memory_map_size
,
251 return _shm_object_table_alloc_mem(table
, memory_map_size
);
258 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
259 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
260 size_t memory_map_size
)
262 struct shm_object
*obj
;
266 if (table
->allocated_len
>= table
->size
)
268 /* streams _must_ be received in sequential order, else fail. */
269 if (stream_nr
+ 1 != table
->allocated_len
)
272 obj
= &table
->objects
[table
->allocated_len
];
274 /* wait_fd: set write end of the pipe. */
275 obj
->wait_fd
[0] = -1; /* read end is unset */
276 obj
->wait_fd
[1] = wakeup_fd
;
277 obj
->shm_fd
= shm_fd
;
278 obj
->shm_fd_ownership
= 1;
280 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
285 /* The write end of the pipe needs to be non-blocking */
286 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
292 /* memory_map: mmap */
293 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
294 MAP_SHARED
, shm_fd
, 0);
295 if (memory_map
== MAP_FAILED
) {
299 obj
->type
= SHM_OBJECT_SHM
;
300 obj
->memory_map
= memory_map
;
301 obj
->memory_map_size
= memory_map_size
;
302 obj
->allocated_len
= memory_map_size
;
303 obj
->index
= table
->allocated_len
++;
313 * Passing ownership of mem to object.
315 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
316 void *mem
, size_t memory_map_size
, int wakeup_fd
)
318 struct shm_object
*obj
;
321 if (table
->allocated_len
>= table
->size
)
323 obj
= &table
->objects
[table
->allocated_len
];
325 obj
->wait_fd
[0] = -1; /* read end is unset */
326 obj
->wait_fd
[1] = wakeup_fd
;
328 obj
->shm_fd_ownership
= 0;
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 obj
->type
= SHM_OBJECT_MEM
;
343 obj
->memory_map
= mem
;
344 obj
->memory_map_size
= memory_map_size
;
345 obj
->allocated_len
= memory_map_size
;
346 obj
->index
= table
->allocated_len
++;
355 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
362 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
368 if (obj
->shm_fd_ownership
) {
369 /* Delete FDs only if called from app (not consumer). */
371 lttng_ust_lock_fd_tracker();
372 ret
= close(obj
->shm_fd
);
374 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
379 lttng_ust_unlock_fd_tracker();
381 ret
= close(obj
->shm_fd
);
388 for (i
= 0; i
< 2; i
++) {
389 if (obj
->wait_fd
[i
] < 0)
392 lttng_ust_lock_fd_tracker();
393 ret
= close(obj
->wait_fd
[i
]);
395 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
400 lttng_ust_unlock_fd_tracker();
402 ret
= close(obj
->wait_fd
[i
]);
415 for (i
= 0; i
< 2; i
++) {
416 if (obj
->wait_fd
[i
] < 0)
419 lttng_ust_lock_fd_tracker();
420 ret
= close(obj
->wait_fd
[i
]);
422 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
427 lttng_ust_unlock_fd_tracker();
429 ret
= close(obj
->wait_fd
[i
]);
436 free(obj
->memory_map
);
444 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
448 for (i
= 0; i
< table
->allocated_len
; i
++)
449 shmp_object_destroy(&table
->objects
[i
], consumer
);
454 * zalloc_shm - allocate memory within a shm object.
456 * Shared memory is already zeroed by shmget.
457 * *NOT* multithread-safe (should be protected by mutex).
458 * Returns a -1, -1 tuple on error.
460 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
463 struct shm_ref shm_ref_error
= { -1, -1 };
465 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
466 return shm_ref_error
;
467 ref
.index
= obj
->index
;
468 ref
.offset
= obj
->allocated_len
;
469 obj
->allocated_len
+= len
;
473 void align_shm(struct shm_object
*obj
, size_t align
)
475 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
476 obj
->allocated_len
+= offset_len
;
This page took 0.042464 seconds and 4 git commands to generate.