scoped_tracker.h revision b8cf94937c52feb53b55c39e3f82094d27de464c
1// Copyright 2014 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 BASE_PROFILER_SCOPED_TRACKER_H_
6#define BASE_PROFILER_SCOPED_TRACKER_H_
7
8//------------------------------------------------------------------------------
9// Utilities for temporarily instrumenting code to dig into issues that were
10// found using profiler data.
11
12#include "base/base_export.h"
13#include "base/bind.h"
14#include "base/callback_forward.h"
15#include "base/location.h"
16#include "base/profiler/scoped_profile.h"
17
18namespace tracked_objects {
19
20// ScopedTracker instruments a region within the code if the instrumentation is
21// enabled. It can be used, for example, to find out if a source of jankiness is
22// inside the instrumented code region.
23// Details:
24// 1. This class creates a task (like ones created by PostTask calls or IPC
25// message handlers). This task can be seen in chrome://profiler and is sent as
26// a part of profiler data to the UMA server. See profiler_event.proto.
27// 2. That task's lifetime is same as the lifetime of the ScopedTracker
28// instance.
29// 3. The execution time associated with the task is the wallclock time between
30// its constructor and destructor, minus wallclock times of directly nested
31// tasks.
32// 4. Task creation that this class utilizes is highly optimized.
33// 5. The class doesn't create a task unless this was enabled for the current
34// process. Search for ScopedTracker::Enable for the current list of processes
35// and channels where it's activated.
36// 6. The class is designed for temporarily instrumenting code to find
37// performance problems, after which the instrumentation must be removed.
38class BASE_EXPORT ScopedTracker {
39 public:
40  ScopedTracker(const Location& location);
41
42  // Enables instrumentation for the remainder of the current process' life. If
43  // this function is not called, all profiler instrumentations are no-ops.
44  static void Enable();
45
46  // Augments a |callback| with provided |location|. This is useful for
47  // instrumenting cases when we know that a jank is in a callback and there are
48  // many possible callbacks, but they come from a relatively small number of
49  // places. We can instrument these few places and at least know which one
50  // passes the janky callback.
51  template <typename P1>
52  static base::Callback<void(P1)> TrackCallback(
53      const Location& location,
54      const base::Callback<void(P1)>& callback) {
55    return base::Bind(&ScopedTracker::ExecuteAndTrackCallback<P1>, location,
56                      callback);
57  }
58
59 private:
60  // Executes |callback|, augmenting it with provided |location|.
61  template <typename P1>
62  static void ExecuteAndTrackCallback(const Location& location,
63                                      const base::Callback<void(P1)>& callback,
64                                      P1 p1) {
65    ScopedTracker tracking_profile(location);
66    callback.Run(p1);
67  }
68
69  const ScopedProfile scoped_profile_;
70
71  DISALLOW_COPY_AND_ASSIGN(ScopedTracker);
72};
73
74}  // namespace tracked_objects
75
76#endif  // BASE_PROFILER_SCOPED_TRACKER_H_
77