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>
40 * Ensure we have the required amount of space available by writing 0
41 * into the entire buffer. Not doing so can trigger SIGBUS when going
42 * beyond the available shm space.
45 int zero_file(int fd
, size_t len
)
53 pagelen
= sysconf(_SC_PAGESIZE
);
56 zeropage
= calloc(pagelen
, 1);
60 while (len
> written
) {
62 retlen
= write(fd
, zeropage
,
63 min_t(size_t, pagelen
, len
- written
));
64 } while (retlen
== -1UL && errno
== EINTR
);
77 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
79 struct shm_object_table
*table
;
81 table
= zmalloc(sizeof(struct shm_object_table
) +
82 max_nb_obj
* sizeof(table
->objects
[0]));
85 table
->size
= max_nb_obj
;
90 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
91 size_t memory_map_size
,
94 int shmfd
, waitfd
[2], ret
, i
;
95 struct shm_object
*obj
;
100 if (table
->allocated_len
>= table
->size
)
102 obj
= &table
->objects
[table
->allocated_len
];
104 /* wait_fd: create pipe */
110 for (i
= 0; i
< 2; i
++) {
111 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
117 /* The write end of the pipe needs to be non-blocking */
118 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
123 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
128 ret
= zero_file(shmfd
, memory_map_size
);
131 goto error_zero_file
;
133 ret
= ftruncate(shmfd
, memory_map_size
);
136 goto error_ftruncate
;
139 * Also ensure the file metadata is synced with the storage by using
147 obj
->shm_fd_ownership
= 0;
150 /* memory_map: mmap */
151 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
152 MAP_SHARED
, shmfd
, 0);
153 if (memory_map
== MAP_FAILED
) {
157 obj
->type
= SHM_OBJECT_SHM
;
158 obj
->memory_map
= memory_map
;
159 obj
->memory_map_size
= memory_map_size
;
160 obj
->allocated_len
= 0;
161 obj
->index
= table
->allocated_len
++;
170 for (i
= 0; i
< 2; i
++) {
171 ret
= close(waitfd
[i
]);
182 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
183 size_t memory_map_size
)
185 struct shm_object
*obj
;
187 int waitfd
[2], i
, ret
;
189 if (table
->allocated_len
>= table
->size
)
191 obj
= &table
->objects
[table
->allocated_len
];
193 memory_map
= zmalloc(memory_map_size
);
197 /* wait_fd: create pipe */
203 for (i
= 0; i
< 2; i
++) {
204 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
210 /* The write end of the pipe needs to be non-blocking */
211 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
216 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
220 obj
->shm_fd_ownership
= 0;
222 obj
->type
= SHM_OBJECT_MEM
;
223 obj
->memory_map
= memory_map
;
224 obj
->memory_map_size
= memory_map_size
;
225 obj
->allocated_len
= 0;
226 obj
->index
= table
->allocated_len
++;
231 for (i
= 0; i
< 2; i
++) {
232 ret
= close(waitfd
[i
]);
244 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
245 size_t memory_map_size
,
246 enum shm_object_type type
,
251 struct shm_object
*shm_object
;
253 oldnode
= numa_preferred();
255 node
= numa_node_of_cpu(cpu
);
257 numa_set_preferred(node
);
259 if (cpu
< 0 || node
< 0)
260 numa_set_localalloc();
263 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
267 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
272 numa_set_preferred(oldnode
);
276 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
277 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
278 size_t memory_map_size
)
280 struct shm_object
*obj
;
284 if (table
->allocated_len
>= table
->size
)
286 /* streams _must_ be received in sequential order, else fail. */
287 if (stream_nr
+ 1 != table
->allocated_len
)
290 obj
= &table
->objects
[table
->allocated_len
];
292 /* wait_fd: set write end of the pipe. */
293 obj
->wait_fd
[0] = -1; /* read end is unset */
294 obj
->wait_fd
[1] = wakeup_fd
;
295 obj
->shm_fd
= shm_fd
;
296 obj
->shm_fd_ownership
= 1;
298 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
303 /* The write end of the pipe needs to be non-blocking */
304 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
310 /* memory_map: mmap */
311 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
312 MAP_SHARED
, shm_fd
, 0);
313 if (memory_map
== MAP_FAILED
) {
317 obj
->type
= SHM_OBJECT_SHM
;
318 obj
->memory_map
= memory_map
;
319 obj
->memory_map_size
= memory_map_size
;
320 obj
->allocated_len
= memory_map_size
;
321 obj
->index
= table
->allocated_len
++;
331 * Passing ownership of mem to object.
333 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
334 void *mem
, size_t memory_map_size
, int wakeup_fd
)
336 struct shm_object
*obj
;
339 if (table
->allocated_len
>= table
->size
)
341 obj
= &table
->objects
[table
->allocated_len
];
343 obj
->wait_fd
[0] = -1; /* read end is unset */
344 obj
->wait_fd
[1] = wakeup_fd
;
346 obj
->shm_fd_ownership
= 0;
348 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
353 /* The write end of the pipe needs to be non-blocking */
354 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
360 obj
->type
= SHM_OBJECT_MEM
;
361 obj
->memory_map
= mem
;
362 obj
->memory_map_size
= memory_map_size
;
363 obj
->allocated_len
= memory_map_size
;
364 obj
->index
= table
->allocated_len
++;
373 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
380 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
386 if (obj
->shm_fd_ownership
) {
387 /* Delete FDs only if called from app (not consumer). */
389 lttng_ust_lock_fd_tracker();
390 ret
= close(obj
->shm_fd
);
392 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
397 lttng_ust_unlock_fd_tracker();
399 ret
= close(obj
->shm_fd
);
406 for (i
= 0; i
< 2; i
++) {
407 if (obj
->wait_fd
[i
] < 0)
410 lttng_ust_lock_fd_tracker();
411 ret
= close(obj
->wait_fd
[i
]);
413 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
418 lttng_ust_unlock_fd_tracker();
420 ret
= close(obj
->wait_fd
[i
]);
433 for (i
= 0; i
< 2; i
++) {
434 if (obj
->wait_fd
[i
] < 0)
437 lttng_ust_lock_fd_tracker();
438 ret
= close(obj
->wait_fd
[i
]);
440 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
445 lttng_ust_unlock_fd_tracker();
447 ret
= close(obj
->wait_fd
[i
]);
454 free(obj
->memory_map
);
462 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
466 for (i
= 0; i
< table
->allocated_len
; i
++)
467 shmp_object_destroy(&table
->objects
[i
], consumer
);
472 * zalloc_shm - allocate memory within a shm object.
474 * Shared memory is already zeroed by shmget.
475 * *NOT* multithread-safe (should be protected by mutex).
476 * Returns a -1, -1 tuple on error.
478 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
481 struct shm_ref shm_ref_error
= { -1, -1 };
483 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
484 return shm_ref_error
;
485 ref
.index
= obj
->index
;
486 ref
.offset
= obj
->allocated_len
;
487 obj
->allocated_len
+= len
;
491 void align_shm(struct shm_object
*obj
, size_t align
)
493 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
494 obj
->allocated_len
+= offset_len
;
This page took 0.062611 seconds and 4 git commands to generate.