blob: 5370645d31bc137ce274ecee7dd0af21854190d5 [file] [log] [blame]
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -08001/*
2 * Copyright (C) 2016 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
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070014 * limitations under the License.
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080015 */
16
17package android.util;
18
Felipe Leme137e7812019-06-10 11:38:05 -070019import android.annotation.NonNull;
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080020import android.os.Build;
21import android.os.SystemClock;
22import android.os.Trace;
23
Felipe Lemeb7692962019-06-24 16:22:45 -070024import com.android.internal.annotations.VisibleForTesting;
25
Felipe Leme137e7812019-06-10 11:38:05 -070026import java.util.ArrayList;
27import java.util.Collections;
Felipe Leme137e7812019-06-10 11:38:05 -070028import java.util.List;
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080029
30/**
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070031 * Helper class for reporting boot and shutdown timing metrics.
Felipe Leme137e7812019-06-10 11:38:05 -070032 *
33 * <p><b>NOTE:</b> This class is not thread-safe. Use a separate copy for other threads.
34 *
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080035 * @hide
36 */
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070037public class TimingsTraceLog {
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080038 // Debug boot time for every step if it's non-user build.
Jeff Sharkey5ab02432017-06-27 11:01:36 -060039 private static final boolean DEBUG_BOOT_TIME = !Build.IS_USER;
Felipe Lemeb7692962019-06-24 16:22:45 -070040
41 // Maximum number of nested calls that are stored
42 private static final int MAX_NESTED_CALLS = 10;
43
44 private final String[] mStartNames;
45 private final long[] mStartTimes;
46
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080047 private final String mTag;
Felipe Lemeb7692962019-06-24 16:22:45 -070048 private final long mTraceTag;
49 private final long mThreadId;
50 private final int mMaxNestedCalls;
51
52 private int mCurrentLevel = -1;
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080053
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070054 public TimingsTraceLog(String tag, long traceTag) {
Felipe Lemeb7692962019-06-24 16:22:45 -070055 this(tag, traceTag, DEBUG_BOOT_TIME ? MAX_NESTED_CALLS : -1);
56 }
57
58 @VisibleForTesting
59 public TimingsTraceLog(String tag, long traceTag, int maxNestedCalls) {
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080060 mTag = tag;
61 mTraceTag = traceTag;
Fyodor Kupolov60422452017-08-28 17:15:44 -070062 mThreadId = Thread.currentThread().getId();
Felipe Lemeb7692962019-06-24 16:22:45 -070063 mMaxNestedCalls = maxNestedCalls;
64 if (maxNestedCalls > 0) {
65 mStartNames = new String[maxNestedCalls];
66 mStartTimes = new long[maxNestedCalls];
67 } else {
68 mStartNames = null;
69 mStartTimes = null;
70 }
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080071 }
72
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070073 /**
74 * Begin tracing named section
75 * @param name name to appear in trace
76 */
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080077 public void traceBegin(String name) {
Fyodor Kupolov60422452017-08-28 17:15:44 -070078 assertSameThread();
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080079 Trace.traceBegin(mTraceTag, name);
Felipe Lemeb7692962019-06-24 16:22:45 -070080
81 if (!DEBUG_BOOT_TIME) return;
82
83 if (mCurrentLevel + 1 >= mMaxNestedCalls) {
84 Slog.w(mTag, "not tracing duration of '" + name + "' because already reached "
85 + mMaxNestedCalls + " levels");
86 return;
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080087 }
Felipe Lemeb7692962019-06-24 16:22:45 -070088
89 mCurrentLevel++;
90 mStartNames[mCurrentLevel] = name;
91 mStartTimes[mCurrentLevel] = SystemClock.elapsedRealtime();
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080092 }
93
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070094 /**
95 * End tracing previously {@link #traceBegin(String) started} section.
Felipe Lemeb7692962019-06-24 16:22:45 -070096 *
97 * <p>Also {@link #logDuration logs} the duration.
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -070098 */
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -080099 public void traceEnd() {
Fyodor Kupolov60422452017-08-28 17:15:44 -0700100 assertSameThread();
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -0800101 Trace.traceEnd(mTraceTag);
Felipe Lemeb7692962019-06-24 16:22:45 -0700102
103 if (!DEBUG_BOOT_TIME) return;
104
105 if (mCurrentLevel < 0) {
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -0800106 Slog.w(mTag, "traceEnd called more times than traceBegin");
107 return;
108 }
Felipe Lemeb7692962019-06-24 16:22:45 -0700109
110 final String name = mStartNames[mCurrentLevel];
111 final long duration = SystemClock.elapsedRealtime() - mStartTimes[mCurrentLevel];
112 mCurrentLevel--;
113
114 logDuration(name, duration);
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -0700115 }
116
Fyodor Kupolov60422452017-08-28 17:15:44 -0700117 private void assertSameThread() {
118 final Thread currentThread = Thread.currentThread();
119 if (currentThread.getId() != mThreadId) {
120 throw new IllegalStateException("Instance of TimingsTraceLog can only be called from "
121 + "the thread it was created on (tid: " + mThreadId + "), but was from "
122 + currentThread.getName() + " (tid: " + currentThread.getId() + ")");
123 }
124 }
125
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -0700126 /**
Felipe Lemeb7692962019-06-24 16:22:45 -0700127 * Logs a duration so it can be parsed by external tools for performance reporting.
Fyodor Kupolov6e3461b2017-08-10 17:00:43 -0700128 */
129 public void logDuration(String name, long timeMs) {
130 Slog.d(mTag, name + " took to complete: " + timeMs + "ms");
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -0800131 }
Felipe Leme137e7812019-06-10 11:38:05 -0700132
133 /**
134 * Gets the names of the traces that {@link #traceBegin(String) have begun} but
135 * {@link #traceEnd() have not finished} yet.
136 *
137 * <p><b>NOTE:</b> this method is expensive and it should not be used in "production" - it
138 * should only be used for debugging purposes during development (and/or guarded by
139 * static {@code DEBUG} constants that are {@code false}).
140 */
141 @NonNull
142 public final List<String> getUnfinishedTracesForDebug() {
Felipe Lemeb7692962019-06-24 16:22:45 -0700143 if (mStartTimes == null || mCurrentLevel < 0) return Collections.emptyList();
144 final ArrayList<String> list = new ArrayList<>(mCurrentLevel + 1);
145 for (int i = 0; i <= mCurrentLevel; i++) {
146 list.add(mStartNames[i]);
147 }
148 return list;
Felipe Leme137e7812019-06-10 11:38:05 -0700149 }
Fyodor Kupolov3235e0c2016-11-17 11:44:49 -0800150}