OLD | NEW |
---|---|
(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 "base/logging.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/stl_util.h" | |
10 #include "base/stringprintf.h" | |
11 | |
12 namespace dbus { | |
13 | |
14 namespace { | |
15 DbusStatistics* g_dbus_statistics = NULL; | |
16 } | |
hashimoto
2012/11/12 04:33:16
nit: Please add "// namespace"
stevenjb
2012/11/12 19:46:40
Re-factored
| |
17 | |
18 //------------------------------------------------------------------------------ | |
19 // DbusStatistics | |
20 | |
21 DbusStatistics::DbusStatistics() : start_time_(base::Time::Now()) { | |
22 } | |
23 | |
24 DbusStatistics::~DbusStatistics() { | |
25 STLDeleteContainerPointers(stats_.begin(), stats_.end()); | |
26 } | |
27 | |
28 // static | |
29 void DbusStatistics::Initialize() { | |
30 if (g_dbus_statistics) | |
31 delete g_dbus_statistics; // reset statistics | |
32 g_dbus_statistics = new DbusStatistics(); | |
33 } | |
34 | |
35 // static | |
36 void DbusStatistics::Shutdown() { | |
37 delete g_dbus_statistics; | |
38 g_dbus_statistics = NULL; | |
39 } | |
40 | |
41 // static | |
42 void DbusStatistics::AddSentCall(const std::string& service, | |
43 const std::string& interface, | |
44 const std::string& method) { | |
45 if (!g_dbus_statistics) | |
46 return; | |
47 g_dbus_statistics->AddStat(service, interface, method, TYPE_SENT); | |
48 } | |
49 | |
50 // static | |
51 void DbusStatistics::AddReceivedCall(const std::string& service, | |
52 const std::string& interface, | |
53 const std::string& method) { | |
54 if (!g_dbus_statistics) | |
55 return; | |
56 g_dbus_statistics->AddStat(service, interface, method, TYPE_RECEIVED); | |
57 } | |
58 | |
59 // static | |
60 std::string DbusStatistics::GetAsString(ShowInString show, | |
61 bool show_per_minute) { | |
62 if (!g_dbus_statistics) | |
63 return "DbusStatistics not intitialized."; | |
64 | |
65 StatSet& stats = g_dbus_statistics->stats_; | |
satorux1
2012/11/12 00:40:43
const StatSet& ?
stevenjb
2012/11/12 19:46:40
Done.
| |
66 if (stats.empty()) | |
67 return "No DBus calls."; | |
68 | |
69 base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time_; | |
70 int dminutes = dtime.InMinutes(); | |
71 dminutes = std::max(dminutes, 1); | |
72 | |
73 std::string result; | |
74 int sent = 0, received = 0; | |
75 // Stats are stored in order by service, then interface, then method. | |
76 for (StatSet::const_iterator iter = stats.begin(); iter != stats.end(); ) { | |
77 StatSet::const_iterator cur_iter = iter++; | |
78 const Stat* stat = *cur_iter; | |
79 sent += stat->sent_calls; | |
80 received += stat->received_calls; | |
81 // If this is the last stat, or if the next stat should be on a different | |
82 // line, add a line to the result and clear the counts. | |
83 if (iter == stats.end() || | |
84 (*iter)->service != stat->service || | |
85 (show >= SHOW_SERVICE && (*iter)->interface != stat->interface) || | |
86 (show >= SHOW_METHOD && (*iter)->method != stat->method)) { | |
87 if (!sent && !received) | |
88 continue; // No stats collected for this line, skip it and continue. | |
89 | |
90 std::string line; | |
91 if (show == SHOW_SERVICE) { | |
92 line += stat->service; | |
93 } else { | |
94 // The interface usually includes the service so don't show both. | |
95 line += stat->interface; | |
96 if (show >= SHOW_METHOD) | |
97 line += "." + stat->method; | |
98 } | |
99 if (sent) { | |
100 line += StringPrintf(": Sent: %d", sent); | |
101 if (show_per_minute) | |
102 line += StringPrintf(" (%d/min)", sent / dminutes); | |
103 } | |
104 if (received) { | |
105 line += StringPrintf(" Received: %d", received); | |
106 if (show_per_minute) | |
107 line += StringPrintf(" (%d/min)", received / dminutes); | |
108 } | |
109 result += line + "\n"; | |
110 sent = 0; | |
111 received = 0; | |
112 } | |
113 } | |
114 return result; | |
115 } | |
116 | |
117 // static | |
118 bool DbusStatistics::GetCallsForTesting(const std::string& service, | |
119 const std::string& interface, | |
120 const std::string& method, | |
121 int* sent, | |
122 int* received) { | |
123 if (!g_dbus_statistics) | |
124 return false; | |
125 Stat* stat = g_dbus_statistics->GetStat(service, interface, method, false); | |
126 if (!stat) | |
127 return false; | |
128 *sent = stat->sent_calls; | |
129 *received = stat->received_calls; | |
130 return true; | |
131 } | |
132 | |
133 // Private methods | |
134 | |
135 void DbusStatistics::AddStat(const std::string& service, | |
136 const std::string& interface, | |
137 const std::string& method, | |
138 StatType type) { | |
139 Stat* stat = GetStat(service, interface, method, true); | |
140 if (type == TYPE_SENT) | |
141 ++stat->sent_calls; | |
142 else if (type == TYPE_RECEIVED) | |
143 ++stat->received_calls; | |
144 else | |
145 NOTREACHED(); | |
146 } | |
147 | |
148 DbusStatistics::Stat* DbusStatistics::GetStat(const std::string& service, | |
149 const std::string& interface, | |
150 const std::string& method, | |
151 bool add_stat) { | |
152 scoped_ptr<Stat> stat(new Stat(service, interface, method)); | |
153 StatSet::iterator found = stats_.find(stat.get()); | |
154 if (found != stats_.end()) | |
155 return *found; | |
156 if (!add_stat) | |
157 return NULL; | |
158 found = stats_.insert(stat.release()).first; | |
159 return *found; | |
160 } | |
161 | |
162 //------------------------------------------------------------------------------ | |
163 // DbusStatistics::Stat | |
164 | |
165 DbusStatistics::Stat::Stat(const std::string& service, | |
166 const std::string& interface, | |
167 const std::string& method) | |
168 : service(service), | |
169 interface(interface), | |
170 method(method), | |
171 sent_calls(0), | |
172 received_calls(0) { | |
173 } | |
174 | |
175 bool DbusStatistics::Stat::Compare(const Stat& other) const { | |
176 if (service != other.service) | |
177 return service < other.service; | |
178 if (interface != other.interface) | |
179 return interface < other.interface; | |
180 return method < other.method; | |
181 } | |
182 | |
183 bool DbusStatistics::Stat::PtrCompare::operator()(Stat* lhs, Stat* rhs) const { | |
184 DCHECK(lhs != NULL && rhs != NULL); | |
185 return lhs->Compare(*rhs); | |
186 } | |
187 | |
188 } // namespace dbus | |
OLD | NEW |