• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.base.metrics;
6 
7 import android.os.Trace;
8 
9 /**
10  * An alternative to @{TraceEvent} that allows us to trace events before native
11  * initialization.
12  *
13  * Note that TraceEvent / EarlyTraceEvent cannot be used before native initialization since
14  * it directly purges to the kernel debug message but that method does not allow tracing events
15  * to be written *after* the event occurrence.
16  */
17 public class ScopedSysTraceEvent implements AutoCloseable {
18     /**
19      * Factory used to support the "try with resource" construct.
20      * Note that currently this is the only allowed pattern. However, this requires heap allocation
21      * so we may consider calling Trace.beginSection() / endSection() directly if it should be used
22      * repeatedly.
23      *
24      * @param name Trace event name.
25      * @return a {@ScopedSysTraceEvent}, or null if tracing is not enabled.
26      */
scoped(String name)27     public static ScopedSysTraceEvent scoped(String name) {
28         return new ScopedSysTraceEvent(name);
29     }
30 
31     /** Constructor used to support the "try with resource" construct. */
ScopedSysTraceEvent(String name)32     private ScopedSysTraceEvent(String name) {
33         Trace.beginSection(name);
34     }
35 
36     @Override
close()37     public void close() {
38         Trace.endSection();
39     }
40 }
41