@@ -49,13 +49,11 @@ LockCacheItem::LockCacheItem(const std::string &Path)
4949
5050LockCacheItem::~LockCacheItem () {
5151 if (Owned && std::remove (FileName.c_str ()))
52- PersistentDeviceCodeCache::trace (" Failed to release lock file: " +
53- FileName);
52+ PersistentDeviceCodeCache::trace (" Failed to release lock file: " , FileName);
5453}
5554
5655// Returns true if the specified format is either SPIRV or a native binary.
57- static bool
58- IsSupportedImageFormat (ur::DeviceBinaryType Format) {
56+ static bool IsSupportedImageFormat (ur::DeviceBinaryType Format) {
5957 return Format == SYCL_DEVICE_BINARY_TYPE_SPIRV ||
6058 Format == SYCL_DEVICE_BINARY_TYPE_NATIVE;
6159}
@@ -210,6 +208,16 @@ void PersistentDeviceCodeCache::repopulateCacheSizeFile(
210208 const std::string CacheSizeFileName = " cache_size.txt" ;
211209 const std::string CacheSizeFile = CacheRoot + " /" + CacheSizeFileName;
212210
211+ // Create cache root, if it does not exist.
212+ try {
213+ if (!OSUtil::isPathPresent (CacheRoot))
214+ OSUtil::makeDir (CacheRoot.c_str ());
215+ } catch (...) {
216+ throw sycl::exception (make_error_code (errc::runtime),
217+ " Failed to create cache root directory: " +
218+ CacheRoot);
219+ }
220+
213221 // If the cache size file is not present, calculate the size of the cache size
214222 // directory and write it to the file.
215223 if (!OSUtil::isPathPresent (CacheSizeFile)) {
@@ -316,6 +324,8 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
316324 auto RemoveFileAndSubtractSize = [&CurrCacheSize](
317325 const std::string &FileName) {
318326 // If the file is not present, return.
327+ // Src file is not present inj kernel_compiler cache, we will
328+ // skip removing it.
319329 if (!OSUtil::isPathPresent (FileName))
320330 return ;
321331
@@ -324,7 +334,7 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
324334 throw sycl::exception (make_error_code (errc::runtime),
325335 " Failed to evict cache entry: " + FileName);
326336 } else {
327- PersistentDeviceCodeCache::trace (" File removed: " + FileName);
337+ PersistentDeviceCodeCache::trace (" File removed: " , FileName);
328338 CurrCacheSize -= FileSize;
329339 }
330340 };
@@ -464,7 +474,7 @@ void PersistentDeviceCodeCache::putItemToDisc(
464474 if (Lock.isOwned ()) {
465475 std::string FullFileName = FileName + " .bin" ;
466476 writeBinaryDataToFile (FullFileName, BinaryData[DeviceIndex]);
467- trace (" device binary has been cached: " + FullFileName);
477+ trace (" device binary has been cached: " , FullFileName);
468478 writeSourceItem (FileName + " .src" , Devices[DeviceIndex], SortedImgs,
469479 SpecConsts, BuildOptionsString);
470480
@@ -474,7 +484,7 @@ void PersistentDeviceCodeCache::putItemToDisc(
474484
475485 saveCurrentTimeInAFile (FileName + CacheEntryAccessTimeSuffix);
476486 } else {
477- PersistentDeviceCodeCache::trace (" cache lock not owned " + FileName);
487+ PersistentDeviceCodeCache::trace (" cache lock not owned " , FileName);
478488 }
479489 } catch (std::exception &e) {
480490 PersistentDeviceCodeCache::trace (
@@ -495,7 +505,20 @@ void PersistentDeviceCodeCache::putItemToDisc(
495505void PersistentDeviceCodeCache::putCompiledKernelToDisc (
496506 const std::vector<device> &Devices, const std::string &BuildOptionsString,
497507 const std::string &SourceStr, const ur_program_handle_t &NativePrg) {
508+
509+ repopulateCacheSizeFile (getRootDir ());
510+
511+ // Do not insert any new item if eviction is in progress.
512+ // Since evictions are rare, we can afford to spin lock here.
513+ const std::string EvictionInProgressFile =
514+ getRootDir () + EvictionInProgressFileSuffix;
515+ // Stall until the other process finishes eviction.
516+ while (OSUtil::isPathPresent (EvictionInProgressFile))
517+ continue ;
518+
498519 auto BinaryData = getProgramBinaryData (NativePrg, Devices);
520+ // Total size of the item that we are writing to the cache.
521+ size_t TotalSize = 0 ;
499522
500523 for (size_t DeviceIndex = 0 ; DeviceIndex < Devices.size (); DeviceIndex++) {
501524 // If we don't have binary for the device, skip it.
@@ -512,10 +535,13 @@ void PersistentDeviceCodeCache::putCompiledKernelToDisc(
512535 std::string FullFileName = FileName + " .bin" ;
513536 writeBinaryDataToFile (FullFileName, BinaryData[DeviceIndex]);
514537 PersistentDeviceCodeCache::trace_KernelCompiler (
515- " binary has been cached: " + FullFileName);
538+ " binary has been cached: " , FullFileName);
539+
540+ TotalSize += getFileSize (FullFileName);
541+ saveCurrentTimeInAFile (FileName + CacheEntryAccessTimeSuffix);
516542 } else {
517- PersistentDeviceCodeCache::trace_KernelCompiler (
518- " cache lock not owned " + FileName);
543+ PersistentDeviceCodeCache::trace_KernelCompiler (" cache lock not owned " ,
544+ FileName);
519545 }
520546 } catch (std::exception &e) {
521547 PersistentDeviceCodeCache::trace_KernelCompiler (
@@ -525,6 +551,10 @@ void PersistentDeviceCodeCache::putCompiledKernelToDisc(
525551 std::string (" error outputting cache: " ) + std::strerror (errno));
526552 }
527553 }
554+
555+ // Update the cache size file and trigger cache eviction if needed.
556+ if (TotalSize)
557+ updateCacheFileSizeAndTriggerEviction (getRootDir (), TotalSize);
528558}
529559
530560/* Program binaries built for one or more devices are read from persistent
@@ -581,7 +611,7 @@ std::vector<std::vector<char>> PersistentDeviceCodeCache::getItemFromDisc(
581611 if (Binaries[DeviceIndex].empty ())
582612 return {};
583613 }
584- PersistentDeviceCodeCache::trace (" using cached device binary: " + FileNames);
614+ PersistentDeviceCodeCache::trace (" using cached device binary: " , FileNames);
585615 return Binaries;
586616}
587617
@@ -611,6 +641,12 @@ PersistentDeviceCodeCache::getCompiledKernelFromDisc(
611641 try {
612642 std::string FullFileName = FileName + " .bin" ;
613643 Binaries[DeviceIndex] = readBinaryDataFromFile (FullFileName);
644+
645+ // Explicitly update the access time of the file. This is required for
646+ // eviction.
647+ if (isEvictionEnabled ())
648+ saveCurrentTimeInAFile (FileName + CacheEntryAccessTimeSuffix);
649+
614650 FileNames += FullFileName + " ;" ;
615651 break ;
616652 } catch (...) {
@@ -623,7 +659,7 @@ PersistentDeviceCodeCache::getCompiledKernelFromDisc(
623659 if (Binaries[DeviceIndex].empty ())
624660 return {};
625661 }
626- PersistentDeviceCodeCache::trace_KernelCompiler (" using cached binary: " +
662+ PersistentDeviceCodeCache::trace_KernelCompiler (" using cached binary: " ,
627663 FileNames);
628664 return Binaries;
629665}
@@ -654,7 +690,7 @@ void PersistentDeviceCodeCache::writeBinaryDataToFile(
654690 FileStream.write ((char *)&Size, sizeof (Size));
655691 FileStream.write (Data.data (), Size);
656692 if (FileStream.fail ())
657- trace (" Failed to write to binary file " + FileName);
693+ trace (" Failed to write to binary file " , FileName);
658694}
659695
660696/* Read built binary from persistent cache. Each persistent cache file contains
@@ -671,7 +707,7 @@ PersistentDeviceCodeCache::readBinaryDataFromFile(const std::string &FileName) {
671707 size_t NumBinaries = 0 ;
672708 FileStream.read ((char *)&NumBinaries, sizeof (NumBinaries));
673709 if (FileStream.fail ()) {
674- trace (" Failed to read number of binaries from " + FileName);
710+ trace (" Failed to read number of binaries from " , FileName);
675711 return {};
676712 }
677713 // Even in the old implementation we could only put a single binary to the
@@ -686,7 +722,7 @@ PersistentDeviceCodeCache::readBinaryDataFromFile(const std::string &FileName) {
686722 FileStream.close ();
687723
688724 if (FileStream.fail ()) {
689- trace (" Failed to read binary file from " + FileName);
725+ trace (" Failed to read binary file from " , FileName);
690726 return {};
691727 }
692728
@@ -726,7 +762,7 @@ void PersistentDeviceCodeCache::writeSourceItem(
726762 FileStream.close ();
727763
728764 if (FileStream.fail ()) {
729- trace (" Failed to write source file to " + FileName);
765+ trace (" Failed to write source file to " , FileName);
730766 }
731767}
732768
@@ -774,7 +810,7 @@ bool PersistentDeviceCodeCache::isCacheItemSrcEqual(
774810 FileStream.close ();
775811
776812 if (FileStream.fail ()) {
777- trace (" Failed to read source file from " + FileName);
813+ trace (" Failed to read source file from " , FileName);
778814 }
779815
780816 return true ;
0 commit comments