Reduce statsd binary size from 730KB-> 664KB
1. StatsdStats does not use the proto object in memory anymore.
2. lite_static -> lite
3. don't use sstream
Bug: 72129300
Test: statsd_test
Change-Id: I8a5adaf222d4d5034e8bf115215fb6dd5f042cac
diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk
index 7f76ab1..7720112 100644
--- a/cmds/statsd/Android.mk
+++ b/cmds/statsd/Android.mk
@@ -69,7 +69,6 @@
src/subscriber/IncidentdReporter.cpp \
src/subscriber/SubscriberReporter.cpp \
src/HashableDimensionKey.cpp \
- src/guardrail/MemoryLeakTrackUtil.cpp \
src/guardrail/StatsdStats.cpp
statsd_common_c_includes := \
@@ -102,8 +101,7 @@
android.hardware.health@2.0 \
android.hardware.power@1.0 \
android.hardware.power@1.1 \
- android.hardware.thermal@1.0 \
- libmemunreachable
+ android.hardware.thermal@1.0
# =========
# statsd
@@ -131,7 +129,7 @@
LOCAL_CFLAGS += \
-Os
endif
-LOCAL_PROTOC_OPTIMIZE_TYPE := lite-static
+LOCAL_PROTOC_OPTIMIZE_TYPE := lite
LOCAL_AIDL_INCLUDES := $(statsd_common_aidl_includes)
LOCAL_C_INCLUDES += $(statsd_common_c_includes)
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index ee4f434..280081e 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -22,7 +22,6 @@
#include "android-base/stringprintf.h"
#include "config/ConfigKey.h"
#include "config/ConfigManager.h"
-#include "guardrail/MemoryLeakTrackUtil.h"
#include "guardrail/StatsdStats.h"
#include "storage/StorageManager.h"
#include "subscriber/SubscriberReporter.h"
@@ -638,9 +637,7 @@
}
status_t StatsService::cmd_dump_memory_info(FILE* out) {
- std::string s = dumpMemInfo(100);
- fprintf(out, "Memory Info\n");
- fprintf(out, "%s", s.c_str());
+ fprintf(out, "meminfo not available.\n");
return NO_ERROR;
}
diff --git a/cmds/statsd/src/config/ConfigKey.cpp b/cmds/statsd/src/config/ConfigKey.cpp
index d791f86..4a2bd27 100644
--- a/cmds/statsd/src/config/ConfigKey.cpp
+++ b/cmds/statsd/src/config/ConfigKey.cpp
@@ -16,14 +16,10 @@
#include "config/ConfigKey.h"
-#include <sstream>
-
namespace android {
namespace os {
namespace statsd {
-using std::ostringstream;
-
ConfigKey::ConfigKey() {
}
@@ -37,9 +33,9 @@
}
string ConfigKey::ToString() const {
- ostringstream out;
- out << '(' << mUid << ',' << mId << ')';
- return out.str();
+ string s;
+ s += "(" + std::to_string(mUid) + " " + std::to_string(mId) + ")";
+ return s;
}
diff --git a/cmds/statsd/src/config/ConfigKey.h b/cmds/statsd/src/config/ConfigKey.h
index 3ad0eed..dc79519 100644
--- a/cmds/statsd/src/config/ConfigKey.h
+++ b/cmds/statsd/src/config/ConfigKey.h
@@ -18,8 +18,6 @@
#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h"
-#include <functional>
-#include <iostream>
#include <string>
namespace android {
@@ -27,7 +25,6 @@
namespace statsd {
using std::hash;
-using std::ostream;
using std::string;
/**
@@ -68,10 +65,6 @@
int mUid;
};
-inline ostream& operator<<(ostream& os, const ConfigKey& config) {
- return os << config.ToString();
-}
-
int64_t StrToInt64(const string& str);
} // namespace statsd
diff --git a/cmds/statsd/src/guardrail/StatsdStats.cpp b/cmds/statsd/src/guardrail/StatsdStats.cpp
index 0881d44..22faaa1 100644
--- a/cmds/statsd/src/guardrail/StatsdStats.cpp
+++ b/cmds/statsd/src/guardrail/StatsdStats.cpp
@@ -36,6 +36,7 @@
using android::util::ProtoOutputStream;
using std::lock_guard;
using std::map;
+using std::shared_ptr;
using std::string;
using std::vector;
@@ -58,6 +59,38 @@
const int FIELD_ID_LOGGER_STATS_TIME = 1;
const int FIELD_ID_LOGGER_STATS_ERROR_CODE = 2;
+const int FIELD_ID_CONFIG_STATS_UID = 1;
+const int FIELD_ID_CONFIG_STATS_ID = 2;
+const int FIELD_ID_CONFIG_STATS_CREATION = 3;
+const int FIELD_ID_CONFIG_STATS_DELETION = 4;
+const int FIELD_ID_CONFIG_STATS_METRIC_COUNT = 5;
+const int FIELD_ID_CONFIG_STATS_CONDITION_COUNT = 6;
+const int FIELD_ID_CONFIG_STATS_MATCHER_COUNT = 7;
+const int FIELD_ID_CONFIG_STATS_ALERT_COUNT = 8;
+const int FIELD_ID_CONFIG_STATS_VALID = 9;
+const int FIELD_ID_CONFIG_STATS_BROADCAST = 10;
+const int FIELD_ID_CONFIG_STATS_DATA_DROP = 11;
+const int FIELD_ID_CONFIG_STATS_DUMP_REPORT = 12;
+const int FIELD_ID_CONFIG_STATS_MATCHER_STATS = 13;
+const int FIELD_ID_CONFIG_STATS_CONDITION_STATS = 14;
+const int FIELD_ID_CONFIG_STATS_METRIC_STATS = 15;
+const int FIELD_ID_CONFIG_STATS_ALERT_STATS = 16;
+
+const int FIELD_ID_MATCHER_STATS_ID = 1;
+const int FIELD_ID_MATCHER_STATS_COUNT = 2;
+const int FIELD_ID_CONDITION_STATS_ID = 1;
+const int FIELD_ID_CONDITION_STATS_COUNT = 2;
+const int FIELD_ID_METRIC_STATS_ID = 1;
+const int FIELD_ID_METRIC_STATS_COUNT = 2;
+const int FIELD_ID_ALERT_STATS_ID = 1;
+const int FIELD_ID_ALERT_STATS_COUNT = 2;
+
+const int FIELD_ID_UID_MAP_SNAPSHOTS = 1;
+const int FIELD_ID_UID_MAP_CHANGES = 2;
+const int FIELD_ID_UID_MAP_BYTES_USED = 3;
+const int FIELD_ID_UID_MAP_DROPPED_SNAPSHOTS = 4;
+const int FIELD_ID_UID_MAP_DROPPED_CHANGES = 5;
+
std::map<int, long> StatsdStats::kPullerCooldownMap = {
{android::util::KERNEL_WAKELOCK, 1},
{android::util::WIFI_BYTES_TRANSFER, 1},
@@ -81,7 +114,7 @@
return statsInstance;
}
-void StatsdStats::addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats) {
+void StatsdStats::addToIceBoxLocked(shared_ptr<ConfigStats>& stats) {
// The size of mIceBox grows strictly by one at a time. It won't be > kMaxIceBoxSize.
if (mIceBox.size() == kMaxIceBoxSize) {
mIceBox.pop_front();
@@ -97,20 +130,20 @@
// If there is an existing config for the same key, icebox the old config.
noteConfigRemovedInternalLocked(key);
- StatsdStatsReport_ConfigStats configStats;
- configStats.set_uid(key.GetUid());
- configStats.set_id(key.GetId());
- configStats.set_creation_time_sec(nowTimeSec);
- configStats.set_metric_count(metricsCount);
- configStats.set_condition_count(conditionsCount);
- configStats.set_matcher_count(matchersCount);
- configStats.set_alert_count(alertsCount);
- configStats.set_is_valid(isValid);
+ shared_ptr<ConfigStats> configStats = std::make_shared<ConfigStats>();
+ configStats->uid = key.GetUid();
+ configStats->id = key.GetId();
+ configStats->creation_time_sec = nowTimeSec;
+ configStats->metric_count = metricsCount;
+ configStats->condition_count = conditionsCount;
+ configStats->matcher_count = matchersCount;
+ configStats->alert_count = alertsCount;
+ configStats->is_valid = isValid;
if (isValid) {
mConfigStats[key] = configStats;
} else {
- configStats.set_deletion_time_sec(nowTimeSec);
+ configStats->deletion_time_sec = nowTimeSec;
addToIceBoxLocked(configStats);
}
}
@@ -119,14 +152,7 @@
auto it = mConfigStats.find(key);
if (it != mConfigStats.end()) {
int32_t nowTimeSec = getWallClockSec();
- it->second.set_deletion_time_sec(nowTimeSec);
- // Add condition stats, metrics stats, matcher stats, alert stats
- addSubStatsToConfigLocked(key, it->second);
- // Remove them after they are added to the config stats.
- mMatcherStats.erase(key);
- mMetricsStats.erase(key);
- mAlertStats.erase(key);
- mConditionStats.erase(key);
+ it->second->deletion_time_sec = nowTimeSec;
addToIceBoxLocked(it->second);
mConfigStats.erase(it);
}
@@ -148,12 +174,10 @@
ALOGE("Config key %s not found!", key.ToString().c_str());
return;
}
- if (it->second.broadcast_sent_time_sec_size() >= kMaxTimestampCount) {
- auto timestampList = it->second.mutable_broadcast_sent_time_sec();
- // This is O(N) operation. It shouldn't happen often, and N is only 20.
- timestampList->erase(timestampList->begin());
+ if (it->second->broadcast_sent_time_sec.size() == kMaxTimestampCount) {
+ it->second->broadcast_sent_time_sec.pop_front();
}
- it->second.add_broadcast_sent_time_sec(timeSec);
+ it->second->broadcast_sent_time_sec.push_back(timeSec);
}
void StatsdStats::noteDataDropped(const ConfigKey& key) {
@@ -167,12 +191,10 @@
ALOGE("Config key %s not found!", key.ToString().c_str());
return;
}
- if (it->second.data_drop_time_sec_size() >= kMaxTimestampCount) {
- auto timestampList = it->second.mutable_data_drop_time_sec();
- // This is O(N) operation. It shouldn't happen often, and N is only 20.
- timestampList->erase(timestampList->begin());
+ if (it->second->data_drop_time_sec.size() == kMaxTimestampCount) {
+ it->second->data_drop_time_sec.pop_front();
}
- it->second.add_data_drop_time_sec(timeSec);
+ it->second->data_drop_time_sec.push_back(timeSec);
}
void StatsdStats::noteMetricsReportSent(const ConfigKey& key) {
@@ -186,39 +208,42 @@
ALOGE("Config key %s not found!", key.ToString().c_str());
return;
}
- if (it->second.dump_report_time_sec_size() >= kMaxTimestampCount) {
- auto timestampList = it->second.mutable_dump_report_time_sec();
- // This is O(N) operation. It shouldn't happen often, and N is only 20.
- timestampList->erase(timestampList->begin());
+ if (it->second->dump_report_time_sec.size() == kMaxTimestampCount) {
+ it->second->dump_report_time_sec.pop_front();
}
- it->second.add_dump_report_time_sec(timeSec);
+ it->second->dump_report_time_sec.push_back(timeSec);
}
void StatsdStats::noteUidMapDropped(int snapshots, int deltas) {
lock_guard<std::mutex> lock(mLock);
- mUidMapStats.set_dropped_snapshots(mUidMapStats.dropped_snapshots() + snapshots);
- mUidMapStats.set_dropped_changes(mUidMapStats.dropped_changes() + deltas);
+ mUidMapStats.dropped_snapshots += mUidMapStats.dropped_snapshots + snapshots;
+ mUidMapStats.dropped_changes += mUidMapStats.dropped_changes + deltas;
}
void StatsdStats::setUidMapSnapshots(int snapshots) {
lock_guard<std::mutex> lock(mLock);
- mUidMapStats.set_snapshots(snapshots);
+ mUidMapStats.snapshots = snapshots;
}
void StatsdStats::setUidMapChanges(int changes) {
lock_guard<std::mutex> lock(mLock);
- mUidMapStats.set_changes(changes);
+ mUidMapStats.changes = changes;
}
void StatsdStats::setCurrentUidMapMemory(int bytes) {
lock_guard<std::mutex> lock(mLock);
- mUidMapStats.set_bytes_used(bytes);
+ mUidMapStats.bytes_used = bytes;
}
void StatsdStats::noteConditionDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
lock_guard<std::mutex> lock(mLock);
// if name doesn't exist before, it will create the key with count 0.
- auto& conditionSizeMap = mConditionStats[key];
+ auto statsIt = mConfigStats.find(key);
+ if (statsIt == mConfigStats.end()) {
+ return;
+ }
+
+ auto& conditionSizeMap = statsIt->second->condition_stats;
if (size > conditionSizeMap[id]) {
conditionSizeMap[id] = size;
}
@@ -227,7 +252,11 @@
void StatsdStats::noteMetricDimensionSize(const ConfigKey& key, const int64_t& id, int size) {
lock_guard<std::mutex> lock(mLock);
// if name doesn't exist before, it will create the key with count 0.
- auto& metricsDimensionMap = mMetricsStats[key];
+ auto statsIt = mConfigStats.find(key);
+ if (statsIt == mConfigStats.end()) {
+ return;
+ }
+ auto& metricsDimensionMap = statsIt->second->metric_stats;
if (size > metricsDimensionMap[id]) {
metricsDimensionMap[id] = size;
}
@@ -235,14 +264,21 @@
void StatsdStats::noteMatcherMatched(const ConfigKey& key, const int64_t& id) {
lock_guard<std::mutex> lock(mLock);
- auto& matcherStats = mMatcherStats[key];
- matcherStats[id]++;
+
+ auto statsIt = mConfigStats.find(key);
+ if (statsIt == mConfigStats.end()) {
+ return;
+ }
+ statsIt->second->matcher_stats[id]++;
}
void StatsdStats::noteAnomalyDeclared(const ConfigKey& key, const int64_t& id) {
lock_guard<std::mutex> lock(mLock);
- auto& alertStats = mAlertStats[key];
- alertStats[id]++;
+ auto statsIt = mConfigStats.find(key);
+ if (statsIt == mConfigStats.end()) {
+ return;
+ }
+ statsIt->second->alert_stats[id]++;
}
void StatsdStats::noteRegisteredAnomalyAlarmChanged() {
@@ -299,69 +335,18 @@
// Reset the historical data, but keep the active ConfigStats
mStartTimeSec = getWallClockSec();
mIceBox.clear();
- mConditionStats.clear();
- mMetricsStats.clear();
std::fill(mPushedAtomStats.begin(), mPushedAtomStats.end(), 0);
- mAlertStats.clear();
mAnomalyAlarmRegisteredStats = 0;
mPeriodicAlarmRegisteredStats = 0;
- mMatcherStats.clear();
mLoggerErrors.clear();
for (auto& config : mConfigStats) {
- config.second.clear_broadcast_sent_time_sec();
- config.second.clear_data_drop_time_sec();
- config.second.clear_dump_report_time_sec();
- config.second.clear_matcher_stats();
- config.second.clear_condition_stats();
- config.second.clear_metric_stats();
- config.second.clear_alert_stats();
- }
-}
-
-void StatsdStats::addSubStatsToConfigLocked(const ConfigKey& key,
- StatsdStatsReport_ConfigStats& configStats) {
- // Add matcher stats
- if (mMatcherStats.find(key) != mMatcherStats.end()) {
- const auto& matcherStats = mMatcherStats[key];
- for (const auto& stats : matcherStats) {
- auto output = configStats.add_matcher_stats();
- output->set_id(stats.first);
- output->set_matched_times(stats.second);
- VLOG("matcher %lld matched %d times",
- (long long)stats.first, stats.second);
- }
- }
- // Add condition stats
- if (mConditionStats.find(key) != mConditionStats.end()) {
- const auto& conditionStats = mConditionStats[key];
- for (const auto& stats : conditionStats) {
- auto output = configStats.add_condition_stats();
- output->set_id(stats.first);
- output->set_max_tuple_counts(stats.second);
- VLOG("condition %lld max output tuple size %d",
- (long long)stats.first, stats.second);
- }
- }
- // Add metrics stats
- if (mMetricsStats.find(key) != mMetricsStats.end()) {
- const auto& conditionStats = mMetricsStats[key];
- for (const auto& stats : conditionStats) {
- auto output = configStats.add_metric_stats();
- output->set_id(stats.first);
- output->set_max_tuple_counts(stats.second);
- VLOG("metrics %lld max output tuple size %d",
- (long long)stats.first, stats.second);
- }
- }
- // Add anomaly detection alert stats
- if (mAlertStats.find(key) != mAlertStats.end()) {
- const auto& alertStats = mAlertStats[key];
- for (const auto& stats : alertStats) {
- auto output = configStats.add_alert_stats();
- output->set_id(stats.first);
- output->set_alerted_times(stats.second);
- VLOG("alert %lld declared %d times", (long long)stats.first, stats.second);
- }
+ config.second->broadcast_sent_time_sec.clear();
+ config.second->data_drop_time_sec.clear();
+ config.second->dump_report_time_sec.clear();
+ config.second->matcher_stats.clear();
+ config.second->condition_stats.clear();
+ config.second->metric_stats.clear();
+ config.second->alert_stats.clear();
}
}
@@ -377,78 +362,57 @@
fprintf(out,
"Config {%d_%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
"#matcher=%d, #alert=%d, valid=%d\n",
- configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
- configStats.deletion_time_sec(), configStats.metric_count(),
- configStats.condition_count(), configStats.matcher_count(),
- configStats.alert_count(), configStats.is_valid());
+ configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
+ configStats->deletion_time_sec, configStats->metric_count,
+ configStats->condition_count, configStats->matcher_count, configStats->alert_count,
+ configStats->is_valid);
- for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
+ for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
}
- for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
+ for (const auto& dataDropTime : configStats->data_drop_time_sec) {
fprintf(out, "\tdata drop time: %d\n", dataDropTime);
}
}
fprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size());
for (auto& pair : mConfigStats) {
- auto& key = pair.first;
auto& configStats = pair.second;
-
fprintf(out,
"Config {%d-%lld}: creation=%d, deletion=%d, #metric=%d, #condition=%d, "
"#matcher=%d, #alert=%d, valid=%d\n",
- configStats.uid(), (long long)configStats.id(), configStats.creation_time_sec(),
- configStats.deletion_time_sec(), configStats.metric_count(),
- configStats.condition_count(), configStats.matcher_count(),
- configStats.alert_count(), configStats.is_valid());
- for (const auto& broadcastTime : configStats.broadcast_sent_time_sec()) {
+ configStats->uid, (long long)configStats->id, configStats->creation_time_sec,
+ configStats->deletion_time_sec, configStats->metric_count,
+ configStats->condition_count, configStats->matcher_count, configStats->alert_count,
+ configStats->is_valid);
+ for (const auto& broadcastTime : configStats->broadcast_sent_time_sec) {
fprintf(out, "\tbroadcast time: %d\n", broadcastTime);
}
- for (const auto& dataDropTime : configStats.data_drop_time_sec()) {
+ for (const auto& dataDropTime : configStats->data_drop_time_sec) {
fprintf(out, "\tdata drop time: %d\n", dataDropTime);
}
- for (const auto& dumpTime : configStats.dump_report_time_sec()) {
+ for (const auto& dumpTime : configStats->dump_report_time_sec) {
fprintf(out, "\tdump report time: %d\n", dumpTime);
}
- // Add matcher stats
- auto matcherIt = mMatcherStats.find(key);
- if (matcherIt != mMatcherStats.end()) {
- const auto& matcherStats = matcherIt->second;
- for (const auto& stats : matcherStats) {
- fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first,
- stats.second);
- }
+ for (const auto& stats : pair.second->matcher_stats) {
+ fprintf(out, "matcher %lld matched %d times\n", (long long)stats.first, stats.second);
}
- // Add condition stats
- auto conditionIt = mConditionStats.find(key);
- if (conditionIt != mConditionStats.end()) {
- const auto& conditionStats = conditionIt->second;
- for (const auto& stats : conditionStats) {
- fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
- stats.second);
- }
+
+ for (const auto& stats : pair.second->condition_stats) {
+ fprintf(out, "condition %lld max output tuple size %d\n", (long long)stats.first,
+ stats.second);
}
- // Add metrics stats
- auto metricIt = mMetricsStats.find(key);
- if (metricIt != mMetricsStats.end()) {
- const auto& conditionStats = metricIt->second;
- for (const auto& stats : conditionStats) {
- fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
- stats.second);
- }
+
+ for (const auto& stats : pair.second->condition_stats) {
+ fprintf(out, "metrics %lld max output tuple size %d\n", (long long)stats.first,
+ stats.second);
}
- // Add anomaly detection alert stats
- auto alertIt = mAlertStats.find(key);
- if (alertIt != mAlertStats.end()) {
- const auto& alertStats = alertIt->second;
- for (const auto& stats : alertStats) {
- fprintf(out, "alert %lld declared %d times\n", (long long)stats.first,
- stats.second);
- }
+
+ for (const auto& stats : pair.second->alert_stats) {
+ fprintf(out, "alert %lld declared %d times\n", (long long)stats.first, stats.second);
}
}
fprintf(out, "********Pushed Atom stats***********\n");
@@ -462,7 +426,7 @@
fprintf(out, "********Pulled Atom stats***********\n");
for (const auto& pair : mPulledAtomStats) {
fprintf(out, "Atom %d->%ld, %ld, %ld\n", (int)pair.first, (long)pair.second.totalPull,
- (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
+ (long)pair.second.totalPullFromCache, (long)pair.second.minPullIntervalSec);
}
if (mAnomalyAlarmRegisteredStats > 0) {
@@ -478,8 +442,8 @@
fprintf(out,
"UID map stats: bytes=%d, snapshots=%d, changes=%d, snapshots lost=%d, changes "
"lost=%d\n",
- mUidMapStats.bytes_used(), mUidMapStats.snapshots(), mUidMapStats.changes(),
- mUidMapStats.dropped_snapshots(), mUidMapStats.dropped_changes());
+ mUidMapStats.bytes_used, mUidMapStats.snapshots, mUidMapStats.changes,
+ mUidMapStats.dropped_snapshots, mUidMapStats.dropped_changes);
for (const auto& error : mLoggerErrors) {
time_t error_time = error.first;
@@ -490,6 +454,73 @@
}
}
+void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* proto) {
+ long long token =
+ proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_UID, configStats.uid);
+ proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_ID, (long long)configStats.id);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CREATION, configStats.creation_time_sec);
+ if (configStats.deletion_time_sec != 0) {
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DELETION,
+ configStats.deletion_time_sec);
+ }
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_METRIC_COUNT, configStats.metric_count);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_CONDITION_COUNT,
+ configStats.condition_count);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_MATCHER_COUNT, configStats.matcher_count);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_ALERT_COUNT, configStats.alert_count);
+ proto->write(FIELD_TYPE_BOOL | FIELD_ID_CONFIG_STATS_VALID, configStats.is_valid);
+
+ for (const auto& broadcast : configStats.broadcast_sent_time_sec) {
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_BROADCAST | FIELD_COUNT_REPEATED,
+ broadcast);
+ }
+
+ for (const auto& drop : configStats.data_drop_time_sec) {
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DATA_DROP | FIELD_COUNT_REPEATED,
+ drop);
+ }
+
+ for (const auto& dump : configStats.dump_report_time_sec) {
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONFIG_STATS_DUMP_REPORT | FIELD_COUNT_REPEATED,
+ dump);
+ }
+
+ for (const auto& pair : configStats.matcher_stats) {
+ long long tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
+ FIELD_ID_CONFIG_STATS_MATCHER_STATS);
+ proto->write(FIELD_TYPE_INT64 | FIELD_ID_MATCHER_STATS_ID, (long long)pair.first);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_MATCHER_STATS_COUNT, pair.second);
+ proto->end(tmpToken);
+ }
+
+ for (const auto& pair : configStats.condition_stats) {
+ long long tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
+ FIELD_ID_CONFIG_STATS_CONDITION_STATS);
+ proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONDITION_STATS_ID, (long long)pair.first);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_CONDITION_STATS_COUNT, pair.second);
+ proto->end(tmpToken);
+ }
+
+ for (const auto& pair : configStats.metric_stats) {
+ long long tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
+ FIELD_ID_CONFIG_STATS_METRIC_STATS);
+ proto->write(FIELD_TYPE_INT64 | FIELD_ID_METRIC_STATS_ID, (long long)pair.first);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_METRIC_STATS_COUNT, pair.second);
+ proto->end(tmpToken);
+ }
+
+ for (const auto& pair : configStats.alert_stats) {
+ long long tmpToken = proto->start(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED |
+ FIELD_ID_CONFIG_STATS_ALERT_STATS);
+ proto->write(FIELD_TYPE_INT64 | FIELD_ID_ALERT_STATS_ID, (long long)pair.first);
+ proto->write(FIELD_TYPE_INT32 | FIELD_ID_ALERT_STATS_COUNT, pair.second);
+ proto->end(tmpToken);
+ }
+
+ proto->end(token);
+}
+
void StatsdStats::dumpStats(std::vector<uint8_t>* output, bool reset) {
lock_guard<std::mutex> lock(mLock);
@@ -498,28 +529,11 @@
proto.write(FIELD_TYPE_INT32 | FIELD_ID_END_TIME, (int32_t)getWallClockSec());
for (const auto& configStats : mIceBox) {
- const int numBytes = configStats.ByteSize();
- vector<char> buffer(numBytes);
- configStats.SerializeToArray(&buffer[0], numBytes);
- proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
- buffer.size());
+ addConfigStatsToProto(*configStats, &proto);
}
for (auto& pair : mConfigStats) {
- auto& configStats = pair.second;
- addSubStatsToConfigLocked(pair.first, configStats);
-
- const int numBytes = configStats.ByteSize();
- vector<char> buffer(numBytes);
- configStats.SerializeToArray(&buffer[0], numBytes);
- proto.write(FIELD_TYPE_MESSAGE | FIELD_COUNT_REPEATED | FIELD_ID_CONFIG_STATS, &buffer[0],
- buffer.size());
- // reset the sub stats, the source of truth is in the individual map
- // they will be repopulated when dumpStats() is called again.
- configStats.clear_matcher_stats();
- configStats.clear_condition_stats();
- configStats.clear_metric_stats();
- configStats.clear_alert_stats();
+ addConfigStatsToProto(*(pair.second), &proto);
}
const size_t atomCounts = mPushedAtomStats.size();
@@ -551,10 +565,14 @@
proto.end(token);
}
- const int numBytes = mUidMapStats.ByteSize();
- vector<char> buffer(numBytes);
- mUidMapStats.SerializeToArray(&buffer[0], numBytes);
- proto.write(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS, &buffer[0], buffer.size());
+ long long uidMapToken = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_UIDMAP_STATS);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_SNAPSHOTS, mUidMapStats.snapshots);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_CHANGES, mUidMapStats.changes);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_BYTES_USED, mUidMapStats.bytes_used);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_SNAPSHOTS,
+ mUidMapStats.dropped_snapshots);
+ proto.write(FIELD_TYPE_INT32 | FIELD_ID_UID_MAP_DROPPED_CHANGES, mUidMapStats.dropped_changes);
+ proto.end(uidMapToken);
for (const auto& error : mLoggerErrors) {
long long token = proto.start(FIELD_TYPE_MESSAGE | FIELD_ID_LOGGER_ERROR_STATS |
diff --git a/cmds/statsd/src/guardrail/StatsdStats.h b/cmds/statsd/src/guardrail/StatsdStats.h
index 24ac688..c3f4013 100644
--- a/cmds/statsd/src/guardrail/StatsdStats.h
+++ b/cmds/statsd/src/guardrail/StatsdStats.h
@@ -30,8 +30,49 @@
namespace os {
namespace statsd {
+struct ConfigStats {
+ int32_t uid;
+ int64_t id;
+ int32_t creation_time_sec;
+ int32_t deletion_time_sec = 0;
+ int32_t metric_count;
+ int32_t condition_count;
+ int32_t matcher_count;
+ int32_t alert_count;
+ bool is_valid;
+
+ std::list<int32_t> broadcast_sent_time_sec;
+ std::list<int32_t> data_drop_time_sec;
+ std::list<int32_t> dump_report_time_sec;
+
+ // Stores how many times a matcher have been matched. The map size is capped by kMaxConfigCount.
+ std::map<const int64_t, int> matcher_stats;
+
+ // Stores the number of output tuple of condition trackers when it's bigger than
+ // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
+ // it means some data has been dropped. The map size is capped by kMaxConfigCount.
+ std::map<const int64_t, int> condition_stats;
+
+ // Stores the number of output tuple of metric producers when it's bigger than
+ // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
+ // it means some data has been dropped. The map size is capped by kMaxConfigCount.
+ std::map<const int64_t, int> metric_stats;
+
+ // Stores the number of times an anomaly detection alert has been declared.
+ // The map size is capped by kMaxConfigCount.
+ std::map<const int64_t, int> alert_stats;
+};
+
+struct UidMapStats {
+ int32_t snapshots;
+ int32_t changes;
+ int32_t bytes_used;
+ int32_t dropped_snapshots;
+ int32_t dropped_changes;
+};
+
// Keeps track of stats of statsd.
-// Single instance shared across the process. All methods are thread safe.
+// Single instance shared across the process. All public methods are thread safe.
class StatsdStats {
public:
static StatsdStats& getInstance();
@@ -233,25 +274,15 @@
int32_t mStartTimeSec;
// Track the number of dropped entries used by the uid map.
- StatsdStatsReport_UidMapStats mUidMapStats;
+ UidMapStats mUidMapStats;
// The stats about the configs that are still in use.
// The map size is capped by kMaxConfigCount.
- std::map<const ConfigKey, StatsdStatsReport_ConfigStats> mConfigStats;
+ std::map<const ConfigKey, std::shared_ptr<ConfigStats>> mConfigStats;
// Stores the stats for the configs that are no longer in use.
// The size of the vector is capped by kMaxIceBoxSize.
- std::list<const StatsdStatsReport_ConfigStats> mIceBox;
-
- // Stores the number of output tuple of condition trackers when it's bigger than
- // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
- // it means some data has been dropped. The map size is capped by kMaxConfigCount.
- std::map<const ConfigKey, std::map<const int64_t, int>> mConditionStats;
-
- // Stores the number of output tuple of metric producers when it's bigger than
- // kDimensionKeySizeSoftLimit. When you see the number is kDimensionKeySizeHardLimit +1,
- // it means some data has been dropped. The map size is capped by kMaxConfigCount.
- std::map<const ConfigKey, std::map<const int64_t, int>> mMetricsStats;
+ std::list<const std::shared_ptr<ConfigStats>> mIceBox;
// Stores the number of times a pushed atom is logged.
// The size of the vector is the largest pushed atom id in atoms.proto + 1. Atoms
@@ -272,27 +303,18 @@
// Stores the number of times statsd registers the periodic alarm changes
int mPeriodicAlarmRegisteredStats = 0;
- // Stores the number of times an anomaly detection alert has been declared
- // (per config, per alert name). The map size is capped by kMaxConfigCount.
- std::map<const ConfigKey, std::map<const int64_t, int>> mAlertStats;
-
- // Stores how many times a matcher have been matched. The map size is capped by kMaxConfigCount.
- std::map<const ConfigKey, std::map<const int64_t, int>> mMatcherStats;
void noteConfigRemovedInternalLocked(const ConfigKey& key);
void resetInternalLocked();
- void addSubStatsToConfigLocked(const ConfigKey& key,
- StatsdStatsReport_ConfigStats& configStats);
-
void noteDataDropped(const ConfigKey& key, int32_t timeSec);
void noteMetricsReportSent(const ConfigKey& key, int32_t timeSec);
void noteBroadcastSent(const ConfigKey& key, int32_t timeSec);
- void addToIceBoxLocked(const StatsdStatsReport_ConfigStats& stats);
+ void addToIceBoxLocked(std::shared_ptr<ConfigStats>& stats);
FRIEND_TEST(StatsdStatsTest, TestValidConfigAdd);
FRIEND_TEST(StatsdStatsTest, TestInvalidConfigAdd);
diff --git a/cmds/statsd/src/logd/LogEvent.cpp b/cmds/statsd/src/logd/LogEvent.cpp
index d282b86..4e4f146 100644
--- a/cmds/statsd/src/logd/LogEvent.cpp
+++ b/cmds/statsd/src/logd/LogEvent.cpp
@@ -17,8 +17,6 @@
#define DEBUG false // STOPSHIP if true
#include "logd/LogEvent.h"
-#include <sstream>
-
#include "stats_log_util.h"
namespace android {
@@ -27,7 +25,6 @@
using namespace android::util;
using android::util::ProtoOutputStream;
-using std::ostringstream;
using std::string;
using std::vector;
@@ -381,16 +378,15 @@
}
string LogEvent::ToString() const {
- ostringstream result;
- result << "{ " << mLogdTimestampNs << " " << mElapsedTimestampNs << " (" << mTagId << ")";
+ string result;
+ result += StringPrintf("{ %lld %lld (%d)", (long long)mLogdTimestampNs,
+ (long long)mElapsedTimestampNs, mTagId);
for (const auto& value : mValues) {
- result << StringPrintf("%#x", value.mField.getField());
- result << "->";
- result << value.mValue.toString();
- result << " ";
+ result +=
+ StringPrintf("%#x", value.mField.getField()) + "->" + value.mValue.toString() + " ";
}
- result << " }";
- return result.str();
+ result += " }";
+ return result;
}
void LogEvent::ToProto(ProtoOutputStream& protoOutput) const {
diff --git a/cmds/statsd/src/matchers/matcher_util.cpp b/cmds/statsd/src/matchers/matcher_util.cpp
index 5d5e64e..364d4e9 100644
--- a/cmds/statsd/src/matchers/matcher_util.cpp
+++ b/cmds/statsd/src/matchers/matcher_util.cpp
@@ -21,7 +21,6 @@
#include "matchers/matcher_util.h"
#include "stats_util.h"
-using std::ostringstream;
using std::set;
using std::string;
using std::unordered_map;
diff --git a/cmds/statsd/src/stats_log.proto b/cmds/statsd/src/stats_log.proto
index 269f25b..3c5f5a2 100644
--- a/cmds/statsd/src/stats_log.proto
+++ b/cmds/statsd/src/stats_log.proto
@@ -165,3 +165,86 @@
repeated ConfigMetricsReport reports = 2;
}
+
+message StatsdStatsReport {
+ optional int32 stats_begin_time_sec = 1;
+
+ optional int32 stats_end_time_sec = 2;
+
+ message MatcherStats {
+ optional int64 id = 1;
+ optional int32 matched_times = 2;
+ }
+
+ message ConditionStats {
+ optional int64 id = 1;
+ optional int32 max_tuple_counts = 2;
+ }
+
+ message MetricStats {
+ optional int64 id = 1;
+ optional int32 max_tuple_counts = 2;
+ }
+
+ message AlertStats {
+ optional int64 id = 1;
+ optional int32 alerted_times = 2;
+ }
+
+ message ConfigStats {
+ optional int32 uid = 1;
+ optional int64 id = 2;
+ optional int32 creation_time_sec = 3;
+ optional int32 deletion_time_sec = 4;
+ optional int32 metric_count = 5;
+ optional int32 condition_count = 6;
+ optional int32 matcher_count = 7;
+ optional int32 alert_count = 8;
+ optional bool is_valid = 9;
+
+ repeated int32 broadcast_sent_time_sec = 10;
+ repeated int32 data_drop_time_sec = 11;
+ repeated int32 dump_report_time_sec = 12;
+ repeated MatcherStats matcher_stats = 13;
+ repeated ConditionStats condition_stats = 14;
+ repeated MetricStats metric_stats = 15;
+ repeated AlertStats alert_stats = 16;
+ }
+
+ repeated ConfigStats config_stats = 3;
+
+ message AtomStats {
+ optional int32 tag = 1;
+ optional int32 count = 2;
+ }
+
+ repeated AtomStats atom_stats = 7;
+
+ message UidMapStats {
+ optional int32 snapshots = 1;
+ optional int32 changes = 2;
+ optional int32 bytes_used = 3;
+ optional int32 dropped_snapshots = 4;
+ optional int32 dropped_changes = 5;
+ }
+ optional UidMapStats uidmap_stats = 8;
+
+ message AnomalyAlarmStats {
+ optional int32 alarms_registered = 1;
+ }
+ optional AnomalyAlarmStats anomaly_alarm_stats = 9;
+
+ message PulledAtomStats {
+ optional int32 atom_id = 1;
+ optional int64 total_pull = 2;
+ optional int64 total_pull_from_cache = 3;
+ optional int64 min_pull_interval_sec = 4;
+ }
+ repeated PulledAtomStats pulled_atom_stats = 10;
+
+ message LoggerErrorStats {
+ optional int32 logger_disconnection_sec = 1;
+ optional int32 error_code = 2;
+ }
+ repeated LoggerErrorStats logger_error_stats = 11;
+}
diff --git a/cmds/statsd/src/stats_log_common.proto b/cmds/statsd/src/stats_log_common.proto
index aeecd23..c41f31e 100644
--- a/cmds/statsd/src/stats_log_common.proto
+++ b/cmds/statsd/src/stats_log_common.proto
@@ -46,87 +46,4 @@
optional int64 version = 5;
}
repeated Change changes = 2;
-}
-
-message StatsdStatsReport {
- optional int32 stats_begin_time_sec = 1;
-
- optional int32 stats_end_time_sec = 2;
-
- message MatcherStats {
- optional int64 id = 1;
- optional int32 matched_times = 2;
- }
-
- message ConditionStats {
- optional int64 id = 1;
- optional int32 max_tuple_counts = 2;
- }
-
- message MetricStats {
- optional int64 id = 1;
- optional int32 max_tuple_counts = 2;
- }
-
- message AlertStats {
- optional int64 id = 1;
- optional int32 alerted_times = 2;
- }
-
- message ConfigStats {
- optional int32 uid = 1;
- optional int64 id = 2;
- optional int32 creation_time_sec = 3;
- optional int32 deletion_time_sec = 4;
- optional int32 metric_count = 5;
- optional int32 condition_count = 6;
- optional int32 matcher_count = 7;
- optional int32 alert_count = 8;
- optional bool is_valid = 9;
-
- repeated int32 broadcast_sent_time_sec = 10;
- repeated int32 data_drop_time_sec = 11;
- repeated int32 dump_report_time_sec = 12;
- repeated MatcherStats matcher_stats = 13;
- repeated ConditionStats condition_stats = 14;
- repeated MetricStats metric_stats = 15;
- repeated AlertStats alert_stats = 16;
- }
-
- repeated ConfigStats config_stats = 3;
-
- message AtomStats {
- optional int32 tag = 1;
- optional int32 count = 2;
- }
-
- repeated AtomStats atom_stats = 7;
-
- message UidMapStats {
- optional int32 snapshots = 1;
- optional int32 changes = 2;
- optional int32 bytes_used = 3;
- optional int32 dropped_snapshots = 4;
- optional int32 dropped_changes = 5;
- }
- optional UidMapStats uidmap_stats = 8;
-
- message AnomalyAlarmStats {
- optional int32 alarms_registered = 1;
- }
- optional AnomalyAlarmStats anomaly_alarm_stats = 9;
-
- message PulledAtomStats {
- optional int32 atom_id = 1;
- optional int64 total_pull = 2;
- optional int64 total_pull_from_cache = 3;
- optional int64 min_pull_interval_sec = 4;
- }
- repeated PulledAtomStats pulled_atom_stats = 10;
-
- message LoggerErrorStats {
- optional int32 logger_disconnection_sec = 1;
- optional int32 error_code = 2;
- }
- repeated LoggerErrorStats logger_error_stats = 11;
}
\ No newline at end of file
diff --git a/cmds/statsd/src/stats_util.h b/cmds/statsd/src/stats_util.h
index c4b47dc..bbaf50a 100644
--- a/cmds/statsd/src/stats_util.h
+++ b/cmds/statsd/src/stats_util.h
@@ -16,7 +16,6 @@
#pragma once
-#include <sstream>
#include "HashableDimensionKey.h"
#include "frameworks/base/cmds/statsd/src/stats_log_common.pb.h"
#include "logd/LogReader.h"
diff --git a/cmds/statsd/tests/guardrail/StatsdStats_test.cpp b/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
index bd11443..5c4eda8 100644
--- a/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
+++ b/cmds/statsd/tests/guardrail/StatsdStats_test.cpp
@@ -277,21 +277,20 @@
EXPECT_TRUE(stats.mConfigStats.find(key) != stats.mConfigStats.end());
const auto& configStats = stats.mConfigStats[key];
- int maxCount = StatsdStats::kMaxTimestampCount;
- EXPECT_EQ(maxCount, configStats.broadcast_sent_time_sec_size());
- EXPECT_EQ(maxCount, configStats.data_drop_time_sec_size());
- EXPECT_EQ(maxCount, configStats.dump_report_time_sec_size());
+ size_t maxCount = StatsdStats::kMaxTimestampCount;
+ EXPECT_EQ(maxCount, configStats->broadcast_sent_time_sec.size());
+ EXPECT_EQ(maxCount, configStats->data_drop_time_sec.size());
+ EXPECT_EQ(maxCount, configStats->dump_report_time_sec.size());
// the oldest timestamp is the second timestamp in history
- EXPECT_EQ(1, configStats.broadcast_sent_time_sec(0));
- EXPECT_EQ(1, configStats.broadcast_sent_time_sec(0));
- EXPECT_EQ(1, configStats.broadcast_sent_time_sec(0));
+ EXPECT_EQ(1, configStats->broadcast_sent_time_sec.front());
+ EXPECT_EQ(1, configStats->broadcast_sent_time_sec.front());
+ EXPECT_EQ(1, configStats->broadcast_sent_time_sec.front());
// the last timestamp is the newest timestamp.
- EXPECT_EQ(newTimestamp,
- configStats.broadcast_sent_time_sec(StatsdStats::kMaxTimestampCount - 1));
- EXPECT_EQ(newTimestamp, configStats.data_drop_time_sec(StatsdStats::kMaxTimestampCount - 1));
- EXPECT_EQ(newTimestamp, configStats.dump_report_time_sec(StatsdStats::kMaxTimestampCount - 1));
+ EXPECT_EQ(newTimestamp, configStats->broadcast_sent_time_sec.back());
+ EXPECT_EQ(newTimestamp, configStats->data_drop_time_sec.back());
+ EXPECT_EQ(newTimestamp, configStats->dump_report_time_sec.back());
}
} // namespace statsd
diff --git a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java
index a12def0..bed4d98 100644
--- a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java
+++ b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/LoadtestActivity.java
@@ -50,7 +50,7 @@
import com.android.os.StatsLog.ConfigMetricsReport;
import com.android.os.StatsLog.ConfigMetricsReportList;
-import com.android.os.StatsLogCommon.StatsdStatsReport;
+import com.android.os.StatsLog.StatsdStatsReport;
import com.android.internal.os.StatsdConfigProto.TimeUnit;
import java.util.ArrayList;
diff --git a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java
index 58cbcd8..3939e7e 100644
--- a/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java
+++ b/cmds/statsd/tools/loadtest/src/com/android/statsd/loadtest/StatsdStatsRecorder.java
@@ -16,7 +16,7 @@
package com.android.statsd.loadtest;
import android.content.Context;
-import com.android.os.StatsLogCommon.StatsdStatsReport;
+import com.android.os.StatsLog.StatsdStatsReport;
import com.android.internal.os.StatsdConfigProto.TimeUnit;
public class StatsdStatsRecorder extends PerfDataRecorder {