Skip to content

Commit fba14a0

Browse files
[CAS] Fix Windows build for CAS unittest
Fix the CAS unit-test build for windows which doesn't have the same setenv function and constructor attributes. Implement the default ondisk cas differently so it can be overwritten from environmental variable, or overwrite the value in process directly. Fix: #7883
1 parent 67c73ed commit fba14a0

File tree

5 files changed

+59
-25
lines changed

5 files changed

+59
-25
lines changed

llvm/lib/CAS/OnDiskCommon.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,40 @@
99
#include "OnDiskCommon.h"
1010
#include "llvm/ADT/StringRef.h"
1111
#include "llvm/Support/Error.h"
12+
#include "llvm/Support/Process.h"
13+
#include <mutex>
14+
#include <optional>
1215

1316
using namespace llvm;
1417

18+
static uint64_t OnDiskCASMaxMappingSize = 0;
19+
1520
Expected<std::optional<uint64_t>> cas::ondisk::getOverriddenMaxMappingSize() {
16-
constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";
17-
const char *Value = getenv(EnvVar);
18-
if (!Value)
21+
static std::once_flag Flag;
22+
Error Err = Error::success();
23+
std::call_once(Flag, [&Err] {
24+
ErrorAsOutParameter EAO(&Err);
25+
constexpr const char *EnvVar = "LLVM_CAS_MAX_MAPPING_SIZE";
26+
auto Value = sys::Process::GetEnv(EnvVar);
27+
if (!Value)
28+
return;
29+
30+
uint64_t Size;
31+
if (StringRef(*Value).getAsInteger(/*auto*/ 0, Size))
32+
Err = createStringError(inconvertibleErrorCode(),
33+
"invalid value for %s: expected integer", EnvVar);
34+
OnDiskCASMaxMappingSize = Size;
35+
});
36+
37+
if (Err)
38+
return std::move(Err);
39+
40+
if (OnDiskCASMaxMappingSize == 0)
1941
return std::nullopt;
2042

21-
uint64_t Size;
22-
if (StringRef(Value).getAsInteger(/*auto*/ 0, Size))
23-
return createStringError(inconvertibleErrorCode(),
24-
"invalid value for %s: expected integer", EnvVar);
25-
return Size;
43+
return OnDiskCASMaxMappingSize;
44+
}
45+
46+
void cas::ondisk::setMaxMappingSize(uint64_t Size) {
47+
OnDiskCASMaxMappingSize = Size;
2648
}

llvm/lib/CAS/OnDiskCommon.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414

1515
namespace llvm::cas::ondisk {
1616

17-
/// Retrieves an overridden maximum mapping size for CAS files, if any, by
18-
/// checking LLVM_CAS_MAX_MAPPING_SIZE in the environment. If the value is
19-
/// unreadable, returns an error.
17+
/// Retrieves an overridden maximum mapping size for CAS files, if any,
18+
/// speicified by LLVM_CAS_MAX_MAPPING_SIZE in the environment or set by
19+
/// `setMaxMappingSize()`. If the value from environment is unreadable, returns
20+
/// an error.
2021
Expected<std::optional<uint64_t>> getOverriddenMaxMappingSize();
2122

23+
/// Set MaxMappingSize for ondisk CAS. This function is not thread-safe and
24+
/// should be set before creaing any ondisk CAS and does not affect CAS already
25+
/// created. Set value 0 to use default size.
26+
void setMaxMappingSize(uint64_t Size);
27+
2228
} // namespace llvm::cas::ondisk
2329

2430
#endif // LLVM_LIB_CAS_ONDISKCOMMON_H

llvm/unittests/CAS/CASTestConfig.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "llvm/CAS/ActionCache.h"
1111
#include "llvm/CAS/ObjectStore.h"
1212
#include "llvm/RemoteCachingService/RemoteCachingService.h"
13+
#include "llvm/Support/CommandLine.h"
1314
#include "gtest/gtest.h"
1415
#include <mutex>
1516

@@ -27,23 +28,20 @@ TestingAndDir createInMemory(int I) {
2728
return TestingAndDir{std::move(CAS), std::move(Cache), nullptr, std::nullopt};
2829
}
2930

30-
__attribute__((constructor)) static void configureCASTestEnv() {
31-
// Restrict the size of the on-disk CAS for tests. This allows testing in
32-
// constrained environments (e.g. small TMPDIR). It also prevents leaving
33-
// behind large files on file systems that do not support sparse files if a
34-
// test crashes before resizing the file.
35-
static std::once_flag Flag;
36-
std::call_once(Flag, [] {
37-
size_t Limit = 100 * 1024 * 1024;
38-
std::string LimitStr = std::to_string(Limit);
39-
setenv("LLVM_CAS_MAX_MAPPING_SIZE", LimitStr.c_str(), /*overwrite=*/false);
40-
});
41-
}
42-
4331
INSTANTIATE_TEST_SUITE_P(InMemoryCAS, CASTest,
4432
::testing::Values(createInMemory));
4533

4634
#if LLVM_ENABLE_ONDISK_CAS
35+
namespace llvm::cas::ondisk {
36+
void setMaxMappingSize(uint64_t Size);
37+
} // namespace llvm::cas::ondisk
38+
39+
void setMaxOnDiskCASMappingSize() {
40+
static std::once_flag Flag;
41+
std::call_once(
42+
Flag, [] { llvm::cas::ondisk::setMaxMappingSize(100 * 1024 * 1024); });
43+
}
44+
4745
TestingAndDir createOnDisk(int I) {
4846
unittest::TempDir Temp("on-disk-cas", /*Unique=*/true);
4947
std::unique_ptr<ObjectStore> CAS;
@@ -55,6 +53,8 @@ TestingAndDir createOnDisk(int I) {
5553
std::move(Temp)};
5654
}
5755
INSTANTIATE_TEST_SUITE_P(OnDiskCAS, CASTest, ::testing::Values(createOnDisk));
56+
#else
57+
void setMaxOnDiskCASMappingSize() {}
5858
#endif /* LLVM_ENABLE_ONDISK_CAS */
5959

6060
#if LLVM_CAS_ENABLE_REMOTE_CACHE

llvm/unittests/CAS/CASTestConfig.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct TestingAndDir {
3333
std::optional<llvm::unittest::TempDir> Temp;
3434
};
3535

36+
void setMaxOnDiskCASMappingSize();
37+
3638
class CASTest
3739
: public testing::TestWithParam<std::function<TestingAndDir(int)>> {
3840
protected:
@@ -58,7 +60,10 @@ class CASTest
5860
Envs.emplace_back(std::move(TD.Env));
5961
return std::move(TD.Cache);
6062
}
61-
void SetUp() { NextCASIndex = 0; }
63+
void SetUp() {
64+
NextCASIndex = 0;
65+
setMaxOnDiskCASMappingSize();
66+
}
6267
void TearDown() {
6368
NextCASIndex = std::nullopt;
6469
Dirs.clear();

llvm/unittests/CAS/ObjectStoreTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ TEST(OnDiskCASTest, BlobsBigParallelMultiCAS) {
416416

417417
#ifndef _WIN32 // FIXME: resize support on Windows.
418418
TEST(OnDiskCASTest, DiskSize) {
419+
setMaxOnDiskCASMappingSize();
419420
unittest::TempDir Temp("on-disk-cas", /*Unique=*/true);
420421
std::unique_ptr<ObjectStore> CAS;
421422
ASSERT_THAT_ERROR(createOnDiskCAS(Temp.path()).moveInto(CAS), Succeeded());

0 commit comments

Comments
 (0)