shared_mem.cc 4.31 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*!
 *  Copyright (c) 2019 by Contributors
 * \file shared_mem.cc
 * \brief Shared memory management.
 */
#ifndef _WIN32
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <string.h>
#include <dmlc/logging.h>
#include <dgl/runtime/shared_mem.h>

16
17
#include "resource_manager.h"

18
19
20
namespace dgl {
namespace runtime {

21
22
23
24
25
26
27
28
29
30
31
32
33
/*
 * Shared memory is a resource that cannot be cleaned up if the process doesn't
 * exit normally. We'll manage the resource with ResourceManager.
 */
class SharedMemoryResource: public Resource {
  std::string name;

 public:
  explicit SharedMemoryResource(const std::string &name) {
    this->name = name;
  }

  void Destroy() {
34
    // LOG(INFO) << "remove " << name << " for shared memory";
35
#ifndef _WIN32
36
    shm_unlink(name.c_str());
37
38
39
40
#else   // _WIN32
    // NOTHING; Windows automatically removes the shared memory object once all handles
    // are unmapped.
#endif
41
42
43
  }
};

44
45
SharedMemory::SharedMemory(const std::string &name) {
  this->name = name;
46
  this->own_ = false;
47
#ifndef _WIN32
48
  this->fd_ = -1;
49
50
51
#else
  this->handle_ = nullptr;
#endif
52
53
  this->ptr_ = nullptr;
  this->size_ = 0;
54
55
56
}

SharedMemory::~SharedMemory() {
57
#ifndef _WIN32
58
59
60
  CHECK(munmap(ptr_, size_) != -1) << strerror(errno);
  close(fd_);
  if (own_) {
61
    // LOG(INFO) << "remove " << name << " for shared memory";
62
    shm_unlink(name.c_str());
63
64
    // The resource has been deleted. We don't need to keep track of it any more.
    DeleteResource(name);
65
  }
66
#else
67
68
69
  CHECK(UnmapViewOfFile(ptr_)) << "Win32 Error: " << GetLastError();
  CloseHandle(handle_);
  // Windows do not need a separate shm_unlink step.
70
#endif  // _WIN32
71
72
}

73
void *SharedMemory::CreateNew(size_t sz) {
74
#ifndef _WIN32
75
  this->own_ = true;
76

77
78
  // We need to create a shared-memory file.
  // TODO(zhengda) we need to report error if the shared-memory file exists.
79
  int flag = O_RDWR|O_CREAT;
80
81
  fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
  CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
82
  // Shared memory cannot be deleted if the process exits abnormally in Linux.
83
  AddResource(name, std::shared_ptr<Resource>(new SharedMemoryResource(name)));
84
  auto res = ftruncate(fd_, sz);
85
86
  CHECK_NE(res, -1)
      << "Failed to truncate the file. " << strerror(errno);
87
88
  ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
  CHECK_NE(ptr_, MAP_FAILED)
89
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
90
91
  this->size_ = sz;
  return ptr_;
92
#else
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
  handle_ = CreateFileMapping(
      INVALID_HANDLE_VALUE,
      nullptr,
      PAGE_READWRITE,
      static_cast<DWORD>(sz >> 32),
      static_cast<DWORD>(sz & 0xFFFFFFFF),
      name.c_str());
  CHECK(handle_ != nullptr) << "fail to open " << name << ", Win32 error: " << GetLastError();
  ptr_ = MapViewOfFile(handle_, FILE_MAP_ALL_ACCESS, 0, 0, sz);
  if (ptr_ == nullptr) {
    LOG(FATAL) << "Memory mapping failed, Win32 error: " << GetLastError();
    CloseHandle(handle_);
    return nullptr;
  }
  this->size_ = sz;
  return ptr_;
109
#endif  // _WIN32
110
111
}

112
void *SharedMemory::Open(size_t sz) {
113
#ifndef _WIN32
114
  int flag = O_RDWR;
115
116
117
118
  fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
  CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
  ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
  CHECK_NE(ptr_, MAP_FAILED)
119
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
120
121
  this->size_ = sz;
  return ptr_;
122
#else
123
124
125
126
127
128
129
130
131
132
  handle_ = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name.c_str());
  CHECK(handle_ != nullptr) << "fail to open " << name << ", Win32 Error: " << GetLastError();
  ptr_ = MapViewOfFile(handle_, FILE_MAP_ALL_ACCESS, 0, 0, sz);
  if (ptr_ == nullptr) {
    LOG(FATAL) << "Memory mapping failed, Win32 error: " << GetLastError();
    CloseHandle(handle_);
    return nullptr;
  }
  this->size_ = sz;
  return ptr_;
133
#endif  // _WIN32
134
}
135
136

bool SharedMemory::Exist(const std::string &name) {
137
#ifndef _WIN32
138
139
140
  int fd = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR);
  if (fd >= 0) {
    close(fd);
141
142
143
144
    return true;
  } else {
    return false;
  }
145
#else
146
147
148
149
150
151
152
  HANDLE handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name.c_str());
  if (handle != nullptr) {
    CloseHandle(handle);
    return true;
  } else {
    return false;
  }
153
#endif  // _WIN32
154
}
155
156
157

}  // namespace runtime
}  // namespace dgl