| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/metrics/renderer_uptime_tracker.h" |
| 6 |
| 7 #include "base/metrics/histogram_macros.h" |
| 8 #include "content/public/browser/notification_service.h" |
| 9 #include "content/public/browser/notification_types.h" |
| 10 #include "content/public/browser/render_process_host.h" |
| 11 #include "extensions/features/features.h" |
| 12 |
| 13 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 14 #include "extensions/browser/process_map.h" |
| 15 #endif |
| 16 |
| 17 namespace metrics { |
| 18 |
| 19 namespace { |
| 20 |
| 21 RendererUptimeTracker* g_instance = nullptr; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 // static |
| 26 void RendererUptimeTracker::Initialize() { |
| 27 DCHECK(!g_instance); |
| 28 g_instance = new RendererUptimeTracker; |
| 29 } |
| 30 |
| 31 // static |
| 32 RendererUptimeTracker* RendererUptimeTracker::Get() { |
| 33 DCHECK(g_instance); |
| 34 return g_instance; |
| 35 } |
| 36 |
| 37 RendererUptimeTracker::RendererUptimeTracker() { |
| 38 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 39 content::NotificationService::AllBrowserContextsAndSources()); |
| 40 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 41 content::NotificationService::AllBrowserContextsAndSources()); |
| 42 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 43 content::NotificationService::AllBrowserContextsAndSources()); |
| 44 } |
| 45 |
| 46 RendererUptimeTracker::~RendererUptimeTracker() {} |
| 47 |
| 48 void RendererUptimeTracker::OnRendererStarted(int pid) { |
| 49 info_map_[pid] = RendererInfo{base::TimeTicks::Now(), 0}; |
| 50 } |
| 51 |
| 52 void RendererUptimeTracker::OnRendererTerminated(int pid) { |
| 53 auto it = info_map_.find(pid); |
| 54 // The pid may not exist when process fails to start up or when process is |
| 55 // terminated without reuse. |
| 56 if (it != info_map_.end()) { |
| 57 auto uptime = base::TimeTicks::Now() - it->second.launched_at_; |
| 58 UMA_HISTOGRAM_CUSTOM_TIMES("Memory.Experimental.Renderer.Uptime", uptime, |
| 59 base::TimeDelta::FromHours(1), |
| 60 base::TimeDelta::FromDays(7), 50); |
| 61 UMA_HISTOGRAM_COUNTS_10000( |
| 62 "Memory.Experimental.Renderer.LoadsInMainFrameDuringUptime", |
| 63 it->second.num_loads_in_main_frame_); |
| 64 info_map_.erase(it); |
| 65 } |
| 66 } |
| 67 |
| 68 void RendererUptimeTracker::OnLoadInMainFrame(int pid) { |
| 69 auto it = info_map_.find(pid); |
| 70 // The pid may not exist in an in-process browser test. |
| 71 if (it != info_map_.end()) { |
| 72 it->second.num_loads_in_main_frame_++; |
| 73 } |
| 74 } |
| 75 |
| 76 void RendererUptimeTracker::Observe( |
| 77 int type, |
| 78 const content::NotificationSource& source, |
| 79 const content::NotificationDetails& details) { |
| 80 switch (type) { |
| 81 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { |
| 82 content::RenderProcessHost* host = |
| 83 content::Source<content::RenderProcessHost>(source).ptr(); |
| 84 #if BUILDFLAG(ENABLE_EXTENSIONS) |
| 85 if (extensions::ProcessMap::Get(host->GetBrowserContext()) |
| 86 ->Contains(host->GetID())) { |
| 87 break; |
| 88 } |
| 89 #endif |
| 90 OnRendererStarted(host->GetID()); |
| 91 break; |
| 92 } |
| 93 |
| 94 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { |
| 95 content::RenderProcessHost* host = |
| 96 content::Source<content::RenderProcessHost>(source).ptr(); |
| 97 OnRendererTerminated(host->GetID()); |
| 98 break; |
| 99 } |
| 100 |
| 101 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: { |
| 102 content::RenderProcessHost* host = |
| 103 content::Source<content::RenderProcessHost>(source).ptr(); |
| 104 OnRendererTerminated(host->GetID()); |
| 105 break; |
| 106 } |
| 107 |
| 108 default: |
| 109 NOTREACHED(); |
| 110 break; |
| 111 } |
| 112 } |
| 113 |
| 114 } // namespace metrics |
| OLD | NEW |