| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.os.Build; |
| 21 |
import android.os.SystemClock; |
| 22 |
import android.os.Trace; |
| 23 |
|
| 24 |
import com.android.internal.annotations.VisibleForTesting; |
| 25 |
|
| 26 |
import java.util.ArrayList; |
| 27 |
import java.util.Collections; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class TimingsTraceLog { |
| 38 |
|
| 39 |
private static final boolean DEBUG_BOOT_TIME = !Build.IS_USER; |
| 40 |
|
| 41 |
|
| 42 |
private static final int MAX_NESTED_CALLS = 10; |
| 43 |
|
| 44 |
private final String[] mStartNames; |
| 45 |
private final long[] mStartTimes; |
| 46 |
|
| 47 |
private final String mTag; |
| 48 |
private final long mTraceTag; |
| 49 |
private final long mThreadId; |
| 50 |
private final int mMaxNestedCalls; |
| 51 |
|
| 52 |
private int mCurrentLevel = -1; |
| 53 |
|
| 54 |
public TimingsTraceLog(String tag, long traceTag) { |
| 55 |
this(tag, traceTag, DEBUG_BOOT_TIME ? MAX_NESTED_CALLS : -1); |
| 56 |
} |
| 57 |
|
| 58 |
@VisibleForTesting |
| 59 |
public TimingsTraceLog(String tag, long traceTag, int maxNestedCalls) { |
| 60 |
mTag = tag; |
| 61 |
mTraceTag = traceTag; |
| 62 |
mThreadId = Thread.currentThread().getId(); |
| 63 |
mMaxNestedCalls = maxNestedCalls; |
| 64 |
this.mStartNames = createAndGetStartNamesArray(); |
| 65 |
this.mStartTimes = createAndGetStartTimesArray(); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
protected TimingsTraceLog(TimingsTraceLog other) { |
| 76 |
this.mTag = other.mTag; |
| 77 |
this.mTraceTag = other.mTraceTag; |
| 78 |
this.mThreadId = Thread.currentThread().getId(); |
| 79 |
this.mMaxNestedCalls = other.mMaxNestedCalls; |
| 80 |
this.mStartNames = createAndGetStartNamesArray(); |
| 81 |
this.mStartTimes = createAndGetStartTimesArray(); |
| 82 |
this.mCurrentLevel = other.mCurrentLevel; |
| 83 |
} |
| 84 |
|
| 85 |
private String[] createAndGetStartNamesArray() { |
| 86 |
return mMaxNestedCalls > 0 ? new String[mMaxNestedCalls] : null; |
| 87 |
} |
| 88 |
|
| 89 |
private long[] createAndGetStartTimesArray() { |
| 90 |
return mMaxNestedCalls > 0 ? new long[mMaxNestedCalls] : null; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public void traceBegin(String name) { |
| 98 |
assertSameThread(); |
| 99 |
Trace.traceBegin(mTraceTag, name); |
| 100 |
|
| 101 |
if (!DEBUG_BOOT_TIME) return; |
| 102 |
|
| 103 |
if (mCurrentLevel + 1 >= mMaxNestedCalls) { |
| 104 |
Slog.w(mTag, "not tracing duration of '" + name + "' because already reached " |
| 105 |
+ mMaxNestedCalls + " levels"); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
mCurrentLevel++; |
| 110 |
mStartNames[mCurrentLevel] = name; |
| 111 |
mStartTimes[mCurrentLevel] = SystemClock.elapsedRealtime(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public void traceEnd() { |
| 120 |
assertSameThread(); |
| 121 |
Trace.traceEnd(mTraceTag); |
| 122 |
|
| 123 |
if (!DEBUG_BOOT_TIME) return; |
| 124 |
|
| 125 |
if (mCurrentLevel < 0) { |
| 126 |
Slog.w(mTag, "traceEnd called more times than traceBegin"); |
| 127 |
return; |
| 128 |
} |
| 129 |
|
| 130 |
final String name = mStartNames[mCurrentLevel]; |
| 131 |
final long duration = SystemClock.elapsedRealtime() - mStartTimes[mCurrentLevel]; |
| 132 |
mCurrentLevel--; |
| 133 |
|
| 134 |
logDuration(name, duration); |
| 135 |
} |
| 136 |
|
| 137 |
private void assertSameThread() { |
| 138 |
final Thread currentThread = Thread.currentThread(); |
| 139 |
if (currentThread.getId() != mThreadId) { |
| 140 |
throw new IllegalStateException("Instance of TimingsTraceLog can only be called from " |
| 141 |
+ "the thread it was created on (tid: " + mThreadId + "), but was from " |
| 142 |
+ currentThread.getName() + " (tid: " + currentThread.getId() + ")"); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public void logDuration(String name, long timeMs) { |
| 150 |
Slog.v(mTag, name + " took to complete: " + timeMs + "ms"); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
@NonNull |
| 162 |
public final List<String> getUnfinishedTracesForDebug() { |
| 163 |
if (mStartTimes == null || mCurrentLevel < 0) return Collections.emptyList(); |
| 164 |
final ArrayList<String> list = new ArrayList<>(mCurrentLevel + 1); |
| 165 |
for (int i = 0; i <= mCurrentLevel; i++) { |
| 166 |
list.add(mStartNames[i]); |
| 167 |
} |
| 168 |
return list; |
| 169 |
} |
| 170 |
} |