Skip to content

Commit a38f847

Browse files
[CAS] Fix deprecation warning in getBootTime (#171168)
For some older Linux distro that still ships deprecated sysctl header, there can be deprecation warnings when building LLVMCAS. This also results LLVMCAS to use the deprecated sysctl function, while it is only intended to be used on Darwin platforms. Fix the issue by only including sysctl on Apple platforms. Also move the platform dependent `getBootTime` code into OnDiskCommon.cpp.
1 parent de53b1a commit a38f847

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

llvm/lib/CAS/OnDiskCommon.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@
3030
#include <sys/mount.h> // statfs
3131
#endif
3232

33+
#ifdef __APPLE__
34+
#if __has_include(<sys/sysctl.h>)
35+
#include <sys/sysctl.h>
36+
#endif
37+
#endif
38+
3339
using namespace llvm;
3440

3541
static uint64_t OnDiskCASMaxMappingSize = 0;
@@ -179,3 +185,30 @@ bool cas::ondisk::useSmallMappingSize(const Twine &P) {
179185
// Default to use regular database file.
180186
return false;
181187
}
188+
189+
Expected<uint64_t> cas::ondisk::getBootTime() {
190+
#ifdef __APPLE__
191+
#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
192+
struct timeval TV;
193+
size_t TVLen = sizeof(TV);
194+
int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
195+
if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0)
196+
return createStringError(llvm::errnoAsErrorCode(),
197+
"failed to get boottime");
198+
if (TVLen != sizeof(TV))
199+
return createStringError("sysctl kern.boottime unexpected format");
200+
return TV.tv_sec;
201+
#else
202+
return 0;
203+
#endif
204+
#elif defined(__linux__)
205+
// Use the mtime for /proc, which is recreated during system boot.
206+
// We could also read /proc/stat and search for 'btime'.
207+
sys::fs::file_status Status;
208+
if (std::error_code EC = sys::fs::status("/proc", Status))
209+
return createFileError("/proc", EC);
210+
return Status.getLastModificationTime().time_since_epoch().count();
211+
#else
212+
return 0;
213+
#endif
214+
}

llvm/lib/CAS/OnDiskCommon.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ std::error_code tryLockFileThreadSafe(
6363
Expected<size_t> preallocateFileTail(int FD, size_t CurrentSize,
6464
size_t NewSize);
6565

66+
/// Get boot time for the OS. This can be used to check if the CAS has been
67+
/// validated since boot.
68+
///
69+
/// \returns the boot time in seconds (0 if operation not supported), or an \c
70+
/// Error.
71+
Expected<uint64_t> getBootTime();
72+
6673
} // namespace llvm::cas::ondisk
6774

6875
#endif // LLVM_LIB_CAS_ONDISKCOMMON_H

llvm/lib/CAS/UnifiedOnDiskCache.cpp

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,6 @@
8888
#include "llvm/Support/raw_ostream.h"
8989
#include <optional>
9090

91-
#if __has_include(<sys/sysctl.h>)
92-
#include <sys/sysctl.h>
93-
#endif
94-
9591
using namespace llvm;
9692
using namespace llvm::cas;
9793
using namespace llvm::cas::ondisk;
@@ -271,29 +267,6 @@ static Error validateInProcess(StringRef RootPath, StringRef HashName,
271267
return Error::success();
272268
}
273269

274-
static Expected<uint64_t> getBootTime() {
275-
#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
276-
struct timeval TV;
277-
size_t TVLen = sizeof(TV);
278-
int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
279-
if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0)
280-
return createStringError(llvm::errnoAsErrorCode(),
281-
"failed to get boottime");
282-
if (TVLen != sizeof(TV))
283-
return createStringError("sysctl kern.boottime unexpected format");
284-
return TV.tv_sec;
285-
#elif defined(__linux__)
286-
// Use the mtime for /proc, which is recreated during system boot.
287-
// We could also read /proc/stat and search for 'btime'.
288-
sys::fs::file_status Status;
289-
if (std::error_code EC = sys::fs::status("/proc", Status))
290-
return createFileError("/proc", EC);
291-
return Status.getLastModificationTime().time_since_epoch().count();
292-
#else
293-
llvm::report_fatal_error("getBootTime unimplemented");
294-
#endif
295-
}
296-
297270
Expected<ValidationResult> UnifiedOnDiskCache::validateIfNeeded(
298271
StringRef RootPath, StringRef HashName, unsigned HashByteSize,
299272
bool CheckHash, bool AllowRecovery, bool ForceValidation,

0 commit comments

Comments
 (0)