Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1179)

Side by Side Diff: base/profiler/cpu_profiler.cc

Issue 888923004: Temporary commit to evaluate perf impact of prototype CPU profiler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkcr
Patch Set: address comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 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/profiler/cpu_profiler.h"
6
7 #include <stddef.h>
8
9 #include "base/debug/stack_trace.h"
10 #include "base/metrics/field_trial.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/threading/thread_id_name_manager.h"
13 #include "base/time/time.h"
14 #include "base/timer/elapsed_timer.h"
15
16 namespace base {
17
18 namespace {
19
20 const char kMode[] = "Mode";
21 const char kInitialDelay[] = "InitialDelay";
22 const char kNumberOfBursts[] = "NumberOfBursts";
23 const char kBurstIdleTime[] = "IdleTime";
24 const char kNumberOfSamples[] = "NumberOfSamples";
25 const char kSamplingSleepTime[] = "SamplingSleepTime";
26
27 } // namespace
28
29 CpuProfiler* CpuProfiler::g_instance_ = NULL;
30
31 SamplingThread::SamplingThread()
32 : thread_running_(false),
33 waitable_event_(false, false) {
34 }
35
36 SamplingThread::~SamplingThread() {}
37
38 void SamplingThread::ThreadMain() {
39 PlatformThread::SetName("Chrome_CPUProfilerThread");
40 thread_running_ = true;
41
cpu_(ooo_6.6-7.5) 2015/02/05 22:41:47 Note that GetInstance() is not thread safe. As-is
Mike Wittman 2015/02/06 20:01:14 Done.
42 CpuProfiler* instance = CpuProfiler::GetInstance();
43 if (instance->GetStringParam(kMode) == "bursts") {
44 int64 initial_delay = instance->GetInt64Param(kInitialDelay);
45 int number_of_bursts = instance->GetIntParam(kNumberOfBursts);
46 int64 burst_idle_time = instance->GetInt64Param(kBurstIdleTime);
47 int number_of_samples = instance->GetIntParam(kNumberOfSamples);
48 int64 sampling_sleep_time = instance->GetInt64Param(kSamplingSleepTime);
49
50 if (waitable_event_.TimedWait(TimeDelta::FromMicroseconds(initial_delay)))
51 return;
52 for (int i = 0; i < number_of_bursts; ++i) {
53 int64 delta = 0;
54 for (int j = 0; ; ++j) {
55 base::ElapsedTimer time;
56 GetSamples();
57 delta = time.Elapsed().InMicroseconds();
58 if (j == (number_of_samples - 1))
59 break;
60 if (delta < sampling_sleep_time) {
61 if (waitable_event_.TimedWait(
62 TimeDelta::FromMicroseconds(sampling_sleep_time - delta)))
63 return;
64 } else {
65 if (waitable_event_.TimedWait(
66 TimeDelta::FromMicroseconds(sampling_sleep_time)))
67 return;
68 }
69 }
70 if (waitable_event_.TimedWait(
71 TimeDelta::FromMicroseconds(burst_idle_time - delta)))
72 return;
73 }
74 }
75 }
76
77 void SamplingThread::Stop() {
78 waitable_event_.Signal();
79 }
80
81 void SamplingThread::GetSamples() {
82 CpuProfiler* instance = CpuProfiler::GetInstance();
83 instance->OnTimer();
84 }
85
86 // static
87 void CpuProfiler::Initialize(const std::map<std::string, std::string>* params) {
88 if (!CpuProfiler::IsPlatformSupported())
89 return;
90
91 CpuProfiler* instance = CpuProfiler::GetInstance();
92
93 // temp code ==================
94 std::map<std::string, std::string> default_params;
95 default_params[kMode] = "bursts";
96 default_params[kInitialDelay] = "2000000"; // 2 sec
97 default_params[kNumberOfBursts] = "10";
98 default_params[kBurstIdleTime] = "10000000"; // 10 sec
99 default_params[kNumberOfSamples] = "20";
100 default_params[kSamplingSleepTime] = "50000"; // 10 ms
101
102 if (!params)
103 instance->SetParams(default_params);
104 else
105 // end temp code ==================
cpu_(ooo_6.6-7.5) 2015/02/05 22:41:47 don't add a comment between single line else and t
Mike Wittman 2015/02/06 20:01:14 Done.
106 instance->SetParams(*params);
107
108 instance->sampling_thread_.reset(new SamplingThread());
109 if (!PlatformThread::Create(
110 0, instance->sampling_thread_.get(),
111 &instance->sampling_thread_handle_)) {
112 LOG(ERROR) << "failed to create thread";
113 }
114 }
115
116 // static
117 CpuProfiler* CpuProfiler::GetInstance() {
118 if (!g_instance_) {
119 g_instance_ = new CpuProfiler();
120 }
121 return g_instance_;
122 }
123
124 // static
125 void CpuProfiler::Stop() {
126 CpuProfiler* instance = CpuProfiler::GetInstance();
127 if (instance && instance->sampling_thread_)
128 instance->sampling_thread_->Stop();
129 }
130
131
132 std::string CpuProfiler::GetStringParam(const std::string& key) {
133 const auto entry = params_.find(key);
cpu_(ooo_6.6-7.5) 2015/02/05 22:41:47 const auto& ?
Mike Wittman 2015/02/06 20:01:14 If you prefer; not sure the additional iterator co
cpu_(ooo_6.6-7.5) 2015/02/07 01:27:27 I misread, as is is fine.
134 if (entry != params_.end()) {
135 return entry->second;
136 }
137 return "";
138 }
139
140 int CpuProfiler::GetIntParam(const std::string& key) {
141 const auto entry = params_.find(key);
142 if (entry != params_.end()) {
143 int output;
144 if (base::StringToInt(entry->second, &output))
145 return output;
146 }
147 return 0;
148 }
149
150 int64 CpuProfiler::GetInt64Param(const std::string& key) {
151 const auto entry = params_.find(key);
cpu_(ooo_6.6-7.5) 2015/02/05 22:41:47 same here I think you mean a reference.
Mike Wittman 2015/02/06 20:01:14 Done.
152 if (entry != params_.end()) {
153 int64 output;
154 if (base::StringToInt64(entry->second, &output))
155 return output;
156 }
157 return 0;
158 }
159
160 void CpuProfiler::SetParams(
161 const std::map<std::string, std::string>& params) {
162 params_ = params;
163 }
164
165 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698