Home | History | Annotate | Download | only in util
      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 and shutdown timing metrics.
     28  * @hide
     29  */
     30 public class TimingsTraceLog {
     31     // Debug boot time for every step if it's non-user build.
     32     private static final boolean DEBUG_BOOT_TIME = !Build.IS_USER;
     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 TimingsTraceLog(String tag, long traceTag) {
     39         mTag = tag;
     40         mTraceTag = traceTag;
     41     }
     42 
     43     /**
     44      * Begin tracing named section
     45      * @param name name to appear in trace
     46      */
     47     public void traceBegin(String name) {
     48         Trace.traceBegin(mTraceTag, name);
     49         if (DEBUG_BOOT_TIME) {
     50             mStartTimes.push(Pair.create(name, SystemClock.elapsedRealtime()));
     51         }
     52     }
     53 
     54     /**
     55      * End tracing previously {@link #traceBegin(String) started} section.
     56      * Also {@link #logDuration logs} the duration.
     57      */
     58     public void traceEnd() {
     59         Trace.traceEnd(mTraceTag);
     60         if (!DEBUG_BOOT_TIME) {
     61             return;
     62         }
     63         if (mStartTimes.peek() == null) {
     64             Slog.w(mTag, "traceEnd called more times than traceBegin");
     65             return;
     66         }
     67         Pair<String, Long> event = mStartTimes.pop();
     68         logDuration(event.first, (SystemClock.elapsedRealtime() - event.second));
     69     }
     70 
     71     /**
     72      * Log the duration so it can be parsed by external tools for performance reporting
     73      */
     74     public void logDuration(String name, long timeMs) {
     75         Slog.d(mTag, name + " took to complete: " + timeMs + "ms");
     76     }
     77 }
     78