[llvm] [CAS] Fix deprecation warning in `getBootTime` (PR #171168)
Steven Wu via llvm-commits
llvm-commits at lists.llvm.org
Mon Dec 8 10:03:52 PST 2025
https://github.com/cachemeifyoucan created https://github.com/llvm/llvm-project/pull/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.
>From 54cf7603cadc79807089219edd3ba491047bb143 Mon Sep 17 00:00:00 2001
From: Steven Wu <stevenwu at apple.com>
Date: Mon, 8 Dec 2025 10:03:40 -0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
llvm/lib/CAS/OnDiskCommon.cpp | 33 +++++++++++++++++++++++++++++
llvm/lib/CAS/OnDiskCommon.h | 7 ++++++
llvm/lib/CAS/UnifiedOnDiskCache.cpp | 27 -----------------------
3 files changed, 40 insertions(+), 27 deletions(-)
diff --git a/llvm/lib/CAS/OnDiskCommon.cpp b/llvm/lib/CAS/OnDiskCommon.cpp
index 281bde981457b..b585fc1e8cd2e 100644
--- a/llvm/lib/CAS/OnDiskCommon.cpp
+++ b/llvm/lib/CAS/OnDiskCommon.cpp
@@ -30,6 +30,12 @@
#include <sys/mount.h> // statfs
#endif
+#ifdef __APPLE__
+#if __has_include(<sys/sysctl.h>)
+#include <sys/sysctl.h>
+#endif
+#endif
+
using namespace llvm;
static uint64_t OnDiskCASMaxMappingSize = 0;
@@ -179,3 +185,30 @@ bool cas::ondisk::useSmallMappingSize(const Twine &P) {
// Default to use regular database file.
return false;
}
+
+Expected<uint64_t> cas::ondisk::getBootTime() {
+#ifdef __APPLE__
+#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
+ struct timeval TV;
+ size_t TVLen = sizeof(TV);
+ int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
+ if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0)
+ return createStringError(llvm::errnoAsErrorCode(),
+ "failed to get boottime");
+ if (TVLen != sizeof(TV))
+ return createStringError("sysctl kern.boottime unexpected format");
+ return TV.tv_sec;
+#else
+ return 0;
+#endif
+#elif defined(__linux__)
+ // Use the mtime for /proc, which is recreated during system boot.
+ // We could also read /proc/stat and search for 'btime'.
+ sys::fs::file_status Status;
+ if (std::error_code EC = sys::fs::status("/proc", Status))
+ return createFileError("/proc", EC);
+ return Status.getLastModificationTime().time_since_epoch().count();
+#else
+ return 0;
+#endif
+}
diff --git a/llvm/lib/CAS/OnDiskCommon.h b/llvm/lib/CAS/OnDiskCommon.h
index 24a5d21bd73fb..cff6b189d67c1 100644
--- a/llvm/lib/CAS/OnDiskCommon.h
+++ b/llvm/lib/CAS/OnDiskCommon.h
@@ -63,6 +63,13 @@ std::error_code tryLockFileThreadSafe(
Expected<size_t> preallocateFileTail(int FD, size_t CurrentSize,
size_t NewSize);
+/// Get boot time for the OS. This can be used to check if the CAS has been
+/// validated since boot.
+///
+/// \returns the boot time in seconds (0 if operation not supported), or an \c
+/// Error.
+Expected<uint64_t> getBootTime();
+
} // namespace llvm::cas::ondisk
#endif // LLVM_LIB_CAS_ONDISKCOMMON_H
diff --git a/llvm/lib/CAS/UnifiedOnDiskCache.cpp b/llvm/lib/CAS/UnifiedOnDiskCache.cpp
index e6b676accb0fe..a03d044029196 100644
--- a/llvm/lib/CAS/UnifiedOnDiskCache.cpp
+++ b/llvm/lib/CAS/UnifiedOnDiskCache.cpp
@@ -88,10 +88,6 @@
#include "llvm/Support/raw_ostream.h"
#include <optional>
-#if __has_include(<sys/sysctl.h>)
-#include <sys/sysctl.h>
-#endif
-
using namespace llvm;
using namespace llvm::cas;
using namespace llvm::cas::ondisk;
@@ -271,29 +267,6 @@ static Error validateInProcess(StringRef RootPath, StringRef HashName,
return Error::success();
}
-static Expected<uint64_t> getBootTime() {
-#if __has_include(<sys/sysctl.h>) && defined(KERN_BOOTTIME)
- struct timeval TV;
- size_t TVLen = sizeof(TV);
- int KernBoot[2] = {CTL_KERN, KERN_BOOTTIME};
- if (sysctl(KernBoot, 2, &TV, &TVLen, nullptr, 0) < 0)
- return createStringError(llvm::errnoAsErrorCode(),
- "failed to get boottime");
- if (TVLen != sizeof(TV))
- return createStringError("sysctl kern.boottime unexpected format");
- return TV.tv_sec;
-#elif defined(__linux__)
- // Use the mtime for /proc, which is recreated during system boot.
- // We could also read /proc/stat and search for 'btime'.
- sys::fs::file_status Status;
- if (std::error_code EC = sys::fs::status("/proc", Status))
- return createFileError("/proc", EC);
- return Status.getLastModificationTime().time_since_epoch().count();
-#else
- llvm::report_fatal_error("getBootTime unimplemented");
-#endif
-}
-
Expected<ValidationResult> UnifiedOnDiskCache::validateIfNeeded(
StringRef RootPath, StringRef HashName, unsigned HashByteSize,
bool CheckHash, bool AllowRecovery, bool ForceValidation,
More information about the llvm-commits
mailing list