| 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 "content/browser/service_worker/service_worker_lifetime_tracker.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" |
| 9 #include "base/time/default_tick_clock.h" |
| 10 #include "content/browser/service_worker/service_worker_metrics.h" |
| 11 |
| 12 namespace content { |
| 13 |
| 14 // Five minutes is somewhat ad-hoc. We want to capture workers that are |
| 15 // running for a long time in UMA even if they never stop. A timer running too |
| 16 // frequently would be wasteful and too infrequently could miss long-running |
| 17 // workers before browser shutdown. Five minutes seems about right; it's also |
| 18 // the default keep-alive time limit for a service worker event so workers |
| 19 // running longer than this are a bit special: they must have had multiple |
| 20 // events in their lifetime. |
| 21 constexpr base::TimeDelta kTimerDuration = base::TimeDelta::FromMinutes(5); |
| 22 |
| 23 ServiceWorkerLifetimeTracker::ServiceWorkerLifetimeTracker() |
| 24 : ServiceWorkerLifetimeTracker(base::MakeUnique<base::DefaultTickClock>()) { |
| 25 } |
| 26 |
| 27 ServiceWorkerLifetimeTracker::ServiceWorkerLifetimeTracker( |
| 28 std::unique_ptr<base::TickClock> tick_clock) |
| 29 : tick_clock_(std::move(tick_clock)), timer_(tick_clock_.get()) {} |
| 30 |
| 31 ServiceWorkerLifetimeTracker::~ServiceWorkerLifetimeTracker() = default; |
| 32 |
| 33 void ServiceWorkerLifetimeTracker::StartTiming(int64_t version_id) { |
| 34 DCHECK(!base::ContainsKey(running_workers_, version_id)); |
| 35 |
| 36 running_workers_[version_id] = tick_clock_->NowTicks(); |
| 37 if (!timer_.IsRunning()) { |
| 38 timer_.Start(FROM_HERE, kTimerDuration, this, |
| 39 &ServiceWorkerLifetimeTracker::RecordHistograms); |
| 40 } |
| 41 } |
| 42 |
| 43 void ServiceWorkerLifetimeTracker::StopTiming(int64_t version_id) { |
| 44 auto it = running_workers_.find(version_id); |
| 45 if (it == running_workers_.end()) |
| 46 return; |
| 47 ServiceWorkerMetrics::RecordRuntime(tick_clock_->NowTicks() - it->second); |
| 48 running_workers_.erase(it); |
| 49 } |
| 50 |
| 51 void ServiceWorkerLifetimeTracker::AbortTiming(int64_t version_id) { |
| 52 auto it = running_workers_.find(version_id); |
| 53 if (it == running_workers_.end()) |
| 54 return; |
| 55 running_workers_.erase(it); |
| 56 } |
| 57 |
| 58 void ServiceWorkerLifetimeTracker::RecordHistograms() { |
| 59 base::TimeTicks now = tick_clock_->NowTicks(); |
| 60 |
| 61 for (auto& item : running_workers_) { |
| 62 base::TimeDelta runtime = now - item.second; |
| 63 if (runtime > kTimerDuration) |
| 64 ServiceWorkerMetrics::RecordStillRunning(runtime); |
| 65 } |
| 66 |
| 67 if (running_workers_.empty()) |
| 68 timer_.Stop(); |
| 69 } |
| 70 |
| 71 } // namespace content |
| OLD | NEW |