You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2013/12/20 01:32:00 UTC
[3/7] git commit: Added os::bootId.
Added os::bootId.
This can be used to identify a reboot, should the boot id change.
For OS X and POSIX systems, the boot time in seconds from sysctl and
utmpx (respectively) are used as a good approximation for a unique
boot id.
Review: https://reviews.apache.org/r/16235
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/8d485a68
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/8d485a68
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/8d485a68
Branch: refs/heads/master
Commit: 8d485a68550e593db3af9244fca876b91597d558
Parents: 83e1547
Author: Benjamin Mahler <bm...@twitter.com>
Authored: Tue Dec 17 14:57:07 2013 -0800
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Thu Dec 19 14:26:27 2013 -0800
----------------------------------------------------------------------
.../3rdparty/stout/include/stout/os.hpp | 22 +++++++++++++++++
.../3rdparty/stout/tests/os_tests.cpp | 26 ++++++++++++++++++++
2 files changed, 48 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/8d485a68/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
index 2382d40..7c1f6ec 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/os.hpp
@@ -885,6 +885,28 @@ inline Try<Bytes> memory()
}
+inline Try<std::string> bootId()
+{
+#ifdef __linux__
+ Try<std::string> read = os::read("/proc/sys/kernel/random/boot_id");
+ if (read.isError()) {
+ return read;
+ }
+ return strings::trim(read.get());
+#elif defined(__APPLE__)
+ // For OS X, we use the boot time in seconds as a unique boot id.
+ // Although imperfect, this works quite well in practice.
+ Try<timeval> bootTime = os::sysctl(CTL_KERN, KERN_BOOTTIME).time();
+ if (bootTime.isError()) {
+ return Error(bootTime.error());
+ }
+ return stringify(bootTime.get().tv_sec);
+#else
+ return Error("Not implemented");
+#endif
+}
+
+
// The structure returned by uname describing the currently running system.
struct UTSInfo
{
http://git-wip-us.apache.org/repos/asf/mesos/blob/8d485a68/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
index 33c2ee1..c40c743 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/os_tests.cpp
@@ -15,8 +15,10 @@
#include <stout/foreach.hpp>
#include <stout/gtest.hpp>
#include <stout/hashset.hpp>
+#include <stout/numify.hpp>
#include <stout/os.hpp>
#include <stout/stopwatch.hpp>
+#include <stout/strings.hpp>
#include <stout/try.hpp>
#include <stout/uuid.hpp>
@@ -178,6 +180,30 @@ TEST_F(OsTest, find)
}
+TEST_F(OsTest, bootId)
+{
+ Try<string> bootId = os::bootId();
+ ASSERT_SOME(bootId);
+ EXPECT_NE("", bootId.get());
+
+#ifdef __linux__
+ Try<string> read = os::read("/proc/sys/kernel/random/boot_id");
+ ASSERT_SOME(read);
+ EXPECT_EQ(bootId.get(), strings::trim(read.get()));
+#elif defined(__APPLE__)
+ // For OS X systems, the boot id is the system boot time in
+ // seconds, so assert it can be numified and is a reasonable value.
+ Try<uint64_t> numified = numify<uint64_t>(bootId.get());
+ ASSERT_SOME(numified);
+
+ timeval time;
+ gettimeofday(&time, NULL);
+ EXPECT_GT(Seconds(numified.get()), Seconds(0));
+ EXPECT_LT(Seconds(numified.get()), Seconds(time.tv_sec));
+#endif
+}
+
+
TEST_F(OsTest, uname)
{
const Try<os::UTSInfo>& info = os::uname();