Bug 1548472 - Collect telemetry about how is the cache split by the content type, r=mayhemer
authorMichal Novotny <michal.novotny@gmail.com>
Wed, 08 May 2019 13:15:40 +0000
changeset 532145 dcf3a023d7d08907c6033a8c61c601a6b06e620a
parent 532144 d87eef05591be2cb8b211a2ca26ef41e7b042f99
child 532146 d8fa440167f565c4553fc09e55caceb6544e2d89
push id11265
push userffxbld-merge
push dateMon, 13 May 2019 10:53:39 +0000
treeherdermozilla-beta@77e0fe8dbdd3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmayhemer
bugs1548472, 1506534
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1548472 - Collect telemetry about how is the cache split by the content type, r=mayhemer Like in case of bug 1506534, this telemetry is sampled periodically every time 2GB of data is written to the cache, i.e. when the cache was used for some time and there is a chance that its content has changed significantly. Differential Revision: https://phabricator.services.mozilla.com/D29678
netwerk/cache2/CacheIndex.cpp
netwerk/cache2/CacheIndex.h
netwerk/cache2/CacheObserver.cpp
netwerk/cache2/CacheObserver.h
toolkit/components/telemetry/Histograms.json
--- a/netwerk/cache2/CacheIndex.cpp
+++ b/netwerk/cache2/CacheIndex.cpp
@@ -3915,31 +3915,31 @@ void CacheIndex::UpdateTotalBytesWritten
   index->mTotalBytesWritten += aBytesWritten;
 
   // Do telemetry report if enough data has been written and the index is
   // in READY state. The data is available also in WRITING state, but we would
   // need to deal with pending updates.
   if (index->mTotalBytesWritten >= kTelemetryReportBytesLimit &&
       index->mState == READY && !index->mIndexNeedsUpdate &&
       !index->mShuttingDown) {
-    index->DoBaseDomainAccessTelemetryReport();
+    index->DoTelemetryReport();
 
     index->mTotalBytesWritten = 0;
     CacheObserver::SetCacheAmountWritten(0);
     return;
   }
 
   uint64_t writtenKB = index->mTotalBytesWritten >> 10;
   // Store number of written kilobytes to prefs after writing at least 10MB.
   if ((writtenKB - CacheObserver::CacheAmountWritten()) > (10 * 1024)) {
     CacheObserver::SetCacheAmountWritten(writtenKB);
   }
 }
 
-void CacheIndex::DoBaseDomainAccessTelemetryReport() {
+void CacheIndex::DoTelemetryReport() {
   static const nsLiteralCString
       contentTypeNames[nsICacheEntry::CONTENT_TYPE_LAST] = {
           NS_LITERAL_CSTRING("UNKNOWN"),    NS_LITERAL_CSTRING("OTHER"),
           NS_LITERAL_CSTRING("JAVASCRIPT"), NS_LITERAL_CSTRING("IMAGE"),
           NS_LITERAL_CSTRING("MEDIA"),      NS_LITERAL_CSTRING("STYLESHEET"),
           NS_LITERAL_CSTRING("WASM")};
 
   // size in kB of all entries
@@ -4016,18 +4016,41 @@ void CacheIndex::DoBaseDomainAccessTelem
 
     if (countByType[i] > 0) {
       Telemetry::Accumulate(
           Telemetry::NETWORK_CACHE_ISOLATION_ENTRY_COUNT_INCREASE,
           contentTypeNames[i],
           round(static_cast<double>(countIncByType[i]) * 100.0 /
                 static_cast<double>(countByType[i])));
     }
+
+    if (size > 0) {
+      Telemetry::Accumulate(Telemetry::NETWORK_CACHE_SIZE_SHARE,
+                            contentTypeNames[i],
+                            round(static_cast<double>(sizeByType[i]) * 100.0 /
+                                  static_cast<double>(size)));
+    }
+
+    if (count > 0) {
+      Telemetry::Accumulate(Telemetry::NETWORK_CACHE_ENTRY_COUNT_SHARE,
+                            contentTypeNames[i],
+                            round(static_cast<double>(countByType[i]) * 100.0 /
+                                  static_cast<double>(count)));
+    }
   }
 
+  nsCString probeKey;
+  if (CacheObserver::SmartCacheSizeEnabled()) {
+    probeKey = NS_LITERAL_CSTRING("SMARTSIZE");
+  } else {
+    probeKey = NS_LITERAL_CSTRING("USERDEFINEDSIZE");
+  }
+  Telemetry::Accumulate(Telemetry::NETWORK_CACHE_ENTRY_COUNT, probeKey, count);
+  Telemetry::Accumulate(Telemetry::NETWORK_CACHE_SIZE, probeKey, size >> 10);
+
   // Change telemetry report ID. This will invalidate eTLD+1 access data stored
   // in all cache entries.
   CacheObserver::SetTelemetryReportID(CacheObserver::TelemetryReportID() + 1);
 }
 
 // static
 void CacheIndex::OnAsyncEviction(bool aEvicting) {
   StaticMutexAutoLock lock(sLock);
--- a/netwerk/cache2/CacheIndex.h
+++ b/netwerk/cache2/CacheIndex.h
@@ -1009,19 +1009,20 @@ class CacheIndex final : public CacheFil
   void ReplaceRecordInIterators(CacheIndexRecord* aOldRecord,
                                 CacheIndexRecord* aNewRecord);
 
   // Memory reporting (private part)
   size_t SizeOfExcludingThisInternal(mozilla::MallocSizeOf mallocSizeOf) const;
 
   void ReportHashStats();
 
-  // Reports first party cache isolation telemetry, clears data stored in the
-  // index entries and bumps a telemetry report ID.
-  void DoBaseDomainAccessTelemetryReport();
+  // Reports telemetry about cache, i.e. size, entry count, content type stats
+  // and first party cache isolation stats. Clears first party cache isolation
+  // counters stored in the index entries and bumps a telemetry report ID.
+  void DoTelemetryReport();
 
   static mozilla::StaticRefPtr<CacheIndex> gInstance;
   static StaticMutex sLock;
 
   nsCOMPtr<nsIFile> mCacheDirectory;
 
   EState mState;
   // Timestamp of time when the index was initialized. We use it to delay
--- a/netwerk/cache2/CacheObserver.cpp
+++ b/netwerk/cache2/CacheObserver.cpp
@@ -48,17 +48,18 @@ static uint32_t const kDefaultDiskFreeSp
 uint32_t CacheObserver::sDiskFreeSpaceSoftLimit =
     kDefaultDiskFreeSpaceSoftLimit;
 
 static uint32_t const kDefaultDiskFreeSpaceHardLimit = 1024;  // 1MB
 uint32_t CacheObserver::sDiskFreeSpaceHardLimit =
     kDefaultDiskFreeSpaceHardLimit;
 
 static bool const kDefaultSmartCacheSizeEnabled = false;
-bool CacheObserver::sSmartCacheSizeEnabled = kDefaultSmartCacheSizeEnabled;
+Atomic<bool, Relaxed> CacheObserver::sSmartCacheSizeEnabled(
+    kDefaultSmartCacheSizeEnabled);
 
 static uint32_t const kDefaultPreloadChunkCount = 4;
 uint32_t CacheObserver::sPreloadChunkCount = kDefaultPreloadChunkCount;
 
 static int32_t const kDefaultMaxMemoryEntrySize = 4 * 1024;  // 4 MB
 int32_t CacheObserver::sMaxMemoryEntrySize = kDefaultMaxMemoryEntrySize;
 
 static int32_t const kDefaultMaxDiskEntrySize = 50 * 1024;  // 50 MB
@@ -152,19 +153,19 @@ void CacheObserver::AttachToPreferences(
 
   mozilla::Preferences::AddUintVarCache(
       &sMetadataMemoryLimit, "browser.cache.disk.metadata_memory_limit",
       kDefaultMetadataMemoryLimit);
 
   mozilla::Preferences::AddAtomicUintVarCache(&sDiskCacheCapacity,
                                               "browser.cache.disk.capacity",
                                               kDefaultDiskCacheCapacity);
-  mozilla::Preferences::AddBoolVarCache(&sSmartCacheSizeEnabled,
-                                        "browser.cache.disk.smart_size.enabled",
-                                        kDefaultSmartCacheSizeEnabled);
+  mozilla::Preferences::AddAtomicBoolVarCache(
+      &sSmartCacheSizeEnabled, "browser.cache.disk.smart_size.enabled",
+      kDefaultSmartCacheSizeEnabled);
   mozilla::Preferences::AddIntVarCache(&sMemoryCacheCapacity,
                                        "browser.cache.memory.capacity",
                                        kDefaultMemoryCacheCapacity);
 
   mozilla::Preferences::AddUintVarCache(
       &sDiskFreeSpaceSoftLimit, "browser.cache.disk.free_space_soft_limit",
       kDefaultDiskFreeSpaceSoftLimit);
   mozilla::Preferences::AddUintVarCache(
--- a/netwerk/cache2/CacheObserver.h
+++ b/netwerk/cache2/CacheObserver.h
@@ -101,17 +101,17 @@ class CacheObserver : public nsIObserver
   static bool sUseMemoryCache;
   static bool sUseDiskCache;
   static uint32_t sMetadataMemoryLimit;
   static int32_t sMemoryCacheCapacity;
   static int32_t sAutoMemoryCacheCapacity;
   static Atomic<uint32_t, Relaxed> sDiskCacheCapacity;
   static uint32_t sDiskFreeSpaceSoftLimit;
   static uint32_t sDiskFreeSpaceHardLimit;
-  static bool sSmartCacheSizeEnabled;
+  static Atomic<bool, Relaxed> sSmartCacheSizeEnabled;
   static uint32_t sPreloadChunkCount;
   static int32_t sMaxMemoryEntrySize;
   static int32_t sMaxDiskEntrySize;
   static uint32_t sMaxDiskChunksMemoryUsage;
   static uint32_t sMaxDiskPriorityChunksMemoryUsage;
   static uint32_t sCompressionLevel;
   static float sHalfLifeHours;
   static bool sSanitizeOnShutdown;
--- a/toolkit/components/telemetry/Histograms.json
+++ b/toolkit/components/telemetry/Histograms.json
@@ -10910,16 +10910,84 @@
       "IMAGE",
       "MEDIA",
       "STYLESHEET",
       "WASM"
     ],
     "n_buckets": 100,
     "description": "Number of unique sites accessing the cache entry keyed by content type."
   },
+  "NETWORK_CACHE_SIZE" : {
+    "record_in_processes": ["main"],
+    "alert_emails": ["necko@mozilla.com", "mnovotny@mozilla.com"],
+    "bug_numbers": [1548472],
+    "expires_in_version": "never",
+    "kind": "linear",
+    "high": 4000,
+    "n_buckets": 100,
+    "keyed": true,
+    "keys": [
+      "SMARTSIZE",
+      "USERDEFINEDSIZE"
+    ],
+    "description": "Cache size in megabytes keyed by cache capacity calculation type. Numbers are sampled periodically, every time 2GB of data is written to the cache."
+  },
+  "NETWORK_CACHE_ENTRY_COUNT" : {
+    "record_in_processes": ["main"],
+    "alert_emails": ["necko@mozilla.com", "mnovotny@mozilla.com"],
+    "bug_numbers": [1548472],
+    "expires_in_version": "never",
+    "kind": "exponential",
+    "high": 100000,
+    "n_buckets": 100,
+    "keyed": true,
+    "keys": [
+      "SMARTSIZE",
+      "USERDEFINEDSIZE"
+    ],
+    "description": "Number of entries in the cache keyed by cache capacity calculation type. Numbers are sampled periodically, every time 2GB of data is written to the cache."
+  },
+  "NETWORK_CACHE_SIZE_SHARE" : {
+    "record_in_processes": ["main"],
+    "alert_emails": ["necko@mozilla.com", "mnovotny@mozilla.com"],
+    "bug_numbers": [1548472],
+    "expires_in_version": "never",
+    "kind": "enumerated",
+    "n_values": 99,
+    "keyed": true,
+    "keys": [
+      "UNKNOWN",
+      "OTHER",
+      "JAVASCRIPT",
+      "IMAGE",
+      "MEDIA",
+      "STYLESHEET",
+      "WASM"
+    ],
+    "description": "Percentage of the cache size occupied by given content type. Numbers are sampled periodically, every time 2GB of data is written to the cache."
+  },
+  "NETWORK_CACHE_ENTRY_COUNT_SHARE" : {
+    "record_in_processes": ["main"],
+    "alert_emails": ["necko@mozilla.com", "mnovotny@mozilla.com"],
+    "bug_numbers": [1548472],
+    "expires_in_version": "never",
+    "kind": "enumerated",
+    "n_values": 99,
+    "keyed": true,
+    "keys": [
+      "UNKNOWN",
+      "OTHER",
+      "JAVASCRIPT",
+      "IMAGE",
+      "MEDIA",
+      "STYLESHEET",
+      "WASM"
+    ],
+    "description": "Percentage of the entries with the given content type. Numbers are sampled periodically, every time 2GB of data is written to the cache."
+  },
   "DATABASE_LOCKED_EXCEPTION": {
     "record_in_processes": ["main", "content"],
     "expires_in_version": "42",
     "kind": "enumerated",
     "description": "Record database locks when opening one of Fennec's databases. The index corresponds to how many attempts, beginning with 0.",
     "n_values": 5
   },
   "DATABASE_SUCCESSFUL_UNLOCK": {