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
;
137 obj
->shm_fd_ownership
= 0;
140 /* memory_map: mmap */
141 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
142 MAP_SHARED
, shmfd
, 0);
143 if (memory_map
== MAP_FAILED
) {
147 obj
->type
= SHM_OBJECT_SHM
;
148 obj
->memory_map
= memory_map
;
149 obj
->memory_map_size
= memory_map_size
;
150 obj
->allocated_len
= 0;
151 obj
->index
= table
->allocated_len
++;
159 for (i
= 0; i
< 2; i
++) {
160 ret
= close(waitfd
[i
]);
171 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
172 size_t memory_map_size
)
174 struct shm_object
*obj
;
176 int waitfd
[2], i
, ret
;
178 if (table
->allocated_len
>= table
->size
)
180 obj
= &table
->objects
[table
->allocated_len
];
182 memory_map
= zmalloc(memory_map_size
);
186 /* wait_fd: create pipe */
192 for (i
= 0; i
< 2; i
++) {
193 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
199 /* The write end of the pipe needs to be non-blocking */
200 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
205 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
209 obj
->shm_fd_ownership
= 0;
211 obj
->type
= SHM_OBJECT_MEM
;
212 obj
->memory_map
= memory_map
;
213 obj
->memory_map_size
= memory_map_size
;
214 obj
->allocated_len
= 0;
215 obj
->index
= table
->allocated_len
++;
220 for (i
= 0; i
< 2; i
++) {
221 ret
= close(waitfd
[i
]);
233 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
234 size_t memory_map_size
,
235 enum shm_object_type type
,
240 return _shm_object_table_alloc_shm(table
, memory_map_size
,
243 return _shm_object_table_alloc_mem(table
, memory_map_size
);
250 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
251 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
252 size_t memory_map_size
)
254 struct shm_object
*obj
;
258 if (table
->allocated_len
>= table
->size
)
260 /* streams _must_ be received in sequential order, else fail. */
261 if (stream_nr
+ 1 != table
->allocated_len
)
264 obj
= &table
->objects
[table
->allocated_len
];
266 /* wait_fd: set write end of the pipe. */
267 obj
->wait_fd
[0] = -1; /* read end is unset */
268 obj
->wait_fd
[1] = wakeup_fd
;
269 obj
->shm_fd
= shm_fd
;
270 obj
->shm_fd_ownership
= 1;
272 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
277 /* The write end of the pipe needs to be non-blocking */
278 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
284 /* memory_map: mmap */
285 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
286 MAP_SHARED
, shm_fd
, 0);
287 if (memory_map
== MAP_FAILED
) {
291 obj
->type
= SHM_OBJECT_SHM
;
292 obj
->memory_map
= memory_map
;
293 obj
->memory_map_size
= memory_map_size
;
294 obj
->allocated_len
= memory_map_size
;
295 obj
->index
= table
->allocated_len
++;
305 * Passing ownership of mem to object.
307 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
308 void *mem
, size_t memory_map_size
, int wakeup_fd
)
310 struct shm_object
*obj
;
313 if (table
->allocated_len
>= table
->size
)
315 obj
= &table
->objects
[table
->allocated_len
];
317 obj
->wait_fd
[0] = -1; /* read end is unset */
318 obj
->wait_fd
[1] = wakeup_fd
;
320 obj
->shm_fd_ownership
= 0;
322 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
327 /* The write end of the pipe needs to be non-blocking */
328 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
334 obj
->type
= SHM_OBJECT_MEM
;
335 obj
->memory_map
= mem
;
336 obj
->memory_map_size
= memory_map_size
;
337 obj
->allocated_len
= memory_map_size
;
338 obj
->index
= table
->allocated_len
++;
347 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
354 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
360 if (obj
->shm_fd_ownership
) {
361 /* Delete FDs only if called from app (not consumer). */
363 lttng_ust_lock_fd_tracker();
364 ret
= close(obj
->shm_fd
);
366 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
371 lttng_ust_unlock_fd_tracker();
373 ret
= close(obj
->shm_fd
);
380 for (i
= 0; i
< 2; i
++) {
381 if (obj
->wait_fd
[i
] < 0)
384 lttng_ust_lock_fd_tracker();
385 ret
= close(obj
->wait_fd
[i
]);
387 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
392 lttng_ust_unlock_fd_tracker();
394 ret
= close(obj
->wait_fd
[i
]);
407 for (i
= 0; i
< 2; i
++) {
408 if (obj
->wait_fd
[i
] < 0)
411 lttng_ust_lock_fd_tracker();
412 ret
= close(obj
->wait_fd
[i
]);
414 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
419 lttng_ust_unlock_fd_tracker();
421 ret
= close(obj
->wait_fd
[i
]);
428 free(obj
->memory_map
);
436 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
440 for (i
= 0; i
< table
->allocated_len
; i
++)
441 shmp_object_destroy(&table
->objects
[i
], consumer
);
446 * zalloc_shm - allocate memory within a shm object.
448 * Shared memory is already zeroed by shmget.
449 * *NOT* multithread-safe (should be protected by mutex).
450 * Returns a -1, -1 tuple on error.
452 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
455 struct shm_ref shm_ref_error
= { -1, -1 };
457 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
458 return shm_ref_error
;
459 ref
.index
= obj
->index
;
460 ref
.offset
= obj
->allocated_len
;
461 obj
->allocated_len
+= len
;
465 void align_shm(struct shm_object
*obj
, size_t align
)
467 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
468 obj
->allocated_len
+= offset_len
;
This page took 0.039773 seconds and 4 git commands to generate.