"src/vscode:/vscode.git/clone" did not exist on "1afc21855eb1f5575bd61037a7ee44522ccf401e"
shared_mem.cc 3.28 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
34
#ifndef _WIN32
/*
 * 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() {
35
    // LOG(INFO) << "remove " << name << " for shared memory";
36
37
38
39
40
    shm_unlink(name.c_str());
  }
};
#endif  // _WIN32

41
SharedMemory::SharedMemory(const std::string &name) {
42
#ifndef _WIN32
43
  this->name = name;
44
45
46
47
  this->own_ = false;
  this->fd_ = -1;
  this->ptr_ = nullptr;
  this->size_ = 0;
48
49
50
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif  // _WIN32
51
52
53
}

SharedMemory::~SharedMemory() {
54
#ifndef _WIN32
55
56
57
  CHECK(munmap(ptr_, size_) != -1) << strerror(errno);
  close(fd_);
  if (own_) {
58
    // LOG(INFO) << "remove " << name << " for shared memory";
59
    shm_unlink(name.c_str());
60
61
    // The resource has been deleted. We don't need to keep track of it any more.
    DeleteResource(name);
62
  }
63
64
65
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif  // _WIN32
66
67
}

68
void *SharedMemory::CreateNew(size_t sz) {
69
#ifndef _WIN32
70
  this->own_ = true;
71

72
73
  // We need to create a shared-memory file.
  // TODO(zhengda) we need to report error if the shared-memory file exists.
74
  int flag = O_RDWR|O_CREAT;
75
76
  fd_ = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
  CHECK_NE(fd_, -1) << "fail to open " << name << ": " << strerror(errno);
77
78
  // Shared memory cannot be deleted if the process exits abnormally.
  AddResource(name, std::shared_ptr<Resource>(new SharedMemoryResource(name)));
79
  auto res = ftruncate(fd_, sz);
80
81
  CHECK_NE(res, -1)
      << "Failed to truncate the file. " << strerror(errno);
82
83
  ptr_ = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd_, 0);
  CHECK_NE(ptr_, MAP_FAILED)
84
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
85
86
  this->size_ = sz;
  return ptr_;
87
88
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
89
  return nullptr;
90
#endif  // _WIN32
91
92
}

93
void *SharedMemory::Open(size_t sz) {
94
#ifndef _WIN32
95
  int flag = O_RDWR;
96
97
98
99
  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)
100
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
101
102
  this->size_ = sz;
  return ptr_;
103
104
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
105
  return nullptr;
106
#endif  // _WIN32
107
}
108
109

bool SharedMemory::Exist(const std::string &name) {
110
#ifndef _WIN32
111
112
113
  int fd_ = shm_open(name.c_str(), O_RDONLY, S_IRUSR | S_IWUSR);
  if (fd_ >= 0) {
    close(fd_);
114
115
116
117
    return true;
  } else {
    return false;
  }
118
119
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
120
  return false;
121
#endif  // _WIN32
122
}
123
124
125

}  // namespace runtime
}  // namespace dgl