eliminate 'struct Stats' in favour of StatsValue
Bug: 294604315 Test: TreeHugger Signed-off-by: Maciej Żenczykowski <maze@google.com> Change-Id: Ib45a130e7fa36ad90334c00de637d523455e000e
This commit is contained in:
parent
91df4360f6
commit
4bcac5dadf
5 changed files with 27 additions and 44 deletions
|
|
@ -60,13 +60,6 @@ typedef struct {
|
|||
} IfaceValue;
|
||||
STRUCT_SIZE(IfaceValue, 16);
|
||||
|
||||
typedef struct {
|
||||
uint64_t rxBytes;
|
||||
uint64_t rxPackets;
|
||||
uint64_t txBytes;
|
||||
uint64_t txPackets;
|
||||
} Stats;
|
||||
|
||||
typedef struct {
|
||||
uint64_t timestampNs;
|
||||
uint32_t ifindex;
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ enum StatsType {
|
|||
TCP_TX_PACKETS = 5, // not supported, always returns UNKNOWN (-1)
|
||||
};
|
||||
|
||||
static uint64_t getStatsType(Stats* stats, StatsType type) {
|
||||
static uint64_t getStatsType(StatsValue* stats, StatsType type) {
|
||||
switch (type) {
|
||||
case RX_BYTES:
|
||||
return stats->rxBytes;
|
||||
|
|
@ -69,7 +69,7 @@ static uint64_t getStatsType(Stats* stats, StatsType type) {
|
|||
}
|
||||
|
||||
static jlong nativeGetTotalStat(JNIEnv* env, jclass clazz, jint type) {
|
||||
Stats stats = {};
|
||||
StatsValue stats = {};
|
||||
|
||||
if (bpfGetIfaceStats(NULL, &stats) == 0) {
|
||||
return getStatsType(&stats, (StatsType) type);
|
||||
|
|
@ -84,7 +84,7 @@ static jlong nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint t
|
|||
return UNKNOWN;
|
||||
}
|
||||
|
||||
Stats stats = {};
|
||||
StatsValue stats = {};
|
||||
|
||||
if (bpfGetIfaceStats(iface8.c_str(), &stats) == 0) {
|
||||
return getStatsType(&stats, (StatsType) type);
|
||||
|
|
@ -94,7 +94,7 @@ static jlong nativeGetIfaceStat(JNIEnv* env, jclass clazz, jstring iface, jint t
|
|||
}
|
||||
|
||||
static jlong nativeGetIfIndexStat(JNIEnv* env, jclass clazz, jint ifindex, jint type) {
|
||||
Stats stats = {};
|
||||
StatsValue stats = {};
|
||||
if (bpfGetIfIndexStats(ifindex, &stats) == 0) {
|
||||
return getStatsType(&stats, (StatsType) type);
|
||||
} else {
|
||||
|
|
@ -103,7 +103,7 @@ static jlong nativeGetIfIndexStat(JNIEnv* env, jclass clazz, jint ifindex, jint
|
|||
}
|
||||
|
||||
static jlong nativeGetUidStat(JNIEnv* env, jclass clazz, jint uid, jint type) {
|
||||
Stats stats = {};
|
||||
StatsValue stats = {};
|
||||
|
||||
if (bpfGetUidStats(uid, &stats) == 0) {
|
||||
return getStatsType(&stats, (StatsType) type);
|
||||
|
|
|
|||
|
|
@ -40,28 +40,23 @@ namespace bpf {
|
|||
|
||||
using base::Result;
|
||||
|
||||
int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
|
||||
int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& appUidStatsMap) {
|
||||
auto statsEntry = appUidStatsMap.readValue(uid);
|
||||
if (!statsEntry.ok()) {
|
||||
*stats = {};
|
||||
return (statsEntry.error().code() == ENOENT) ? 0 : -statsEntry.error().code();
|
||||
}
|
||||
*stats = {
|
||||
.rxPackets = statsEntry.value().rxPackets,
|
||||
.txPackets = statsEntry.value().txPackets,
|
||||
.rxBytes = statsEntry.value().rxBytes,
|
||||
.txBytes = statsEntry.value().txBytes,
|
||||
};
|
||||
*stats = statsEntry.value();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bpfGetUidStats(uid_t uid, Stats* stats) {
|
||||
int bpfGetUidStats(uid_t uid, StatsValue* stats) {
|
||||
static BpfMapRO<uint32_t, StatsValue> appUidStatsMap(APP_UID_STATS_MAP_PATH);
|
||||
return bpfGetUidStatsInternal(uid, stats, appUidStatsMap);
|
||||
}
|
||||
|
||||
int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
|
||||
int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
|
||||
const BpfMap<uint32_t, IfaceValue>& ifaceNameMap) {
|
||||
*stats = {};
|
||||
|
|
@ -91,29 +86,24 @@ int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
|
|||
return res.ok() ? 0 : -res.error().code();
|
||||
}
|
||||
|
||||
int bpfGetIfaceStats(const char* iface, Stats* stats) {
|
||||
int bpfGetIfaceStats(const char* iface, StatsValue* stats) {
|
||||
static BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH);
|
||||
static BpfMapRO<uint32_t, IfaceValue> ifaceIndexNameMap(IFACE_INDEX_NAME_MAP_PATH);
|
||||
return bpfGetIfaceStatsInternal(iface, stats, ifaceStatsMap, ifaceIndexNameMap);
|
||||
}
|
||||
|
||||
int bpfGetIfIndexStatsInternal(uint32_t ifindex, Stats* stats,
|
||||
int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap) {
|
||||
auto statsEntry = ifaceStatsMap.readValue(ifindex);
|
||||
if (!statsEntry.ok()) {
|
||||
*stats = {};
|
||||
return (statsEntry.error().code() == ENOENT) ? 0 : -statsEntry.error().code();
|
||||
}
|
||||
*stats = {
|
||||
.rxPackets = statsEntry.value().rxPackets,
|
||||
.txPackets = statsEntry.value().txPackets,
|
||||
.rxBytes = statsEntry.value().rxBytes,
|
||||
.txBytes = statsEntry.value().txBytes,
|
||||
};
|
||||
*stats = statsEntry.value();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bpfGetIfIndexStats(int ifindex, Stats* stats) {
|
||||
int bpfGetIfIndexStats(int ifindex, StatsValue* stats) {
|
||||
static BpfMapRO<uint32_t, StatsValue> ifaceStatsMap(IFACE_STATS_MAP_PATH);
|
||||
return bpfGetIfIndexStatsInternal(ifindex, stats, ifaceStatsMap);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ class BpfNetworkStatsHelperTest : public testing::Test {
|
|||
EXPECT_RESULT_OK(mFakeIfaceIndexNameMap.writeValue(ifaceIndex, iface, BPF_ANY));
|
||||
}
|
||||
|
||||
void expectStatsEqual(const StatsValue& target, const Stats& result) {
|
||||
void expectStatsEqual(const StatsValue& target, const StatsValue& result) {
|
||||
EXPECT_EQ(target.rxPackets, result.rxPackets);
|
||||
EXPECT_EQ(target.rxBytes, result.rxBytes);
|
||||
EXPECT_EQ(target.txPackets, result.txPackets);
|
||||
|
|
@ -194,7 +194,7 @@ TEST_F(BpfNetworkStatsHelperTest, TestUidStatsNoTraffic) {
|
|||
.txPackets = 0,
|
||||
.txBytes = 0,
|
||||
};
|
||||
Stats result1 = {};
|
||||
StatsValue result1 = {};
|
||||
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID1, &result1, mFakeAppUidStatsMap));
|
||||
expectStatsEqual(value1, result1);
|
||||
}
|
||||
|
|
@ -217,11 +217,11 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetUidStatsTotal) {
|
|||
};
|
||||
ASSERT_RESULT_OK(mFakeAppUidStatsMap.writeValue(TEST_UID1, value1, BPF_ANY));
|
||||
ASSERT_RESULT_OK(mFakeAppUidStatsMap.writeValue(TEST_UID2, value2, BPF_ANY));
|
||||
Stats result1 = {};
|
||||
StatsValue result1 = {};
|
||||
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID1, &result1, mFakeAppUidStatsMap));
|
||||
expectStatsEqual(value1, result1);
|
||||
|
||||
Stats result2 = {};
|
||||
StatsValue result2 = {};
|
||||
ASSERT_EQ(0, bpfGetUidStatsInternal(TEST_UID2, &result2, mFakeAppUidStatsMap));
|
||||
expectStatsEqual(value2, result2);
|
||||
std::vector<stats_line> lines;
|
||||
|
|
@ -255,15 +255,15 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetIfaceStatsInternal) {
|
|||
ifaceStatsKey = IFACE_INDEX3;
|
||||
EXPECT_RESULT_OK(mFakeIfaceStatsMap.writeValue(ifaceStatsKey, value1, BPF_ANY));
|
||||
|
||||
Stats result1 = {};
|
||||
StatsValue result1 = {};
|
||||
ASSERT_EQ(0, bpfGetIfaceStatsInternal(IFACE_NAME1, &result1, mFakeIfaceStatsMap,
|
||||
mFakeIfaceIndexNameMap));
|
||||
expectStatsEqual(value1, result1);
|
||||
Stats result2 = {};
|
||||
StatsValue result2 = {};
|
||||
ASSERT_EQ(0, bpfGetIfaceStatsInternal(IFACE_NAME2, &result2, mFakeIfaceStatsMap,
|
||||
mFakeIfaceIndexNameMap));
|
||||
expectStatsEqual(value2, result2);
|
||||
Stats totalResult = {};
|
||||
StatsValue totalResult = {};
|
||||
ASSERT_EQ(0, bpfGetIfaceStatsInternal(NULL, &totalResult, mFakeIfaceStatsMap,
|
||||
mFakeIfaceIndexNameMap));
|
||||
StatsValue totalValue = {
|
||||
|
|
@ -284,7 +284,7 @@ TEST_F(BpfNetworkStatsHelperTest, TestGetIfIndexStatsInternal) {
|
|||
};
|
||||
EXPECT_RESULT_OK(mFakeIfaceStatsMap.writeValue(IFACE_INDEX1, value, BPF_ANY));
|
||||
|
||||
Stats result = {};
|
||||
StatsValue result = {};
|
||||
ASSERT_EQ(0, bpfGetIfIndexStatsInternal(IFACE_INDEX1, &result, mFakeIfaceStatsMap));
|
||||
expectStatsEqual(value, result);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,14 +56,14 @@ bool operator==(const stats_line& lhs, const stats_line& rhs);
|
|||
bool operator<(const stats_line& lhs, const stats_line& rhs);
|
||||
|
||||
// For test only
|
||||
int bpfGetUidStatsInternal(uid_t uid, Stats* stats,
|
||||
int bpfGetUidStatsInternal(uid_t uid, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& appUidStatsMap);
|
||||
// For test only
|
||||
int bpfGetIfaceStatsInternal(const char* iface, Stats* stats,
|
||||
int bpfGetIfaceStatsInternal(const char* iface, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap,
|
||||
const BpfMap<uint32_t, IfaceValue>& ifaceNameMap);
|
||||
// For test only
|
||||
int bpfGetIfIndexStatsInternal(uint32_t ifindex, Stats* stats,
|
||||
int bpfGetIfIndexStatsInternal(uint32_t ifindex, StatsValue* stats,
|
||||
const BpfMap<uint32_t, StatsValue>& ifaceStatsMap);
|
||||
// For test only
|
||||
int parseBpfNetworkStatsDetailInternal(std::vector<stats_line>& lines,
|
||||
|
|
@ -113,9 +113,9 @@ int parseBpfNetworkStatsDevInternal(std::vector<stats_line>& lines,
|
|||
const BpfMap<uint32_t, StatsValue>& statsMap,
|
||||
const BpfMap<uint32_t, IfaceValue>& ifaceMap);
|
||||
|
||||
int bpfGetUidStats(uid_t uid, Stats* stats);
|
||||
int bpfGetIfaceStats(const char* iface, Stats* stats);
|
||||
int bpfGetIfIndexStats(int ifindex, Stats* stats);
|
||||
int bpfGetUidStats(uid_t uid, StatsValue* stats);
|
||||
int bpfGetIfaceStats(const char* iface, StatsValue* stats);
|
||||
int bpfGetIfIndexStats(int ifindex, StatsValue* stats);
|
||||
int parseBpfNetworkStatsDetail(std::vector<stats_line>* lines);
|
||||
|
||||
int parseBpfNetworkStatsDev(std::vector<stats_line>* lines);
|
||||
|
|
|
|||
Loading…
Reference in a new issue