@@ -29,6 +29,188 @@ namespace paddle {
2929namespace memory {
3030namespace allocation {
3131
32+ std::string GetIPCName () {
33+ static std::random_device rd;
34+ std::string handle = " /paddle_" ;
35+ #ifdef _WIN32
36+ handle += std::to_string (GetCurrentProcessId ());
37+ #else
38+ handle += std::to_string (getpid ());
39+ #endif
40+ handle += " _" ;
41+ handle += std::to_string (rd ());
42+ return handle;
43+ }
44+
45+ struct CountInfo {
46+ std::atomic<int > refcount;
47+ };
48+
49+ void AllocateMemoryMap (std::string filename, int flags, size_t size,
50+ void **map_ptr_, int *fd_) {
51+ // TODO(@ZHUI): support win32
52+ int file_flags = 0 ;
53+ int fd = -1 ;
54+ if (flags & MAPPED_SHAREDMEM) {
55+ file_flags = O_RDWR | O_CREAT;
56+ } else {
57+ file_flags = O_RDONLY;
58+ }
59+ if (flags & MAPPED_EXCLUSIVE) {
60+ file_flags |= O_EXCL;
61+ }
62+ if (flags & MAPPED_NOCREATE) {
63+ file_flags &= ~O_CREAT;
64+ }
65+
66+ if (!(flags & MAPPED_FROMFD)) {
67+ if (flags & MAPPED_SHAREDMEM) {
68+ fd = shm_open (filename.c_str (), file_flags, (mode_t )0600 );
69+ PADDLE_ENFORCE_NE (
70+ fd, -1 ,
71+ platform::errors::Unavailable (
72+ " File descriptor %s open failed, unable in read-write mode" ,
73+ filename.c_str ()));
74+ VLOG (6 ) << " shm_open: " << filename;
75+ }
76+ } else {
77+ fd = -1 ;
78+ }
79+
80+ PADDLE_ENFORCE_EQ (ftruncate (fd, size), 0 ,
81+ platform::errors::Unavailable (
82+ " Fruncate a file to a specified length failed!" ));
83+
84+ if (flags & MAPPED_SHAREDMEM) {
85+ *map_ptr_ = mmap (nullptr , size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
86+ } else {
87+ *map_ptr_ = mmap (nullptr , size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0 );
88+ }
89+
90+ PADDLE_ENFORCE_NE (*map_ptr_, MAP_FAILED,
91+ platform::errors::Unavailable (
92+ " Memory map failed when create shared memory." ));
93+
94+ if (flags & MAPPED_KEEPFD) {
95+ *fd_ = fd;
96+ } else {
97+ PADDLE_ENFORCE_NE (::close (fd), -1 ,
98+ platform::errors::Unavailable (
99+ false , " Error closing file <" , filename, " >" ));
100+
101+ *fd_ = -1 ;
102+ }
103+ }
104+
105+ std::shared_ptr<MemoryMapAllocation> AllocateMemoryMapAllocation (
106+ std::string filename, int flags, size_t size) {
107+ int fd = -1 ;
108+ void *base_ptr = nullptr ;
109+ AllocateMemoryMap (filename, flags, size, &base_ptr, &fd);
110+ return std::make_shared<MemoryMapAllocation>(base_ptr, size, filename, flags,
111+ fd);
112+ }
113+
114+ std::shared_ptr<RefcountedMemoryMapAllocation>
115+ AllocateRefcountedMemoryMapAllocation (std::string filename, int flags,
116+ size_t size) {
117+ int fd = -1 ;
118+ void *base_ptr = nullptr ;
119+ AllocateMemoryMap (filename, flags, size + mmap_alignment, &base_ptr, &fd);
120+ void *aliged_base_ptr =
121+ static_cast <void *>(static_cast <char *>(base_ptr) + mmap_alignment);
122+ return std::make_shared<RefcountedMemoryMapAllocation>(aliged_base_ptr, size,
123+ filename, flags, fd);
124+ }
125+
126+ RefcountedMemoryMapAllocation::RefcountedMemoryMapAllocation (
127+ void *ptr, size_t size, std::string ipc_name, int fd, int flags)
128+ : MemoryMapAllocation(ptr, size, ipc_name, fd, flags) {
129+ // must reset base ptr first.
130+ resetBaseptr ();
131+ initializeRefercount ();
132+ }
133+
134+ void MemoryMapAllocation::close () {
135+ if (closed_) {
136+ return ;
137+ }
138+ closed_ = true ;
139+ if (map_ptr_ == nullptr ) {
140+ return ;
141+ }
142+ if (flags_ & MAPPED_KEEPFD) {
143+ PADDLE_ENFORCE_NE (
144+ ::close (fd_), -1,
145+ platform::errors::Unavailable(" could not close file descriptor " , fd_,
146+ " :" , strerror(errno), " (" , errno, " )"));
147+ }
148+
149+ PADDLE_ENFORCE_NE (
150+ munmap (map_ptr_, map_size_), -1 ,
151+ platform::errors::Unavailable (" could not unmap the shared memory file: " ,
152+ strerror (errno), " (" , errno, " )" ));
153+
154+ if (!(flags_ & (MAPPED_FROMFD | MAPPED_UNLINK))) {
155+ if (flags_ & MAPPED_SHAREDMEM) {
156+ PADDLE_ENFORCE_NE (
157+ shm_unlink (ipc_name_.c_str ()), -1 ,
158+ platform::errors::Unavailable (
159+ " could not unlink the shared memory file " , ipc_name_, " : " ,
160+ strerror (errno), " (" , errno, " )" ));
161+ }
162+ }
163+ }
164+
165+ MemoryMapAllocation::~MemoryMapAllocation () { close (); }
166+
167+ void RefcountedMemoryMapAllocation::incref () {
168+ CountInfo *info = static_cast <CountInfo *>(map_ptr_);
169+ ++info->refcount ;
170+ }
171+
172+ int RefcountedMemoryMapAllocation::decref () {
173+ CountInfo *info = static_cast <CountInfo *>(map_ptr_);
174+ return --info->refcount == 0 ;
175+ }
176+
177+ void RefcountedMemoryMapAllocation::resetBaseptr () {
178+ map_ptr_ =
179+ static_cast <void *>(static_cast <char *>(map_ptr_) - mmap_alignment);
180+ map_size_ = map_size_ + mmap_alignment;
181+ }
182+
183+ void RefcountedMemoryMapAllocation::initializeRefercount () {
184+ CountInfo *info = reinterpret_cast <CountInfo *>(map_ptr_);
185+
186+ if (flags_ & MAPPED_EXCLUSIVE) {
187+ new (&info->refcount ) std::atomic<int >(1 );
188+ } else {
189+ info->refcount ++;
190+ }
191+ }
192+
193+ void RefcountedMemoryMapAllocation::close () {
194+ if (closed_) {
195+ return ;
196+ }
197+ closed_ = true ;
198+ void *data = map_ptr_;
199+ CountInfo *info = reinterpret_cast <CountInfo *>(data);
200+ if (--info->refcount == 0 ) {
201+ PADDLE_ENFORCE_NE (
202+ shm_unlink (ipc_name_.c_str ()), -1 ,
203+ platform::errors::Unavailable (
204+ " could not unlink the shared memory file " , ipc_name_));
205+ VLOG (6 ) << " shm_unlink file: " << ipc_name;
206+ }
207+
208+ PADDLE_ENFORCE_NE (
209+ munmap (map_ptr_, map_size_), -1 ,
210+ platform::errors::Unavailable (" could not unmap the shared memory file: " ,
211+ strerror (errno), " (" , errno, " )" ));
212+ }
213+
32214MemoryMapWriterAllocation::~MemoryMapWriterAllocation () {
33215 PADDLE_ENFORCE_NE (
34216 munmap (this ->ptr (), this ->size ()), -1 ,
@@ -44,30 +226,30 @@ MemoryMapReaderAllocation::~MemoryMapReaderAllocation() {
44226 /* Here we do not pay attention to the result of shm_unlink,
45227 because the memory mapped file may have been cleared due to the
46228 MemoryMapFdSet::Clear() */
229+
230+ // Code of DataLoader subprocess:
231+ //
232+ // core._array_to_share_memory_tensor(b)
233+ // out_queue.put((idx, tensor_list, structure))
234+ // core._remove_tensor_list_mmap_fds(tensor_list)
235+
236+ /* If the tensor in already in the send queue, the tensor will be
237+ * deconstructed by the function. If the tensor not send yet, it
238+ * will be cleared by MemoryMapFdSet::Clear().
239+ * If the `_remove_tensor_list_mmap_fds` have be interrupted, the
240+ * tensor will be cleared by both methods.
241+ * */
242+
47243 shm_unlink (this ->ipc_name ().c_str ());
48244 MemoryMapFdSet::Instance ().Remove (this ->ipc_name ());
49245 VLOG (3 ) << " ~MemoryMapReaderAllocation: " << this ->ipc_name ();
50246}
51247
52- std::string GetIPCName () {
53- static std::random_device rd;
54- std::string handle = " /paddle_" ;
55- #ifdef _WIN32
56- handle += std::to_string (GetCurrentProcessId ());
57- #else
58- handle += std::to_string (getpid ());
59- #endif
60- handle += " _" ;
61- handle += std::to_string (rd ());
62- return handle;
63- }
64-
65248std::shared_ptr<MemoryMapWriterAllocation> AllocateMemoryMapWriterAllocation (
66249 size_t size) {
67250 const std::string &ipc_name = GetIPCName ();
68251 int flags = O_RDWR | O_CREAT;
69-
70- int fd = shm_open (ipc_name.c_str (), flags, 0644 );
252+ int fd = shm_open (ipc_name.c_str (), flags, 0600 );
71253 PADDLE_ENFORCE_NE (
72254 fd, -1 , platform::errors::Unavailable (" File descriptor %s open failed" ,
73255 ipc_name.c_str ()));
@@ -86,12 +268,16 @@ std::shared_ptr<MemoryMapWriterAllocation> AllocateMemoryMapWriterAllocation(
86268
87269std::shared_ptr<MemoryMapReaderAllocation> RebuildMemoryMapReaderAllocation (
88270 const std::string &ipc_name, size_t size) {
89- int fd = shm_open (ipc_name.c_str (), O_RDONLY, 0644 );
271+ // TODO(@ZHUI): support win32
272+ int flags = O_RDWR | O_CREAT;
273+ flags &= ~O_CREAT;
274+
275+ int fd = shm_open (ipc_name.c_str (), flags, 0600 );
90276 PADDLE_ENFORCE_NE (
91277 fd, -1 , platform::errors::Unavailable (" File descriptor %s open failed" ,
92278 ipc_name.c_str ()));
93-
94- void *ptr = mmap (NULL , size, PROT_READ, MAP_SHARED, fd, 0 );
279+ // pass
280+ void *ptr = mmap (nullptr , size, PROT_READ | PROT_WRITE , MAP_SHARED, fd, 0 );
95281 PADDLE_ENFORCE_NE (ptr, MAP_FAILED,
96282 platform::errors::Unavailable (
97283 " Memory map failed when rebuild shared memory." ));
0 commit comments