Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Android.bp
Original file line number Diff line number Diff line change
Expand Up @@ -19057,6 +19057,9 @@ java_defaults {
"src/android_sdk/java/main/dev/perfetto/sdk/PerfettoTrackEventBuilder.java",
"src/android_sdk/java/main/dev/perfetto/sdk/PerfettoTrackEventExtra.java",
],
libs: [
"error_prone_annotations",
],
sdk_version: "module_current",
}

Expand Down
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -5245,6 +5245,7 @@ perfetto_android_library(
],
manifest = "src/android_sdk/java/main/AndroidManifest.xml",
deps = [
":gn_error_prone_annotations",
":src_android_sdk_jni_libperfetto_jni",
],
tags = [
Expand Down
3 changes: 3 additions & 0 deletions gn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,9 @@ perfetto_py_library("protobuf_py") {
perfetto_android_library("android_test_common") {
}

perfetto_android_library("error_prone_annotations") {
}

if (enable_perfetto_etm_importer) {
group("open_csd") {
visibility = [ "//src/trace_processor/importers/etm" ]
Expand Down
3 changes: 3 additions & 0 deletions include/perfetto/public/abi/track_event_hl_abi.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,11 @@ enum PerfettoTeHlExtraType {
PERFETTO_TE_HL_EXTRA_TYPE_PROTO_FIELDS = 17,
PERFETTO_TE_HL_EXTRA_TYPE_PROTO_TRACK = 18,
PERFETTO_TE_HL_EXTRA_TYPE_NESTED_TRACKS = 19,
PERFETTO_TE_HL_EXTRA_TYPE_NAMED_TRACK_STATIC = 20,
};

#define PERFETTO_TE_NAMED_TRACK_MAGIC 0xCD571EC5EAD37024ul

// An extra event parameter. Each type of parameter should embed this as its
// first member.
struct PerfettoTeHlExtra {
Expand Down
70 changes: 64 additions & 6 deletions include/perfetto/public/track_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,17 @@ static inline void PerfettoTeCounterTrackFillDesc(
struct perfetto_protos_TrackDescriptor* desc,
const char* name,
uint64_t parent_track_uuid,
uint64_t track_uuid) {
uint64_t track_uuid,
bool is_static) {
perfetto_protos_TrackDescriptor_set_uuid(desc, track_uuid);
if (parent_track_uuid) {
perfetto_protos_TrackDescriptor_set_parent_uuid(desc, parent_track_uuid);
}
perfetto_protos_TrackDescriptor_set_cstr_name(desc, name);
if (is_static) {
perfetto_protos_TrackDescriptor_set_cstr_static_name(desc, name);
} else {
perfetto_protos_TrackDescriptor_set_cstr_name(desc, name);
}
{
struct perfetto_protos_CounterDescriptor counter;
perfetto_protos_TrackDescriptor_begin_counter(desc, &counter);
Expand All @@ -168,13 +173,18 @@ static inline void PerfettoTeNamedTrackFillDesc(
const char* track_name,
uint64_t id,
uint64_t parent_track_uuid,
uint64_t track_uuid) {
uint64_t track_uuid,
bool is_static) {
(void)id;
perfetto_protos_TrackDescriptor_set_uuid(desc, track_uuid);
if (parent_track_uuid) {
perfetto_protos_TrackDescriptor_set_parent_uuid(desc, parent_track_uuid);
}
perfetto_protos_TrackDescriptor_set_cstr_name(desc, track_name);
if (is_static) {
perfetto_protos_TrackDescriptor_set_cstr_static_name(desc, track_name);
} else {
perfetto_protos_TrackDescriptor_set_cstr_name(desc, track_name);
}
}

// Registers a track named `name` with unique `id` and `parent_uuid` into
Expand All @@ -193,7 +203,32 @@ static inline void PerfettoTeNamedTrackRegister(

uuid = PerfettoTeNamedTrackUuid(name, id, parent_track_uuid);

PerfettoTeNamedTrackFillDesc(&desc, name, id, parent_track_uuid, uuid);
PerfettoTeNamedTrackFillDesc(&desc, name, id, parent_track_uuid, uuid, false);

track->impl.descriptor_size =
PerfettoStreamWriterGetWrittenSize(&writer.writer);
track->impl.descriptor = malloc(track->impl.descriptor_size);
track->impl.uuid = uuid;
PerfettoHeapBufferCopyInto(hb, &writer.writer, track->impl.descriptor,
track->impl.descriptor_size);
PerfettoHeapBufferDestroy(hb, &writer.writer);
}

static inline void PerfettoTeNamedTrackRegisterWithStaticName(
struct PerfettoTeRegisteredTrack* track,
const char* name,
uint64_t id,
uint64_t parent_track_uuid) {
uint64_t uuid;
// Build the TrackDescriptor protobuf message.
struct PerfettoPbMsgWriter writer;
struct PerfettoHeapBuffer* hb = PerfettoHeapBufferCreate(&writer.writer);
struct perfetto_protos_TrackDescriptor desc;
PerfettoPbMsgInit(&desc.msg, &writer);

uuid = PerfettoTeNamedTrackUuid(name, id, parent_track_uuid);

PerfettoTeNamedTrackFillDesc(&desc, name, id, parent_track_uuid, uuid, true);

track->impl.descriptor_size =
PerfettoStreamWriterGetWrittenSize(&writer.writer);
Expand All @@ -217,7 +252,30 @@ static inline void PerfettoTeCounterTrackRegister(

uuid = PerfettoTeCounterTrackUuid(name, parent_track_uuid);

PerfettoTeCounterTrackFillDesc(&desc, name, parent_track_uuid, uuid);
PerfettoTeCounterTrackFillDesc(&desc, name, parent_track_uuid, uuid, false);

track->impl.descriptor_size =
PerfettoStreamWriterGetWrittenSize(&writer.writer);
track->impl.descriptor = malloc(track->impl.descriptor_size);
track->impl.uuid = uuid;
PerfettoHeapBufferCopyInto(hb, &writer.writer, track->impl.descriptor,
track->impl.descriptor_size);
PerfettoHeapBufferDestroy(hb, &writer.writer);
}

static inline void PerfettoTeCounterTrackRegisterWithStaticName(
struct PerfettoTeRegisteredTrack* track,
const char* name,
uint64_t parent_track_uuid) {
uint64_t uuid;
struct PerfettoPbMsgWriter writer;
struct PerfettoHeapBuffer* hb = PerfettoHeapBufferCreate(&writer.writer);
struct perfetto_protos_TrackDescriptor desc;
PerfettoPbMsgInit(&desc.msg, &writer);

uuid = PerfettoTeCounterTrackUuid(name, parent_track_uuid);

PerfettoTeCounterTrackFillDesc(&desc, name, parent_track_uuid, uuid, true);

track->impl.descriptor_size =
PerfettoStreamWriterGetWrittenSize(&writer.writer);
Expand Down
19 changes: 18 additions & 1 deletion src/android_sdk/java/main/BUILD.gn
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("../../../../gn/perfetto.gni")
import("../../../../gn/perfetto_android_sdk.gni")

Expand All @@ -10,7 +24,10 @@ perfetto_android_library("perfetto_trace_lib") {
"dev/perfetto/sdk/PerfettoTrackEventBuilder.java",
"dev/perfetto/sdk/PerfettoTrackEventExtra.java",
]
deps = [ "../../jni:libperfetto_jni" ]
deps = [
"../../../../gn:error_prone_annotations",
"../../jni:libperfetto_jni",
]
manifest = "AndroidManifest.xml"
android_bp_java_target_name_suffix = "_java"
android_bp_copy_java_target_name_suffix = "_framework_java"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package dev.perfetto.sdk;

import com.google.errorprone.annotations.CompileTimeConstant;
import dev.perfetto.sdk.PerfettoNativeMemoryCleaner.AllocationStats;
import dev.perfetto.sdk.PerfettoTrace.Category;
import dev.perfetto.sdk.PerfettoTrackEventExtra.Arg;
Expand Down Expand Up @@ -357,6 +358,19 @@ public PerfettoTrackEventBuilder addTerminatingFlow(long id) {

/** Adds the events to a named track instead of the thread track where the event occurred. */
public PerfettoTrackEventBuilder usingNamedTrack(long id, String name, long parentUuid) {
return usingNamedTrack(id, name, parentUuid, false);
}

/**
* Adds the events to a named track with a static name (populated in field 10 of TrackDescriptor).
*/
public PerfettoTrackEventBuilder usingStaticNamedTrack(
long id, @CompileTimeConstant String name, long parentUuid) {
return usingNamedTrack(id, name, parentUuid, true);
}

private PerfettoTrackEventBuilder usingNamedTrack(
long id, String name, long parentUuid, boolean isStatic) {
if (!mIsCategoryEnabled) {
return this;
}
Expand All @@ -365,8 +379,8 @@ public PerfettoTrackEventBuilder usingNamedTrack(long id, String name, long pare
}

NamedTrack track = mObjectsCache.mNamedTrackCache.get(name.hashCode());
if (track == null || !track.getName().equals(name)) {
track = new NamedTrack(id, name, parentUuid, mNativeMemoryCleaner);
if (track == null || !track.getName().equals(name) || track.isStatic() != isStatic) {
track = new NamedTrack(id, name, parentUuid, isStatic, mNativeMemoryCleaner);
mObjectsCache.mNamedTrackCache.put(name.hashCode(), track);
}
addPerfettoPointerToExtra(track);
Expand All @@ -378,25 +392,51 @@ public PerfettoTrackEventBuilder usingNamedTrack(long id, String name, long pare
* occurred.
*/
public PerfettoTrackEventBuilder usingProcessNamedTrack(long id, String name) {
if (!mIsCategoryEnabled) {
return this;
}
return usingNamedTrack(id, name, PerfettoTrace.getProcessTrackUuid());
}

/**
* Adds the events to a process scoped named track with a static name instead of the thread track
* where the event occurred.
*/
public PerfettoTrackEventBuilder usingStaticProcessNamedTrack(
long id, @CompileTimeConstant String name) {
return usingStaticNamedTrack(id, name, PerfettoTrace.getProcessTrackUuid());
}

/**
* Adds the events to a thread scoped named track instead of the thread track where the event
* occurred.
*/
public PerfettoTrackEventBuilder usingThreadNamedTrack(long id, String name, long tid) {
if (!mIsCategoryEnabled) {
return this;
}
return usingNamedTrack(id, name, PerfettoTrace.getThreadTrackUuid(tid));
}

/**
* Adds the events to a thread scoped named track with a static name instead of the thread track
* where the event occurred.
*/
public PerfettoTrackEventBuilder usingStaticThreadNamedTrack(
long id, @CompileTimeConstant String name, long tid) {
return usingStaticNamedTrack(id, name, PerfettoTrace.getThreadTrackUuid(tid));
}

/** Adds the events to a counter track instead. This is required for setting counter values. */
public PerfettoTrackEventBuilder usingCounterTrack(long parentUuid, String name) {
return usingCounterTrack(parentUuid, name, false);
}

/**
* Adds the events to a counter track with a static name instead. This is required for setting
* counter values.
*/
public PerfettoTrackEventBuilder usingStaticCounterTrack(
long parentUuid, @CompileTimeConstant String name) {
return usingCounterTrack(parentUuid, name, true);
}

private PerfettoTrackEventBuilder usingCounterTrack(
long parentUuid, String name, boolean isStatic) {
if (!mIsCategoryEnabled) {
return this;
}
Expand All @@ -405,8 +445,8 @@ public PerfettoTrackEventBuilder usingCounterTrack(long parentUuid, String name)
}

CounterTrack track = mObjectsCache.mCounterTrackCache.get(name.hashCode());
if (track == null || !track.getName().equals(name)) {
track = new CounterTrack(name, parentUuid, mNativeMemoryCleaner);
if (track == null || !track.getName().equals(name) || track.isStatic() != isStatic) {
track = new CounterTrack(name, parentUuid, isStatic, mNativeMemoryCleaner);
mObjectsCache.mCounterTrackCache.put(name.hashCode(), track);
}
addPerfettoPointerToExtra(track);
Expand All @@ -418,23 +458,35 @@ public PerfettoTrackEventBuilder usingCounterTrack(long parentUuid, String name)
* values.
*/
public PerfettoTrackEventBuilder usingProcessCounterTrack(String name) {
if (!mIsCategoryEnabled) {
return this;
}
return usingCounterTrack(PerfettoTrace.getProcessTrackUuid(), name);
}

/**
* Adds the events to a process scoped counter track with a static name instead. This is required
* for setting counter values.
*/
public PerfettoTrackEventBuilder usingStaticProcessCounterTrack(
@CompileTimeConstant String name) {
return usingStaticCounterTrack(PerfettoTrace.getProcessTrackUuid(), name);
}

/**
* Adds the events to a thread scoped counter track instead. This is required for setting counter
* values.
*/
public PerfettoTrackEventBuilder usingThreadCounterTrack(long tid, String name) {
if (!mIsCategoryEnabled) {
return this;
}
return usingCounterTrack(PerfettoTrace.getThreadTrackUuid(tid), name);
}

/**
* Adds the events to a thread scoped counter track with a static name instead. This is required for
* setting counter values.
*/
public PerfettoTrackEventBuilder usingStaticThreadCounterTrack(
long tid, @CompileTimeConstant String name) {
return usingStaticCounterTrack(PerfettoTrace.getThreadTrackUuid(tid), name);
}

/** Sets a long counter value on the event. */
public PerfettoTrackEventBuilder setCounter(long val) {
if (!mIsCategoryEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,19 @@ static final class NamedTrack implements PerfettoPointer {
private final long mExtraPtr;
private final String mName;
private final long mId;

NamedTrack(long id, String name, long parentUuid, PerfettoNativeMemoryCleaner memoryCleaner) {
mPtr = native_init(id, name, parentUuid);
private final boolean mIsStatic;

NamedTrack(
long id,
String name,
long parentUuid,
boolean isStatic,
PerfettoNativeMemoryCleaner memoryCleaner) {
mPtr = native_init(id, name, parentUuid, isStatic);
mExtraPtr = native_get_extra_ptr(mPtr);
mName = name;
mId = id;
mIsStatic = isStatic;
memoryCleaner.registerNativeAllocation(this, mPtr, native_delete());
}

Expand All @@ -137,8 +144,12 @@ public String getName() {
return mName;
}

public boolean isStatic() {
return mIsStatic;
}

@FastNative
private static native long native_init(long id, String name, long parentUuid);
private static native long native_init(long id, String name, long parentUuid, boolean isStatic);

@CriticalNative
private static native long native_delete();
Expand All @@ -151,11 +162,17 @@ static final class CounterTrack implements PerfettoPointer {
private final long mPtr;
private final long mExtraPtr;
private final String mName;

CounterTrack(String name, long parentUuid, PerfettoNativeMemoryCleaner memoryCleaner) {
mPtr = native_init(name, parentUuid);
private final boolean mIsStatic;

CounterTrack(
String name,
long parentUuid,
boolean isStatic,
PerfettoNativeMemoryCleaner memoryCleaner) {
mPtr = native_init(name, parentUuid, isStatic);
mExtraPtr = native_get_extra_ptr(mPtr);
mName = name;
mIsStatic = isStatic;
memoryCleaner.registerNativeAllocation(this, mPtr, native_delete());
}

Expand All @@ -168,8 +185,12 @@ public String getName() {
return mName;
}

public boolean isStatic() {
return mIsStatic;
}

@FastNative
private static native long native_init(String name, long parentUuid);
private static native long native_init(String name, long parentUuid, boolean isStatic);

@CriticalNative
private static native long native_delete();
Expand Down
Loading
Loading