diff --git a/service-t/jni/com_android_server_net_NetworkStatsService.cpp b/service-t/jni/com_android_server_net_NetworkStatsService.cpp index 39cbaf716f..af0b8d88ca 100644 --- a/service-t/jni/com_android_server_net_NetworkStatsService.cpp +++ b/service-t/jni/com_android_server_net_NetworkStatsService.cpp @@ -30,9 +30,11 @@ #include "bpf/BpfUtils.h" #include "netdbpf/BpfNetworkStats.h" +#include "netdbpf/NetworkTraceHandler.h" using android::bpf::bpfGetUidStats; using android::bpf::bpfGetIfaceStats; +using android::bpf::NetworkTraceHandler; namespace android { @@ -67,7 +69,7 @@ static uint64_t getStatsType(Stats* stats, StatsType type) { } } -static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) { +static jlong nativeGetTotalStat(JNIEnv* env, jclass clazz, jint type) { Stats stats = {}; if (bpfGetIfaceStats(NULL, &stats) == 0) { @@ -77,7 +79,7 @@ static jlong getTotalStat(JNIEnv* env, jclass clazz, jint type) { } } -static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) { +static jlong nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) { ScopedUtfChars iface8(env, iface); if (iface8.c_str() == NULL) { return UNKNOWN; @@ -92,7 +94,7 @@ static jlong getIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint type) { } } -static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) { +static jlong nativeGetUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) { Stats stats = {}; if (bpfGetUidStats(uid, &stats) == 0) { @@ -102,10 +104,15 @@ static jlong getUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) { } } +static void nativeInitNetworkTracing(JNIEnv* env, jclass clazz) { + NetworkTraceHandler::InitPerfettoTracing(); +} + static const JNINativeMethod gMethods[] = { - {"nativeGetTotalStat", "(I)J", (void*)getTotalStat}, - {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)getIfaceStat}, - {"nativeGetUidStat", "(II)J", (void*)getUidStat}, + {"nativeGetTotalStat", "(I)J", (void*)nativeGetTotalStat}, + {"nativeGetIfaceStat", "(Ljava/lang/String;I)J", (void*)nativeGetIfaceStat}, + {"nativeGetUidStat", "(II)J", (void*)nativeGetUidStat}, + {"nativeInitNetworkTracing", "()V", (void*)nativeInitNetworkTracing}, }; int register_android_server_net_NetworkStatsService(JNIEnv* env) { diff --git a/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp b/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp index a57dba3c98..4c37b8d31a 100644 --- a/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp +++ b/service-t/native/libs/libnetworkstats/NetworkTraceHandler.cpp @@ -25,6 +25,7 @@ #include #include #include +#include // Note: this is initializing state for a templated Perfetto type that resides // in the `perfetto` namespace. This must be defined in the global scope. @@ -45,6 +46,14 @@ void NetworkTraceHandler::RegisterDataSource() { NetworkTraceHandler::Register(dsd); } +// static +void NetworkTraceHandler::InitPerfettoTracing() { + perfetto::TracingInitArgs args = {}; + args.backends |= perfetto::kSystemBackend; + perfetto::Tracing::Initialize(args); + NetworkTraceHandler::RegisterDataSource(); +} + NetworkTraceHandler::NetworkTraceHandler() : NetworkTraceHandler([this](const PacketTrace& pkt) { NetworkTraceHandler::Trace( diff --git a/service-t/native/libs/libnetworkstats/include/netdbpf/NetworkTraceHandler.h b/service-t/native/libs/libnetworkstats/include/netdbpf/NetworkTraceHandler.h index d7c9432df8..c257aa090b 100644 --- a/service-t/native/libs/libnetworkstats/include/netdbpf/NetworkTraceHandler.h +++ b/service-t/native/libs/libnetworkstats/include/netdbpf/NetworkTraceHandler.h @@ -36,6 +36,9 @@ class NetworkTraceHandler : public perfetto::DataSource { // Registers this DataSource. static void RegisterDataSource(); + // Connects to the system Perfetto daemon and registers the trace handler. + static void InitPerfettoTracing(); + // Initialize with the default Perfetto callback. NetworkTraceHandler(); diff --git a/service-t/src/com/android/server/NetworkStatsServiceInitializer.java b/service-t/src/com/android/server/NetworkStatsServiceInitializer.java index 0ea126a487..82a4fbdfe4 100644 --- a/service-t/src/com/android/server/NetworkStatsServiceInitializer.java +++ b/service-t/src/com/android/server/NetworkStatsServiceInitializer.java @@ -18,6 +18,7 @@ package com.android.server; import android.content.Context; import android.net.TrafficStats; +import android.os.Build; import android.util.Log; import com.android.modules.utils.build.SdkLevel; @@ -46,6 +47,15 @@ public final class NetworkStatsServiceInitializer extends SystemService { /* allowIsolated= */ false); TrafficStats.init(getContext()); } + + // The following code registers the Perfetto Network Trace Handler on non-user builds. + // The enhanced tracing is intended to be used for debugging and diagnosing issues. This + // is conditional on the build type rather than `isDebuggable` to match the system_server + // selinux rules which only allow the Perfetto connection under the same circumstances. + if (SdkLevel.isAtLeastU() && !Build.TYPE.equals("user")) { + Log.i(TAG, "Initializing network tracing hooks"); + NetworkStatsService.nativeInitNetworkTracing(); + } } @Override diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index 5852a30d01..4eeaf6b7b2 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -3269,4 +3269,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { private static native long nativeGetTotalStat(int type); private static native long nativeGetIfaceStat(String iface, int type); private static native long nativeGetUidStat(int uid, int type); + + /** Initializes and registers the Perfetto Network Trace data source */ + public static native void nativeInitNetworkTracing(); }