Home | History | Annotate | Download | only in profiler
      1 // Copyright (c) 2011 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 
      6 #ifndef BASE_PROFILER_SCOPED_PROFILE_H_
      7 #define BASE_PROFILER_SCOPED_PROFILE_H_
      8 
      9 //------------------------------------------------------------------------------
     10 // ScopedProfile provides basic helper functions for profiling a short
     11 // region of code within a scope.  It is separate from the related ThreadData
     12 // class so that it can be included without much other cruft, and provide the
     13 // macros listed below.
     14 
     15 #include "base/base_export.h"
     16 #include "base/location.h"
     17 #include "base/macros.h"
     18 #include "base/profiler/tracked_time.h"
     19 #include "base/trace_event/heap_profiler.h"
     20 #include "base/tracked_objects.h"
     21 
     22 // Two level indirection is required for correct macro substitution.
     23 #define PASTE_COUNTER_ON_NAME2(name, counter) name##counter
     24 #define PASTE_COUNTER_ON_NAME(name, counter) \
     25   PASTE_COUNTER_ON_NAME2(name, counter)
     26 
     27 #define COUNTER_BASED_VARIABLE_NAME_FOR_PROFILING \
     28   PASTE_COUNTER_ON_NAME(some_profiler_variable_, __COUNTER__)
     29 
     30 // Defines the containing scope as a profiled region. This allows developers to
     31 // profile their code and see results on their about:profiler page, as well as
     32 // on the UMA dashboard and heap profiler.
     33 #define TRACK_RUN_IN_THIS_SCOPED_REGION(dispatch_function_name)               \
     34   const ::tracked_objects::Location& location =                               \
     35       FROM_HERE_WITH_EXPLICIT_FUNCTION(#dispatch_function_name);              \
     36   TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION                               \
     37   COUNTER_BASED_VARIABLE_NAME_FOR_PROFILING(location.file_name());            \
     38   ::tracked_objects::ScopedProfile COUNTER_BASED_VARIABLE_NAME_FOR_PROFILING( \
     39       location, ::tracked_objects::ScopedProfile::ENABLED)
     40 
     41 // Same as TRACK_RUN_IN_THIS_SCOPED_REGION except that there's an extra param
     42 // which is concatenated with the function name for better filtering.
     43 #define TRACK_SCOPED_REGION(category_name, dispatch_function_name)            \
     44   const ::tracked_objects::Location& location =                               \
     45       FROM_HERE_WITH_EXPLICIT_FUNCTION("[" category_name                      \
     46                                        "]" dispatch_function_name);           \
     47   TRACE_HEAP_PROFILER_API_SCOPED_TASK_EXECUTION                               \
     48   COUNTER_BASED_VARIABLE_NAME_FOR_PROFILING(location.file_name());            \
     49   ::tracked_objects::ScopedProfile COUNTER_BASED_VARIABLE_NAME_FOR_PROFILING( \
     50       location, ::tracked_objects::ScopedProfile::ENABLED)
     51 
     52 namespace tracked_objects {
     53 class Births;
     54 
     55 class BASE_EXPORT ScopedProfile {
     56  public:
     57   // Mode of operation. Specifies whether ScopedProfile should be a no-op or
     58   // needs to create and tally a task.
     59   enum Mode {
     60     DISABLED,  // Do nothing.
     61     ENABLED    // Create and tally a task.
     62   };
     63 
     64   ScopedProfile(const Location& location, Mode mode);
     65   ~ScopedProfile();
     66 
     67  private:
     68   Births* birth_;  // Place in code where tracking started.
     69   TaskStopwatch stopwatch_;
     70 
     71   DISALLOW_COPY_AND_ASSIGN(ScopedProfile);
     72 };
     73 
     74 }  // namespace tracked_objects
     75 
     76 #endif  // BASE_PROFILER_SCOPED_PROFILE_H_
     77