1 // Copyright (c) 2010 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 "base/debug/profiler.h" 6 7 #include <string> 8 9 #include "base/process_util.h" 10 #include "base/string_util.h" 11 12 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) 13 #include "third_party/tcmalloc/chromium/src/google/profiler.h" 14 #endif 15 16 namespace base { 17 namespace debug { 18 19 #if defined(ENABLE_PROFILING) && !defined(NO_TCMALLOC) 20 21 static int profile_count = 0; 22 23 void StartProfiling(const std::string& name) { 24 ++profile_count; 25 std::string full_name(name); 26 std::string pid = StringPrintf("%d", GetCurrentProcId()); 27 std::string count = StringPrintf("%d", profile_count); 28 ReplaceSubstringsAfterOffset(&full_name, 0, "{pid}", pid); 29 ReplaceSubstringsAfterOffset(&full_name, 0, "{count}", count); 30 ProfilerStart(full_name.c_str()); 31 } 32 33 void StopProfiling() { 34 ProfilerFlush(); 35 ProfilerStop(); 36 } 37 38 void FlushProfiling() { 39 ProfilerFlush(); 40 } 41 42 bool BeingProfiled() { 43 return ProfilingIsEnabledForAllThreads(); 44 } 45 46 #else 47 48 void StartProfiling(const std::string& name) { 49 } 50 51 void StopProfiling() { 52 } 53 54 void FlushProfiling() { 55 } 56 57 bool BeingProfiled() { 58 return false; 59 } 60 61 #endif 62 63 } // namespace debug 64 } // namespace base 65 66