Skip to content

Commit 2375a18

Browse files
committed
Evict half the cache when triggered
1 parent 202c71c commit 2375a18

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
248248
const std::string &CacheRoot, size_t CacheSize, size_t MaxCacheSize) {
249249
PersistentDeviceCodeCache::trace("Cache eviction triggered.");
250250

251+
// EVict half of the cache.
252+
constexpr float HowMuchCacheToEvict = 0.5;
253+
251254
// Create a file eviction_in_progress.lock to indicate that eviction is in
252255
// progress. This file is used to prevent two processes from evicting the
253256
// cache at the same time.
@@ -340,7 +343,7 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
340343
}
341344

342345
// If the cache size is less than the threshold, break.
343-
if (CurrCacheSize <= MaxCacheSize)
346+
if (CurrCacheSize <= (size_t)(HowMuchCacheToEvict * MaxCacheSize))
344347
break;
345348
}
346349

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ TEST_P(PersistentDeviceCodeCache, BasicEviction) {
562562
detail::PersistentDeviceCodeCache::putItemToDisc({Dev}, {&Img}, {},
563563
BuildOptions, NativeProg);
564564

565+
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
566+
Dev, {&Img}, {}, BuildOptions);
567+
size_t SizeOfOneEntry = (size_t)(detail::getDirectorySize(ItemDir));
568+
565569
detail::PersistentDeviceCodeCache::putItemToDisc({Dev}, {&Img}, {},
566570
BuildOptions, NativeProg);
567571

@@ -573,35 +577,33 @@ TEST_P(PersistentDeviceCodeCache, BasicEviction) {
573577
{Dev}, {&Img}, {}, BuildOptions);
574578

575579
// Get the number of binary files in the cached item folder.
576-
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath(
577-
Dev, {&Img}, {}, BuildOptions);
578580
auto BinFiles = getBinaryFileNames(ItemDir);
579-
580581
EXPECT_EQ(BinFiles.size(), static_cast<size_t>(3))
581582
<< "Missing binary files. Eviction should not have happened.";
582583

583-
// Get Cache size and size of each entry. Set eviction threshold so that
584-
// just one item is evicted.
585-
size_t SizeOfOneEntry =
586-
(size_t)(detail::getDirectorySize(CacheRoot, false)) + 10;
587-
588584
// Set SYCL_CACHE_MAX_SIZE.
589-
SetDiskCacheEvictionEnv(std::to_string(SizeOfOneEntry).c_str());
585+
SetDiskCacheEvictionEnv(std::to_string(3 * SizeOfOneEntry).c_str());
590586

591-
// Put 4th item to the cache. This should trigger eviction. Only the first
592-
// item should be evicted.
587+
// Put 4th item to the cache. This should trigger eviction. Three of the
588+
// items should be evicted as we evict till the size of cache is less than
589+
// the half of cache size.
593590
detail::PersistentDeviceCodeCache::putItemToDisc({Dev}, {&Img}, {},
594591
BuildOptions, NativeProg);
595592

596-
// We should have three binary files: 0.bin, 2.bin, 3.bin.
593+
// We should have two binary files: 0.bin, 3.bin.
597594
BinFiles = getBinaryFileNames(ItemDir);
598-
EXPECT_EQ(BinFiles.size(), static_cast<size_t>(3))
595+
EXPECT_EQ(BinFiles.size(), static_cast<size_t>(1))
599596
<< "Eviction failed. Wrong number of binary files in the cache.";
600597

601-
// Check that 1.bin was evicted.
602-
for (const auto &File : BinFiles)
598+
// Check that 1.bin, 2.bin, and 0.bin was evicted.
599+
for (const auto &File : BinFiles) {
603600
EXPECT_NE(File, "1.bin")
604601
<< "Eviction failed. 1.bin should have been evicted.";
602+
EXPECT_NE(File, "2.bin")
603+
<< "Eviction failed. 2.bin should have been evicted.";
604+
EXPECT_NE(File, "0.bin")
605+
<< "Eviction failed. 0.bin should have been evicted.";
606+
}
605607

606608
ASSERT_NO_ERROR(llvm::sys::fs::remove_directories(ItemDir));
607609
}

0 commit comments

Comments
 (0)