1 /* 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 14 * limitations under the License 15 */ 16 17 package android.util; 18 19 import android.os.Build; 20 import android.os.SystemClock; 21 import android.os.Trace; 22 23 import java.util.ArrayDeque; 24 import java.util.Deque; 25 26 /** 27 * Helper class for reporting boot timing metrics. 28 * @hide 29 */ 30 public class BootTimingsTraceLog { 31 // Debug boot time for every step if it's non-user build. 32 private static final boolean DEBUG_BOOT_TIME = !"user".equals(Build.TYPE); 33 private final Deque<Pair<String, Long>> mStartTimes 34 = DEBUG_BOOT_TIME ? new ArrayDeque<>() : null; 35 private final String mTag; 36 private long mTraceTag; 37 38 public BootTimingsTraceLog(String tag, long traceTag) { 39 mTag = tag; 40 mTraceTag = traceTag; 41 } 42 43 public void traceBegin(String name) { 44 Trace.traceBegin(mTraceTag, name); 45 if (DEBUG_BOOT_TIME) { 46 mStartTimes.push(Pair.create(name, SystemClock.elapsedRealtime())); 47 } 48 } 49 50 public void traceEnd() { 51 Trace.traceEnd(mTraceTag); 52 if (!DEBUG_BOOT_TIME) { 53 return; 54 } 55 if (mStartTimes.peek() == null) { 56 Slog.w(mTag, "traceEnd called more times than traceBegin"); 57 return; 58 } 59 Pair<String, Long> event = mStartTimes.pop(); 60 // Log the duration so it can be parsed by external tools for performance reporting 61 Slog.d(mTag, event.first + " took to complete: " 62 + (SystemClock.elapsedRealtime() - event.second) + "ms"); 63 } 64 } 65