Merge "Report sparse per-iface stats using atrace." into main

This commit is contained in:
Ryan Zuklie
2023-11-02 20:40:00 +00:00
committed by Gerrit Code Review
2 changed files with 34 additions and 0 deletions

View File

@@ -25,9 +25,15 @@
#include <perfetto/tracing/platform.h>
#include <perfetto/tracing/tracing.h>
#include <unordered_map>
#include <unordered_set>
#include "netdbpf/BpfNetworkStats.h"
namespace android {
namespace bpf {
namespace internal {
using ::android::base::StringPrintf;
void NetworkTracePoller::PollAndSchedule(perfetto::base::TaskRunner* runner,
uint32_t poll_ms) {
@@ -116,6 +122,28 @@ bool NetworkTracePoller::Stop() {
return res.ok();
}
void NetworkTracePoller::TraceIfaces(const std::vector<PacketTrace>& packets) {
if (packets.empty()) return;
std::unordered_set<uint32_t> uniqueIfindex;
for (const PacketTrace& pkt : packets) {
uniqueIfindex.insert(pkt.ifindex);
}
for (uint32_t ifindex : uniqueIfindex) {
char ifname[IF_NAMESIZE] = {};
if (if_indextoname(ifindex, ifname) != ifname) continue;
StatsValue stats = {};
if (bpfGetIfIndexStats(ifindex, &stats) != 0) continue;
std::string rxTrack = StringPrintf("%s [%d] Rx Bytes", ifname, ifindex);
std::string txTrack = StringPrintf("%s [%d] Tx Bytes", ifname, ifindex);
ATRACE_INT64(rxTrack.c_str(), stats.rxBytes);
ATRACE_INT64(txTrack.c_str(), stats.txBytes);
}
}
bool NetworkTracePoller::ConsumeAll() {
std::scoped_lock<std::mutex> lock(mMutex);
return ConsumeAllLocked();
@@ -137,6 +165,7 @@ bool NetworkTracePoller::ConsumeAllLocked() {
ATRACE_INT("NetworkTracePackets", packets.size());
TraceIfaces(packets);
mCallback(packets);
return true;

View File

@@ -61,6 +61,11 @@ class NetworkTracePoller {
void PollAndSchedule(perfetto::base::TaskRunner* runner, uint32_t poll_ms);
bool ConsumeAllLocked() REQUIRES(mMutex);
// Record sparse iface stats via atrace. This queries the per-iface stats maps
// for any iface present in the vector of packets. This is inexact, but should
// have sufficient coverage given these are cumulative counters.
void TraceIfaces(const std::vector<PacketTrace>& packets) REQUIRES(mMutex);
std::mutex mMutex;
// Records the number of successfully started active sessions so that only the