• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 #pragma once
18 
19 #include <chrono>
20 #include <cmath>
21 #include <functional>
22 #include <string>
23 
24 #include <cutils/compiler.h>
25 #include <utils/Trace.h>
26 
27 namespace std {
28 template <class Rep, class Period>
signbit(std::chrono::duration<Rep,Period> v)29 bool signbit(std::chrono::duration<Rep, Period> v) {
30     return signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count());
31 }
32 } // namespace std
33 
34 namespace android {
35 
36 namespace {
37 template <typename T>
to_int64(T v)38 int64_t to_int64(T v) {
39     return int64_t(v);
40 }
41 
42 template <class Rep, class Period>
to_int64(std::chrono::duration<Rep,Period> v)43 int64_t to_int64(std::chrono::duration<Rep, Period> v) {
44     return int64_t(v.count());
45 }
46 } // namespace
47 
48 template <typename T>
49 class TracedOrdinal {
50 public:
51     static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) ||
52                           std::is_same<std::chrono::nanoseconds, T>(),
53                   "Type is not supported. Please test it with systrace before adding "
54                   "it to the list.");
55 
TracedOrdinal(std::string name,T initialValue)56     TracedOrdinal(std::string name, T initialValue)
57           : mName(std::move(name)),
58             mHasGoneNegative(std::signbit(initialValue)),
59             mData(initialValue) {
60         trace();
61     }
62 
get()63     T get() const { return mData; }
64 
T()65     operator T() const { return get(); }
66 
67     TracedOrdinal& operator=(T other) {
68         mData = other;
69         mHasGoneNegative = mHasGoneNegative || std::signbit(mData);
70         trace();
71         return *this;
72     }
73 
74 private:
trace()75     void trace() {
76         if (CC_LIKELY(!ATRACE_ENABLED())) {
77             return;
78         }
79 
80         if (mNameNegative.empty()) {
81             mNameNegative = mName + "Negative";
82         }
83 
84         if (!std::signbit(mData)) {
85             ATRACE_INT64(mName.c_str(), to_int64(mData));
86             if (mHasGoneNegative) {
87                 ATRACE_INT64(mNameNegative.c_str(), 0);
88             }
89         } else {
90             ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData));
91             ATRACE_INT64(mName.c_str(), 0);
92         }
93     }
94 
95     const std::string mName;
96     std::string mNameNegative;
97     bool mHasGoneNegative;
98     T mData;
99 };
100 
101 } // namespace android
102