1// Copyright 2013 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#ifndef MEDIA_BASE_SCOPED_HISTOGRAM_TIMER_H_
6#define MEDIA_BASE_SCOPED_HISTOGRAM_TIMER_H_
7
8#include "base/metrics/histogram.h"
9#include "base/time/time.h"
10
11// Scoped class which logs its time on this earth as a UMA statistic.  Must be
12// a #define macro since UMA macros prevent variables as names.  The nested
13// macro is necessary to expand __COUNTER__ to an actual value.
14#define SCOPED_UMA_HISTOGRAM_TIMER(name) \
15  SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, __COUNTER__)
16
17#define SCOPED_UMA_HISTOGRAM_TIMER_EXPANDER(name, key) \
18  SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, key)
19
20#define SCOPED_UMA_HISTOGRAM_TIMER_UNIQUE(name, key) \
21  class ScopedHistogramTimer##key { \
22   public: \
23    ScopedHistogramTimer##key() : constructed_(base::TimeTicks::Now()) {} \
24    ~ScopedHistogramTimer##key() { \
25      base::TimeDelta elapsed = base::TimeTicks::Now() - constructed_; \
26      UMA_HISTOGRAM_TIMES(name, elapsed); \
27    } \
28   private: \
29    base::TimeTicks constructed_; \
30  } scoped_histogram_timer_##key
31
32#endif  // MEDIA_BASE_SCOPED_HISTOGRAM_TIMER_H_
33