1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "services/resource_coordinator/public/cpp/memory_instrumentation/browser_metrics.h"
6 
7 #include <cmath>
8 
9 #include "base/rand_util.h"
10 #include "base/time/time.h"
11 #include "build/build_config.h"
12 
13 namespace memory_instrumentation {
14 namespace {
15 
16 const char kAudioServiceHistogramName[] = "AudioService";
17 const char kBrowserHistogramName[] = "Browser";
18 const char kExtensionHistogramName[] = "Extension";
19 const char kGpuHistogramName[] = "Gpu";
20 const char kNetworkServiceHistogramName[] = "NetworkService";
21 const char kPaintPreviewCompositorHistogramName[] = "PaintPreviewCompositor";
22 const char kRendererHistogramName[] = "Renderer";
23 const char kUtilityHistogramName[] = "Utility";
24 
25 }  // namespace
26 
27 const char kMemoryHistogramPrefix[] = "Memory.";
28 
HistogramProcessTypeToString(HistogramProcessType type)29 const char* HistogramProcessTypeToString(HistogramProcessType type) {
30   switch (type) {
31     case HistogramProcessType::kAudioService:
32       return kAudioServiceHistogramName;
33     case HistogramProcessType::kBrowser:
34       return kBrowserHistogramName;
35     case HistogramProcessType::kExtension:
36       return kExtensionHistogramName;
37     case HistogramProcessType::kGpu:
38       return kGpuHistogramName;
39     case HistogramProcessType::kNetworkService:
40       return kNetworkServiceHistogramName;
41     case HistogramProcessType::kPaintPreviewCompositor:
42       return kPaintPreviewCompositorHistogramName;
43     case HistogramProcessType::kRenderer:
44       return kRendererHistogramName;
45     case HistogramProcessType::kUtility:
46       return kUtilityHistogramName;
47   }
48 }
49 
GetPrivateFootprintHistogramName(HistogramProcessType type)50 std::string GetPrivateFootprintHistogramName(HistogramProcessType type) {
51   return std::string(kMemoryHistogramPrefix) +
52          HistogramProcessTypeToString(type) + ".PrivateMemoryFootprint";
53 }
54 
GetDelayForNextMemoryLog()55 base::TimeDelta GetDelayForNextMemoryLog() {
56 #if defined(OS_ANDROID)
57   base::TimeDelta mean_time = base::TimeDelta::FromMinutes(5);
58 #else
59   base::TimeDelta mean_time = base::TimeDelta::FromMinutes(30);
60 #endif
61   // Compute the actual delay before sampling using a Poisson process.
62   double uniform = base::RandDouble();
63   return -std::log(uniform) * mean_time;
64 }
65 
66 }  // namespace memory_instrumentation
67