blob: 30dd173ffd60270a674825cc79b5685c764049b1 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/ipc_lib/shared_mem.h"
brians343bc112013-02-10 01:53:46 +00002
brians343bc112013-02-10 01:53:46 +00003#include <fcntl.h>
Stephan Pleines682928d2024-05-31 20:43:48 -07004#include <stdint.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07005#include <sys/mman.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07006#include <unistd.h>
7
8#include <cassert>
9#include <cerrno>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
Stephan Pleines682928d2024-05-31 20:43:48 -070013#include <ostream>
brians343bc112013-02-10 01:53:46 +000014
Philipp Schrader790cb542023-07-05 21:06:52 -070015#include "glog/logging.h"
16
John Park398c74a2018-10-20 21:17:39 -070017#include "aos/ipc_lib/aos_sync.h"
Brian Silverman08661c72013-09-01 17:24:38 -070018
brians343bc112013-02-10 01:53:46 +000019// the path for the shared memory segment. see shm_open(3) for restrictions
20#define AOS_SHM_NAME "/aos_shared_mem"
21// Size of the shared mem segment.
Austin Schuh0480bc82014-10-25 18:01:51 -070022// This must fit in the tmpfs for /dev/shm/
Austin Schuh3e252e72019-12-29 16:25:58 -080023#define SIZEOFSHMSEG (4096 * 0x800)
brians343bc112013-02-10 01:53:46 +000024
Brian Silverman08661c72013-09-01 17:24:38 -070025void init_shared_mem_core(aos_shm_core *shm_core) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070026 memset(&shm_core->time_offset, 0, sizeof(shm_core->time_offset));
Brian Silvermandc1eb272014-08-19 14:25:59 -040027 memset(&shm_core->msg_alloc_lock, 0, sizeof(shm_core->msg_alloc_lock));
Brian Silverman4aeac5f2014-02-11 22:17:07 -080028 shm_core->queues.pointer = NULL;
Brian Silvermandc1eb272014-08-19 14:25:59 -040029 memset(&shm_core->queues.lock, 0, sizeof(shm_core->queues.lock));
Brian Silverman4aeac5f2014-02-11 22:17:07 -080030 shm_core->queue_types.pointer = NULL;
Brian Silvermandc1eb272014-08-19 14:25:59 -040031 memset(&shm_core->queue_types.lock, 0, sizeof(shm_core->queue_types.lock));
Brian Silverman08661c72013-09-01 17:24:38 -070032}
33
brians343bc112013-02-10 01:53:46 +000034ptrdiff_t aos_core_get_mem_usage(void) {
Tyler Chatowbf0609c2021-07-31 16:13:27 -070035 return global_core->size - ((ptrdiff_t)global_core->mem_struct->msg_alloc -
36 (ptrdiff_t)global_core->mem_struct);
brians343bc112013-02-10 01:53:46 +000037}
38
brians343bc112013-02-10 01:53:46 +000039struct aos_core *global_core = NULL;
40
Brian Silverman67e34f52014-03-13 15:52:57 -070041// TODO(brians): madvise(2) it to put this shm in core dumps.
Brian Silverman8a6dac92015-02-21 20:08:24 -050042void aos_core_create_shared_mem(int create, int lock) {
Brian Silverman6da04272014-05-18 18:47:48 -070043 assert(global_core == NULL);
Brian Silverman08661c72013-09-01 17:24:38 -070044 static struct aos_core global_core_data;
brians343bc112013-02-10 01:53:46 +000045 global_core = &global_core_data;
Brian Silvermanbf2f9462014-02-25 13:33:52 -080046
47 {
48 char *shm_name = getenv("AOS_SHM_NAME");
49 if (shm_name == NULL) {
50 global_core->shm_name = AOS_SHM_NAME;
51 } else {
Austin Schuh293d37c2015-03-08 18:47:45 -070052 printf("AOS_SHM_NAME defined, using %s\n", shm_name);
Brian Silvermanbf2f9462014-02-25 13:33:52 -080053 global_core->shm_name = shm_name;
54 }
55 }
56
brians343bc112013-02-10 01:53:46 +000057 int shm;
Brian Silverman8a6dac92015-02-21 20:08:24 -050058 if (create) {
Brian48610152014-04-02 12:28:19 -070059 while (1) {
Brian48610152014-04-02 12:28:19 -070060 shm = shm_open(global_core->shm_name, O_RDWR | O_CREAT | O_EXCL, 0666);
61 global_core->owner = 1;
62 if (shm == -1 && errno == EEXIST) {
63 printf("shared_mem: going to shm_unlink(%s)\n", global_core->shm_name);
64 if (shm_unlink(global_core->shm_name) == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070065 PLOG(WARNING) << "shm_unlink(" << global_core->shm_name << ") failed";
Brian48610152014-04-02 12:28:19 -070066 break;
67 }
brians343bc112013-02-10 01:53:46 +000068 } else {
Brian48610152014-04-02 12:28:19 -070069 break;
brians343bc112013-02-10 01:53:46 +000070 }
71 }
72 } else {
Brian Silvermanbf2f9462014-02-25 13:33:52 -080073 shm = shm_open(global_core->shm_name, O_RDWR, 0);
brians343bc112013-02-10 01:53:46 +000074 global_core->owner = 0;
75 }
76 if (shm == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 PLOG(FATAL) << "shm_open(" << global_core->shm_name
78 << ", O_RDWR [| O_CREAT | O_EXCL, 0|0666) failed";
brians343bc112013-02-10 01:53:46 +000079 }
80 if (global_core->owner) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070081 PCHECK(ftruncate(shm, SIZEOFSHMSEG) == 0)
82 << ": fruncate(" << shm << ", 0x" << std::hex << (size_t)SIZEOFSHMSEG
83 << ") failed";
brians343bc112013-02-10 01:53:46 +000084 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050085 int flags = MAP_SHARED | MAP_FIXED;
86 if (lock) flags |= MAP_LOCKED | MAP_POPULATE;
87 void *shm_address = mmap((void *)SHM_START, SIZEOFSHMSEG,
88 PROT_READ | PROT_WRITE, flags, shm, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -070089 PCHECK(shm_address != MAP_FAILED)
90 << std::hex << "shared_mem: mmap(" << (void *)SHM_START << ", 0x"
91 << (size_t)SIZEOFSHMSEG << ", stuff, " << flags << ", " << shm
92 << ", 0) failed";
Austin Schuh293d37c2015-03-08 18:47:45 -070093 if (create) {
94 printf("shared_mem: creating %s, shm at: %p\n", global_core->shm_name,
95 shm_address);
96 } else {
97 printf("shared_mem: not creating, shm at: %p\n", shm_address);
98 }
brians343bc112013-02-10 01:53:46 +000099 if (close(shm) == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700100 PLOG(WARNING) << "close(" << shm << "(=shm) failed";
brians343bc112013-02-10 01:53:46 +0000101 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 PCHECK(shm_address == (void *)SHM_START)
103 << "shm isn't at hard-coded " << (void *)SHM_START << ". at "
104 << shm_address << " instead";
Brian Silverman01be0002014-05-10 15:44:38 -0700105 aos_core_use_address_as_shared_mem(shm_address, SIZEOFSHMSEG);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 LOG(INFO) << "shared_mem: end of create_shared_mem owner="
107 << global_core->owner;
brians343bc112013-02-10 01:53:46 +0000108}
109
Brian Silverman01be0002014-05-10 15:44:38 -0700110void aos_core_use_address_as_shared_mem(void *address, size_t size) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700111 global_core->mem_struct = reinterpret_cast<aos_shm_core_t *>(address);
brians343bc112013-02-10 01:53:46 +0000112 global_core->size = size;
Brian Silvermanbf2f9462014-02-25 13:33:52 -0800113 global_core->shared_mem =
114 (uint8_t *)address + sizeof(*global_core->mem_struct);
brians343bc112013-02-10 01:53:46 +0000115 if (global_core->owner) {
116 global_core->mem_struct->msg_alloc = (uint8_t *)address + global_core->size;
117 init_shared_mem_core(global_core->mem_struct);
Brian Silvermanaf221b82013-09-01 13:57:50 -0700118 futex_set(&global_core->mem_struct->creation_condition);
brians343bc112013-02-10 01:53:46 +0000119 } else {
Brian Silvermanaf221b82013-09-01 13:57:50 -0700120 if (futex_wait(&global_core->mem_struct->creation_condition) != 0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700121 LOG(FATAL) << "waiting on creation_condition failed";
brians343bc112013-02-10 01:53:46 +0000122 }
123 }
brians343bc112013-02-10 01:53:46 +0000124}
125
Brian Silverman01be0002014-05-10 15:44:38 -0700126void aos_core_free_shared_mem() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800127 if (global_core != nullptr) {
128 void *shm_address = global_core->shared_mem;
129 PCHECK(munmap((void *)SHM_START, SIZEOFSHMSEG) != -1)
130 << ": munmap(" << shm_address << ", 0x" << std::hex
131 << (size_t)SIZEOFSHMSEG << ") failed";
132 if (global_core->owner) {
133 PCHECK(shm_unlink(global_core->shm_name) == 0)
134 << ": shared_mem: shm_unlink(" << global_core->shm_name << ") failed";
135 }
136 global_core = nullptr;
Brian Silverman01be0002014-05-10 15:44:38 -0700137 }
brians343bc112013-02-10 01:53:46 +0000138}
Brian Silvermanb263d302014-02-16 00:01:43 -0800139
Austin Schuh32fd5a72019-12-01 22:20:26 -0800140int aos_core_is_init(void) { return global_core != NULL; }