- /* Calculate the shm allocation layout */
- shmsize = sizeof(struct shm_header);
- shmsize += offset_align(shmsize, __alignof__(struct channel));
- shmsize += sizeof(struct channel);
-
- /* Per-cpu buffer size: control (prior to backend) */
- shmsize += offset_align(shmsize, __alignof__(struct lib_ring_buffer));
- bufshmsize = sizeof(struct lib_ring_buffer);
- shmsize += bufshmsize * num_possible_cpus();
-
- /* Per-cpu buffer size: backend */
- shmsize += offset_align(shmsize, PAGE_SIZE);
- /* num_subbuf + 1 is the worse case */
- num_subbuf_alloc = num_subbuf + 1;
- bufshmsize = sizeof(struct lib_ring_buffer_backend_pages *) * num_subbuf_alloc;
- bufshmsize += offset_align(bufshmsize, PAGE_SIZE);
- bufshmsize += subbuf_size * num_subbuf_alloc;
- bufshmsize += offset_align(bufshmsize, __alignof__(struct lib_ring_buffer_backend_pages));
- bufshmsize += sizeof(struct lib_ring_buffer_backend_pages) * num_subbuf_alloc;
- bufshmsize += offset_align(bufshmsize, __alignof__(struct lib_ring_buffer_backend_subbuffer));
- bufshmsize += sizeof(struct lib_ring_buffer_backend_subbuffer) * num_subbuf;
- bufshmsize += offset_align(bufshmsize, PAGE_SIZE);
- shmsize += bufshmsize * num_possible_cpus();
-
- /* Per-cpu buffer size: control (after backend) */
- shmsize += offset_align(shmsize,
- max(__alignof__(struct commit_counters_hot),
- __alignof__(struct commit_counters_cold)));
- bufshmsize = sizeof(struct commit_counters_hot) * num_subbuf;
- bufshmsize += offset_align(bufshmsize, __alignof__(struct commit_counters_cold));
- bufshmsize += sizeof(struct commit_counters_cold) * num_subbuf;
- shmsize += bufshmsize * num_possible_cpus();
-
- /*
- * Allocate shm, and immediately unlink its shm oject, keeping
- * only the file descriptor as a reference to the object. If it
- * already exists (caused by short race window during which the
- * global object exists in a concurrent shm_open), simply retry.
- */
- do {
- shmfd = shm_open("/ust-shm-tmp",
- O_CREAT | O_EXCL | O_RDWR, 0700);
- } while (shmfd < 0 && errno == EEXIST);
- if (shmfd < 0) {
- PERROR("shm_open");
- goto error_shm_open;
- }
- ret = shm_unlink("/ust-shm-tmp");
- if (ret) {
- PERROR("shm_unlink");
- goto error_unlink;
- }
- ret = ftruncate(shmfd, shmsize);
- if (ret) {
- PERROR("ftruncate");
- goto error_ftruncate;
- }
-
- shm_header = mmap(NULL, shmsize, PROT_READ | PROT_WRITE,
- MAP_SHARED, shmfd, 0);
- if (shm_header == MAP_FAILED) {
- PERROR("mmap");
- goto error_mmap;
- }