service_metrics.cc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright 2018 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. #include "services/audio/service_metrics.h"
  5. #include "base/metrics/histogram_macros.h"
  6. #include "base/time/tick_clock.h"
  7. #include "base/trace_event/trace_event.h"
  8. namespace audio {
  9. ServiceMetrics::ServiceMetrics(const base::TickClock* clock) : clock_(clock) {}
  10. ServiceMetrics::~ServiceMetrics() {
  11. LogHasNoConnectionsDuration();
  12. }
  13. void ServiceMetrics::HasConnections() {
  14. TRACE_EVENT_NESTABLE_ASYNC_BEGIN0("audio", "Audio service has connections",
  15. TRACE_ID_LOCAL(this));
  16. has_connections_start_ = clock_->NowTicks();
  17. LogHasNoConnectionsDuration();
  18. }
  19. void ServiceMetrics::HasNoConnections() {
  20. TRACE_EVENT_NESTABLE_ASYNC_END0("audio", "Audio service has connections",
  21. TRACE_ID_LOCAL(this));
  22. has_no_connections_start_ = clock_->NowTicks();
  23. DCHECK_NE(base::TimeTicks(), has_connections_start_);
  24. UMA_HISTOGRAM_CUSTOM_TIMES("Media.AudioService.HasConnectionsDuration",
  25. clock_->NowTicks() - has_connections_start_,
  26. base::TimeDelta(), base::Days(7), 50);
  27. has_connections_start_ = base::TimeTicks();
  28. }
  29. void ServiceMetrics::LogHasNoConnectionsDuration() {
  30. // Service shuts down without having accepted any connections in its lifetime
  31. // or with active connections, meaning there is no "no connection" interval in
  32. // progress.
  33. if (has_no_connections_start_.is_null())
  34. return;
  35. UMA_HISTOGRAM_CUSTOM_TIMES("Media.AudioService.HasNoConnectionsDuration",
  36. clock_->NowTicks() - has_no_connections_start_,
  37. base::TimeDelta(), base::Minutes(10), 50);
  38. has_no_connections_start_ = base::TimeTicks();
  39. }
  40. } // namespace audio