1 #include "file_store_handler_op.h" 19 #include "c10/util/StringUtil.h" 21 #include "caffe2/utils/murmur_hash3.h" 25 static std::string encodeName(
const std::string& name) {
26 std::array<uint64_t, 2> out;
27 MurmurHash3_x64_128(name.data(), name.size(), 0xcafef00d, out.data());
30 std::array<char, 33> buf;
31 for (
int i = 0; i < 16; i++) {
32 snprintf(&buf[i * 2], buf.size() - (i * 2),
"%02x", ((
char*)out.data())[i]);
36 return std::string(buf.data(), buf.size() - 1);
39 FileStoreHandler::FileStoreHandler(
40 const std::string& path,
41 const std::string& prefix) {
42 basePath_ = realPath(path);
43 if (!prefix.empty()) {
44 basePath_ = basePath_ +
"/" + encodeName(prefix);
47 auto ret = _mkdir(basePath_.c_str());
49 auto ret = mkdir(basePath_.c_str(), 0777);
50 #endif // defined(_MSC_VER) 52 CHECK_EQ(errno, EEXIST) <<
"mkdir: " << strerror(errno);
56 FileStoreHandler::~FileStoreHandler() {}
58 std::string FileStoreHandler::realPath(
const std::string& path) {
60 std::array<char, _MAX_PATH> buf;
61 auto ret = _fullpath(buf.data(), path.c_str(), buf.size());
63 std::array<char, PATH_MAX> buf;
64 auto ret = realpath(path.c_str(), buf.data());
66 CHECK_EQ(buf.data(), ret) <<
"realpath: " << strerror(errno);
67 return std::string(buf.data());
70 std::string FileStoreHandler::tmpPath(
const std::string& name) {
71 return basePath_ +
"/." + encodeName(name);
74 std::string FileStoreHandler::objectPath(
const std::string& name) {
75 return basePath_ +
"/" + encodeName(name);
78 void FileStoreHandler::set(
const std::string& name,
const std::string& data) {
79 auto tmp = tmpPath(name);
80 auto path = objectPath(name);
83 std::ofstream ofs(tmp.c_str(), std::ios::out | std::ios::trunc);
86 false,
"File cannot be created: ", tmp,
" (", ofs.rdstate(),
")");
92 auto rv = rename(tmp.c_str(), path.c_str());
93 CAFFE_ENFORCE_EQ(rv, 0,
"rename: ", strerror(errno));
96 std::string FileStoreHandler::get(
97 const std::string& name,
98 const std::chrono::milliseconds& timeout) {
99 auto path = objectPath(name);
103 wait({name}, timeout);
105 std::ifstream ifs(path.c_str(), std::ios::in);
108 false,
"File cannot be opened: ", path,
" (", ifs.rdstate(),
")");
110 ifs.seekg(0, std::ios::end);
111 size_t n = ifs.tellg();
114 ifs.read(&result[0], n);
118 int64_t FileStoreHandler::add(
121 CHECK(
false) <<
"add not implemented for FileStoreHandler";
125 bool FileStoreHandler::check(
const std::vector<std::string>& names) {
126 std::vector<std::string> paths;
127 for (
const auto& name : names) {
128 paths.push_back(objectPath(name));
131 for (
const auto& path : paths) {
132 int fd = open(path.c_str(), O_RDONLY);
136 CHECK_EQ(errno, ENOENT);
148 void FileStoreHandler::wait(
149 const std::vector<std::string>& names,
150 const std::chrono::milliseconds& timeout) {
153 const auto start = std::chrono::steady_clock::now();
154 while (!check(names)) {
155 const auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(
156 std::chrono::steady_clock::now() - start);
157 if (timeout != kNoTimeout && elapsed > timeout) {
158 STORE_HANDLER_TIMEOUT(
159 "Wait timeout for name(s): ", c10::Join(
" ", names));
162 std::this_thread::sleep_for(std::chrono::milliseconds(10));
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...