Some fixes in StatsdStats, and add some unit tests
+ Add timestamp for when metric data is reported.
Test: statsd_test
Change-Id: Ief5ec5172feed4ec74b7422b77cf69ec8361ef2f
diff --git a/cmds/statsd/src/guardrail/StatsdStats.cpp b/cmds/statsd/src/guardrail/StatsdStats.cpp
index 815e03f..2ab1146 100644
--- a/cmds/statsd/src/guardrail/StatsdStats.cpp
+++ b/cmds/statsd/src/guardrail/StatsdStats.cpp
@@ -61,7 +61,7 @@
// TODO: add stats for pulled atoms.
StatsdStats::StatsdStats() {
mPushedAtomStats.resize(android::util::kMaxPushedAtomId + 1);
- mStartTime = time(nullptr);
+ mStartTimeSec = time(nullptr);
}
StatsdStats& StatsdStats::getInstance() {
@@ -107,6 +107,7 @@
mMetricsStats.erase(key);
mConditionStats.erase(key);
mIceBox.push_back(it->second);
+ mConfigStats.erase(it);
}
}
@@ -126,7 +127,7 @@
it->second.add_broadcast_sent_time_sec(time(nullptr));
}
-void StatsdStats::noteDataDrop(const ConfigKey& key) {
+void StatsdStats::noteDataDropped(const ConfigKey& key) {
lock_guard<std::mutex> lock(mLock);
auto it = mConfigStats.find(key);
if (it == mConfigStats.end()) {
@@ -137,6 +138,17 @@
it->second.add_data_drop_time_sec(time(nullptr));
}
+void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
+ lock_guard<std::mutex> lock(mLock);
+ auto it = mConfigStats.find(key);
+ if (it == mConfigStats.end()) {
+ ALOGE("Config key %s not found!", key.ToString().c_str());
+ return;
+ }
+
+ it->second.add_dump_report_time_sec(time(nullptr));
+}
+
void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const string& name, int size) {
lock_guard<std::mutex> lock(mLock);
// if name doesn't exist before, it will create the key with count 0.
@@ -164,7 +176,7 @@
void StatsdStats::noteAtomLogged(int atomId, int32_t timeSec) {
lock_guard<std::mutex> lock(mLock);
- if (timeSec < mStartTime) {
+ if (timeSec < mStartTimeSec) {
return;
}
@@ -183,7 +195,7 @@
void StatsdStats::resetInternalLocked() {
// Reset the historical data, but keep the active ConfigStats
- mStartTime = time(nullptr);
+ mStartTimeSec = time(nullptr);
mIceBox.clear();
mConditionStats.clear();
mMetricsStats.clear();
@@ -225,11 +237,11 @@
}
}
-void StatsdStats::dumpStats(std::vector<int8_t>* output, bool reset) {
+void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
lock_guard<std::mutex> lock(mLock);
if (DEBUG) {
- time_t t = time(nullptr);
+ time_t t = mStartTimeSec;
struct tm* tm = localtime(&t);
char timeBuffer[80];
strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %I:%M%p", tm);
@@ -237,7 +249,7 @@
VLOG("Stats collection start second: %s", timeBuffer);
}
ProtoOutputStream proto;
- proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTime);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_BEGIN_TIME, mStartTimeSec);
proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)time(nullptr));
VLOG("%lu Config in icebox: ", (unsigned long)mIceBox.size());
@@ -286,6 +298,10 @@
for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
VLOG("\tdata drop time: %d", dataDropTime);
}
+
+ for (const auto& dumpTime : configStats.dump_report_time_sec()) {
+ VLOG("\tdump report time: %d", dumpTime);
+ }
}
addSubStatsToConfig(pair.first, configStats);
diff --git a/cmds/statsd/src/guardrail/StatsdStats.h b/cmds/statsd/src/guardrail/StatsdStats.h
index 73ce279..6fd9e4b 100644
--- a/cmds/statsd/src/guardrail/StatsdStats.h
+++ b/cmds/statsd/src/guardrail/StatsdStats.h
@@ -18,6 +18,7 @@
#include "config/ConfigKey.h"
#include "frameworks/base/cmds/statsd/src/stats_log.pb.h"
+#include <gtest/gtest_prod.h>
#include <mutex>
#include <string>
#include <vector>
@@ -63,7 +64,14 @@
/**
* Report a config's metrics data has been dropped.
*/
- void noteDataDrop(const ConfigKey& key);
+ void noteDataDropped(const ConfigKey& key);
+
+ /**
+ * Report metrics data report has been sent.
+ *
+ * The report may be requested via StatsManager API, or through adb cmd.
+ */
+ void noteMetricsReportSent(const ConfigKey& key);
/**
* Report the size of output tuple of a condition.
@@ -114,14 +122,14 @@
*
* [reset]: whether to clear the historical stats after the call.
*/
- void dumpStats(std::vector<int8_t>* buffer, bool reset);
+ void dumpStats(std::vector<uint8_t>* buffer, bool reset);
private:
StatsdStats();
mutable std::mutex mLock;
- int32_t mStartTime;
+ int32_t mStartTimeSec;
// The stats about the configs that are still in use.
std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats;
@@ -153,6 +161,12 @@
void resetInternalLocked();
void addSubStatsToConfig(const ConfigKey& key, StatsdStatsReport_ConfigStats& configStats);
+
+ FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd);
+ FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd);
+ FRIEND_TEST(StatsdStatsTest, TestConfigRemove);
+ FRIEND_TEST(StatsdStatsTest, TestSubStats);
+ FRIEND_TEST(StatsdStatsTest, TestAtomLog);
};
} // namespace statsd