Skip to content

Commit 1e6c088

Browse files
committed
Use nano seconds file timestamps for Linux
1 parent 983bc98 commit 1e6c088

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

sycl/source/detail/os_util.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ namespace fs = std::experimental::filesystem;
3939
#include <linux/limits.h> // for PATH_MAX
4040
#include <sys/stat.h>
4141
#include <sys/sysinfo.h>
42-
#include <utime.h>
4342

4443
#elif defined(__SYCL_RT_OS_WINDOWS)
4544

@@ -55,7 +54,6 @@ namespace fs = std::experimental::filesystem;
5554
#include <sys/stat.h>
5655
#include <sys/sysctl.h>
5756
#include <sys/types.h>
58-
#include <utime.h>
5957

6058
#endif // __SYCL_RT_OS
6159

@@ -330,7 +328,7 @@ getFilesWithLastModificationTime(const std::string &Path,
330328
#if defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN)
331329
struct stat StatBuf;
332330
if (stat(FileName.c_str(), &StatBuf) == 0)
333-
Files.push_back({StatBuf.st_mtime, FileName});
331+
Files.push_back({StatBuf.st_atim.tv_nsec, FileName});
334332
#elif defined(__SYCL_RT_OS_WINDOWS)
335333
// Use GetFileAttributeExA to get file modification time.
336334
WIN32_FILE_ATTRIBUTE_DATA FileAttr;
@@ -384,11 +382,8 @@ void updateFileModificationTime(const std::string &Path) {
384382
}
385383

386384
#elif defined(__SYCL_RT_OS_LINUX) || defined(__SYCL_RT_OS_DARWIN)
387-
// For Linux and Darwin, use utime to update file modification time.
388-
struct utimbuf UtimeBuf;
389-
UtimeBuf.actime = UtimeBuf.actime;
390-
UtimeBuf.modtime = time(nullptr);
391-
utime(Path.c_str(), &UtimeBuf);
385+
// For Linux and Darwin, use utimensat to update file modification time.
386+
utimensat(0, Path.c_str(), nullptr, 0);
392387
#endif // __SYCL_RT_OS
393388
}
394389

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -282,32 +282,37 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
282282
const std::string SrcFile = FileNameWOExt + ".src";
283283

284284
while (OSUtil::isPathPresent(BinFile) || OSUtil::isPathPresent(SrcFile)) {
285-
// Remove the file and subtract its size from the cache size.
286-
auto RemoveFileAndSubtractSize =
287-
[&CurrCacheSize](const std::string &FileName) {
288-
// If the file is not present, return.
289-
if (!OSUtil::isPathPresent(FileName))
290-
return;
291-
292-
auto FileSize = getFileSize(FileName);
293-
if (std::remove(FileName.c_str())) {
294-
throw sycl::exception(make_error_code(errc::runtime),
295-
"Failed to evict cache entry: " + FileName);
296-
} else {
297-
PersistentDeviceCodeCache::trace("File removed: " + FileName);
298-
CurrCacheSize -= FileSize;
299-
}
300-
};
301-
302-
// If removal fails due to a race, retry.
303-
// Races are rare, but can happen if another process is reading the file.
304-
// Locking down the entire cache and blocking all readers would be
305-
// inefficient.
306-
try {
307-
RemoveFileAndSubtractSize(SrcFile);
308-
RemoveFileAndSubtractSize(BinFile);
309-
} catch (...) {
310-
continue;
285+
286+
// Lock to prevent race between writer and eviction thread.
287+
LockCacheItem Lock{FileNameWOExt};
288+
if (Lock.isOwned()) {
289+
// Remove the file and subtract its size from the cache size.
290+
auto RemoveFileAndSubtractSize = [&CurrCacheSize](
291+
const std::string &FileName) {
292+
// If the file is not present, return.
293+
if (!OSUtil::isPathPresent(FileName))
294+
return;
295+
296+
auto FileSize = getFileSize(FileName);
297+
if (std::remove(FileName.c_str())) {
298+
throw sycl::exception(make_error_code(errc::runtime),
299+
"Failed to evict cache entry: " + FileName);
300+
} else {
301+
PersistentDeviceCodeCache::trace("File removed: " + FileName);
302+
CurrCacheSize -= FileSize;
303+
}
304+
};
305+
306+
// If removal fails due to a race, retry.
307+
// Races are rare, but can happen if another process is reading the
308+
// file. Locking down the entire cache and blocking all readers would be
309+
// inefficient.
310+
try {
311+
RemoveFileAndSubtractSize(SrcFile);
312+
RemoveFileAndSubtractSize(BinFile);
313+
} catch (...) {
314+
continue;
315+
}
311316
}
312317
}
313318

@@ -426,7 +431,6 @@ void PersistentDeviceCodeCache::putItemToDisc(
426431
return;
427432

428433
std::string FileName;
429-
bool IsWriteSuccess = false;
430434
try {
431435
OSUtil::makeDir(DirName.c_str());
432436
FileName = getUniqueFilename(DirName);
@@ -437,7 +441,10 @@ void PersistentDeviceCodeCache::putItemToDisc(
437441
trace("device binary has been cached: " + FullFileName);
438442
writeSourceItem(FileName + ".src", Devices[DeviceIndex], SortedImgs,
439443
SpecConsts, BuildOptionsString);
440-
IsWriteSuccess = true;
444+
445+
// Update Total cache size after adding the new items.
446+
TotalSize += getFileSize(FileName + ".src");
447+
TotalSize += getFileSize(FileName + ".bin");
441448
} else {
442449
PersistentDeviceCodeCache::trace("cache lock not owned " + FileName);
443450
}
@@ -450,11 +457,6 @@ void PersistentDeviceCodeCache::putItemToDisc(
450457
std::string("error outputting persistent cache: ") +
451458
std::strerror(errno));
452459
}
453-
454-
if (IsWriteSuccess) {
455-
TotalSize += getFileSize(FileName + ".src");
456-
TotalSize += getFileSize(FileName + ".bin");
457-
}
458460
}
459461

460462
// Update the cache size file and trigger cache eviction if needed.

sycl/unittests/kernel-and-program/PersistentDeviceCodeCache.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,8 @@ TEST_P(PersistentDeviceCodeCache, ConcurentReadWriteCacheFileSize) {
623623
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(CacheRoot));
624624
ASSERT_NO_ERROR(llvm::sys::fs::create_directories(CacheRoot));
625625

626-
// Insanely large value (1GB) to not trigger eviction. This test just checks
627-
// for deadlocks/crashes when updating the size file concurrently.
626+
// Insanely large value (1GB) to not trigger eviction. This test just
627+
// checks for deadlocks/crashes when updating the size file concurrently.
628628
SetDiskCacheEvictionEnv("1000000000");
629629
ConcurentReadWriteCache(1, 100);
630630
}

0 commit comments

Comments
 (0)