Skip to content

Commit f6e73e8

Browse files
[SYCL] persistent cache fix - directory creation and reporting improvements (#13019)
We have a report of persistent cache failures. Traced to the directory creation so I switched it to use C++17 std::filesystem routines for `OSUtil::makeDir`. Also improved trace reporting.
1 parent 0c0b586 commit f6e73e8

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

sycl/include/sycl/detail/os_util.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class __SYCL_EXPORT OSUtil {
6565
/// Deallocates the memory referenced by \p Ptr.
6666
static void alignedFree(void *Ptr);
6767

68-
/// Make directory recursively and returns zero code on success
68+
/// Make all directories on the path, throws on error.
6969
static int makeDir(const char *Dir);
7070

7171
/// Checks if specified path is present

sycl/source/detail/os_util.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
#include <cassert>
1212
#include <limits>
1313

14+
#if __GNUC__ && __GNUC__ < 8
15+
// Don't include <filesystem> for GCC versions less than 8
16+
#else
17+
#include <filesystem> // C++ 17 std::create_directories
18+
#endif
19+
1420
#if defined(__SYCL_RT_OS_LINUX)
1521

1622
#ifndef _GNU_SOURCE
@@ -32,7 +38,6 @@
3238
#include <detail/windows_os_utils.hpp>
3339

3440
#include <Windows.h>
35-
#include <direct.h>
3641
#include <malloc.h>
3742
#include <shlwapi.h>
3843

@@ -233,15 +238,14 @@ void OSUtil::alignedFree(void *Ptr) {
233238
#endif
234239
}
235240

236-
/* This is temporary solution until std::filesystem is available when SYCL RT
237-
* is moved to c++17 standard*/
238-
239-
/* Create directory recursively and return non zero code on success*/
241+
// Make all directories on the path, throws on error.
240242
int OSUtil::makeDir(const char *Dir) {
241243
assert((Dir != nullptr) && "Passed null-pointer as directory name.");
242244
if (isPathPresent(Dir))
243245
return 0;
244246

247+
// older GCC doesn't have full C++ 17 support.
248+
#if __GNUC__ && __GNUC__ < 8
245249
std::string Path{Dir}, CurPath;
246250
size_t pos = 0;
247251

@@ -254,8 +258,15 @@ int OSUtil::makeDir(const char *Dir) {
254258
auto Res = _mkdir(CurPath.c_str());
255259
#endif
256260
if (Res && errno != EEXIST)
257-
return Res;
261+
throw std::runtime_error("Failed to mkdir: " + CurPath + " (" +
262+
std::strerror(errno) + ")");
263+
258264
} while (pos != std::string::npos);
265+
#else
266+
// using filesystem is simpler, more reliable, works better on Win
267+
std::filesystem::path path(Dir);
268+
std::filesystem::create_directories(path.make_preferred());
269+
#endif
259270
return 0;
260271
}
261272

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <detail/plugin.hpp>
1212
#include <detail/program_manager/program_manager.hpp>
1313

14+
#include <cerrno>
1415
#include <cstdio>
1516
#include <fstream>
1617
#include <optional>
@@ -38,7 +39,10 @@ LockCacheItem::LockCacheItem(const std::string &Path)
3839
close(fd);
3940
Owned = true;
4041
} else {
41-
PersistentDeviceCodeCache::trace("Failed to aquire lock file: " + FileName);
42+
PersistentDeviceCodeCache::trace("Failed to acquire lock file: " +
43+
FileName + " " + std::strerror(errno));
44+
PersistentDeviceCodeCache::trace("Failed to acquire lock file: " +
45+
FileName + " " + std::strerror(errno));
4246
}
4347
}
4448

@@ -100,12 +104,6 @@ void PersistentDeviceCodeCache::putItemToDisc(
100104

101105
auto Plugin = detail::getSyclObjImpl(Device)->getPlugin();
102106

103-
size_t i = 0;
104-
std::string FileName;
105-
do {
106-
FileName = DirName + "/" + std::to_string(i++);
107-
} while (OSUtil::isPathPresent(FileName + ".bin"));
108-
109107
unsigned int DeviceNum = 0;
110108

111109
Plugin->call<PiApiKind::piProgramGetInfo>(
@@ -127,6 +125,12 @@ void PersistentDeviceCodeCache::putItemToDisc(
127125
Plugin->call<PiApiKind::piProgramGetInfo>(NativePrg, PI_PROGRAM_INFO_BINARIES,
128126
sizeof(char *) * Pointers.size(),
129127
Pointers.data(), nullptr);
128+
size_t i = 0;
129+
std::string FileName;
130+
do {
131+
FileName = DirName + "/" + std::to_string(i++);
132+
} while (OSUtil::isPathPresent(FileName + ".bin") ||
133+
OSUtil::isPathPresent(FileName + ".lock"));
130134

131135
try {
132136
OSUtil::makeDir(DirName.c_str());
@@ -137,9 +141,17 @@ void PersistentDeviceCodeCache::putItemToDisc(
137141
trace("device binary has been cached: " + FullFileName);
138142
writeSourceItem(FileName + ".src", Device, Img, SpecConsts,
139143
BuildOptionsString);
144+
} else {
145+
PersistentDeviceCodeCache::trace("cache lock not owned " + FileName);
140146
}
147+
} catch (std::exception &e) {
148+
PersistentDeviceCodeCache::trace(
149+
std::string("exception encountered making persistent cache: ") +
150+
e.what());
141151
} catch (...) {
142-
// If a problem happens on storing cache item, do nothing
152+
PersistentDeviceCodeCache::trace(
153+
std::string("error outputting persistent cache: ") +
154+
std::strerror(errno));
143155
}
144156
}
145157

0 commit comments

Comments
 (0)