提交 91f2b780 编写于 作者: L liuqi

Refacotr KVStorage apis with return status.

上级 36458f35
......@@ -49,16 +49,28 @@ std::unique_ptr<KVStorage> 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<unsigned char *>(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<unsigned char> *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
......@@ -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<unsigned char> &value) override;
const std::vector<unsigned char> *Find(const std::string &key) override;
void Flush() override;
KVStorageStatus Flush() override;
private:
std::string file_path_;
......
......@@ -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;
}
}
......
......@@ -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<unsigned char> &value) = 0;
virtual const std::vector<unsigned char> *Find(const std::string &key) = 0;
virtual void Flush() = 0;
virtual KVStorageStatus Flush() = 0;
};
class KVStorageFactory {
......
......@@ -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,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册