blob: 49cf80cc68ee207a34b95efcba9c56697286389c [file] [log] [blame]
Ady Abraham50204dd2019-07-19 15:47:11 -07001/*
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#include <android-base/stringprintf.h>
Dan Stoza8ea808c2020-04-01 13:58:56 -070019#include <cutils/compiler.h>
Ady Abraham50204dd2019-07-19 15:47:11 -070020#include <utils/Trace.h>
21#include <cmath>
22#include <string>
23
Ady Abraham9c53ee72020-07-22 21:16:18 -070024namespace std {
25template <class Rep, class Period>
26bool signbit(std::chrono::duration<Rep, Period> v) {
27 return signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count());
28}
29} // namespace std
30
Dan Stoza8ea808c2020-04-01 13:58:56 -070031namespace android {
32
Ady Abraham9c53ee72020-07-22 21:16:18 -070033namespace {
34template <typename T>
35int64_t to_int64(T v) {
36 return int64_t(v);
37}
38
39template <class Rep, class Period>
40int64_t to_int64(std::chrono::duration<Rep, Period> v) {
41 return int64_t(v.count());
42}
43} // namespace
44
Ady Abraham50204dd2019-07-19 15:47:11 -070045template <typename T>
46class TracedOrdinal {
47public:
Ady Abraham9c53ee72020-07-22 21:16:18 -070048 static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) ||
49 std::is_same<std::chrono::nanoseconds, T>(),
Ady Abraham50204dd2019-07-19 15:47:11 -070050 "Type is not supported. Please test it with systrace before adding "
51 "it to the list.");
52
Dan Stoza8ea808c2020-04-01 13:58:56 -070053 TracedOrdinal(std::string name, T initialValue)
54 : mName(std::move(name)),
Ady Abraham50204dd2019-07-19 15:47:11 -070055 mHasGoneNegative(std::signbit(initialValue)),
56 mData(initialValue) {
57 trace();
58 }
59
60 operator T() const { return mData; }
61
62 TracedOrdinal& operator=(T other) {
63 mData = other;
64 mHasGoneNegative = mHasGoneNegative || std::signbit(mData);
65 trace();
66 return *this;
67 }
68
69private:
70 void trace() {
Dan Stoza8ea808c2020-04-01 13:58:56 -070071 if (CC_LIKELY(!ATRACE_ENABLED())) {
72 return;
73 }
74
75 if (mNameNegative.empty()) {
76 mNameNegative = base::StringPrintf("%sNegative", mName.c_str());
77 }
78
Ady Abraham50204dd2019-07-19 15:47:11 -070079 if (!std::signbit(mData)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070080 ATRACE_INT64(mName.c_str(), to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070081 if (mHasGoneNegative) {
82 ATRACE_INT64(mNameNegative.c_str(), 0);
83 }
84 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070085 ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070086 ATRACE_INT64(mName.c_str(), 0);
87 }
88 }
89
90 const std::string mName;
Dan Stoza8ea808c2020-04-01 13:58:56 -070091 std::string mNameNegative;
Ady Abraham50204dd2019-07-19 15:47:11 -070092 bool mHasGoneNegative;
93 T mData;
94};
Dan Stoza8ea808c2020-04-01 13:58:56 -070095
96} // namespace android