Apache Mesos
bootid.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_OS_WINDOWS_BOOTID_HPP__
14 #define __STOUT_OS_WINDOWS_BOOTID_HPP__
15 
16 #include <chrono>
17 #include <string>
18 
19 #include <stout/stringify.hpp>
20 #include <stout/try.hpp>
21 
22 
23 namespace os {
24 
25 inline Try<std::string> bootId()
26 {
27  // NOTE: we follow the precedent of the OS X design here and use the boot
28  // time in seconds since the Unix epoch as a boot ID. See comment in
29  // `stout/os/posix/bootid.hpp` for discussion of this approach. Note also
30  // that we can't use milliseconds here instead of seconds because the
31  // relatively high imprecision of millisecond resolution would cause `bootId`
32  // to return a different number nearly every time it was called.
33 
34  std::chrono::milliseconds uptime =
35  std::chrono::milliseconds(GetTickCount64());
36 
37  std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
38  std::chrono::system_clock::time_point boot_time = now - uptime;
39 
40  long long boot_time_secs = std::chrono::duration_cast<std::chrono::seconds>(
41  boot_time.time_since_epoch()).count();
42 
43  return stringify(boot_time_secs);
44 }
45 
46 } // namespace os {
47 
48 #endif // __STOUT_OS_WINDOWS_BOOTID_HPP__
Try< std::string > bootId()
Definition: bootid.hpp:33
Definition: try.hpp:34
std::string stringify(int flags)