@@ -54,11 +54,14 @@ struct CountInfo {
5454 std::atomic<int > refcount;
5555};
5656
57- void AllocateMemoryMap (
58- std::string filename, int flags, size_t size, void **map_ptr_, int *fd_) {
57+ void AllocateMemoryMap (std::string filename,
58+ int *shared_fd,
59+ int flags,
60+ size_t size,
61+ void **map_ptr_) {
5962 // TODO(@ZHUI): support win32
6063 int file_flags = 0 ;
61- int fd = - 1 ;
64+ int fd = *shared_fd ;
6265 if (flags & MAPPED_SHAREDMEM) {
6366 file_flags = O_RDWR | O_CREAT;
6467 } else {
@@ -71,7 +74,7 @@ void AllocateMemoryMap(
7174 file_flags &= ~O_CREAT;
7275 }
7376
74- if (!(flags & MAPPED_FROMFD)) {
77+ if (!(flags & MAPPED_FROMFD) && fd == - 1 ) {
7578 if (flags & MAPPED_SHAREDMEM) {
7679 fd = shm_open (filename.c_str (), file_flags, (mode_t )0600 );
7780 PADDLE_ENFORCE_NE (
@@ -83,8 +86,6 @@ void AllocateMemoryMap(
8386 VLOG (6 ) << " shm_open: " << filename;
8487 MemoryMapFdSet::Instance ().Insert (filename);
8588 }
86- } else {
87- fd = -1 ;
8889 }
8990
9091 PADDLE_ENFORCE_EQ (ftruncate (fd, size),
@@ -98,32 +99,38 @@ void AllocateMemoryMap(
9899 *map_ptr_ = mmap (nullptr , size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0 );
99100 }
100101
102+ if (flags & MAPPED_UNLINK) {
103+ VLOG (6 ) << " shm_unlink: " << filename;
104+ shm_unlink (filename.c_str ());
105+ }
106+
101107 PADDLE_ENFORCE_NE (*map_ptr_,
102108 MAP_FAILED,
103109 platform::errors::Unavailable (
104110 " Memory map failed when create shared memory." ));
105-
106111 if (flags & MAPPED_KEEPFD) {
107- *fd_ = fd;
112+ *shared_fd = fd;
113+ VLOG (6 ) << " keep fd: " << *shared_fd;
108114 } else {
109115 PADDLE_ENFORCE_NE (::close (fd),
110116 -1 ,
111117 platform::errors::Unavailable (
112118 " Error closing memory mapped file <" , filename, " >" ));
113119
114- *fd_ = -1 ;
120+ *shared_fd = -1 ;
115121 }
116122}
117123
118124std::shared_ptr<RefcountedMemoryMapAllocation>
119125AllocateRefcountedMemoryMapAllocation (std::string filename,
126+ int shared_fd,
120127 int flags,
121128 size_t size,
122129 int buffer_id) {
123- int fd = - 1 ;
130+ int fd = shared_fd ;
124131 void *base_ptr = nullptr ;
125132 if (buffer_id == -1 ) {
126- AllocateMemoryMap (filename, flags, size + mmap_alignment, &base_ptr, &fd );
133+ AllocateMemoryMap (filename, &fd, flags, size + mmap_alignment, &base_ptr);
127134 VLOG (4 ) << " Create and mmap a new shm: " << filename;
128135 } else {
129136 base_ptr = MemoryMapAllocationPool::Instance ().GetById (buffer_id).mmap_ptr_ ;
@@ -132,7 +139,7 @@ AllocateRefcountedMemoryMapAllocation(std::string filename,
132139 void *aligned_base_ptr =
133140 static_cast <void *>(static_cast <char *>(base_ptr) + mmap_alignment);
134141 return std::make_shared<RefcountedMemoryMapAllocation>(
135- aligned_base_ptr, size, filename, flags, fd , buffer_id);
142+ aligned_base_ptr, size, filename, fd, flags , buffer_id);
136143}
137144
138145RefcountedMemoryMapAllocation::RefcountedMemoryMapAllocation (
@@ -145,11 +152,23 @@ RefcountedMemoryMapAllocation::RefcountedMemoryMapAllocation(
145152 : MemoryMapAllocation(ptr, size, ipc_name, fd, flags) {
146153 // must reset base ptr first.
147154 buffer_id_ = buffer_id;
155+ fd_ = fd;
156+ flags_ = flags;
148157 resetBaseptr ();
149158 initializeRefercount ();
150159}
151160
152161void MemoryMapAllocation::close () {
162+ if (!closed_fd_) {
163+ closed_fd_ = true ;
164+ if (flags_ & MAPPED_KEEPFD) {
165+ VLOG (6 ) << " one close fd: " << fd_;
166+ PADDLE_ENFORCE_NE (::close (fd_),
167+ -1 ,
168+ platform::errors::Unavailable (
169+ " Error closing file descriptor <" , fd_, " >" ));
170+ }
171+ }
153172 if (closed_) {
154173 return ;
155174 }
@@ -193,6 +212,15 @@ void RefcountedMemoryMapAllocation::close() {
193212 void *data = map_ptr_;
194213 CountInfo *info = reinterpret_cast <CountInfo *>(data);
195214 --info->refcount ;
215+ if (flags_ & MAPPED_KEEPFD) {
216+ closed_fd_ = true ;
217+ PADDLE_ENFORCE_NE (::close (fd_),
218+ -1 ,
219+ platform::errors::Unavailable (
220+ " Error closing file descriptor <" , fd_, " >" ));
221+ VLOG (6 ) << " close fd: " << fd_;
222+ }
223+
196224 if (FLAGS_use_shm_cache && buffer_id_ != -1 ) {
197225 return ;
198226 } else {
@@ -260,6 +288,7 @@ std::shared_ptr<MemoryMapWriterAllocation> AllocateMemoryMapWriterAllocation(
260288 const std::string &ipc_name = GetIPCName ();
261289 int flags = O_RDWR | O_CREAT;
262290 int fd = shm_open (ipc_name.c_str (), flags, 0600 );
291+
263292 PADDLE_ENFORCE_NE (fd,
264293 -1 ,
265294 platform::errors::Unavailable (
@@ -283,7 +312,6 @@ std::shared_ptr<MemoryMapReaderAllocation> RebuildMemoryMapReaderAllocation(
283312 const std::string &ipc_name, size_t size) {
284313 int flags = O_RDWR | O_CREAT;
285314 flags &= ~O_CREAT;
286-
287315 int fd = shm_open (ipc_name.c_str (), flags, 0600 );
288316 PADDLE_ENFORCE_NE (fd,
289317 -1 ,
0 commit comments