diff --git a/mace/core/file_storage.cc b/mace/core/file_storage.cc index d0030d2bd91b470f7bbaaa09a5d3c9ee9fb39ca9..960cbf6120a3c1f523c15a62150693f538846b82 100644 --- a/mace/core/file_storage.cc +++ b/mace/core/file_storage.cc @@ -49,16 +49,28 @@ std::unique_ptr FileStorageFactory::CreateStorage( FileStorage::FileStorage(const std::string &file_path): file_path_(file_path) {} -void FileStorage::Load() { +KVStorageStatus FileStorage::Load() { struct stat st; - stat(file_path_.c_str(), &st); - size_t file_size = st.st_size; + if (stat(file_path_.c_str(), &st) == -1) { + if (errno == ENOENT) { + return KVStorageStatus::STORAGE_FILE_NOT_EXIST; + } else { + LOG(WARNING) << "stat file " << file_path_ + << " failed, error code: " << errno; + return KVStorageStatus::STORAGE_ERROR; + } + } int fd = open(file_path_.c_str(), O_RDONLY); - if (fd == -1) { - LOG(WARNING) << "open file " << file_path_ - << " failed, error code: " << errno; - return; + if (fd < 0) { + if (errno == ENOENT) { + return KVStorageStatus::STORAGE_FILE_NOT_EXIST; + } else { + LOG(WARNING) << "open file " << file_path_ + << " failed, error code: " << errno; + return KVStorageStatus::STORAGE_ERROR; + } } + size_t file_size = st.st_size; unsigned char *file_data = static_cast(mmap(nullptr, file_size, PROT_READ, MAP_PRIVATE, fd, 0)); @@ -72,7 +84,7 @@ void FileStorage::Load() { LOG(WARNING) << "close file " << file_path_ << " failed, error code: " << errno; } - return; + return KVStorageStatus::STORAGE_ERROR; } unsigned char *file_data_ptr = file_data; @@ -104,12 +116,20 @@ void FileStorage::Load() { if (res != 0) { LOG(WARNING) << "munmap file " << file_path_ << " failed, error code: " << errno; + res = close(fd); + if (res != 0) { + LOG(WARNING) << "close file " << file_path_ + << " failed, error code: " << errno; + } + return KVStorageStatus::STORAGE_ERROR; } res = close(fd); if (res != 0) { LOG(WARNING) << "close file " << file_path_ << " failed, error code: " << errno; + return KVStorageStatus::STORAGE_ERROR; } + return KVStorageStatus::STORAGE_SUCCESS; } bool FileStorage::Insert(const std::string &key, @@ -125,12 +145,12 @@ const std::vector *FileStorage::Find(const std::string &key) { return &(iter->second); } -void FileStorage::Flush() { +KVStorageStatus FileStorage::Flush() { int fd = open(file_path_.c_str(), O_WRONLY | O_CREAT, 0600); if (fd < 0) { LOG(WARNING) << "open file " << file_path_ << " failed, error code:" << errno; - return; + return KVStorageStatus::STORAGE_ERROR; } const size_t int_size = sizeof(int32_t); @@ -169,7 +189,12 @@ void FileStorage::Flush() { if (res == -1) { LOG(WARNING) << "write file " << file_path_ << " failed, error code: " << errno; - return; + res = close(fd); + if (res != 0) { + LOG(WARNING) << "close file " << file_path_ + << " failed, error code: " << errno; + } + return KVStorageStatus::STORAGE_ERROR; } remain_size -= buffer_size; buffer_ptr += buffer_size; @@ -179,7 +204,9 @@ void FileStorage::Flush() { if (res != 0) { LOG(WARNING) << "close file " << file_path_ << " failed, error code: " << errno; + return KVStorageStatus::STORAGE_ERROR; } + return KVStorageStatus::STORAGE_SUCCESS; } }; // namespace mace diff --git a/mace/core/file_storage.h b/mace/core/file_storage.h index 7c777e7edd6c32a6694c92f2d538cceadb06d1c6..716cf336db31d705acf913c9c544a06953340f37 100644 --- a/mace/core/file_storage.h +++ b/mace/core/file_storage.h @@ -18,11 +18,11 @@ class FileStorage : public KVStorage { explicit FileStorage(const std::string &file_path); public: - void Load() override; + KVStorageStatus Load() override; bool Insert(const std::string &key, const std::vector &value) override; const std::vector *Find(const std::string &key) override; - void Flush() override; + KVStorageStatus Flush() override; private: std::string file_path_; diff --git a/mace/core/runtime/opencl/opencl_runtime.cc b/mace/core/runtime/opencl/opencl_runtime.cc index 9d7045f2ca4728f4a76cc4ced522dab4151392ac..44be3e959ca265020789982fe4d5487011136978 100644 --- a/mace/core/runtime/opencl/opencl_runtime.cc +++ b/mace/core/runtime/opencl/opencl_runtime.cc @@ -317,7 +317,9 @@ OpenCLRuntime::OpenCLRuntime(GPUPerfHint gpu_perf_hint, storage_ = kStorageFactory->CreateStorage(cl_compiled_file_name); if (platform_info_ != kCompiledProgramPlatform) { - storage_->Load(); + if (storage_->Load() == KVStorageStatus::STORAGE_ERROR) { + LOG(FATAL) << "Load opencl compiled kernel file failed"; + } } } } @@ -504,7 +506,9 @@ cl::Kernel OpenCLRuntime::BuildKernel( void OpenCLRuntime::SaveBuiltCLProgram() { if (program_map_changed_ && storage_ != nullptr) { - storage_->Flush(); + if (storage_->Flush() != KVStorageStatus::STORAGE_SUCCESS) { + LOG(FATAL) << "Store opencl compiled kernel to file failed"; + } program_map_changed_ = false; } } diff --git a/mace/public/mace_runtime.h b/mace/public/mace_runtime.h index 46500994642b29e16dadacfa2cac3a1c597c9cb8..09de430cd77a877a1a30bd4f64303cbad5d7f4aa 100644 --- a/mace/public/mace_runtime.h +++ b/mace/public/mace_runtime.h @@ -31,13 +31,19 @@ enum GPUPriorityHint { enum CPUPowerOption { DEFAULT = 0, HIGH_PERFORMANCE = 1, BATTERY_SAVE = 2 }; +enum KVStorageStatus { + STORAGE_SUCCESS = 0, + STORAGE_FILE_NOT_EXIST = 1, + STORAGE_ERROR = 2 +}; + class KVStorage { public: - virtual void Load() = 0; + virtual KVStorageStatus Load() = 0; virtual bool Insert(const std::string &key, const std::vector &value) = 0; virtual const std::vector *Find(const std::string &key) = 0; - virtual void Flush() = 0; + virtual KVStorageStatus Flush() = 0; }; class KVStorageFactory { diff --git a/tools/mace_tools.py b/tools/mace_tools.py index 002e430e744d3bac46100643bee30f1f3c7e2ff9..d229573a5f1210068eb00616b9cf9b5e7b360b17 100644 --- a/tools/mace_tools.py +++ b/tools/mace_tools.py @@ -215,7 +215,7 @@ def build_mace_run_prod(model_name, target_runtime, target_abi, target_soc, tuning_run( model_name, target_runtime, - target_abi, + target_abi, target_soc, model_output_dir, running_round=0,