shared_mem.cc 3.14 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
35
36
37
38
39
40
#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() {
    LOG(INFO) << "remove " << name << " for shared memory";
    shm_unlink(name.c_str());
  }
};
#endif  // _WIN32

41
SharedMemory::SharedMemory(const std::string &name) {
42
#ifndef _WIN32
43
44
45
46
47
  this->name = name;
  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
58
59
  munmap(ptr, size);
  close(fd);
  if (own) {
    LOG(INFO) << "remove " << name << " for shared memory";
    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 size) {
69
#ifndef _WIN32
70
71
  this->own = true;

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
80
81
82
83
84
85
  auto res = ftruncate(fd, size);
  CHECK_NE(res, -1)
      << "Failed to truncate the file. " << strerror(errno);
  ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  CHECK_NE(ptr, MAP_FAILED)
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
  return ptr;
86
87
88
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif  // _WIN32
89
90
}

91
void *SharedMemory::Open(size_t size) {
92
#ifndef _WIN32
93
94
95
96
97
98
99
  int flag = O_RDWR;
  fd = shm_open(name.c_str(), flag, S_IRUSR | S_IWUSR);
  CHECK_NE(fd, -1) << "fail to open " << name << ": " << strerror(errno);
  ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  CHECK_NE(ptr, MAP_FAILED)
      << "Failed to map shared memory. mmap failed with error " << strerror(errno);
  return ptr;
100
101
102
#else
  LOG(FATAL) << "Shared memory is not supported on Windows.";
#endif  // _WIN32
103
}
104
105

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

}  // namespace runtime
}  // namespace dgl