From 91f2b780664b5722bf7b4669d4b01b54e98e2a07 Mon Sep 17 00:00:00 2001 From: liuqi Date: Sun, 8 Apr 2018 14:03:21 +0800 Subject: [PATCH] Refacotr KVStorage apis with return status. --- mace/core/file_storage.cc | 49 +++++++++++++++++----- mace/core/file_storage.h | 4 +- mace/core/runtime/opencl/opencl_runtime.cc | 8 +++- mace/public/mace_runtime.h | 10 ++++- tools/mace_tools.py | 2 +- 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/mace/core/file_storage.cc b/mace/core/file_storage.cc index d0030d2b..960cbf61 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 7c777e7e..716cf336 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 9d7045f2..44be3e95 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 46500994..09de430c 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 002e430e..d229573a 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, -- GitLab