Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "chrome/browser/ui/webui/performance_monitor/web_ui_handler.h" | 5 #include "chrome/browser/ui/webui/performance_monitor/web_ui_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" | |
| 10 #include "base/time.h" | 9 #include "base/time.h" |
| 11 #include "base/values.h" | 10 #include "base/values.h" |
| 12 #include "chrome/browser/performance_monitor/database.h" | 11 #include "chrome/browser/performance_monitor/database.h" |
| 12 #include "chrome/browser/performance_monitor/event.h" | |
| 13 #include "chrome/browser/performance_monitor/metric_details.h" | |
| 13 #include "chrome/browser/performance_monitor/performance_monitor.h" | 14 #include "chrome/browser/performance_monitor/performance_monitor.h" |
| 14 #include "chrome/browser/performance_monitor/performance_monitor_util.h" | 15 #include "chrome/browser/performance_monitor/performance_monitor_util.h" |
| 15 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/web_ui.h" | 17 #include "content/public/browser/web_ui.h" |
| 17 | 18 |
| 18 namespace performance_monitor { | 19 namespace performance_monitor { |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 22 // Queries the performance monitor database for active intervals between | |
| 23 // |start| and |end| times and appends the results to |results|. | |
| 21 void DoGetActiveIntervals(ListValue* results, | 24 void DoGetActiveIntervals(ListValue* results, |
| 22 const base::Time& start, const base::Time& end) { | 25 const base::Time& start, const base::Time& end) { |
| 23 std::vector<TimeRange> intervals = | 26 Database* db = PerformanceMonitor::GetInstance()->database(); |
| 24 PerformanceMonitor::GetInstance()->database()->GetActiveIntervals( | 27 std::vector<TimeRange> intervals = db->GetActiveIntervals(start, end); |
| 25 start, end); | 28 |
| 26 for (std::vector<TimeRange>::iterator it = intervals.begin(); | 29 for (std::vector<TimeRange>::iterator it = intervals.begin(); |
| 27 it != intervals.end(); ++it) { | 30 it != intervals.end(); ++it) { |
| 28 DictionaryValue* interval_value = new DictionaryValue(); | 31 DictionaryValue* interval_value = new DictionaryValue(); |
| 29 interval_value->SetDouble("start", it->start.ToDoubleT()); | 32 interval_value->SetDouble("start", it->start.ToDoubleT()); |
| 30 interval_value->SetDouble("end", it->end.ToDoubleT()); | 33 interval_value->SetDouble("end", it->end.ToDoubleT()); |
| 31 results->Append(interval_value); | 34 results->Append(interval_value); |
| 32 } | 35 } |
| 33 } | 36 } |
| 34 | 37 |
| 38 // Queries the performance monitor database for events of type |event_type| | |
| 39 // between |start| and |end| times and appends the results to |results|. | |
| 35 // TODO(mtytel): Add an internationalized longDescription field to each event. | 40 // TODO(mtytel): Add an internationalized longDescription field to each event. |
| 36 void DoGetEvents(ListValue* results, EventType event, | 41 void DoGetEvents(ListValue* results, EventType event_type, |
| 37 const base::Time& start, const base::Time& end) { | 42 const base::Time& start, const base::Time& end) { |
| 43 Database* db = PerformanceMonitor::GetInstance()->database(); | |
| 38 std::vector<linked_ptr<Event> > events = | 44 std::vector<linked_ptr<Event> > events = |
| 39 PerformanceMonitor::GetInstance()->database()->GetEvents( | 45 db->GetEvents(event_type, start, end); |
| 40 event, start, end); | 46 |
| 41 for (std::vector<linked_ptr<Event> >::iterator it = events.begin(); | 47 for (std::vector<linked_ptr<Event> >::iterator it = events.begin(); |
| 42 it != events.end(); ++it) { | 48 it != events.end(); ++it) { |
| 43 results->Append((*it)->data()); | 49 results->Append((*it)->data()->DeepCopy()); |
| 44 } | 50 } |
| 45 } | 51 } |
| 46 | 52 |
| 47 bool PostTaskToDatabaseAndReply(const base::Closure& request, | 53 // Queries the performance monitor database for metrics of type |metric_type| |
| 48 const base::Closure& reply) { | 54 // between |start| and |end| times and appends the results to |results|. |
| 49 base::SequencedWorkerPool* pool = content::BrowserThread::GetBlockingPool(); | 55 void DoGetMetric(ListValue* results, |
| 50 base::SequencedWorkerPool::SequenceToken token = pool->GetSequenceToken(); | 56 MetricType metric_type, |
| 51 return pool->GetSequencedTaskRunner(token)->PostTaskAndReply( | 57 const base::Time& start, const base::Time& end, |
| 52 FROM_HERE, request, reply); | 58 const base::TimeDelta& resolution) { |
| 59 Database* db = PerformanceMonitor::GetInstance()->database(); | |
| 60 Database::MetricVectorMap metric_vector_map = db->GetStatsForMetricByActivity( | |
| 61 MetricTypeToString(metric_type), start, end); | |
| 62 | |
| 63 linked_ptr<Database::MetricInfoVector> metric_vector = | |
| 64 metric_vector_map[kProcessChromeAggregate]; | |
| 65 if (!metric_vector.get()) | |
| 66 metric_vector.reset(new Database::MetricInfoVector()); | |
| 67 | |
| 68 Database::MetricInfoVector aggregated_metrics = | |
| 69 util::AggregateMetric(*(metric_vector.get()), start, resolution); | |
|
koz (OOO until 15th September)
2012/07/11 00:19:58
*(metric_vector.get()) -> *metric_vector
| |
| 70 for (Database::MetricInfoVector::iterator it = aggregated_metrics.begin(); | |
| 71 it != aggregated_metrics.end(); ++it) { | |
| 72 DictionaryValue* metric_value = new DictionaryValue(); | |
| 73 metric_value->SetDouble("time", it->time.ToDoubleT()); | |
| 74 metric_value->SetDouble("value", it->value); | |
| 75 results->Append(metric_value); | |
| 76 } | |
| 53 } | 77 } |
| 54 | 78 |
| 55 } // namespace | 79 } // namespace |
| 56 | 80 |
| 57 WebUIHandler::WebUIHandler() {} | 81 WebUIHandler::WebUIHandler() {} |
| 58 WebUIHandler::~WebUIHandler() {} | 82 WebUIHandler::~WebUIHandler() {} |
| 59 | 83 |
| 60 void WebUIHandler::RegisterMessages() { | 84 void WebUIHandler::RegisterMessages() { |
| 61 web_ui()->RegisterMessageCallback( | 85 web_ui()->RegisterMessageCallback( |
| 62 "getActiveIntervals", | 86 "getActiveIntervals", |
| 63 base::Bind(&WebUIHandler::HandleGetActiveIntervals, | 87 base::Bind(&WebUIHandler::HandleGetActiveIntervals, |
| 64 base::Unretained(this))); | 88 base::Unretained(this))); |
| 65 web_ui()->RegisterMessageCallback( | 89 web_ui()->RegisterMessageCallback( |
| 66 "getAllEventTypes", | 90 "getAllEventTypes", |
| 67 base::Bind(&WebUIHandler::HandleGetAllEventTypes, | 91 base::Bind(&WebUIHandler::HandleGetAllEventTypes, |
| 68 base::Unretained(this))); | 92 base::Unretained(this))); |
| 69 web_ui()->RegisterMessageCallback( | 93 web_ui()->RegisterMessageCallback( |
| 70 "getEvents", | 94 "getEvents", |
| 71 base::Bind(&WebUIHandler::HandleGetEvents, | 95 base::Bind(&WebUIHandler::HandleGetEvents, |
| 72 base::Unretained(this))); | 96 base::Unretained(this))); |
| 97 web_ui()->RegisterMessageCallback( | |
| 98 "getAllMetricTypes", | |
| 99 base::Bind(&WebUIHandler::HandleGetAllMetricTypes, | |
| 100 base::Unretained(this))); | |
| 101 web_ui()->RegisterMessageCallback( | |
| 102 "getMetric", | |
| 103 base::Bind(&WebUIHandler::HandleGetMetric, | |
| 104 base::Unretained(this))); | |
| 73 } | 105 } |
| 74 | 106 |
| 75 void WebUIHandler::ReturnResults(const std::string& function, | 107 void WebUIHandler::ReturnResults(const std::string& function, |
| 76 const ListValue* results) { | 108 const ListValue* results) { |
| 77 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 109 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 78 web_ui()->CallJavascriptFunction(function, *results); | 110 web_ui()->CallJavascriptFunction(function, *results); |
| 79 } | 111 } |
| 80 | 112 |
| 81 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { | 113 void WebUIHandler::HandleGetActiveIntervals(const ListValue* args) { |
| 82 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 83 CHECK_EQ(2u, args->GetSize()); | 115 CHECK_EQ(2u, args->GetSize()); |
| 84 double double_time = 0.0; | 116 double double_time = 0.0; |
| 85 CHECK(args->GetDouble(0, &double_time)); | 117 CHECK(args->GetDouble(0, &double_time)); |
| 86 base::Time start = base::Time::FromJsTime(double_time); | 118 base::Time start = base::Time::FromJsTime(double_time); |
| 87 CHECK(args->GetDouble(1, &double_time)); | 119 CHECK(args->GetDouble(1, &double_time)); |
| 88 base::Time end = base::Time::FromJsTime(double_time); | 120 base::Time end = base::Time::FromJsTime(double_time); |
| 89 | 121 |
| 90 ListValue* results = new ListValue(); | 122 ListValue* results = new ListValue(); |
| 91 PostTaskToDatabaseAndReply( | 123 util::PostTaskToDatabaseThreadAndReply( |
| 92 base::Bind(&DoGetActiveIntervals, results, start, end), | 124 base::Bind(&DoGetActiveIntervals, results, start, end), |
| 93 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 125 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 94 "performance_monitor.getActiveIntervalsCallback", | 126 "performance_monitor.getActiveIntervalsCallback", |
| 95 base::Owned(results))); | 127 base::Owned(results))); |
| 96 } | 128 } |
| 97 | 129 |
| 98 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { | 130 void WebUIHandler::HandleGetAllEventTypes(const ListValue* args) { |
| 99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 131 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 100 CHECK_EQ(0u, args->GetSize()); | 132 CHECK_EQ(0u, args->GetSize()); |
| 101 ListValue results; | 133 ListValue results; |
| 102 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { | 134 for (int i = 0; i < EVENT_NUMBER_OF_EVENTS; ++i) { |
| 103 EventType event_type = static_cast<EventType>(i); | 135 EventType event_type = static_cast<EventType>(i); |
| 104 DictionaryValue* event_type_info = new DictionaryValue(); | 136 DictionaryValue* event_type_info = new DictionaryValue(); |
| 105 event_type_info->SetInteger("eventType", event_type); | 137 event_type_info->SetInteger("eventType", event_type); |
| 106 event_type_info->SetString("shortDescription", | 138 event_type_info->SetString("shortDescription", |
| 107 EventTypeToString(event_type)); | 139 EventTypeToString(event_type)); |
| 108 results.Append(event_type_info); | 140 results.Append(event_type_info); |
| 109 } | 141 } |
| 142 | |
| 110 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); | 143 ReturnResults("performance_monitor.getAllEventTypesCallback", &results); |
| 111 } | 144 } |
| 112 | 145 |
| 113 void WebUIHandler::HandleGetEvents(const ListValue* args) { | 146 void WebUIHandler::HandleGetEvents(const ListValue* args) { |
| 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 147 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 115 CHECK_EQ(3u, args->GetSize()); | 148 CHECK_EQ(3u, args->GetSize()); |
| 116 int event = 0; | 149 double event = 0; |
| 117 CHECK(args->GetInteger(0, &event)); | 150 CHECK(args->GetDouble(0, &event)); |
| 118 double double_time = 0.0; | 151 double double_time = 0.0; |
| 119 CHECK(args->GetDouble(1, &double_time)); | 152 CHECK(args->GetDouble(1, &double_time)); |
| 120 base::Time start = base::Time::FromJsTime(double_time); | 153 base::Time start = base::Time::FromJsTime(double_time); |
| 121 CHECK(args->GetDouble(2, &double_time)); | 154 CHECK(args->GetDouble(2, &double_time)); |
| 122 base::Time end = base::Time::FromJsTime(double_time); | 155 base::Time end = base::Time::FromJsTime(double_time); |
| 123 | 156 |
| 124 ListValue* results = new ListValue(); | 157 ListValue* results = new ListValue(); |
| 125 PostTaskToDatabaseAndReply( | 158 util::PostTaskToDatabaseThreadAndReply( |
| 126 base::Bind(&DoGetEvents, results, static_cast<EventType>(event), | 159 base::Bind(&DoGetEvents, results, static_cast<EventType>(event), |
| 127 start, end), | 160 start, end), |
| 128 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | 161 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), |
| 129 "performance_monitor.getEventsCallback", | 162 "performance_monitor.getEventsCallback", |
| 130 base::Owned(results))); | 163 base::Owned(results))); |
| 131 } | 164 } |
| 132 | 165 |
| 166 void WebUIHandler::HandleGetAllMetricTypes(const ListValue* args) { | |
| 167 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 168 CHECK_EQ(0u, args->GetSize()); | |
| 169 ListValue results; | |
| 170 for (int i = 0; i < METRIC_NUMBER_OF_METRICS; ++i) { | |
| 171 MetricType metric_type = static_cast<MetricType>(i); | |
| 172 DictionaryValue* metric_type_info = new DictionaryValue(); | |
| 173 metric_type_info->SetInteger("metricType", metric_type); | |
| 174 metric_type_info->SetString("shortDescription", | |
| 175 MetricTypeToString(metric_type)); | |
| 176 results.Append(metric_type_info); | |
| 177 } | |
| 178 | |
| 179 ReturnResults("performance_monitor.getAllMetricTypesCallback", &results); | |
| 180 } | |
| 181 | |
| 182 void WebUIHandler::HandleGetMetric(const ListValue* args) { | |
| 183 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 184 CHECK_EQ(4u, args->GetSize()); | |
| 185 double metric = 0; | |
| 186 CHECK(args->GetDouble(0, &metric)); | |
| 187 | |
| 188 double double_time = 0.0; | |
| 189 CHECK(args->GetDouble(1, &double_time)); | |
| 190 base::Time start = base::Time::FromJsTime(double_time); | |
| 191 CHECK(args->GetDouble(2, &double_time)); | |
| 192 base::Time end = base::Time::FromJsTime(double_time); | |
| 193 | |
| 194 double resolution_in_milliseconds = 0; | |
| 195 CHECK(args->GetDouble(3, &resolution_in_milliseconds)); | |
| 196 base::TimeDelta resolution = | |
| 197 base::TimeDelta::FromMilliseconds(resolution_in_milliseconds); | |
| 198 | |
| 199 ListValue* results = new ListValue(); | |
| 200 util::PostTaskToDatabaseThreadAndReply( | |
| 201 base::Bind(&DoGetMetric, results, static_cast<MetricType>(metric), | |
| 202 start, end, resolution), | |
| 203 base::Bind(&WebUIHandler::ReturnResults, AsWeakPtr(), | |
| 204 "performance_monitor.getMetricCallback", | |
| 205 base::Owned(results))); | |
| 206 } | |
| 207 | |
| 133 } // namespace performance_monitor | 208 } // namespace performance_monitor |
| OLD | NEW |