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

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
« no previous file with comments | « base/profiler/cpu_profiler.h ('k') | base/profiler/cpu_profiler_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 class CpuProfiler::SamplingThread : public PlatformThread::Delegate {
30 public:
31 SamplingThread(CpuProfiler* cpu_profiler);
32 ~SamplingThread() override;
33
34 // Implementation of PlatformThread::Delegate:
35 void ThreadMain() override;
36
37 void Stop();
38
39 void GetSamples();
40
41 private:
42 CpuProfiler* cpu_profiler_;
43
44 bool thread_running_;
45
46 base::WaitableEvent waitable_event_;
47
48 DISALLOW_COPY_AND_ASSIGN(SamplingThread);
49 };
50
51 CpuProfiler::SamplingThread::SamplingThread(CpuProfiler* cpu_profiler)
52 : cpu_profiler_(cpu_profiler),
53 thread_running_(false),
54 waitable_event_(false, false) {
55 }
56
57 CpuProfiler::SamplingThread::~SamplingThread() {}
58
59 void CpuProfiler::SamplingThread::ThreadMain() {
60 PlatformThread::SetName("Chrome_CPUProfilerThread");
61 thread_running_ = true;
62
63 if (cpu_profiler_->GetStringParam(kMode) == "bursts") {
64 int64 initial_delay = cpu_profiler_->GetInt64Param(kInitialDelay);
65 int number_of_bursts = cpu_profiler_->GetIntParam(kNumberOfBursts);
66 int64 burst_idle_time = cpu_profiler_->GetInt64Param(kBurstIdleTime);
67 int number_of_samples = cpu_profiler_->GetIntParam(kNumberOfSamples);
68 int64 sampling_sleep_time =
69 cpu_profiler_->GetInt64Param(kSamplingSleepTime);
70
71 if (waitable_event_.TimedWait(TimeDelta::FromMicroseconds(initial_delay)))
72 return;
73 for (int i = 0; i < number_of_bursts; ++i) {
74 int64 delta = 0;
75 for (int j = 0; ; ++j) {
76 base::ElapsedTimer time;
77 GetSamples();
78 delta = time.Elapsed().InMicroseconds();
79 if (j == (number_of_samples - 1))
80 break;
81 if (delta < sampling_sleep_time) {
82 if (waitable_event_.TimedWait(
83 TimeDelta::FromMicroseconds(sampling_sleep_time - delta)))
84 return;
85 } else {
86 if (waitable_event_.TimedWait(
87 TimeDelta::FromMicroseconds(sampling_sleep_time)))
88 return;
89 }
90 }
91 if (waitable_event_.TimedWait(
92 TimeDelta::FromMicroseconds(burst_idle_time - delta)))
93 return;
94 }
95 }
96 }
97
98 void CpuProfiler::SamplingThread::Stop() {
99 waitable_event_.Signal();
100 }
101
102 void CpuProfiler::SamplingThread::GetSamples() {
103 cpu_profiler_->OnTimer();
104 }
105
106 void CpuProfiler::SamplingThreadDeleter::operator()(
107 SamplingThread* thread) const {
108 delete thread;
109 }
110
111 void CpuProfiler::Initialize(const std::map<std::string, std::string>* params) {
112 if (!CpuProfiler::IsPlatformSupported())
113 return;
114
115 std::map<std::string, std::string> default_params;
116 default_params[kMode] = "bursts";
117 default_params[kInitialDelay] = "0"; // 0 sec
118 default_params[kNumberOfBursts] = "1";
119 default_params[kBurstIdleTime] = "10000000"; // 10 sec
120 default_params[kNumberOfSamples] = "300";
121 default_params[kSamplingSleepTime] = "100000"; // 100 ms
122
123 if (!params)
124 SetParams(default_params);
125 else
126 SetParams(*params);
127
128 sampling_thread_.reset(new SamplingThread(this));
129 if (!PlatformThread::Create(0, sampling_thread_.get(),
130 &sampling_thread_handle_)) {
131 LOG(ERROR) << "failed to create thread";
132 }
133 }
134
135 void CpuProfiler::Stop() {
136 if (sampling_thread_)
137 sampling_thread_->Stop();
138 }
139
140
141 std::string CpuProfiler::GetStringParam(const std::string& key) const{
142 const auto entry = params_.find(key);
143 if (entry != params_.end()) {
144 return entry->second;
145 }
146 return "";
147 }
148
149 int CpuProfiler::GetIntParam(const std::string& key) const {
150 const auto entry = params_.find(key);
151 if (entry != params_.end()) {
152 int output;
153 if (base::StringToInt(entry->second, &output))
154 return output;
155 }
156 return 0;
157 }
158
159 int64 CpuProfiler::GetInt64Param(const std::string& key) const {
160 const auto entry = params_.find(key);
161 if (entry != params_.end()) {
162 int64 output;
163 if (base::StringToInt64(entry->second, &output))
164 return output;
165 }
166 return 0;
167 }
168
169 void CpuProfiler::SetParams(
170 const std::map<std::string, std::string>& params) {
171 params_ = params;
172 }
173
174 } // namespace base
OLDNEW
« no previous file with comments | « base/profiler/cpu_profiler.h ('k') | base/profiler/cpu_profiler_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698