diff options
author | Jeffrey Huang <jeffreyhuang@google.com> | 2023-05-10 13:54:37 -0700 |
---|---|---|
committer | Jeffrey Huang <jeffreyhuang@google.com> | 2023-05-11 10:05:32 -0700 |
commit | a58701717af75b12021be743209f4086b47c3f6a (patch) | |
tree | 6327b20c02047ef6db3105fdd061625c40114399 | |
parent | ca900a3139e944b561732f488c4c88bb2b24a32d (diff) | |
download | StatsD-a58701717af75b12021be743209f4086b47c3f6a.tar.gz |
Add db sizes to statsdstats
Ignore-AOSP-First: Android u feature
Bug: 280914743
Test: atest statsd_test
Change-Id: If894efca9ca010f19187c097a4932d31a3b2227b
-rw-r--r-- | statsd/src/guardrail/StatsdStats.cpp | 36 | ||||
-rw-r--r-- | statsd/src/guardrail/StatsdStats.h | 12 | ||||
-rw-r--r-- | statsd/src/stats_log.proto | 2 | ||||
-rw-r--r-- | statsd/src/storage/StorageManager.cpp | 2 | ||||
-rw-r--r-- | statsd/tests/guardrail/StatsdStats_test.cpp | 5 |
5 files changed, 57 insertions, 0 deletions
diff --git a/statsd/src/guardrail/StatsdStats.cpp b/statsd/src/guardrail/StatsdStats.cpp index 2b3d498f..cb457a3c 100644 --- a/statsd/src/guardrail/StatsdStats.cpp +++ b/statsd/src/guardrail/StatsdStats.cpp @@ -114,6 +114,8 @@ const int FIELD_ID_CONFIG_STATS_RESTRICTED_METRIC_STATS = 25; const int FIELD_ID_CONFIG_STATS_DEVICE_INFO_TABLE_CREATION_FAILED = 26; const int FIELD_ID_CONFIG_STATS_RESTRICTED_DB_CORRUPTED_COUNT = 27; const int FIELD_ID_CONFIG_STATS_RESTRICTED_CONFIG_FLUSH_LATENCY = 28; +const int FIELD_ID_CONFIG_STATS_RESTRICTED_CONFIG_DB_SIZE_TIME_SEC = 29; +const int FIELD_ID_CONFIG_STATS_RESTRICTED_CONFIG_DB_SIZE_BYTES = 30; const int FIELD_ID_INVALID_CONFIG_REASON_ENUM = 1; const int FIELD_ID_INVALID_CONFIG_REASON_METRIC_ID = 2; @@ -756,6 +758,24 @@ void StatsdStats::noteRestrictedConfigFlushLatency(const ConfigKey& configKey, totalFlushLatencies.push_back(totalFlushLatencyNs); } +void StatsdStats::noteRestrictedConfigDbSize(const ConfigKey& configKey, + const int64_t elapsedTimeNs, const int64_t dbSize) { + lock_guard<std::mutex> lock(mLock); + auto it = mConfigStats.find(configKey); + if (it == mConfigStats.end()) { + ALOGE("Config key %s not found!", configKey.ToString().c_str()); + return; + } + std::list<int64_t>& totalDbSizeTimestamps = it->second->total_db_size_timestamps; + std::list<int64_t>& totaDbSizes = it->second->total_db_sizes; + if (totalDbSizeTimestamps.size() == kMaxRestrictedConfigDbSizeCount) { + totalDbSizeTimestamps.pop_front(); + totaDbSizes.pop_front(); + } + totalDbSizeTimestamps.push_back(elapsedTimeNs); + totaDbSizes.push_back(dbSize); +} + void StatsdStats::noteRestrictedMetricCategoryChanged(const ConfigKey& configKey, const int64_t metricId) { lock_guard<std::mutex> lock(mLock); @@ -810,6 +830,8 @@ void StatsdStats::resetInternalLocked() { config.second->restricted_metric_stats.clear(); config.second->db_corrupted_count = 0; config.second->total_flush_latency_ns.clear(); + config.second->total_db_size_timestamps.clear(); + config.second->total_db_sizes.clear(); } for (auto& pullStats : mPulledAtomStats) { pullStats.second.totalPull = 0; @@ -913,6 +935,10 @@ void StatsdStats::dumpStats(int out) const { for (const int64_t flushLatency : configStats->total_flush_latency_ns) { dprintf(out, "\tflush latency time ns: %lld\n", (long long)flushLatency); } + + for (const int64_t dbSize : configStats->total_db_sizes) { + dprintf(out, "\tdb size: %lld\n", (long long)dbSize); + } } dprintf(out, "%lu Active Configs\n", (unsigned long)mConfigStats.size()); for (auto& pair : mConfigStats) { @@ -1295,6 +1321,16 @@ void addConfigStatsToProto(const ConfigStats& configStats, ProtoOutputStream* pr FIELD_COUNT_REPEATED, latency); } + for (int64_t dbSizeTimestamp : configStats.total_db_size_timestamps) { + proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_RESTRICTED_CONFIG_DB_SIZE_TIME_SEC | + FIELD_COUNT_REPEATED, + dbSizeTimestamp); + } + for (int64_t dbSize : configStats.total_db_sizes) { + proto->write(FIELD_TYPE_INT64 | FIELD_ID_CONFIG_STATS_RESTRICTED_CONFIG_DB_SIZE_BYTES | + FIELD_COUNT_REPEATED, + dbSize); + } proto->end(token); } diff --git a/statsd/src/guardrail/StatsdStats.h b/statsd/src/guardrail/StatsdStats.h index b29b0240..b13b4d99 100644 --- a/statsd/src/guardrail/StatsdStats.h +++ b/statsd/src/guardrail/StatsdStats.h @@ -133,6 +133,12 @@ struct ConfigStats { std::map<int64_t, RestrictedMetricStats> restricted_metric_stats; std::list<int64_t> total_flush_latency_ns; + + // Stores the last 20 timestamps for computing sqlite db size. + std::list<int64_t> total_db_size_timestamps; + + // Stores the last 20 sizes of the sqlite db. + std::list<int64_t> total_db_sizes; }; struct UidMapStats { @@ -180,6 +186,8 @@ public: const static int kMaxRestrictedConfigFlushLatencyCount = 20; + const static int kMaxRestrictedConfigDbSizeCount = 20; + // Max memory allowed for storing metrics per configuration. If this limit is exceeded, statsd // drops the metrics data in memory. static const size_t kMaxMetricsBytesPerConfig = 2 * 1024 * 1024; @@ -598,6 +606,10 @@ public: void noteRestrictedConfigFlushLatency(const ConfigKey& configKey, const int64_t totalFlushLatencyNs); + // Reports the size of the internal sqlite db. + void noteRestrictedConfigDbSize(const ConfigKey& configKey, const int64_t elapsedTimeNs, + const int64_t dbSize); + /** * Reset the historical stats. Including all stats in icebox, and the tracked stats about * metrics, matchers, and atoms. The active configs will be kept and StatsdStats will continue diff --git a/statsd/src/stats_log.proto b/statsd/src/stats_log.proto index 3ea049f1..d2214542 100644 --- a/statsd/src/stats_log.proto +++ b/statsd/src/stats_log.proto @@ -508,6 +508,8 @@ message StatsdStatsReport { optional bool device_info_table_creation_failed = 26; optional int32 restricted_db_corrupted_count = 27; repeated int64 restricted_flush_latency = 28; + repeated int64 restricted_db_size_time_sec = 29; + repeated int64 restricted_db_size_bytes = 30; } repeated ConfigStats config_stats = 3; diff --git a/statsd/src/storage/StorageManager.cpp b/statsd/src/storage/StorageManager.cpp index 1b5b069d..3a6f71c0 100644 --- a/statsd/src/storage/StorageManager.cpp +++ b/statsd/src/storage/StorageManager.cpp @@ -842,6 +842,8 @@ void StorageManager::enforceDbGuardrails(const char* path, const int64_t currWal remove(fullPathName.c_str()); continue; } + StatsdStats::getInstance().noteRestrictedConfigDbSize(key, currWallClockSec, + fileInfo.st_size); if (fileInfo.st_mtime <= deleteThresholdSec || fileInfo.st_size >= maxBytes) { remove(fullPathName.c_str()); } diff --git a/statsd/tests/guardrail/StatsdStats_test.cpp b/statsd/tests/guardrail/StatsdStats_test.cpp index 8f6db63b..c206bc3f 100644 --- a/statsd/tests/guardrail/StatsdStats_test.cpp +++ b/statsd/tests/guardrail/StatsdStats_test.cpp @@ -496,6 +496,7 @@ TEST(StatsdStatsTest, TestRestrictedMetricsStats) { stats.noteConfigReceived(configKeyWithoutError, 2, 3, 4, 5, {}, nullopt); stats.noteDbCorrupted(key); stats.noteDbCorrupted(key); + stats.noteRestrictedConfigDbSize(key, 999, 111); vector<uint8_t> output; stats.dumpStats(&output, false); @@ -513,6 +514,10 @@ TEST(StatsdStatsTest, TestRestrictedMetricsStats) { ASSERT_EQ(2, report.config_stats(1).restricted_metric_stats(0).flush_latency_ns().size()); EXPECT_EQ(3000, report.config_stats(1).restricted_metric_stats(0).flush_latency_ns(0)); EXPECT_EQ(3001, report.config_stats(1).restricted_metric_stats(0).flush_latency_ns(1)); + ASSERT_EQ(1, report.config_stats(1).restricted_db_size_time_sec().size()); + EXPECT_EQ(999, report.config_stats(1).restricted_db_size_time_sec(0)); + ASSERT_EQ(1, report.config_stats(1).restricted_db_size_bytes().size()); + EXPECT_EQ(111, report.config_stats(1).restricted_db_size_bytes(0)); ASSERT_EQ(1, report.config_stats(1).restricted_flush_latency().size()); EXPECT_EQ(4000, report.config_stats(1).restricted_flush_latency(0)); EXPECT_TRUE(report.config_stats(1).device_info_table_creation_failed()); |