Skip to content

Commit

Permalink
Merge pull request #20 from toppers/mmap
Browse files Browse the repository at this point in the history
fix #16
  • Loading branch information
tmori authored Feb 11, 2024
2 parents 4b99a0d + ec0d869 commit 279132c
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 60 deletions.
10 changes: 5 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ endif(WIN32)
MESSAGE(STATUS "OS_TYPE=" ${OS_TYPE})

include(FetchContent)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
FetchContent_Declare(json URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz DOWNLOAD_EXTRACT_TIMESTAMP true)
FetchContent_MakeAvailable(json)

add_library(
Expand All @@ -23,10 +23,10 @@ add_library(
hako/core/simulation/time/hako_time.cpp
hako/core/rpc/hako_internal_rpc.cpp
#for filelock && mmap
#hako/utils/hako_share/impl/posix/hako_sem_flock.cpp
#hako/utils/hako_share/impl/posix/hako_shared_memory_mmap.cpp
#hako/utils/hako_share/impl/${OS_TYPE}/hako_mmap.cpp
#hako/utils/hako_share/impl/${OS_TYPE}/hako_flock.cpp
hako/utils/hako_share/impl/hako_sem_flock.cpp
hako/utils/hako_share/impl/hako_shared_memory_mmap.cpp
hako/utils/hako_share/impl/${OS_TYPE}/hako_mmap.cpp
hako/utils/hako_share/impl/${OS_TYPE}/hako_flock.cpp

#for sem && shared memory
hako/utils/hako_share/impl/hako_shared_memory_factory.cpp
Expand Down
6 changes: 5 additions & 1 deletion src/hako/data/hako_master_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,15 @@ namespace hako::data {
return this->shmp_->get_semid(HAKO_SHARED_MEMORY_ID_0);
}

bool load()
bool load(const std::string& type="shm")
{
if (this->shmp_ != nullptr) {
return true;
}
if (type != "shm")
{
this->shm_type_ = type;
}
this->shmp_ = hako::utils::hako_shared_memory_create(this->get_shm_type());
void *datap = this->shmp_->load_memory(HAKO_SHARED_MEMORY_ID_0, sizeof(HakoMasterDataType));
if (datap == nullptr) {
Expand Down
43 changes: 36 additions & 7 deletions src/hako/hako_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "hako_master_impl.hpp"
#include "hako_asset_impl.hpp"
#include "hako_simevent_impl.hpp"
#include "utils/hako_config_loader.hpp"
//#include "utils/hako_logger.hpp"
#include "core/context/hako_context.hpp"
#include "hako_log.hpp"
Expand All @@ -16,9 +17,17 @@ void hako::init()
{
//hako::utils::logger::init("core");
if (master_data_ptr == nullptr) {
HakoConfigType config;
hako_config_load(config);
master_data_ptr = std::make_shared<hako::data::HakoMasterData>();
master_data_ptr->init();
if (config.param == nullptr) {
master_data_ptr->init();
}
else {
master_data_ptr->init("mmap");
}
}
HAKO_LOG_INFO("hako::init(): shared memory type = %s", master_data_ptr->get_shm_type().c_str());
//hako::utils::logger::get("core")->info("hakoniwa initialized");
return;
}
Expand Down Expand Up @@ -62,11 +71,22 @@ std::shared_ptr<hako::IHakoAssetController> hako::create_asset_controller()
}
else if (master_data_ptr == nullptr) {
master_data_ptr = std::make_shared<hako::data::HakoMasterData>();
if (master_data_ptr->load() == false) {
return nullptr;
HakoConfigType config;
hako_config_load(config);
if (config.param == nullptr) {
if (master_data_ptr->load() == false) {
return nullptr;
}
}
else
{
if (master_data_ptr->load("mmap") == false) {
return nullptr;
}
}
}
asset_ptr = std::make_shared<hako::HakoAssetControllerImpl>(master_data_ptr);
HAKO_LOG_INFO("hako::create_asset_controller(): shared memory type = %s", master_data_ptr->get_shm_type().c_str());

return asset_ptr;
}
Expand All @@ -78,13 +98,22 @@ std::shared_ptr<hako::IHakoSimulationEventController> hako::get_simevent_control
}
else if (master_data_ptr == nullptr) {
master_data_ptr = std::make_shared<hako::data::HakoMasterData>();
if (master_data_ptr->load() == false) {
//hako::utils::logger::get("core")->error("get_simevent_controller() can not load master data");
//hako::utils::logger::get("core")->flush();
return nullptr;
HakoConfigType config;
hako_config_load(config);
if (config.param == nullptr) {
if (master_data_ptr->load() == false) {
return nullptr;
}
}
else
{
if (master_data_ptr->load("mmap") == false) {
return nullptr;
}
}
}
simevent_ptr = std::make_shared<hako::HakoSimulationEventController>(master_data_ptr);
HAKO_LOG_INFO("hako::get_simevent_controller(): shared memory type = %s", master_data_ptr->get_shm_type().c_str());
return simevent_ptr;
}

Expand Down
15 changes: 15 additions & 0 deletions src/hako/utils/hako_share/hako_sem_flock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef _HAKO_SEM_HPP_
#define _HAKO_SEM_HPP_

#include "types/hako_types.hpp"

namespace hako::utils::sem::flock {
int32_t create(int32_t key);
void destroy(int32_t sem_id);
void master_lock(int32_t sem_id);
void master_unlock(int32_t sem_id);
void asset_down(int32_t sem_id, int32_t asset_id);
void asset_up(int32_t sem_id, int32_t asset_id);
}

#endif /* _HAKO_SEM_HPP_ */
31 changes: 31 additions & 0 deletions src/hako/utils/hako_share/hako_shared_memory_mmap.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef _HAKO_SHARED_MEMORY_MMAP_HPP_
#define _HAKO_SHARED_MEMORY_MMAP_HPP_

#include "types/hako_types.hpp"
#include "utils/hako_share/hako_shared_memory.hpp"
#include <map>

namespace hako::utils {

class HakoSharedMemoryMmap : public HakoSharedMemory {
public:
HakoSharedMemoryMmap() {}
virtual ~HakoSharedMemoryMmap() {}

virtual int32_t create_memory(int32_t key, int32_t size) override;
virtual void* load_memory(int32_t key, int32_t size) override;

virtual void* lock_memory(int32_t key) override;
virtual void unlock_memory(int32_t key) override;
virtual void destroy_memory(int32_t key) override;

virtual int32_t get_semid(int32_t key) override;

private:
void* load_memory_shmid(int32_t key, int32_t shmid);
std::map<int32_t, SharedMemoryInfoType> shared_memory_map_;
};
}


#endif /* _HAKO_SHARED_MEMORY_MMAP_HPP_ */
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "utils/hako_share/hako_sem.hpp"
#include "utils/hako_share/hako_sem_flock.hpp"
#include "utils/hako_share/impl/hako_flock.hpp"
#include "utils/hako_config_loader.hpp"
//#include "utils/hako_logger.hpp"
Expand Down Expand Up @@ -87,7 +87,7 @@ static void hako_sem_up(int index)
}


int32_t hako::utils::sem::create(int32_t key)
int32_t hako::utils::sem::flock::create(int32_t key)
{
char buf[4096];
HakoConfigType config;
Expand Down Expand Up @@ -115,32 +115,32 @@ int32_t hako::utils::sem::create(int32_t key)
return key;
}

void hako::utils::sem::destroy(int32_t sem_id)
void hako::utils::sem::flock::destroy(int32_t sem_id)
{
HAKO_ASSERT(sem_id >= 0);
hako_flock_destroy(flock_handle);
return;
}

void hako::utils::sem::asset_down(int32_t sem_id, int32_t asset_id)
void hako::utils::sem::flock::asset_down(int32_t sem_id, int32_t asset_id)
{
HAKO_ASSERT(sem_id >= 0);
hako_sem_down(HAKO_SEM_INX_ASSETS + asset_id);
return;
}
void hako::utils::sem::asset_up(int32_t sem_id, int32_t asset_id)
void hako::utils::sem::flock::asset_up(int32_t sem_id, int32_t asset_id)
{
HAKO_ASSERT(sem_id >= 0);
hako_sem_up(HAKO_SEM_INX_ASSETS + asset_id);
return;
}
void hako::utils::sem::master_lock(int32_t sem_id)
void hako::utils::sem::flock::master_lock(int32_t sem_id)
{
HAKO_ASSERT(sem_id >= 0);
hako_sem_down(HAKO_SEM_INX_MASTER);
return;
}
void hako::utils::sem::master_unlock(int32_t sem_id)
void hako::utils::sem::flock::master_unlock(int32_t sem_id)
{
HAKO_ASSERT(sem_id >= 0);
hako_sem_up(HAKO_SEM_INX_MASTER);
Expand Down
4 changes: 4 additions & 0 deletions src/hako/utils/hako_share/impl/hako_shared_memory_factory.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include "utils/hako_share/hako_shared_memory_shm.hpp"
#include "utils/hako_share/hako_shared_memory_mmap.hpp"

std::shared_ptr<hako::utils::HakoSharedMemory> hako::utils::hako_shared_memory_create(const std::string& type)
{
if (type == "shm") {
return std::make_shared<hako::utils::HakoSharedMemoryShm>();
}
else if (type == "mmap") {
return std::make_shared<hako::utils::HakoSharedMemoryMmap>();
}
else
{
return nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "types/hako_types.hpp"

#include "utils/hako_share/hako_shared_memory.hpp"
#include "utils/hako_share/hako_sem.hpp"
#include "utils/hako_share/hako_shared_memory_mmap.hpp"
#include "utils/hako_share/hako_sem_flock.hpp"
#include "utils/hako_share/impl/hako_mmap.hpp"
#include "utils/hako_config_loader.hpp"

int32_t hako::utils::HakoSharedMemory::create_memory(int32_t key, int32_t size)
int32_t hako::utils::HakoSharedMemoryMmap::create_memory(int32_t key, int32_t size)
{
int32_t total_size = size + sizeof(SharedMemoryMetaDataType);
char buf[4096];
Expand All @@ -29,7 +29,7 @@ int32_t hako::utils::HakoSharedMemory::create_memory(int32_t key, int32_t size)
//printf("INFO: hako_mmap_create() key=%d size=%d \n", key, size);
void *shared_memory = mmap_obj->mmap_addr;

int32_t sem_id = hako::utils::sem::create(key);
int32_t sem_id = hako::utils::sem::flock::create(key);
if (sem_id < 0) {
hako_mmap_destroy(mmap_obj);
return -1;
Expand All @@ -47,15 +47,15 @@ int32_t hako::utils::HakoSharedMemory::create_memory(int32_t key, int32_t size)
this->shared_memory_map_.insert(std::make_pair(key, info));
return 0;
}
void* hako::utils::HakoSharedMemory::load_memory(int32_t key, int32_t size)
void* hako::utils::HakoSharedMemoryMmap::load_memory(int32_t key, int32_t size)
{
if (size == 0) {
return nullptr;
}
return this->load_memory_shmid(key, -1);
}

void* hako::utils::HakoSharedMemory::load_memory_shmid(int32_t key, int32_t shmid)
void* hako::utils::HakoSharedMemoryMmap::load_memory_shmid(int32_t key, int32_t shmid)
{
if (shmid >= 0) {
return nullptr;
Expand Down Expand Up @@ -90,28 +90,28 @@ void* hako::utils::HakoSharedMemory::load_memory_shmid(int32_t key, int32_t shmi
return &this->shared_memory_map_[key].addr->data[0];
}

void* hako::utils::HakoSharedMemory::lock_memory(int32_t key)
void* hako::utils::HakoSharedMemoryMmap::lock_memory(int32_t key)
{
hako::utils::sem::master_lock(this->shared_memory_map_[key].sem_id);
hako::utils::sem::flock::master_lock(this->shared_memory_map_[key].sem_id);
return &this->shared_memory_map_[key].addr->data[0];
}

void hako::utils::HakoSharedMemory::unlock_memory(int32_t key)
void hako::utils::HakoSharedMemoryMmap::unlock_memory(int32_t key)
{
hako::utils::sem::master_unlock(this->shared_memory_map_[key].sem_id);
hako::utils::sem::flock::master_unlock(this->shared_memory_map_[key].sem_id);
return;
}
int32_t hako::utils::HakoSharedMemory::get_semid(int32_t key)
int32_t hako::utils::HakoSharedMemoryMmap::get_semid(int32_t key)
{
return this->shared_memory_map_[key].sem_id;
}

void hako::utils::HakoSharedMemory::destroy_memory(int32_t key)
void hako::utils::HakoSharedMemoryMmap::destroy_memory(int32_t key)
{
void *addr = this->shared_memory_map_[key].addr;
if (addr != nullptr) {
hako_mmap_destroy(this->shared_memory_map_[key].mmap_obj);
hako::utils::sem::destroy(this->shared_memory_map_[key].sem_id);
hako::utils::sem::flock::destroy(this->shared_memory_map_[key].sem_id);
this->shared_memory_map_.erase(key);
}
return;
Expand Down
19 changes: 18 additions & 1 deletion test/hako/hako_shared_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class HakoSharedMemoryTest : public ::testing::Test {

};

TEST_F(HakoSharedMemoryTest, HakoSharedMemory_01)
TEST_F(HakoSharedMemoryTest, HakoSharedMemory_shm01)
{
std::shared_ptr<hako::utils::HakoSharedMemory> shm = hako::utils::hako_shared_memory_create("shm");
EXPECT_TRUE(shm.get() != nullptr);
Expand All @@ -34,3 +34,20 @@ TEST_F(HakoSharedMemoryTest, HakoSharedMemory_01)
shm->destroy_memory(HAKO_SHARED_MEMORY_ID_0);

}

TEST_F(HakoSharedMemoryTest, HakoSharedMemory_mmap01)
{
std::shared_ptr<hako::utils::HakoSharedMemory> shm = hako::utils::hako_shared_memory_create("mmap");
EXPECT_TRUE(shm.get() != nullptr);

int32_t shmid = shm->create_memory(HAKO_SHARED_MEMORY_ID_0, 1024);
EXPECT_TRUE(shmid >= 0);

void *value = shm->lock_memory(HAKO_SHARED_MEMORY_ID_0);
EXPECT_TRUE(value != nullptr);

shm->unlock_memory(HAKO_SHARED_MEMORY_ID_0);

shm->destroy_memory(HAKO_SHARED_MEMORY_ID_0);

}
26 changes: 0 additions & 26 deletions test/hako/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,6 @@
#include "utils/hako_share/hako_shared_memory_factory.hpp"
#include <unistd.h>

void do_shared_memory_multi_proc_test()
{
std::shared_ptr<hako::utils::HakoSharedMemory> shm = hako::utils::hako_shared_memory_create("shm");
int32_t shmid = shm->create_memory(HAKO_SHARED_MEMORY_ID_0, 1024);
EXPECT_TRUE(shmid > 0);

void *value = shm->lock_memory(HAKO_SHARED_MEMORY_ID_0);
EXPECT_TRUE(value != nullptr);

for (int i = 0; i < 5; i++)
{
printf("LOCKING:sleep 1sec...\n");
usleep (1000000);
}

shm->unlock_memory(HAKO_SHARED_MEMORY_ID_0);

for (int i = 0; i < 5; i++)
{
printf("UNLOCKING:sleep 1sec...\n");
usleep (1000000);
}

shm->destroy_memory(HAKO_SHARED_MEMORY_ID_0);
return;
}

int main(int argc, char *argv[])
{
Expand Down

0 comments on commit 279132c

Please sign in to comment.