Skip to content

Commit

Permalink
Merge pull request #48475 from robert-kalmar:shmem-unlink-for-non-adroid
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 374851010
Change-Id: I10191ea32762a257136f6b8eee2062c29677ca8f
  • Loading branch information
tensorflower-gardener committed May 20, 2021
2 parents f63a890 + be48e0c commit 2eb5ed7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
19 changes: 19 additions & 0 deletions tensorflow/lite/delegates/nnapi/nnapi_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,22 @@ NNMemory::NNMemory(const NnApi* nnapi, const char* name, size_t size) {
if (name && size > 0) {
nnapi_ = nnapi;
byte_size_ = size;
#ifdef __ANDROID__
fd_ = nnapi_->ASharedMemory_create(name, size);
#else
// For non-Android platforms ASharedMemory_create needs unique name to
// create a shared memory object (see nnapi_implementation.cc).
char shm_name_buffer[L_tmpnam];
if (tmpnam(shm_name_buffer) == nullptr) {
shm_name_buffer[0] = '\0';
}
// tmpnam will produce a string containing with slashes, but shm_open
// won't like that.
shm_region_name_ = std::string(name) + std::string(shm_name_buffer);
std::replace(shm_region_name_.begin(), shm_region_name_.end(), '/', '-');
fd_ = nnapi_->ASharedMemory_create(shm_region_name_.c_str(), size);
#endif

data_ptr_ = reinterpret_cast<uint8_t*>(
mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0));
nnapi_->ANeuralNetworksMemory_createFromFd(size, PROT_READ | PROT_WRITE,
Expand All @@ -691,7 +706,11 @@ NNMemory::~NNMemory() {
if (nn_memory_handle_) {
nnapi_->ANeuralNetworksMemory_free(nn_memory_handle_);
}
#ifdef __ANDROID__
if (fd_ >= 0) close(fd_);
#else
if (!shm_region_name_.empty()) shm_unlink(shm_region_name_.c_str());
#endif
#endif
}

Expand Down
3 changes: 3 additions & 0 deletions tensorflow/lite/delegates/nnapi/nnapi_delegate_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class NNMemory {
size_t byte_size_ = 0;
uint8_t* data_ptr_ = nullptr;
ANeuralNetworksMemory* nn_memory_handle_ = nullptr;
#ifndef __ANDROID__
std::string shm_region_name_;
#endif
};

// LINT.IfChange
Expand Down
20 changes: 7 additions & 13 deletions tensorflow/lite/nnapi/nnapi_implementation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,21 +65,15 @@ void* LoadFunction(void* handle, const char* name, bool optional) {

#ifndef __ANDROID__
// Add /dev/shm implementation of shared memory for non-Android platforms
int ASharedMemory_create(const char* /* name */, size_t size) {
int ASharedMemory_create(const char* name, size_t size) {
// Each call to ASharedMemory_create produces a unique memory space, hence
// name should not be used to create the shared memory file, otherwise
// two calls to create memory regions using the same 'name', will collide.
char shm_name_buffer[L_tmpnam];
if (tmpnam(shm_name_buffer) == nullptr) {
return -1;
}

// tmpnam will produce a string containing with slashes, but shm_open
// won't like that.
std::string shm_region_name = std::string(shm_name_buffer);
std::replace(shm_region_name.begin(), shm_region_name.end(), '/', '-');
// name should be unique, otherwise two calls to create memory regions using
// the same 'name', will collide.
// Caller is responsible to provide a unique name.

int fd = shm_open(shm_region_name.c_str(), O_RDWR | O_CREAT, 0644);
// Make sure new shared memory region is created: shm_open return an error if
// shm object with given name already exists (O_CREAT | O_EXCL)
int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0644);
if (fd < 0) {
return fd;
}
Expand Down

0 comments on commit 2eb5ed7

Please sign in to comment.