blob: 461e2541e01a8be74a4ae39ac6c85034e6c41d54 [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
3#include <stdio.h>
4#include <string.h>
5#include <sys/mman.h>
6#include <fcntl.h>
7#include <unistd.h>
8#include <sys/types.h>
9#include <errno.h>
Brian Silvermanbf2f9462014-02-25 13:33:52 -080010#include <stdlib.h>
Brian Silverman6da04272014-05-18 18:47:48 -070011#include <assert.h>
brians343bc112013-02-10 01:53:46 +000012
John Park398c74a2018-10-20 21:17:39 -070013#include "aos/ipc_lib/aos_sync.h"
Alex Perrycb7da4b2019-08-28 19:35:56 -070014#include "aos/ipc_lib/core_lib.h"
15#include "glog/logging.h"
Brian Silverman08661c72013-09-01 17:24:38 -070016
brians343bc112013-02-10 01:53:46 +000017// the path for the shared memory segment. see shm_open(3) for restrictions
18#define AOS_SHM_NAME "/aos_shared_mem"
19// Size of the shared mem segment.
Austin Schuh0480bc82014-10-25 18:01:51 -070020// This must fit in the tmpfs for /dev/shm/
Austin Schuh3e252e72019-12-29 16:25:58 -080021#define SIZEOFSHMSEG (4096 * 0x800)
brians343bc112013-02-10 01:53:46 +000022
Brian Silverman08661c72013-09-01 17:24:38 -070023void init_shared_mem_core(aos_shm_core *shm_core) {
Brian Silvermand0575692015-02-21 16:24:02 -050024 memset(&shm_core->time_offset, 0 , sizeof(shm_core->time_offset));
Brian Silvermandc1eb272014-08-19 14:25:59 -040025 memset(&shm_core->msg_alloc_lock, 0, sizeof(shm_core->msg_alloc_lock));
Brian Silverman4aeac5f2014-02-11 22:17:07 -080026 shm_core->queues.pointer = NULL;
Brian Silvermandc1eb272014-08-19 14:25:59 -040027 memset(&shm_core->queues.lock, 0, sizeof(shm_core->queues.lock));
Brian Silverman4aeac5f2014-02-11 22:17:07 -080028 shm_core->queue_types.pointer = NULL;
Brian Silvermandc1eb272014-08-19 14:25:59 -040029 memset(&shm_core->queue_types.lock, 0, sizeof(shm_core->queue_types.lock));
Brian Silverman08661c72013-09-01 17:24:38 -070030}
31
brians343bc112013-02-10 01:53:46 +000032ptrdiff_t aos_core_get_mem_usage(void) {
33 return global_core->size -
34 ((ptrdiff_t)global_core->mem_struct->msg_alloc -
35 (ptrdiff_t)global_core->mem_struct);
36}
37
brians343bc112013-02-10 01:53:46 +000038struct aos_core *global_core = NULL;
39
Brian Silverman67e34f52014-03-13 15:52:57 -070040// TODO(brians): madvise(2) it to put this shm in core dumps.
Brian Silverman8a6dac92015-02-21 20:08:24 -050041void aos_core_create_shared_mem(int create, int lock) {
Brian Silverman6da04272014-05-18 18:47:48 -070042 assert(global_core == NULL);
Brian Silverman08661c72013-09-01 17:24:38 -070043 static struct aos_core global_core_data;
brians343bc112013-02-10 01:53:46 +000044 global_core = &global_core_data;
Brian Silvermanbf2f9462014-02-25 13:33:52 -080045
46 {
47 char *shm_name = getenv("AOS_SHM_NAME");
48 if (shm_name == NULL) {
49 global_core->shm_name = AOS_SHM_NAME;
50 } else {
Austin Schuh293d37c2015-03-08 18:47:45 -070051 printf("AOS_SHM_NAME defined, using %s\n", shm_name);
Brian Silvermanbf2f9462014-02-25 13:33:52 -080052 global_core->shm_name = shm_name;
53 }
54 }
55
brians343bc112013-02-10 01:53:46 +000056 int shm;
Brian Silverman8a6dac92015-02-21 20:08:24 -050057 if (create) {
Brian48610152014-04-02 12:28:19 -070058 while (1) {
Brian48610152014-04-02 12:28:19 -070059 shm = shm_open(global_core->shm_name, O_RDWR | O_CREAT | O_EXCL, 0666);
60 global_core->owner = 1;
61 if (shm == -1 && errno == EEXIST) {
62 printf("shared_mem: going to shm_unlink(%s)\n", global_core->shm_name);
63 if (shm_unlink(global_core->shm_name) == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070064 PLOG(WARNING) << "shm_unlink(" << global_core->shm_name << ") failed";
Brian48610152014-04-02 12:28:19 -070065 break;
66 }
brians343bc112013-02-10 01:53:46 +000067 } else {
Brian48610152014-04-02 12:28:19 -070068 break;
brians343bc112013-02-10 01:53:46 +000069 }
70 }
71 } else {
Brian Silvermanbf2f9462014-02-25 13:33:52 -080072 shm = shm_open(global_core->shm_name, O_RDWR, 0);
brians343bc112013-02-10 01:53:46 +000073 global_core->owner = 0;
74 }
75 if (shm == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 PLOG(FATAL) << "shm_open(" << global_core->shm_name
77 << ", O_RDWR [| O_CREAT | O_EXCL, 0|0666) failed";
brians343bc112013-02-10 01:53:46 +000078 }
79 if (global_core->owner) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070080 PCHECK(ftruncate(shm, SIZEOFSHMSEG) == 0)
81 << ": fruncate(" << shm << ", 0x" << std::hex << (size_t)SIZEOFSHMSEG
82 << ") failed";
brians343bc112013-02-10 01:53:46 +000083 }
Brian Silverman8a6dac92015-02-21 20:08:24 -050084 int flags = MAP_SHARED | MAP_FIXED;
85 if (lock) flags |= MAP_LOCKED | MAP_POPULATE;
86 void *shm_address = mmap((void *)SHM_START, SIZEOFSHMSEG,
87 PROT_READ | PROT_WRITE, flags, shm, 0);
Alex Perrycb7da4b2019-08-28 19:35:56 -070088 PCHECK(shm_address != MAP_FAILED)
89 << std::hex << "shared_mem: mmap(" << (void *)SHM_START << ", 0x"
90 << (size_t)SIZEOFSHMSEG << ", stuff, " << flags << ", " << shm
91 << ", 0) failed";
Austin Schuh293d37c2015-03-08 18:47:45 -070092 if (create) {
93 printf("shared_mem: creating %s, shm at: %p\n", global_core->shm_name,
94 shm_address);
95 } else {
96 printf("shared_mem: not creating, shm at: %p\n", shm_address);
97 }
brians343bc112013-02-10 01:53:46 +000098 if (close(shm) == -1) {
Alex Perrycb7da4b2019-08-28 19:35:56 -070099 PLOG(WARNING) << "close(" << shm << "(=shm) failed";
brians343bc112013-02-10 01:53:46 +0000100 }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 PCHECK(shm_address == (void *)SHM_START)
102 << "shm isn't at hard-coded " << (void *)SHM_START << ". at "
103 << shm_address << " instead";
Brian Silverman01be0002014-05-10 15:44:38 -0700104 aos_core_use_address_as_shared_mem(shm_address, SIZEOFSHMSEG);
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 LOG(INFO) << "shared_mem: end of create_shared_mem owner="
106 << global_core->owner;
brians343bc112013-02-10 01:53:46 +0000107}
108
Brian Silverman01be0002014-05-10 15:44:38 -0700109void aos_core_use_address_as_shared_mem(void *address, size_t size) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700110 global_core->mem_struct = reinterpret_cast<aos_shm_core_t *>(address);
brians343bc112013-02-10 01:53:46 +0000111 global_core->size = size;
Brian Silvermanbf2f9462014-02-25 13:33:52 -0800112 global_core->shared_mem =
113 (uint8_t *)address + sizeof(*global_core->mem_struct);
brians343bc112013-02-10 01:53:46 +0000114 if (global_core->owner) {
115 global_core->mem_struct->msg_alloc = (uint8_t *)address + global_core->size;
116 init_shared_mem_core(global_core->mem_struct);
Brian Silvermanaf221b82013-09-01 13:57:50 -0700117 futex_set(&global_core->mem_struct->creation_condition);
brians343bc112013-02-10 01:53:46 +0000118 } else {
Brian Silvermanaf221b82013-09-01 13:57:50 -0700119 if (futex_wait(&global_core->mem_struct->creation_condition) != 0) {
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120 LOG(FATAL) << "waiting on creation_condition failed";
brians343bc112013-02-10 01:53:46 +0000121 }
122 }
brians343bc112013-02-10 01:53:46 +0000123}
124
Brian Silverman01be0002014-05-10 15:44:38 -0700125void aos_core_free_shared_mem() {
Austin Schuh32fd5a72019-12-01 22:20:26 -0800126 if (global_core != nullptr) {
127 void *shm_address = global_core->shared_mem;
128 PCHECK(munmap((void *)SHM_START, SIZEOFSHMSEG) != -1)
129 << ": munmap(" << shm_address << ", 0x" << std::hex
130 << (size_t)SIZEOFSHMSEG << ") failed";
131 if (global_core->owner) {
132 PCHECK(shm_unlink(global_core->shm_name) == 0)
133 << ": shared_mem: shm_unlink(" << global_core->shm_name << ") failed";
134 }
135 global_core = nullptr;
Brian Silverman01be0002014-05-10 15:44:38 -0700136 }
brians343bc112013-02-10 01:53:46 +0000137}
Brian Silvermanb263d302014-02-16 00:01:43 -0800138
Austin Schuh32fd5a72019-12-01 22:20:26 -0800139int aos_core_is_init(void) { return global_core != NULL; }