1 /**
2 * Copyright 2023 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "runtime/device/gsm/io_handle.h"
18 #ifdef _MSC_VER
19 #include <windows.h>
20 #else
21 #include <dlfcn.h>
22 #endif
23 #include <memory>
24 #include "utils/system/env.h"
25 #include "utils/file_utils.h"
26 #include "include/common/utils/offload_context.h"
27
28 namespace mindspore {
29 namespace device {
30 constexpr size_t kFileHeadOffset = 0;
31 constexpr char kReadFileMode[] = "r+";
32 constexpr size_t kAlignSize = 0x1ff;
33
LoadAio(const std::string & aio_shared_lib_name,const std::string & instance_func_name)34 void IOHandle::LoadAio(const std::string &aio_shared_lib_name, const std::string &instance_func_name) {
35 #ifdef _MSC_VER
36 auto handle = LoadLibrary(aio_shared_lib_name.c_str());
37 if (handle == nullptr) {
38 MS_LOG(WARNING) << "Loading " << aio_shared_lib_name << " failed.";
39 }
40 auto get_aio_instance = reinterpret_cast<AsyncIO *(*)()>(GetProcAddress(handle, instance_func_name.c_str()));
41 if (get_aio_instance == nullptr) {
42 MS_LOG(WARNING) << "Getting function " << instance_func_name << " from " << aio_shared_lib_name << " failed.";
43 return;
44 }
45 #else
46 auto handle = dlopen(aio_shared_lib_name.c_str(), RTLD_NOW);
47 if (handle == nullptr) {
48 MS_LOG(WARNING) << "Loading " << aio_shared_lib_name << " failed. Error message: " << dlerror();
49 return;
50 }
51 void *get_aio_instance_ptr = dlsym(handle, instance_func_name.c_str());
52 auto get_aio_instance = reinterpret_cast<AsyncIO *(*)()>(get_aio_instance_ptr);
53 if (get_aio_instance == nullptr) {
54 MS_LOG(WARNING) << "Getting function " << instance_func_name << " from " << aio_shared_lib_name
55 << " failed. Error message: " << dlerror();
56 return;
57 }
58 #endif
59 aio_ = get_aio_instance();
60 MS_EXCEPTION_IF_NULL(aio_);
61 const auto &offload_context = OffloadContext::GetInstance();
62 MS_EXCEPTION_IF_NULL(offload_context);
63 if (!aio_->Init({offload_context->aio_block_size(), offload_context->aio_queue_depth()})) {
64 MS_LOG(WARNING) << "Init aio plugin failed, block size: " << offload_context->aio_block_size()
65 << ", queue depth: " << offload_context->aio_queue_depth();
66 aio_ = nullptr;
67 }
68 }
69
Read(const std::string & file_name,void * data,size_t byte_num) const70 bool IOHandle::Read(const std::string &file_name, void *data, size_t byte_num) const {
71 if (aio_ != nullptr && IsAligned(data, byte_num)) {
72 return aio_->Read(file_name, data, byte_num);
73 }
74 const auto &fs = system::Env::GetFileSystem();
75 MS_EXCEPTION_IF_NULL(fs);
76 auto file = fs->CreateWriteFile(file_name, kReadFileMode);
77 MS_EXCEPTION_IF_NULL(file);
78 return file->PRead(data, byte_num, kFileHeadOffset);
79 }
80
Write(const std::string & file_name,const void * data,size_t byte_num) const81 bool IOHandle::Write(const std::string &file_name, const void *data, size_t byte_num) const {
82 if (aio_ != nullptr && IsAligned(data, byte_num)) {
83 return aio_->Write(file_name, data, byte_num);
84 }
85 const auto &fs = system::Env::GetFileSystem();
86 MS_EXCEPTION_IF_NULL(fs);
87 auto file = fs->CreateWriteFile(file_name);
88 MS_EXCEPTION_IF_NULL(file);
89 return file->PWrite(data, byte_num, kFileHeadOffset);
90 }
91
ReadAsync(const std::string & file_name,void * data,size_t byte_num,AsyncIOToken * token) const92 bool IOHandle::ReadAsync(const std::string &file_name, void *data, size_t byte_num, AsyncIOToken *token) const {
93 if (aio_ != nullptr && IsAligned(data, byte_num)) {
94 return aio_->ReadAsync(file_name, data, byte_num, token);
95 }
96 const auto &fs = system::Env::GetFileSystem();
97 MS_EXCEPTION_IF_NULL(fs);
98 auto file = fs->CreateWriteFile(file_name, kReadFileMode);
99 MS_EXCEPTION_IF_NULL(file);
100 *token = kInvalidAsyncIOToken;
101 return file->PRead(data, byte_num, kFileHeadOffset);
102 }
103
WriteAsync(const std::string & file_name,const void * data,size_t byte_num,AsyncIOToken * token) const104 bool IOHandle::WriteAsync(const std::string &file_name, const void *data, size_t byte_num, AsyncIOToken *token) const {
105 if (aio_ != nullptr && IsAligned(data, byte_num)) {
106 return aio_->WriteAsync(file_name, data, byte_num, token);
107 }
108 const auto &fs = system::Env::GetFileSystem();
109 MS_EXCEPTION_IF_NULL(fs);
110 auto file = fs->CreateWriteFile(file_name);
111 MS_EXCEPTION_IF_NULL(file);
112 *token = kInvalidAsyncIOToken;
113 return file->PWrite(data, byte_num, kFileHeadOffset);
114 }
IsAligned(const void * data,const size_t byte_num) const115 bool IOHandle::IsAligned(const void *data, const size_t byte_num) const {
116 return ((byte_num & kAlignSize) == 0) && ((reinterpret_cast<size_t>(data) & kAlignSize) == 0);
117 }
118
Wait(AsyncIOToken token) const119 bool IOHandle::Wait(AsyncIOToken token) const { return aio_ == nullptr || aio_->Wait(token); }
120
DeleteSwapFile(const std::string & file_name) const121 bool IOHandle::DeleteSwapFile(const std::string &file_name) const {
122 const auto &fs = system::Env::GetFileSystem();
123 MS_EXCEPTION_IF_NULL(fs);
124 return fs->DeleteFile(file_name);
125 }
126
CreateSwapFile(const std::string & file_name) const127 bool IOHandle::CreateSwapFile(const std::string &file_name) const {
128 const auto &fs = system::Env::GetFileSystem();
129 MS_EXCEPTION_IF_NULL(fs);
130 auto file = fs->CreateWriteFile(file_name);
131 MS_EXCEPTION_IF_NULL(file);
132 return true;
133 }
134 } // namespace device
135 } // namespace mindspore
136