OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/renderer/stats_collection_controller.h" | 5 #include "content/renderer/stats_collection_controller.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/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/metrics/statistics_recorder.h" | 11 #include "base/metrics/statistics_recorder.h" |
| 12 #include "base/metrics/stats_table.h" |
12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
13 #include "content/common/child_process_messages.h" | 14 #include "content/common/child_process_messages.h" |
14 #include "content/renderer/render_view_impl.h" | 15 #include "content/renderer/render_view_impl.h" |
15 #include "third_party/WebKit/public/web/WebFrame.h" | 16 #include "third_party/WebKit/public/web/WebFrame.h" |
16 #include "third_party/WebKit/public/web/WebView.h" | 17 #include "third_party/WebKit/public/web/WebView.h" |
17 | 18 |
18 using webkit_glue::CppArgumentList; | 19 using webkit_glue::CppArgumentList; |
19 using webkit_glue::CppVariant; | 20 using webkit_glue::CppVariant; |
20 | 21 |
21 namespace content { | 22 namespace content { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 } | 67 } |
67 if (load_start_time.is_null() || load_stop_time.is_null()) { | 68 if (load_start_time.is_null() || load_stop_time.is_null()) { |
68 item.Set("load_duration_ms", base::Value::CreateNullValue()); | 69 item.Set("load_duration_ms", base::Value::CreateNullValue()); |
69 } else { | 70 } else { |
70 item.SetDouble("load_duration_ms", | 71 item.SetDouble("load_duration_ms", |
71 (load_stop_time - load_start_time).InMilliseconds()); | 72 (load_stop_time - load_start_time).InMilliseconds()); |
72 } | 73 } |
73 base::JSONWriter::Write(&item, result); | 74 base::JSONWriter::Write(&item, result); |
74 } | 75 } |
75 | 76 |
| 77 |
| 78 // Encodes StatsTable as json. |
| 79 // - |stats_table| - StatsTable to encode. |
| 80 // - |pid| - The process id to get stats for (0 for all processes). |
| 81 // - |result| - returned JSON. |
| 82 // Example return value: |
| 83 // {'c:V8:TotalEvalSize': 102664, 'c:V8:TotalLoadSize': 164356} |
| 84 void ConvertStatsTableToJSON( |
| 85 const base::StatsTable* stats_table, |
| 86 const base::ProcessId pid, |
| 87 std::string *result) { |
| 88 base::DictionaryValue counters; |
| 89 // NOTE: Counters start at index 1. |
| 90 for (int index = 1; index <= stats_table->GetMaxCounters(); index++) { |
| 91 std::string name = stats_table->GetRowName(index); |
| 92 if (name.length() == 0) |
| 93 break; |
| 94 |
| 95 // JSON doesn't allow '.' in names. |
| 96 size_t pos; |
| 97 while ((pos = name.find(".")) != std::string::npos) |
| 98 name.replace(pos, 1, ":"); |
| 99 |
| 100 counters.SetInteger(name, stats_table->GetRowValue(index, pid)); |
| 101 } |
| 102 base::JSONWriter::Write(&counters, result); |
| 103 } |
| 104 |
76 } // namespace | 105 } // namespace |
77 | 106 |
78 StatsCollectionController::StatsCollectionController() | 107 StatsCollectionController::StatsCollectionController() |
79 : sender_(NULL) { | 108 : sender_(NULL) { |
80 BindCallback("getHistogram", | 109 BindCallback("getHistogram", |
81 base::Bind(&StatsCollectionController::GetHistogram, | 110 base::Bind(&StatsCollectionController::GetHistogram, |
82 base::Unretained(this))); | 111 base::Unretained(this))); |
83 BindCallback("getBrowserHistogram", | 112 BindCallback("getBrowserHistogram", |
84 base::Bind(&StatsCollectionController::GetBrowserHistogram, | 113 base::Bind(&StatsCollectionController::GetBrowserHistogram, |
85 base::Unretained(this))); | 114 base::Unretained(this))); |
| 115 BindCallback("getStatsTable", |
| 116 base::Bind( |
| 117 &StatsCollectionController::GetStatsTable, |
| 118 base::Unretained(this))); |
86 BindCallback("tabLoadTiming", | 119 BindCallback("tabLoadTiming", |
87 base::Bind( | 120 base::Bind( |
88 &StatsCollectionController::GetTabLoadTiming, | 121 &StatsCollectionController::GetTabLoadTiming, |
89 base::Unretained(this))); | 122 base::Unretained(this))); |
90 } | 123 } |
91 | 124 |
92 void StatsCollectionController::GetHistogram(const CppArgumentList& args, | 125 void StatsCollectionController::GetHistogram(const CppArgumentList& args, |
93 CppVariant* result) { | 126 CppVariant* result) { |
94 if (args.size() != 1) { | 127 if (args.size() != 1) { |
95 result->SetNull(); | 128 result->SetNull(); |
96 return; | 129 return; |
97 } | 130 } |
98 base::HistogramBase* histogram = | 131 base::HistogramBase* histogram = |
99 base::StatisticsRecorder::FindHistogram(args[0].ToString()); | 132 base::StatisticsRecorder::FindHistogram(args[0].ToString()); |
100 std::string output; | 133 std::string output; |
101 if (!histogram) { | 134 if (!histogram) { |
102 output = "{}"; | 135 output = "{}"; |
103 } else { | 136 } else { |
104 histogram->WriteJSON(&output); | 137 histogram->WriteJSON(&output); |
105 } | 138 } |
106 result->Set(output); | 139 result->Set(output); |
107 } | 140 } |
108 | 141 |
109 void StatsCollectionController::GetBrowserHistogram(const CppArgumentList& args, | 142 void StatsCollectionController::GetBrowserHistogram(const CppArgumentList& args, |
110 CppVariant* result) { | 143 CppVariant* result) { |
111 if (args.size() != 1) { | 144 if (args.size() != 1) { |
112 result->SetNull(); | 145 result->SetNull(); |
113 return; | 146 return; |
114 } | 147 } |
115 | 148 |
116 if (!sender_) { | 149 if (!sender_) { |
117 NOTREACHED(); | 150 NOTREACHED(); |
118 result->SetNull(); | 151 result->SetNull(); |
119 return; | 152 return; |
120 } | 153 } |
121 | 154 |
122 std::string histogram_json; | 155 std::string histogram_json; |
123 sender_->Send(new ChildProcessHostMsg_GetBrowserHistogram( | 156 sender_->Send(new ChildProcessHostMsg_GetBrowserHistogram( |
124 args[0].ToString(), &histogram_json)); | 157 args[0].ToString(), &histogram_json)); |
125 result->Set(histogram_json); | 158 result->Set(histogram_json); |
126 } | 159 } |
127 | 160 |
| 161 void StatsCollectionController::GetStatsTable(const CppArgumentList& args, |
| 162 CppVariant* result) { |
| 163 base::StatsTable* stats_table = base::StatsTable::current(); |
| 164 std::string output; |
| 165 if (!stats_table) { |
| 166 output = "{}"; |
| 167 } else { |
| 168 ConvertStatsTableToJSON(stats_table, base::GetCurrentProcId(), &output); |
| 169 } |
| 170 result->Set(output); |
| 171 } |
| 172 |
128 void StatsCollectionController::GetTabLoadTiming( | 173 void StatsCollectionController::GetTabLoadTiming( |
129 const CppArgumentList& args, | 174 const CppArgumentList& args, |
130 CppVariant* result) { | 175 CppVariant* result) { |
131 if (!sender_) { | 176 if (!sender_) { |
132 NOTREACHED(); | 177 NOTREACHED(); |
133 result->SetNull(); | 178 result->SetNull(); |
134 return; | 179 return; |
135 } | 180 } |
136 | 181 |
137 RenderViewImpl *render_view_impl = NULL; | 182 RenderViewImpl *render_view_impl = NULL; |
(...skipping 12 matching lines...) Expand all Loading... |
150 } | 195 } |
151 | 196 |
152 std::string tab_timing_json; | 197 std::string tab_timing_json; |
153 ConvertLoadTimeToJSON( | 198 ConvertLoadTimeToJSON( |
154 observer->load_start_time(), observer->load_stop_time(), | 199 observer->load_start_time(), observer->load_stop_time(), |
155 &tab_timing_json); | 200 &tab_timing_json); |
156 result->Set(tab_timing_json); | 201 result->Set(tab_timing_json); |
157 } | 202 } |
158 | 203 |
159 } // namespace content | 204 } // namespace content |
OLD | NEW |