Register network tracing in system_server after boot

This runs the Perfetto and NetworkTraceHandler initialization on system
initialization along with the existing NetworkStatsService. The code is
run within the context of the system_server and is only executed on U or
later devices running eng or userdebug builds.

Bug: 246985031
Test: build & flash
Change-Id: I2b091e31c3ded54c0d7062d7d73f33ed47e0bf98

Former-commit-id: 1b71b7f70b
This commit is contained in:
Ryan Zuklie 2023-01-10 15:31:15 -08:00
parent 4d2de1605f
commit 4e71b2d1e2
5 changed files with 38 additions and 6 deletions

View File

@ -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) {

View File

@ -25,6 +25,7 @@
#include <perfetto/trace/android/network_trace.pbzero.h>
#include <perfetto/trace/profiling/profile_packet.pbzero.h>
#include <perfetto/tracing/platform.h>
#include <perfetto/tracing/tracing.h>
// 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(

View File

@ -36,6 +36,9 @@ class NetworkTraceHandler : public perfetto::DataSource<NetworkTraceHandler> {
// 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();

View File

@ -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

View File

@ -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();
}