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

Side by Side Diff: dbus/dbus_statistics.cc

Issue 11363173: Add DBusStatistics and DBusLogSource to log and show dbus stats (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase Created 8 years, 1 month 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
« no previous file with comments | « dbus/dbus_statistics.h ('k') | dbus/dbus_statistics_unittest.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 (c) 2012 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 "dbus/dbus_statistics.h"
6
7 #include <set>
8
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/stl_util.h"
12 #include "base/stringprintf.h"
13 #include "base/time.h"
14
15 namespace dbus {
16
17 namespace {
18
19 // Used to store dbus statistics sorted alphabetically by service, interface,
20 // then method (using std::string <).
21 struct Stat {
22 Stat(const std::string& service,
23 const std::string& interface,
24 const std::string& method)
25 : service(service),
26 interface(interface),
27 method(method),
28 sent_method_calls(0),
29 received_signals(0),
30 sent_blocking_method_calls(0) {
31 }
32 std::string service;
33 std::string interface;
34 std::string method;
35 int sent_method_calls;
36 int received_signals;
37 int sent_blocking_method_calls;
38
39 bool Compare(const Stat& other) const {
40 if (service != other.service)
41 return service < other.service;
42 if (interface != other.interface)
43 return interface < other.interface;
44 return method < other.method;
45 }
46
47 struct PtrCompare {
48 bool operator()(Stat* lhs, Stat* rhs) const {
49 DCHECK(lhs && rhs);
50 return lhs->Compare(*rhs);
51 }
52 };
53 };
54
55 typedef std::set<Stat*, Stat::PtrCompare> StatSet;
56
57 //------------------------------------------------------------------------------
58 // DBusStatistics
59
60 // Simple class for gathering DBus usage statistics.
61 class DBusStatistics {
62 public:
63 DBusStatistics() : start_time_(base::Time::Now()) {
64 }
65
66 ~DBusStatistics() {
67 STLDeleteContainerPointers(stats_.begin(), stats_.end());
68 }
69
70 // Enum to specify which field in Stat to increment in AddStat
71 enum StatType {
72 TYPE_SENT_METHOD_CALLS,
73 TYPE_RECEIVED_SIGNALS,
74 TYPE_SENT_BLOCKING_METHOD_CALLS
75 };
76
77 // Add a call to |method| for |interface|. See also MethodCall in message.h.
78 void AddStat(const std::string& service,
79 const std::string& interface,
80 const std::string& method,
81 StatType type) {
82 Stat* stat = GetStat(service, interface, method, true);
83 DCHECK(stat);
84 if (type == TYPE_SENT_METHOD_CALLS)
85 ++stat->sent_method_calls;
86 else if (type == TYPE_RECEIVED_SIGNALS)
87 ++stat->received_signals;
88 else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS)
89 ++stat->sent_blocking_method_calls;
90 else
91 NOTREACHED();
92 }
93
94 // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry
95 // if one does not already exist.
96 Stat* GetStat(const std::string& service,
97 const std::string& interface,
98 const std::string& method,
99 bool add_stat) {
100 scoped_ptr<Stat> stat(new Stat(service, interface, method));
101 StatSet::iterator found = stats_.find(stat.get());
102 if (found != stats_.end())
103 return *found;
104 if (!add_stat)
105 return NULL;
106 found = stats_.insert(stat.release()).first;
107 return *found;
108 }
109
110 StatSet& stats() { return stats_; }
111 base::Time start_time() { return start_time_; }
112
113 private:
114 StatSet stats_;
115 base::Time start_time_;
116
117 DISALLOW_COPY_AND_ASSIGN(DBusStatistics);
118 };
119
120 DBusStatistics* g_dbus_statistics = NULL;
121
122 } // namespace
123
124 //------------------------------------------------------------------------------
125
126 namespace statistics {
127
128 void Initialize() {
129 if (g_dbus_statistics)
130 delete g_dbus_statistics; // reset statistics
131 g_dbus_statistics = new DBusStatistics();
132 }
133
134 void Shutdown() {
135 delete g_dbus_statistics;
136 g_dbus_statistics = NULL;
137 }
138
139 void AddSentMethodCall(const std::string& service,
140 const std::string& interface,
141 const std::string& method) {
142 if (!g_dbus_statistics)
143 return;
144 g_dbus_statistics->AddStat(
145 service, interface, method, DBusStatistics::TYPE_SENT_METHOD_CALLS);
146 }
147
148 void AddReceivedSignal(const std::string& service,
149 const std::string& interface,
150 const std::string& method) {
151 if (!g_dbus_statistics)
152 return;
153 g_dbus_statistics->AddStat(
154 service, interface, method, DBusStatistics::TYPE_RECEIVED_SIGNALS);
155 }
156
157 void AddBlockingSentMethodCall(const std::string& service,
158 const std::string& interface,
159 const std::string& method) {
160 if (!g_dbus_statistics)
161 return;
162 g_dbus_statistics->AddStat(
163 service, interface, method,
164 DBusStatistics::TYPE_SENT_BLOCKING_METHOD_CALLS);
165 }
166
167 // NOTE: If the output format is changed, be certain to change the test
168 // expectations as well.
169 std::string GetAsString(ShowInString show, FormatString format) {
170 if (!g_dbus_statistics)
171 return "DBusStatistics not initialized.";
172
173 const StatSet& stats = g_dbus_statistics->stats();
174 if (stats.empty())
175 return "No DBus calls.";
176
177 base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time();
178 int dminutes = dtime.InMinutes();
179 dminutes = std::max(dminutes, 1);
180
181 std::string result;
182 int sent = 0, received = 0, sent_blocking = 0;
183 // Stats are stored in order by service, then interface, then method.
184 for (StatSet::const_iterator iter = stats.begin(); iter != stats.end(); ) {
185 StatSet::const_iterator cur_iter = iter;
186 StatSet::const_iterator next_iter = ++iter;
187 const Stat* stat = *cur_iter;
188 sent += stat->sent_method_calls;
189 received += stat->received_signals;
190 sent_blocking += stat->sent_blocking_method_calls;
191 // If this is not the last stat, and if the next stat matches the current
192 // stat, continue.
193 if (next_iter != stats.end() &&
194 (*next_iter)->service == stat->service &&
195 (show < SHOW_INTERFACE || (*next_iter)->interface == stat->interface) &&
196 (show < SHOW_METHOD || (*next_iter)->method == stat->method))
197 continue;
198
199 if (!sent && !received && !sent_blocking)
200 continue; // No stats collected for this line, skip it and continue.
201
202 // Add a line to the result and clear the counts.
203 std::string line;
204 if (show == SHOW_SERVICE) {
205 line += stat->service;
206 } else {
207 // The interface usually includes the service so don't show both.
208 line += stat->interface;
209 if (show >= SHOW_METHOD)
210 line += "." + stat->method;
211 }
212 line += StringPrintf(":");
213 if (sent_blocking) {
214 line += StringPrintf(" Sent (BLOCKING):");
215 if (format == FORMAT_TOTALS)
216 line += StringPrintf(" %d", sent_blocking);
217 else if (format == FORMAT_PER_MINUTE)
218 line += StringPrintf(" %d/min", sent_blocking / dminutes);
219 else if (format == FORMAT_ALL)
220 line += StringPrintf(" %d (%d/min)",
221 sent_blocking, sent_blocking / dminutes);
222 }
223 if (sent) {
224 line += StringPrintf(" Sent:");
225 if (format == FORMAT_TOTALS)
226 line += StringPrintf(" %d", sent);
227 else if (format == FORMAT_PER_MINUTE)
228 line += StringPrintf(" %d/min", sent / dminutes);
229 else if (format == FORMAT_ALL)
230 line += StringPrintf(" %d (%d/min)", sent, sent / dminutes);
231 }
232 if (received) {
233 line += StringPrintf(" Received:");
234 if (format == FORMAT_TOTALS)
235 line += StringPrintf(" %d", received);
236 else if (format == FORMAT_PER_MINUTE)
237 line += StringPrintf(" %d/min", received / dminutes);
238 else if (format == FORMAT_ALL)
239 line += StringPrintf(" %d (%d/min)", received, received / dminutes);
240 }
241 result += line + "\n";
242 sent = 0;
243 sent_blocking = 0;
244 received = 0;
245 }
246 return result;
247 }
248
249 namespace testing {
250
251 bool GetCalls(const std::string& service,
252 const std::string& interface,
253 const std::string& method,
254 int* sent,
255 int* received,
256 int* blocking) {
257 if (!g_dbus_statistics)
258 return false;
259 Stat* stat = g_dbus_statistics->GetStat(service, interface, method, false);
260 if (!stat)
261 return false;
262 *sent = stat->sent_method_calls;
263 *received = stat->received_signals;
264 *blocking = stat->sent_blocking_method_calls;
265 return true;
266 }
267
268 } // namespace testing
269
270 } // namespace statistics
271 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/dbus_statistics.h ('k') | dbus/dbus_statistics_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698