@@ -49,13 +49,11 @@ LockCacheItem::LockCacheItem(const std::string &Path)
49
49
50
50
LockCacheItem::~LockCacheItem () {
51
51
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);
54
53
}
55
54
56
55
// 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) {
59
57
return Format == SYCL_DEVICE_BINARY_TYPE_SPIRV ||
60
58
Format == SYCL_DEVICE_BINARY_TYPE_NATIVE;
61
59
}
@@ -210,6 +208,16 @@ void PersistentDeviceCodeCache::repopulateCacheSizeFile(
210
208
const std::string CacheSizeFileName = " cache_size.txt" ;
211
209
const std::string CacheSizeFile = CacheRoot + " /" + CacheSizeFileName;
212
210
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
+
213
221
// If the cache size file is not present, calculate the size of the cache size
214
222
// directory and write it to the file.
215
223
if (!OSUtil::isPathPresent (CacheSizeFile)) {
@@ -316,6 +324,8 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
316
324
auto RemoveFileAndSubtractSize = [&CurrCacheSize](
317
325
const std::string &FileName) {
318
326
// If the file is not present, return.
327
+ // Src file is not present inj kernel_compiler cache, we will
328
+ // skip removing it.
319
329
if (!OSUtil::isPathPresent (FileName))
320
330
return ;
321
331
@@ -324,7 +334,7 @@ void PersistentDeviceCodeCache::evictItemsFromCache(
324
334
throw sycl::exception (make_error_code (errc::runtime),
325
335
" Failed to evict cache entry: " + FileName);
326
336
} else {
327
- PersistentDeviceCodeCache::trace (" File removed: " + FileName);
337
+ PersistentDeviceCodeCache::trace (" File removed: " , FileName);
328
338
CurrCacheSize -= FileSize;
329
339
}
330
340
};
@@ -464,7 +474,7 @@ void PersistentDeviceCodeCache::putItemToDisc(
464
474
if (Lock.isOwned ()) {
465
475
std::string FullFileName = FileName + " .bin" ;
466
476
writeBinaryDataToFile (FullFileName, BinaryData[DeviceIndex]);
467
- trace (" device binary has been cached: " + FullFileName);
477
+ trace (" device binary has been cached: " , FullFileName);
468
478
writeSourceItem (FileName + " .src" , Devices[DeviceIndex], SortedImgs,
469
479
SpecConsts, BuildOptionsString);
470
480
@@ -474,7 +484,7 @@ void PersistentDeviceCodeCache::putItemToDisc(
474
484
475
485
saveCurrentTimeInAFile (FileName + CacheEntryAccessTimeSuffix);
476
486
} else {
477
- PersistentDeviceCodeCache::trace (" cache lock not owned " + FileName);
487
+ PersistentDeviceCodeCache::trace (" cache lock not owned " , FileName);
478
488
}
479
489
} catch (std::exception &e) {
480
490
PersistentDeviceCodeCache::trace (
@@ -495,7 +505,20 @@ void PersistentDeviceCodeCache::putItemToDisc(
495
505
void PersistentDeviceCodeCache::putCompiledKernelToDisc (
496
506
const std::vector<device> &Devices, const std::string &BuildOptionsString,
497
507
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
+
498
519
auto BinaryData = getProgramBinaryData (NativePrg, Devices);
520
+ // Total size of the item that we are writing to the cache.
521
+ size_t TotalSize = 0 ;
499
522
500
523
for (size_t DeviceIndex = 0 ; DeviceIndex < Devices.size (); DeviceIndex++) {
501
524
// If we don't have binary for the device, skip it.
@@ -512,10 +535,13 @@ void PersistentDeviceCodeCache::putCompiledKernelToDisc(
512
535
std::string FullFileName = FileName + " .bin" ;
513
536
writeBinaryDataToFile (FullFileName, BinaryData[DeviceIndex]);
514
537
PersistentDeviceCodeCache::trace_KernelCompiler (
515
- " binary has been cached: " + FullFileName);
538
+ " binary has been cached: " , FullFileName);
539
+
540
+ TotalSize += getFileSize (FullFileName);
541
+ saveCurrentTimeInAFile (FileName + CacheEntryAccessTimeSuffix);
516
542
} else {
517
- PersistentDeviceCodeCache::trace_KernelCompiler (
518
- " cache lock not owned " + FileName);
543
+ PersistentDeviceCodeCache::trace_KernelCompiler (" cache lock not owned " ,
544
+ FileName);
519
545
}
520
546
} catch (std::exception &e) {
521
547
PersistentDeviceCodeCache::trace_KernelCompiler (
@@ -525,6 +551,10 @@ void PersistentDeviceCodeCache::putCompiledKernelToDisc(
525
551
std::string (" error outputting cache: " ) + std::strerror (errno));
526
552
}
527
553
}
554
+
555
+ // Update the cache size file and trigger cache eviction if needed.
556
+ if (TotalSize)
557
+ updateCacheFileSizeAndTriggerEviction (getRootDir (), TotalSize);
528
558
}
529
559
530
560
/* Program binaries built for one or more devices are read from persistent
@@ -581,7 +611,7 @@ std::vector<std::vector<char>> PersistentDeviceCodeCache::getItemFromDisc(
581
611
if (Binaries[DeviceIndex].empty ())
582
612
return {};
583
613
}
584
- PersistentDeviceCodeCache::trace (" using cached device binary: " + FileNames);
614
+ PersistentDeviceCodeCache::trace (" using cached device binary: " , FileNames);
585
615
return Binaries;
586
616
}
587
617
@@ -611,6 +641,12 @@ PersistentDeviceCodeCache::getCompiledKernelFromDisc(
611
641
try {
612
642
std::string FullFileName = FileName + " .bin" ;
613
643
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
+
614
650
FileNames += FullFileName + " ;" ;
615
651
break ;
616
652
} catch (...) {
@@ -623,7 +659,7 @@ PersistentDeviceCodeCache::getCompiledKernelFromDisc(
623
659
if (Binaries[DeviceIndex].empty ())
624
660
return {};
625
661
}
626
- PersistentDeviceCodeCache::trace_KernelCompiler (" using cached binary: " +
662
+ PersistentDeviceCodeCache::trace_KernelCompiler (" using cached binary: " ,
627
663
FileNames);
628
664
return Binaries;
629
665
}
@@ -654,7 +690,7 @@ void PersistentDeviceCodeCache::writeBinaryDataToFile(
654
690
FileStream.write ((char *)&Size , sizeof (Size ));
655
691
FileStream.write (Data.data (), Size );
656
692
if (FileStream.fail ())
657
- trace (" Failed to write to binary file " + FileName);
693
+ trace (" Failed to write to binary file " , FileName);
658
694
}
659
695
660
696
/* Read built binary from persistent cache. Each persistent cache file contains
@@ -671,7 +707,7 @@ PersistentDeviceCodeCache::readBinaryDataFromFile(const std::string &FileName) {
671
707
size_t NumBinaries = 0 ;
672
708
FileStream.read ((char *)&NumBinaries, sizeof (NumBinaries));
673
709
if (FileStream.fail ()) {
674
- trace (" Failed to read number of binaries from " + FileName);
710
+ trace (" Failed to read number of binaries from " , FileName);
675
711
return {};
676
712
}
677
713
// Even in the old implementation we could only put a single binary to the
@@ -686,7 +722,7 @@ PersistentDeviceCodeCache::readBinaryDataFromFile(const std::string &FileName) {
686
722
FileStream.close ();
687
723
688
724
if (FileStream.fail ()) {
689
- trace (" Failed to read binary file from " + FileName);
725
+ trace (" Failed to read binary file from " , FileName);
690
726
return {};
691
727
}
692
728
@@ -726,7 +762,7 @@ void PersistentDeviceCodeCache::writeSourceItem(
726
762
FileStream.close ();
727
763
728
764
if (FileStream.fail ()) {
729
- trace (" Failed to write source file to " + FileName);
765
+ trace (" Failed to write source file to " , FileName);
730
766
}
731
767
}
732
768
@@ -774,7 +810,7 @@ bool PersistentDeviceCodeCache::isCacheItemSrcEqual(
774
810
FileStream.close ();
775
811
776
812
if (FileStream.fail ()) {
777
- trace (" Failed to read source file from " + FileName);
813
+ trace (" Failed to read source file from " , FileName);
778
814
}
779
815
780
816
return true ;
0 commit comments