Skip to content

Commit 0ca7bef

Browse files
committed
Instrument cache operations
1 parent 1e6c088 commit 0ca7bef

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

sycl/source/detail/persistent_device_code_cache.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <fstream>
1717
#include <optional>
1818

19+
#include <time.h>
20+
1921
#if defined(__SYCL_RT_OS_POSIX_SUPPORT)
2022
#include <unistd.h>
2123
#else
@@ -402,6 +404,10 @@ void PersistentDeviceCodeCache::putItemToDisc(
402404
const SerializedObj &SpecConsts, const std::string &BuildOptionsString,
403405
const ur_program_handle_t &NativePrg) {
404406

407+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
408+
InstrumentCache Instrument{"putItemToDisc: "};
409+
#endif
410+
405411
if (!areImagesCacheable(Imgs))
406412
return;
407413

@@ -460,8 +466,12 @@ void PersistentDeviceCodeCache::putItemToDisc(
460466
}
461467

462468
// Update the cache size file and trigger cache eviction if needed.
463-
if (TotalSize)
469+
if (TotalSize) {
470+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
471+
InstrumentCache Instrument{"Eviction: "};
472+
#endif
464473
updateCacheFileSizeAndTriggerEviction(getRootDir(), TotalSize);
474+
}
465475
}
466476

467477
void PersistentDeviceCodeCache::putCompiledKernelToDisc(
@@ -513,6 +523,10 @@ std::vector<std::vector<char>> PersistentDeviceCodeCache::getItemFromDisc(
513523
if (!areImagesCacheable(Imgs))
514524
return {};
515525

526+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
527+
InstrumentCache Instrument{"getItemFromDisc: "};
528+
#endif
529+
516530
std::vector<const RTDeviceBinaryImage *> SortedImgs = getSortedImages(Imgs);
517531
std::vector<std::vector<char>> Binaries(Devices.size());
518532
std::string FileNames;
@@ -538,8 +552,12 @@ std::vector<std::vector<char>> PersistentDeviceCodeCache::getItemFromDisc(
538552

539553
// Explicitly update the access time of the file. This is required for
540554
// eviction.
541-
if (isEvictionEnabled())
555+
if (isEvictionEnabled()) {
556+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
557+
InstrumentCache Instrument{"Updating file access time: "};
558+
#endif
542559
updateFileModificationTime(FileName + ".bin");
560+
}
543561

544562
FileNames += FullFileName + ";";
545563
break;

sycl/source/detail/persistent_device_code_cache.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010

11+
#include <chrono>
1112
#include <detail/config.hpp>
1213
#include <detail/device_binary_image.hpp>
1314
#include <fcntl.h>
@@ -20,6 +21,12 @@
2021
#include <thread>
2122
#include <vector>
2223

24+
#define __SYCL_INSTRUMENT_PERSISTENT_CACHE
25+
26+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
27+
#include <chrono>
28+
#endif
29+
2330
namespace sycl {
2431
inline namespace _V1 {
2532
namespace detail {
@@ -90,6 +97,29 @@ class PersistentDeviceCodeCache {
9097
* - on cache read operation it is treated as cache miss.
9198
*/
9299
private:
100+
#ifdef __SYCL_INSTRUMENT_PERSISTENT_CACHE
101+
// Class to instrument cache operations.
102+
class InstrumentCache {
103+
std::string PrintMsg;
104+
std::chrono::high_resolution_clock::time_point StartTime;
105+
106+
public:
107+
InstrumentCache(const std::string &Name) : PrintMsg(Name) {
108+
// Store start time.
109+
StartTime = std::chrono::high_resolution_clock::now();
110+
}
111+
~InstrumentCache() {
112+
// Calculate time spent and print message.
113+
auto EndTime = std::chrono::high_resolution_clock::now();
114+
auto Duration = std::chrono::duration_cast<std::chrono::nanoseconds>(
115+
EndTime - StartTime)
116+
.count();
117+
PersistentDeviceCodeCache::trace(PrintMsg + std::to_string(Duration) +
118+
"ns");
119+
}
120+
};
121+
#endif
122+
93123
/* Write built binary to persistent cache
94124
* Format: BinarySize, Binary
95125
*/

0 commit comments

Comments
 (0)