1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125

base / android / early_trace_event_binding.cc [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/android/early_trace_event_binding.h"

#include <stdint.h>

#include "base/android/jni_string.h"
#include "base/android/trace_event_binding.h"
#include "base/time/time.h"
#include "base/trace_event/base_tracing.h"
#include "base/tracing_buildflags.h"

// Must come after all headers that specialize FromJniType() / ToJniType().
#include "base/tasks_jni/EarlyTraceEvent_jni.h"

namespace base {
namespace android {

static void JNI_EarlyTraceEvent_RecordEarlyBeginEvent(JNIEnv* env,
                                                      std::string& name,
                                                      jlong time_ns,
                                                      jint thread_id,
                                                      jlong thread_time_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  static const unsigned char* category_group_enabled =
      TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(internal::kJavaTraceCategory);
  trace_event_internal::AddTraceEventWithThreadIdAndTimestamps(
      TRACE_EVENT_PHASE_BEGIN, category_group_enabled, name.c_str(),
      /*scope=*/nullptr, trace_event_internal::kNoId, thread_id,
      TimeTicks::FromJavaNanoTime(time_ns),
      ThreadTicks() + Milliseconds(thread_time_ms),
      TRACE_EVENT_FLAG_JAVA_STRING_LITERALS | TRACE_EVENT_FLAG_COPY);
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)
}

static void JNI_EarlyTraceEvent_RecordEarlyEndEvent(JNIEnv* env,
                                                    std::string& name,
                                                    jlong time_ns,
                                                    jint thread_id,
                                                    jlong thread_time_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  static const unsigned char* category_group_enabled =
      TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(internal::kJavaTraceCategory);
  trace_event_internal::AddTraceEventWithThreadIdAndTimestamps(
      TRACE_EVENT_PHASE_END, category_group_enabled, name.c_str(),
      /*scope=*/nullptr, trace_event_internal::kNoId, thread_id,
      TimeTicks::FromJavaNanoTime(time_ns),
      ThreadTicks() + Milliseconds(thread_time_ms),
      TRACE_EVENT_FLAG_JAVA_STRING_LITERALS | TRACE_EVENT_FLAG_COPY);
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)
}

static void JNI_EarlyTraceEvent_RecordEarlyToplevelBeginEvent(
    JNIEnv* env,
    std::string& name,
    jlong time_ns,
    jint thread_id,
    jlong thread_time_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  static const unsigned char* category_group_enabled =
      TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
          internal::kToplevelTraceCategory);
  trace_event_internal::AddTraceEventWithThreadIdAndTimestamps(
      TRACE_EVENT_PHASE_BEGIN, category_group_enabled, name.c_str(),
      /*scope=*/nullptr, trace_event_internal::kNoId, thread_id,
      TimeTicks::FromJavaNanoTime(time_ns),
      ThreadTicks() + Milliseconds(thread_time_ms),
      TRACE_EVENT_FLAG_JAVA_STRING_LITERALS | TRACE_EVENT_FLAG_COPY);
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)
}

static void JNI_EarlyTraceEvent_RecordEarlyToplevelEndEvent(
    JNIEnv* env,
    std::string& name,
    jlong time_ns,
    jint thread_id,
    jlong thread_time_ms) {
#if BUILDFLAG(ENABLE_BASE_TRACING)
  static const unsigned char* category_group_enabled =
      TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(
          internal::kToplevelTraceCategory);
  trace_event_internal::AddTraceEventWithThreadIdAndTimestamps(
      TRACE_EVENT_PHASE_END, category_group_enabled, name.c_str(),
      /*scope=*/nullptr, trace_event_internal::kNoId, thread_id,
      TimeTicks::FromJavaNanoTime(time_ns),
      ThreadTicks() + Milliseconds(thread_time_ms),
      TRACE_EVENT_FLAG_JAVA_STRING_LITERALS | TRACE_EVENT_FLAG_COPY);
#endif  // BUILDFLAG(ENABLE_BASE_TRACING)
}

static void JNI_EarlyTraceEvent_RecordEarlyAsyncBeginEvent(JNIEnv* env,
                                                           std::string& name,
                                                           jlong id,
                                                           jlong time_ns) {
  TRACE_EVENT_BEGIN(internal::kJavaTraceCategory, nullptr,
                    perfetto::Track(static_cast<uint64_t>(id)),
                    TimeTicks::FromJavaNanoTime(time_ns),
                    [&](::perfetto::EventContext& ctx) {
                      ctx.event()->set_name(name.c_str());
                    });
}

static void JNI_EarlyTraceEvent_RecordEarlyAsyncEndEvent(JNIEnv* env,
                                                         jlong id,
                                                         jlong time_ns) {
  TRACE_EVENT_END(internal::kJavaTraceCategory,
                  perfetto::Track(static_cast<uint64_t>(id)));
}

bool GetBackgroundStartupTracingFlag() {
  JNIEnv* env = jni_zero::AttachCurrentThread();
  return base::android::Java_EarlyTraceEvent_getBackgroundStartupTracingFlag(
      env);
}

void SetBackgroundStartupTracingFlag(bool enabled) {
  JNIEnv* env = jni_zero::AttachCurrentThread();
  base::android::Java_EarlyTraceEvent_setBackgroundStartupTracingFlag(env,
                                                                      enabled);
}

}  // namespace android
}  // namespace base