blob: 558b3be2737a10ae628913f7824d7ae4f56c036d [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
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080018
19#include <chrono>
20#include <cmath>
21#include <functional>
22#include <string>
23
Dan Stoza8ea808c2020-04-01 13:58:56 -070024#include <cutils/compiler.h>
Ady Abraham50204dd2019-07-19 15:47:11 -070025#include <utils/Trace.h>
Ady Abraham50204dd2019-07-19 15:47:11 -070026
Ady Abraham9c53ee72020-07-22 21:16:18 -070027namespace std {
28template <class Rep, class Period>
29bool signbit(std::chrono::duration<Rep, Period> v) {
30 return signbit(std::chrono::duration_cast<std::chrono::nanoseconds>(v).count());
31}
32} // namespace std
33
Dan Stoza8ea808c2020-04-01 13:58:56 -070034namespace android {
35
Ady Abraham9c53ee72020-07-22 21:16:18 -070036namespace {
37template <typename T>
38int64_t to_int64(T v) {
39 return int64_t(v);
40}
41
42template <class Rep, class Period>
43int64_t to_int64(std::chrono::duration<Rep, Period> v) {
44 return int64_t(v.count());
45}
46} // namespace
47
Ady Abraham50204dd2019-07-19 15:47:11 -070048template <typename T>
49class TracedOrdinal {
50public:
Ady Abraham9c53ee72020-07-22 21:16:18 -070051 static_assert(std::is_same<bool, T>() || (std::is_signed<T>() && std::is_integral<T>()) ||
52 std::is_same<std::chrono::nanoseconds, T>(),
Ady Abraham50204dd2019-07-19 15:47:11 -070053 "Type is not supported. Please test it with systrace before adding "
54 "it to the list.");
55
Dan Stoza8ea808c2020-04-01 13:58:56 -070056 TracedOrdinal(std::string name, T initialValue)
57 : mName(std::move(name)),
Ady Abraham50204dd2019-07-19 15:47:11 -070058 mHasGoneNegative(std::signbit(initialValue)),
59 mData(initialValue) {
60 trace();
61 }
62
Ady Abraham326ecde2020-11-06 15:05:53 -080063 T get() const { return mData; }
64
65 operator T() const { return get(); }
Ady Abraham50204dd2019-07-19 15:47:11 -070066
67 TracedOrdinal& operator=(T other) {
68 mData = other;
69 mHasGoneNegative = mHasGoneNegative || std::signbit(mData);
70 trace();
71 return *this;
72 }
73
74private:
75 void trace() {
Dan Stoza8ea808c2020-04-01 13:58:56 -070076 if (CC_LIKELY(!ATRACE_ENABLED())) {
77 return;
78 }
79
80 if (mNameNegative.empty()) {
Dominik Laskowski4e0d20d2021-12-06 11:31:02 -080081 mNameNegative = mName + "Negative";
Dan Stoza8ea808c2020-04-01 13:58:56 -070082 }
83
Ady Abraham50204dd2019-07-19 15:47:11 -070084 if (!std::signbit(mData)) {
Ady Abraham9c53ee72020-07-22 21:16:18 -070085 ATRACE_INT64(mName.c_str(), to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070086 if (mHasGoneNegative) {
87 ATRACE_INT64(mNameNegative.c_str(), 0);
88 }
89 } else {
Ady Abraham9c53ee72020-07-22 21:16:18 -070090 ATRACE_INT64(mNameNegative.c_str(), -to_int64(mData));
Ady Abraham50204dd2019-07-19 15:47:11 -070091 ATRACE_INT64(mName.c_str(), 0);
92 }
93 }
94
95 const std::string mName;
Dan Stoza8ea808c2020-04-01 13:58:56 -070096 std::string mNameNegative;
Ady Abraham50204dd2019-07-19 15:47:11 -070097 bool mHasGoneNegative;
98 T mData;
99};
Dan Stoza8ea808c2020-04-01 13:58:56 -0700100
101} // namespace android