16
16
#include < gtest/gtest.h>
17
17
#include < helpers/PiMock.hpp>
18
18
#include < llvm/Support/FileSystem.h>
19
+ #include < optional>
19
20
#include < vector>
20
21
22
+ #define ASSERT_NO_ERROR (x ) \
23
+ if (std::error_code EC = x) { \
24
+ FAIL () << #x " : did not return errc::success.\n " \
25
+ << " error number: " << EC.value () << " \n " \
26
+ << " error message: " << EC.message () << " \n " ; \
27
+ }
28
+
21
29
// TODO: Introduce common unit tests header and move it there
22
30
static void set_env (const char *name, const char *value) {
23
31
#ifdef _WIN32
24
- (void )_putenv_s (name, value);
32
+ (void )_putenv_s (name, value ? value : " " );
25
33
#else
26
- (void )setenv (name, value, /* overwrite*/ 1 );
34
+ if (value)
35
+ (void )setenv (name, value, /* overwrite*/ 1 );
36
+ else
37
+ (void )unsetenv (name);
27
38
#endif
28
39
}
29
40
@@ -81,12 +92,47 @@ class PersistenDeviceCodeCache : public ::testing::Test {
81
92
return _putenv_s (name, value);
82
93
}
83
94
#endif
95
+
96
+ std::optional<std::string> SYCLCachePersistentBefore;
97
+ bool SYCLCachePersistentChanged = false ;
98
+
99
+ // Caches the initial value of the SYCL_CACHE_PERSISTENT environment variable
100
+ // before overwriting it with the new value.
101
+ // Tear-down will reset the environment variable.
102
+ void SetSYCLCachePersistentEnv (const char *NewValue) {
103
+ char *SYCLCachePersistent = getenv (" SYCL_CACHE_PERSISTENT" );
104
+ // We can skip if the new value is the same as the old one.
105
+ if ((!NewValue && !SYCLCachePersistent) ||
106
+ (NewValue && SYCLCachePersistent &&
107
+ !strcmp (NewValue, SYCLCachePersistent)))
108
+ return ;
109
+
110
+ // Cache the old value of SYCL_CACHE_PERSISTENT if it is not already saved.
111
+ if (!SYCLCachePersistentChanged && SYCLCachePersistent)
112
+ SYCLCachePersistentBefore = std::string{SYCLCachePersistent};
113
+
114
+ // Set the environment variable and signal the configuration file and the
115
+ // persistent cache.
116
+ set_env (" SYCL_CACHE_PERSISTENT" , NewValue);
117
+ sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
118
+ detail::PersistentDeviceCodeCache::reparseConfig ();
119
+ SYCLCachePersistentChanged = true ;
120
+ }
121
+
84
122
virtual void SetUp () {
85
123
EXPECT_NE (getenv (" SYCL_CACHE_DIR" ), nullptr )
86
124
<< " Please set SYCL_CACHE_DIR environment variable pointing to cache "
87
125
" location." ;
88
126
}
89
127
128
+ virtual void TearDown () {
129
+ // If we changed the cache, set it back to the old value.
130
+ if (SYCLCachePersistentChanged)
131
+ SetSYCLCachePersistentEnv (SYCLCachePersistentBefore
132
+ ? SYCLCachePersistentBefore->c_str ()
133
+ : nullptr );
134
+ }
135
+
90
136
PersistenDeviceCodeCache () : Plt{default_selector ()} {
91
137
92
138
if (Plt.is_host () || Plt.get_backend () != backend::opencl) {
@@ -112,16 +158,15 @@ class PersistenDeviceCodeCache : public ::testing::Test {
112
158
return ;
113
159
}
114
160
115
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
116
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
161
+ SetSYCLCachePersistentEnv (" 1" );
117
162
118
163
std::string BuildOptions{" --concurrent-access=" +
119
164
std::to_string (ThreadCount)};
120
165
DeviceCodeID = ProgramID;
121
166
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
122
167
Dev, Img, {' S' , ' p' , ' e' , ' c' , ' C' , ' o' , ' n' , ' s' , ' t' , ProgramID},
123
168
BuildOptions);
124
- llvm::sys::fs::remove_directories (ItemDir);
169
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
125
170
126
171
Barrier b (ThreadCount);
127
172
{
@@ -147,7 +192,7 @@ class PersistenDeviceCodeCache : public ::testing::Test {
147
192
148
193
ThreadPool MPool (ThreadCount, testLambda);
149
194
}
150
- llvm::sys::fs::remove_directories (ItemDir);
195
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
151
196
}
152
197
153
198
protected:
@@ -181,14 +226,13 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
181
226
return ;
182
227
}
183
228
184
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
185
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
229
+ SetSYCLCachePersistentEnv (" 1" );
186
230
187
231
std::string Key{' 1' , ' \0 ' , ' 3' , ' 4' , ' \0 ' };
188
232
std::vector<unsigned char > SpecConst (Key.begin (), Key.end ());
189
233
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
190
234
Dev, Img, SpecConst, Key);
191
- llvm::sys::fs::remove_directories (ItemDir);
235
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
192
236
193
237
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, SpecConst, Key,
194
238
NativeProg);
@@ -204,7 +248,7 @@ TEST_F(PersistenDeviceCodeCache, KeysWithNullTermSymbol) {
204
248
}
205
249
}
206
250
207
- llvm::sys::fs::remove_directories (ItemDir);
251
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
208
252
}
209
253
210
254
/* Do read/write for the same cache item to/from 300 threads for small device
@@ -240,13 +284,12 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
240
284
return ;
241
285
}
242
286
243
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
244
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
287
+ SetSYCLCachePersistentEnv (" 1" );
245
288
246
289
std::string BuildOptions{" --corrupted-file" };
247
290
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
248
291
Dev, Img, {}, BuildOptions);
249
- llvm::sys::fs::remove_directories (ItemDir);
292
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
250
293
251
294
// Only source file is present
252
295
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -257,7 +300,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
257
300
BuildOptions);
258
301
EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
259
302
<< " Item with missed binary file was read" ;
260
- llvm::sys::fs::remove_directories (ItemDir);
303
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
261
304
262
305
// Only binary file is present
263
306
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -268,7 +311,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
268
311
BuildOptions);
269
312
EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
270
313
<< " Item with missed source file was read" ;
271
- llvm::sys::fs::remove_directories (ItemDir);
314
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
272
315
273
316
// Binary file is corrupted
274
317
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -286,7 +329,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
286
329
EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
287
330
<< " Item with corrupted binary file was read" ;
288
331
289
- llvm::sys::fs::remove_directories (ItemDir);
332
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
290
333
291
334
// Source file is empty
292
335
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -299,7 +342,7 @@ TEST_F(PersistenDeviceCodeCache, CorruptedCacheFiles) {
299
342
BuildOptions);
300
343
EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
301
344
<< " Item with corrupted binary file was read" ;
302
- llvm::sys::fs::remove_directories (ItemDir);
345
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
303
346
}
304
347
305
348
/* Checks that lock file affects cache operations as expected:
@@ -311,13 +354,12 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
311
354
return ;
312
355
}
313
356
314
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
315
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
357
+ SetSYCLCachePersistentEnv (" 1" );
316
358
317
359
std::string BuildOptions{" --obsolete-lock" };
318
360
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
319
361
Dev, Img, {}, BuildOptions);
320
- llvm::sys::fs::remove_directories (ItemDir);
362
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
321
363
322
364
// Create 1st cahe item
323
365
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
@@ -356,7 +398,7 @@ TEST_F(PersistenDeviceCodeCache, LockFile) {
356
398
<< " Corrupted image loaded from persistent cache" ;
357
399
}
358
400
}
359
- llvm::sys::fs::remove_directories (ItemDir);
401
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
360
402
}
361
403
362
404
#ifndef _WIN32
@@ -368,32 +410,35 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
368
410
return ;
369
411
}
370
412
371
- set_env (" SYCL_CACHE_PERSISTENT" , " 1" );
372
- sycl::detail::SYCLConfig<sycl::detail::SYCL_CACHE_PERSISTENT>::reset ();
413
+ SetSYCLCachePersistentEnv (" 1" );
373
414
374
415
std::string BuildOptions{" --build-options" };
375
416
std::string ItemDir = detail::PersistentDeviceCodeCache::getCacheItemPath (
376
417
Dev, Img, {}, BuildOptions);
377
- llvm::sys::fs::remove_directories (ItemDir);
418
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
378
419
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
379
420
NativeProg);
380
421
EXPECT_TRUE (llvm::sys::fs::exists (ItemDir + " /0.bin" )) << " No file created" ;
381
- llvm::sys::fs::setPermissions (ItemDir + " /0.bin" , llvm::sys::fs::no_perms);
422
+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /0.bin" ,
423
+ llvm::sys::fs::no_perms));
382
424
// No access to binary file new cache item to be created
383
425
detail::PersistentDeviceCodeCache::putItemToDisc (Dev, Img, {}, BuildOptions,
384
426
NativeProg);
385
427
EXPECT_TRUE (llvm::sys::fs::exists (ItemDir + " /1.bin" )) << " No file created" ;
386
428
387
- llvm::sys::fs::setPermissions (ItemDir + " /1.bin" , llvm::sys::fs::no_perms);
429
+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /1.bin" ,
430
+ llvm::sys::fs::no_perms));
388
431
auto Res = detail::PersistentDeviceCodeCache::getItemFromDisc (Dev, Img, {},
389
432
BuildOptions);
390
433
391
434
// No image to be read due to lack of permissions from source file
392
435
EXPECT_EQ (Res.size (), static_cast <size_t >(0 ))
393
436
<< " Read from the file without permissions." ;
394
437
395
- llvm::sys::fs::setPermissions (ItemDir + " /0.bin" , llvm::sys::fs::all_perms);
396
- llvm::sys::fs::setPermissions (ItemDir + " /1.bin" , llvm::sys::fs::all_perms);
438
+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /0.bin" ,
439
+ llvm::sys::fs::all_perms));
440
+ ASSERT_NO_ERROR (llvm::sys::fs::setPermissions (ItemDir + " /1.bin" ,
441
+ llvm::sys::fs::all_perms));
397
442
398
443
Res = detail::PersistentDeviceCodeCache::getItemFromDisc (Dev, Img, {},
399
444
BuildOptions);
@@ -404,7 +449,7 @@ TEST_F(PersistenDeviceCodeCache, AccessDeniedForCacheDir) {
404
449
<< " Corrupted image loaded from persistent cache" ;
405
450
}
406
451
}
407
- llvm::sys::fs::remove_directories (ItemDir);
452
+ ASSERT_NO_ERROR ( llvm::sys::fs::remove_directories (ItemDir) );
408
453
}
409
454
#endif // _WIN32
410
455
} // namespace
0 commit comments