GpuMem: refactor and add unittest for traverseGpuMemTotals
Use explicit std::function instead of lambda to make the interface more
stable and reliable. The trivial perf difference doesn't matter since
the data is only collected once upon tracing start and we just need to
ensure that the timestamp stays close enough to the map query.
Bug: 164194338
Test: atest gpuservice_unittest:GpuMemTest
Change-Id: I0b1e92214085eac5f75e7cdf4c00c017c520ff5b
diff --git a/services/gpuservice/gpumem/GpuMem.cpp b/services/gpuservice/gpumem/GpuMem.cpp
index 245edb8..c78322e 100644
--- a/services/gpuservice/gpumem/GpuMem.cpp
+++ b/services/gpuservice/gpumem/GpuMem.cpp
@@ -25,6 +25,7 @@
#include <libbpf_android.h>
#include <log/log.h>
#include <unistd.h>
+#include <utils/Timers.h>
#include <utils/Trace.h>
#include <unordered_map>
@@ -128,4 +129,24 @@
}
}
+void GpuMem::traverseGpuMemTotals(const std::function<void(int64_t ts, uint32_t gpuId, uint32_t pid,
+ uint64_t size)>& callback) {
+ auto res = mGpuMemTotalMap.getFirstKey();
+ if (!res.ok()) return;
+ uint64_t key = res.value();
+ while (true) {
+ uint32_t gpu_id = key >> 32;
+ uint32_t pid = key;
+
+ res = mGpuMemTotalMap.readValue(key);
+ if (!res.ok()) break;
+ uint64_t size = res.value();
+
+ callback(systemTime(), gpu_id, pid, size);
+ res = mGpuMemTotalMap.getNextKey(key);
+ if (!res.ok()) break;
+ key = res.value();
+ }
+}
+
} // namespace android