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

Side by Side Diff: content/browser/tracing/tracing_ui.cc

Issue 109933006: Implement sampling profiler (chromium side change) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: All comments addressed Created 7 years 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/tracing/tracing_ui.h" 5 #include "content/browser/tracing/tracing_ui.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/base64.h" 9 #include "base/base64.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 23 matching lines...) Expand all
34 for (std::set<std::string>::const_iterator it = categorySet.begin(); 34 for (std::set<std::string>::const_iterator it = categorySet.begin();
35 it != categorySet.end(); it++) { 35 it != categorySet.end(); it++) {
36 category_list->AppendString(*it); 36 category_list->AppendString(*it);
37 } 37 }
38 38
39 base::RefCountedString* res = new base::RefCountedString(); 39 base::RefCountedString* res = new base::RefCountedString();
40 base::JSONWriter::Write(category_list.get(), &res->data()); 40 base::JSONWriter::Write(category_list.get(), &res->data());
41 callback.Run(res); 41 callback.Run(res);
42 } 42 }
43 43
44 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback); 44 bool GetTracingOptions(const std::string& data64,
45 45 std::string* category_filter_string,
46 bool OnBeginRecording(const std::string& data64, 46 int* tracing_options)
47 const WebUIDataSource::GotDataCallback& callback) { 47 {
48 std::string data; 48 std::string data;
49 if (!base::Base64Decode(data64, &data)) { 49 if (!base::Base64Decode(data64, &data)) {
50 LOG(ERROR) << "Options were not base64 encoded."; 50 LOG(ERROR) << "Options were not base64 encoded.";
51 return false; 51 return false;
52 } 52 }
53 53
54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data)); 54 scoped_ptr<base::Value> optionsRaw(base::JSONReader::Read(data));
55 if (!optionsRaw) { 55 if (!optionsRaw) {
56 LOG(ERROR) << "Options were not valid JSON"; 56 LOG(ERROR) << "Options were not valid JSON";
57 return false; 57 return false;
58 } 58 }
59 base::DictionaryValue* options; 59 base::DictionaryValue* options;
60 if (!optionsRaw->GetAsDictionary(&options)) { 60 if (!optionsRaw->GetAsDictionary(&options)) {
61 LOG(ERROR) << "Options must be dict"; 61 LOG(ERROR) << "Options must be dict";
62 return false; 62 return false;
63 } 63 }
64 64
65 std::string category_filter_string;
66 bool use_system_tracing; 65 bool use_system_tracing;
67 bool use_continuous_tracing; 66 bool use_continuous_tracing;
68 bool use_sampling; 67 bool use_sampling;
69 68
70 bool options_ok = true; 69 bool options_ok = true;
71 options_ok &= options->GetString("categoryFilter", &category_filter_string); 70 options_ok &= options->GetString("categoryFilter", category_filter_string);
72 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing); 71 options_ok &= options->GetBoolean("useSystemTracing", &use_system_tracing);
73 options_ok &= options->GetBoolean("useContinuousTracing", 72 options_ok &= options->GetBoolean("useContinuousTracing",
74 &use_continuous_tracing); 73 &use_continuous_tracing);
75 options_ok &= options->GetBoolean("useSampling", &use_sampling); 74 options_ok &= options->GetBoolean("useSampling", &use_sampling);
76 if (!options_ok) { 75 if (!options_ok) {
77 LOG(ERROR) << "Malformed options"; 76 LOG(ERROR) << "Malformed options";
78 return false; 77 return false;
79 } 78 }
80 79
80 *tracing_options = 0;
81 if (use_system_tracing)
82 *tracing_options |= TracingController::ENABLE_SYSTRACE;
83 if (use_sampling)
84 *tracing_options |= TracingController::ENABLE_SAMPLING;
85 if (use_continuous_tracing)
86 *tracing_options |= TracingController::RECORD_CONTINUOUSLY;
87 return true;
88 }
89
90 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback);
91
92 bool OnBeginRecording(const std::string& data64,
93 const WebUIDataSource::GotDataCallback& callback) {
94 std::string category_filter_string;
81 int tracing_options = 0; 95 int tracing_options = 0;
82 if (use_system_tracing) 96 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options))
83 tracing_options |= TracingController::ENABLE_SYSTRACE; 97 return false;
84 if (use_sampling)
85 tracing_options |= TracingController::ENABLE_SAMPLING;
86 if (use_continuous_tracing)
87 tracing_options |= TracingController::RECORD_CONTINUOUSLY;
88 98
89 return TracingController::GetInstance()->EnableRecording( 99 return TracingController::GetInstance()->EnableRecording(
90 category_filter_string, 100 category_filter_string,
91 static_cast<TracingController::Options>(tracing_options), 101 static_cast<TracingController::Options>(tracing_options),
92 base::Bind(&OnRecordingEnabledAck, callback)); 102 base::Bind(&OnRecordingEnabledAck, callback));
93 } 103 }
94 104
95 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) { 105 void OnRecordingEnabledAck(const WebUIDataSource::GotDataCallback& callback) {
96 base::RefCountedString* res = new base::RefCountedString(); 106 base::RefCountedString* res = new base::RefCountedString();
97 callback.Run(res); 107 callback.Run(res);
(...skipping 15 matching lines...) Expand all
113 } 123 }
114 124
115 void BeginReadingRecordingResult( 125 void BeginReadingRecordingResult(
116 const WebUIDataSource::GotDataCallback& callback, 126 const WebUIDataSource::GotDataCallback& callback,
117 const base::FilePath& path) { 127 const base::FilePath& path) {
118 BrowserThread::PostTask( 128 BrowserThread::PostTask(
119 BrowserThread::FILE, FROM_HERE, 129 BrowserThread::FILE, FROM_HERE,
120 base::Bind(ReadRecordingResult, callback, path)); 130 base::Bind(ReadRecordingResult, callback, path));
121 } 131 }
122 132
123 bool OnBeginRequest(const std::string& path, 133 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback);
134
135 bool OnEnableMonitoring(const std::string& data64,
136 const WebUIDataSource::GotDataCallback& callback) {
137 std::string category_filter_string;
138 int tracing_options = 0;
139 if (!GetTracingOptions(data64, &category_filter_string, &tracing_options))
140 return false;
141
142 return TracingController::GetInstance()->EnableMonitoring(
143 category_filter_string,
144 static_cast<TracingController::Options>(tracing_options),
145 base::Bind(OnMonitoringEnabledAck, callback));
146 }
147
148 void OnMonitoringEnabledAck(const WebUIDataSource::GotDataCallback& callback) {
149 base::RefCountedString* res = new base::RefCountedString();
150 callback.Run(res);
151 }
152
153 void OnMonitoringDisabled(const WebUIDataSource::GotDataCallback& callback) {
154 base::RefCountedString* res = new base::RefCountedString();
155 callback.Run(res);
156 }
157
158 void ReadMonitoringSnapshot(const WebUIDataSource::GotDataCallback& callback,
159 const base::FilePath& path) {
160 std::string tmp;
161 if (!base::ReadFileToString(path, &tmp))
162 LOG(ERROR) << "Failed to read file " << path.value();
163 base::DeleteFile(path, false);
164 callback.Run(base::RefCountedString::TakeString(&tmp));
165 }
166
167 void OnMonitoringSnapshotCaptured(
168 const WebUIDataSource::GotDataCallback& callback,
169 const base::FilePath& path) {
170 BrowserThread::PostTask(
171 BrowserThread::FILE, FROM_HERE,
172 base::Bind(ReadMonitoringSnapshot, callback, path));
173 }
174
175 bool OnTracingRequest(const std::string& path,
124 const WebUIDataSource::GotDataCallback& callback) { 176 const WebUIDataSource::GotDataCallback& callback) {
125 if (path == "json/categories") { 177 if (path == "json/categories") {
126 TracingController::GetInstance()->GetCategories( 178 TracingController::GetInstance()->GetCategories(
127 base::Bind(OnGotCategories, callback)); 179 base::Bind(OnGotCategories, callback));
128 return true; 180 return true;
129 } 181 }
182
130 const char* beginRecordingPath = "json/begin_recording?"; 183 const char* beginRecordingPath = "json/begin_recording?";
131 if (path.find(beginRecordingPath) == 0) { 184 if (path.find(beginRecordingPath) == 0) {
132 std::string data = path.substr(strlen(beginRecordingPath)); 185 std::string data = path.substr(strlen(beginRecordingPath));
133 return OnBeginRecording(data, callback); 186 return OnBeginRecording(data, callback);
134 } 187 }
135 if (path == "json/get_buffer_percent_full") { 188 if (path == "json/get_buffer_percent_full") {
136 return TracingController::GetInstance()->GetTraceBufferPercentFull( 189 return TracingController::GetInstance()->GetTraceBufferPercentFull(
137 base::Bind(OnTraceBufferPercentFullResult, callback)); 190 base::Bind(OnTraceBufferPercentFullResult, callback));
138 } 191 }
139 if (path == "json/end_recording") { 192 if (path == "json/end_recording") {
140 return TracingController::GetInstance()->DisableRecording( 193 return TracingController::GetInstance()->DisableRecording(
141 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback)); 194 base::FilePath(), base::Bind(BeginReadingRecordingResult, callback));
142 } 195 }
196
197 const char* enableMonitoringPath = "json/begin_monitoring?";
198 if (path.find(enableMonitoringPath) == 0) {
199 std::string data = path.substr(strlen(enableMonitoringPath));
200 return OnEnableMonitoring(data, callback);
201 }
202 if (path == "json/end_monitoring") {
203 return TracingController::GetInstance()->DisableMonitoring(
204 base::Bind(OnMonitoringDisabled, callback));
205 }
206 if (path == "json/capture_monitoring") {
207 TracingController::GetInstance()->CaptureMonitoringSnapshot(
208 base::FilePath(), base::Bind(OnMonitoringSnapshotCaptured, callback));
209 return true;
210 }
211
143 if (StartsWithASCII(path, "json/", true)) 212 if (StartsWithASCII(path, "json/", true))
144 LOG(ERROR) << "Unhandled request to " << path; 213 LOG(ERROR) << "Unhandled request to " << path;
145 return false; 214 return false;
146 } 215 }
147 216
148 } // namespace 217 } // namespace
149 218
150 219
151 //////////////////////////////////////////////////////////////////////////////// 220 ////////////////////////////////////////////////////////////////////////////////
152 // 221 //
153 // TracingUI 222 // TracingUI
154 // 223 //
155 //////////////////////////////////////////////////////////////////////////////// 224 ////////////////////////////////////////////////////////////////////////////////
156 225
157 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) { 226 TracingUI::TracingUI(WebUI* web_ui) : WebUIController(web_ui) {
158 // Set up the chrome://tracing/ source. 227 // Set up the chrome://tracing/ source.
159 BrowserContext* browser_context = 228 BrowserContext* browser_context =
160 web_ui->GetWebContents()->GetBrowserContext(); 229 web_ui->GetWebContents()->GetBrowserContext();
161 230
162 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost); 231 WebUIDataSource* source = WebUIDataSource::Create(kChromeUITracingHost);
163 source->SetJsonPath("strings.js"); 232 source->SetJsonPath("strings.js");
164 source->SetDefaultResource(IDR_TRACING_HTML); 233 source->SetDefaultResource(IDR_TRACING_HTML);
165 source->AddResourcePath("tracing.js", IDR_TRACING_JS); 234 source->AddResourcePath("tracing.js", IDR_TRACING_JS);
166 source->SetRequestFilter(base::Bind(OnBeginRequest)); 235 source->SetRequestFilter(base::Bind(OnTracingRequest));
167 WebUIDataSource::Add(browser_context, source); 236 WebUIDataSource::Add(browser_context, source);
168 } 237 }
169 238
170 } // namespace content 239 } // namespace content
OLDNEW
« base/debug/trace_event_impl.cc ('K') | « content/browser/tracing/tracing_controller_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698