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>
43 * Ensure we have the required amount of space available by writing 0
44 * into the entire buffer. Not doing so can trigger SIGBUS when going
45 * beyond the available shm space.
48 int zero_file(int fd
, size_t len
)
56 pagelen
= sysconf(_SC_PAGESIZE
);
59 zeropage
= calloc(pagelen
, 1);
63 while (len
> written
) {
65 retlen
= write(fd
, zeropage
,
66 min_t(size_t, pagelen
, len
- written
));
67 } while (retlen
== -1UL && errno
== EINTR
);
80 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
)
82 struct shm_object_table
*table
;
84 table
= zmalloc(sizeof(struct shm_object_table
) +
85 max_nb_obj
* sizeof(table
->objects
[0]));
88 table
->size
= max_nb_obj
;
93 struct shm_object
*_shm_object_table_alloc_shm(struct shm_object_table
*table
,
94 size_t memory_map_size
,
97 int shmfd
, waitfd
[2], ret
, i
;
98 struct shm_object
*obj
;
103 if (table
->allocated_len
>= table
->size
)
105 obj
= &table
->objects
[table
->allocated_len
];
107 /* wait_fd: create pipe */
113 for (i
= 0; i
< 2; i
++) {
114 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
120 /* The write end of the pipe needs to be non-blocking */
121 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
126 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
131 ret
= zero_file(shmfd
, memory_map_size
);
134 goto error_zero_file
;
136 ret
= ftruncate(shmfd
, memory_map_size
);
139 goto error_ftruncate
;
142 * Also ensure the file metadata is synced with the storage by using
150 obj
->shm_fd_ownership
= 0;
153 /* memory_map: mmap */
154 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
155 MAP_SHARED
, shmfd
, 0);
156 if (memory_map
== MAP_FAILED
) {
160 obj
->type
= SHM_OBJECT_SHM
;
161 obj
->memory_map
= memory_map
;
162 obj
->memory_map_size
= memory_map_size
;
163 obj
->allocated_len
= 0;
164 obj
->index
= table
->allocated_len
++;
173 for (i
= 0; i
< 2; i
++) {
174 ret
= close(waitfd
[i
]);
185 struct shm_object
*_shm_object_table_alloc_mem(struct shm_object_table
*table
,
186 size_t memory_map_size
)
188 struct shm_object
*obj
;
190 int waitfd
[2], i
, ret
;
192 if (table
->allocated_len
>= table
->size
)
194 obj
= &table
->objects
[table
->allocated_len
];
196 memory_map
= zmalloc(memory_map_size
);
200 /* wait_fd: create pipe */
206 for (i
= 0; i
< 2; i
++) {
207 ret
= fcntl(waitfd
[i
], F_SETFD
, FD_CLOEXEC
);
213 /* The write end of the pipe needs to be non-blocking */
214 ret
= fcntl(waitfd
[1], F_SETFL
, O_NONBLOCK
);
219 memcpy(obj
->wait_fd
, waitfd
, sizeof(waitfd
));
223 obj
->shm_fd_ownership
= 0;
225 obj
->type
= SHM_OBJECT_MEM
;
226 obj
->memory_map
= memory_map
;
227 obj
->memory_map_size
= memory_map_size
;
228 obj
->allocated_len
= 0;
229 obj
->index
= table
->allocated_len
++;
234 for (i
= 0; i
< 2; i
++) {
235 ret
= close(waitfd
[i
]);
247 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
248 size_t memory_map_size
,
249 enum shm_object_type type
,
253 struct shm_object
*shm_object
;
257 oldnode
= numa_preferred();
259 node
= numa_node_of_cpu(cpu
);
261 numa_set_preferred(node
);
263 if (cpu
< 0 || node
< 0)
264 numa_set_localalloc();
265 #endif /* HAVE_LIBNUMA */
268 shm_object
= _shm_object_table_alloc_shm(table
, memory_map_size
,
272 shm_object
= _shm_object_table_alloc_mem(table
, memory_map_size
);
278 numa_set_preferred(oldnode
);
279 #endif /* HAVE_LIBNUMA */
283 struct shm_object
*shm_object_table_append_shm(struct shm_object_table
*table
,
284 int shm_fd
, int wakeup_fd
, uint32_t stream_nr
,
285 size_t memory_map_size
)
287 struct shm_object
*obj
;
291 if (table
->allocated_len
>= table
->size
)
293 /* streams _must_ be received in sequential order, else fail. */
294 if (stream_nr
+ 1 != table
->allocated_len
)
297 obj
= &table
->objects
[table
->allocated_len
];
299 /* wait_fd: set write end of the pipe. */
300 obj
->wait_fd
[0] = -1; /* read end is unset */
301 obj
->wait_fd
[1] = wakeup_fd
;
302 obj
->shm_fd
= shm_fd
;
303 obj
->shm_fd_ownership
= 1;
305 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
310 /* The write end of the pipe needs to be non-blocking */
311 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
317 /* memory_map: mmap */
318 memory_map
= mmap(NULL
, memory_map_size
, PROT_READ
| PROT_WRITE
,
319 MAP_SHARED
, shm_fd
, 0);
320 if (memory_map
== MAP_FAILED
) {
324 obj
->type
= SHM_OBJECT_SHM
;
325 obj
->memory_map
= memory_map
;
326 obj
->memory_map_size
= memory_map_size
;
327 obj
->allocated_len
= memory_map_size
;
328 obj
->index
= table
->allocated_len
++;
338 * Passing ownership of mem to object.
340 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
341 void *mem
, size_t memory_map_size
, int wakeup_fd
)
343 struct shm_object
*obj
;
346 if (table
->allocated_len
>= table
->size
)
348 obj
= &table
->objects
[table
->allocated_len
];
350 obj
->wait_fd
[0] = -1; /* read end is unset */
351 obj
->wait_fd
[1] = wakeup_fd
;
353 obj
->shm_fd_ownership
= 0;
355 ret
= fcntl(obj
->wait_fd
[1], F_SETFD
, FD_CLOEXEC
);
360 /* The write end of the pipe needs to be non-blocking */
361 ret
= fcntl(obj
->wait_fd
[1], F_SETFL
, O_NONBLOCK
);
367 obj
->type
= SHM_OBJECT_MEM
;
368 obj
->memory_map
= mem
;
369 obj
->memory_map_size
= memory_map_size
;
370 obj
->allocated_len
= memory_map_size
;
371 obj
->index
= table
->allocated_len
++;
380 void shmp_object_destroy(struct shm_object
*obj
, int consumer
)
387 ret
= munmap(obj
->memory_map
, obj
->memory_map_size
);
393 if (obj
->shm_fd_ownership
) {
394 /* Delete FDs only if called from app (not consumer). */
396 lttng_ust_lock_fd_tracker();
397 ret
= close(obj
->shm_fd
);
399 lttng_ust_delete_fd_from_tracker(obj
->shm_fd
);
404 lttng_ust_unlock_fd_tracker();
406 ret
= close(obj
->shm_fd
);
413 for (i
= 0; i
< 2; i
++) {
414 if (obj
->wait_fd
[i
] < 0)
417 lttng_ust_lock_fd_tracker();
418 ret
= close(obj
->wait_fd
[i
]);
420 lttng_ust_delete_fd_from_tracker(obj
->wait_fd
[i
]);
425 lttng_ust_unlock_fd_tracker();
427 ret
= close(obj
->wait_fd
[i
]);
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
]);
461 free(obj
->memory_map
);
469 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
)
473 for (i
= 0; i
< table
->allocated_len
; i
++)
474 shmp_object_destroy(&table
->objects
[i
], consumer
);
479 * zalloc_shm - allocate memory within a shm object.
481 * Shared memory is already zeroed by shmget.
482 * *NOT* multithread-safe (should be protected by mutex).
483 * Returns a -1, -1 tuple on error.
485 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
)
488 struct shm_ref shm_ref_error
= { -1, -1 };
490 if (obj
->memory_map_size
- obj
->allocated_len
< len
)
491 return shm_ref_error
;
492 ref
.index
= obj
->index
;
493 ref
.offset
= obj
->allocated_len
;
494 obj
->allocated_len
+= len
;
498 void align_shm(struct shm_object
*obj
, size_t align
)
500 size_t offset_len
= offset_align(obj
->allocated_len
, align
);
501 obj
->allocated_len
+= offset_len
;
This page took 0.038983 seconds and 4 git commands to generate.