Chromium Code Reviews| Index: dbus/dbus_statistics.cc |
| diff --git a/dbus/dbus_statistics.cc b/dbus/dbus_statistics.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..27c3cba492b0da0d1090aec3a87df7bc068e474c |
| --- /dev/null |
| +++ b/dbus/dbus_statistics.cc |
| @@ -0,0 +1,188 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "dbus/dbus_statistics.h" |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/stl_util.h" |
| +#include "base/stringprintf.h" |
| + |
| +namespace dbus { |
| + |
| +namespace { |
| +DbusStatistics* g_dbus_statistics = NULL; |
| +} |
|
hashimoto
2012/11/12 04:33:16
nit: Please add "// namespace"
stevenjb
2012/11/12 19:46:40
Re-factored
|
| + |
| +//------------------------------------------------------------------------------ |
| +// DbusStatistics |
| + |
| +DbusStatistics::DbusStatistics() : start_time_(base::Time::Now()) { |
| +} |
| + |
| +DbusStatistics::~DbusStatistics() { |
| + STLDeleteContainerPointers(stats_.begin(), stats_.end()); |
| +} |
| + |
| +// static |
| +void DbusStatistics::Initialize() { |
| + if (g_dbus_statistics) |
| + delete g_dbus_statistics; // reset statistics |
| + g_dbus_statistics = new DbusStatistics(); |
| +} |
| + |
| +// static |
| +void DbusStatistics::Shutdown() { |
| + delete g_dbus_statistics; |
| + g_dbus_statistics = NULL; |
| +} |
| + |
| +// static |
| +void DbusStatistics::AddSentCall(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method) { |
| + if (!g_dbus_statistics) |
| + return; |
| + g_dbus_statistics->AddStat(service, interface, method, TYPE_SENT); |
| +} |
| + |
| +// static |
| +void DbusStatistics::AddReceivedCall(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method) { |
| + if (!g_dbus_statistics) |
| + return; |
| + g_dbus_statistics->AddStat(service, interface, method, TYPE_RECEIVED); |
| +} |
| + |
| +// static |
| +std::string DbusStatistics::GetAsString(ShowInString show, |
| + bool show_per_minute) { |
| + if (!g_dbus_statistics) |
| + return "DbusStatistics not intitialized."; |
| + |
| + StatSet& stats = g_dbus_statistics->stats_; |
|
satorux1
2012/11/12 00:40:43
const StatSet& ?
stevenjb
2012/11/12 19:46:40
Done.
|
| + if (stats.empty()) |
| + return "No DBus calls."; |
| + |
| + base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time_; |
| + int dminutes = dtime.InMinutes(); |
| + dminutes = std::max(dminutes, 1); |
| + |
| + std::string result; |
| + int sent = 0, received = 0; |
| + // Stats are stored in order by service, then interface, then method. |
| + for (StatSet::const_iterator iter = stats.begin(); iter != stats.end(); ) { |
| + StatSet::const_iterator cur_iter = iter++; |
| + const Stat* stat = *cur_iter; |
| + sent += stat->sent_calls; |
| + received += stat->received_calls; |
| + // If this is the last stat, or if the next stat should be on a different |
| + // line, add a line to the result and clear the counts. |
| + if (iter == stats.end() || |
| + (*iter)->service != stat->service || |
| + (show >= SHOW_SERVICE && (*iter)->interface != stat->interface) || |
| + (show >= SHOW_METHOD && (*iter)->method != stat->method)) { |
| + if (!sent && !received) |
| + continue; // No stats collected for this line, skip it and continue. |
| + |
| + std::string line; |
| + if (show == SHOW_SERVICE) { |
| + line += stat->service; |
| + } else { |
| + // The interface usually includes the service so don't show both. |
| + line += stat->interface; |
| + if (show >= SHOW_METHOD) |
| + line += "." + stat->method; |
| + } |
| + if (sent) { |
| + line += StringPrintf(": Sent: %d", sent); |
| + if (show_per_minute) |
| + line += StringPrintf(" (%d/min)", sent / dminutes); |
| + } |
| + if (received) { |
| + line += StringPrintf(" Received: %d", received); |
| + if (show_per_minute) |
| + line += StringPrintf(" (%d/min)", received / dminutes); |
| + } |
| + result += line + "\n"; |
| + sent = 0; |
| + received = 0; |
| + } |
| + } |
| + return result; |
| +} |
| + |
| +// static |
| +bool DbusStatistics::GetCallsForTesting(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method, |
| + int* sent, |
| + int* received) { |
| + if (!g_dbus_statistics) |
| + return false; |
| + Stat* stat = g_dbus_statistics->GetStat(service, interface, method, false); |
| + if (!stat) |
| + return false; |
| + *sent = stat->sent_calls; |
| + *received = stat->received_calls; |
| + return true; |
| +} |
| + |
| +// Private methods |
| + |
| +void DbusStatistics::AddStat(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method, |
| + StatType type) { |
| + Stat* stat = GetStat(service, interface, method, true); |
| + if (type == TYPE_SENT) |
| + ++stat->sent_calls; |
| + else if (type == TYPE_RECEIVED) |
| + ++stat->received_calls; |
| + else |
| + NOTREACHED(); |
| +} |
| + |
| +DbusStatistics::Stat* DbusStatistics::GetStat(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method, |
| + bool add_stat) { |
| + scoped_ptr<Stat> stat(new Stat(service, interface, method)); |
| + StatSet::iterator found = stats_.find(stat.get()); |
| + if (found != stats_.end()) |
| + return *found; |
| + if (!add_stat) |
| + return NULL; |
| + found = stats_.insert(stat.release()).first; |
| + return *found; |
| +} |
| + |
| +//------------------------------------------------------------------------------ |
| +// DbusStatistics::Stat |
| + |
| +DbusStatistics::Stat::Stat(const std::string& service, |
| + const std::string& interface, |
| + const std::string& method) |
| + : service(service), |
| + interface(interface), |
| + method(method), |
| + sent_calls(0), |
| + received_calls(0) { |
| +} |
| + |
| +bool DbusStatistics::Stat::Compare(const Stat& other) const { |
| + if (service != other.service) |
| + return service < other.service; |
| + if (interface != other.interface) |
| + return interface < other.interface; |
| + return method < other.method; |
| +} |
| + |
| +bool DbusStatistics::Stat::PtrCompare::operator()(Stat* lhs, Stat* rhs) const { |
| + DCHECK(lhs != NULL && rhs != NULL); |
| + return lhs->Compare(*rhs); |
| +} |
| + |
| +} // namespace dbus |