• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_BASE_CHRONO_UTILS_H
18 #define ANDROID_BASE_CHRONO_UTILS_H
19 
20 #include <chrono>
21 #include <sstream>
22 
23 using namespace std::chrono_literals;
24 
25 namespace android {
26 namespace base {
27 
28 // A std::chrono clock based on CLOCK_BOOTTIME.
29 class boot_clock {
30  public:
31   typedef std::chrono::nanoseconds duration;
32   typedef std::chrono::time_point<boot_clock, duration> time_point;
33 
34   static time_point now();
35 };
36 
37 class Timer {
38  public:
Timer()39   Timer() : start_(boot_clock::now()) {}
40 
duration()41   std::chrono::milliseconds duration() const {
42     return std::chrono::duration_cast<std::chrono::milliseconds>(boot_clock::now() - start_);
43   }
44 
45  private:
46   boot_clock::time_point start_;
47 };
48 
49 std::ostream& operator<<(std::ostream& os, const Timer& t);
50 
51 }  // namespace base
52 }  // namespace android
53 
54 #endif  // ANDROID_BASE_CHRONO_UTILS_H
55