0

Update dbus_statistics to use C++11 more effectively, remove obsolete stl_util use.

BUG=555865

Review-Url: https://codereview.chromium.org/2402893002
Cr-Commit-Position: refs/heads/master@{#425105}
This commit is contained in:
avi
2016-10-13 11:45:51 -07:00
committed by Commit bot
parent 4fdb683b59
commit 80df073716

@ -4,12 +4,11 @@
#include "dbus/dbus_statistics.h" #include "dbus/dbus_statistics.h"
#include <memory> #include <map>
#include <set> #include <tuple>
#include "base/logging.h" #include "base/logging.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/stl_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h" #include "base/threading/platform_thread.h"
#include "base/time/time.h" #include "base/time/time.h"
@ -18,43 +17,24 @@ namespace dbus {
namespace { namespace {
// Used to store dbus statistics sorted alphabetically by service, interface, struct StatKey {
// then method (using std::string <).
struct Stat {
Stat(const std::string& service,
const std::string& interface,
const std::string& method)
: service(service),
interface(interface),
method(method),
sent_method_calls(0),
received_signals(0),
sent_blocking_method_calls(0) {
}
std::string service; std::string service;
std::string interface; std::string interface;
std::string method; std::string method;
int sent_method_calls;
int received_signals;
int sent_blocking_method_calls;
bool 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;
}
struct PtrCompare {
bool operator()(Stat* lhs, Stat* rhs) const {
DCHECK(lhs && rhs);
return lhs->Compare(*rhs);
}
};
}; };
typedef std::set<Stat*, Stat::PtrCompare> StatSet; bool operator<(const StatKey& lhs, const StatKey& rhs) {
return std::tie(lhs.service, lhs.interface, lhs.method) <
std::tie(rhs.service, rhs.interface, rhs.method);
}
struct StatValue {
int sent_method_calls = 0;
int received_signals = 0;
int sent_blocking_method_calls = 0;
};
using StatMap = std::map<StatKey, StatValue>;
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// DBusStatistics // DBusStatistics
@ -69,10 +49,9 @@ class DBusStatistics {
~DBusStatistics() { ~DBusStatistics() {
DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
base::STLDeleteContainerPointers(stats_.begin(), stats_.end());
} }
// Enum to specify which field in Stat to increment in AddStat // Enum to specify which field in Stat to increment in AddStat.
enum StatType { enum StatType {
TYPE_SENT_METHOD_CALLS, TYPE_SENT_METHOD_CALLS,
TYPE_RECEIVED_SIGNALS, TYPE_RECEIVED_SIGNALS,
@ -89,7 +68,7 @@ class DBusStatistics {
<< base::PlatformThread::CurrentId(); << base::PlatformThread::CurrentId();
return; return;
} }
Stat* stat = GetStat(service, interface, method, true); StatValue* stat = GetStats(service, interface, method, true);
DCHECK(stat); DCHECK(stat);
if (type == TYPE_SENT_METHOD_CALLS) if (type == TYPE_SENT_METHOD_CALLS)
++stat->sent_method_calls; ++stat->sent_method_calls;
@ -103,33 +82,35 @@ class DBusStatistics {
// Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry
// if one does not already exist. // if one does not already exist.
Stat* GetStat(const std::string& service, StatValue* GetStats(const std::string& service,
const std::string& interface, const std::string& interface,
const std::string& method, const std::string& method,
bool add_stat) { bool add_stat) {
DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
std::unique_ptr<Stat> stat(new Stat(service, interface, method));
StatSet::iterator found = stats_.find(stat.get()); StatKey key = {service, interface, method};
if (found != stats_.end()) auto it = stats_.find(key);
return *found; if (it != stats_.end())
return &(it->second);
if (!add_stat) if (!add_stat)
return NULL; return nullptr;
found = stats_.insert(stat.release()).first;
return *found; return &(stats_[key]);
} }
StatSet& stats() { return stats_; } StatMap& stats() { return stats_; }
base::Time start_time() { return start_time_; } base::Time start_time() { return start_time_; }
private: private:
StatSet stats_; StatMap stats_;
base::Time start_time_; base::Time start_time_;
base::PlatformThreadId origin_thread_id_; base::PlatformThreadId origin_thread_id_;
DISALLOW_COPY_AND_ASSIGN(DBusStatistics); DISALLOW_COPY_AND_ASSIGN(DBusStatistics);
}; };
DBusStatistics* g_dbus_statistics = NULL; DBusStatistics* g_dbus_statistics = nullptr;
} // namespace } // namespace
@ -145,7 +126,7 @@ void Initialize() {
void Shutdown() { void Shutdown() {
delete g_dbus_statistics; delete g_dbus_statistics;
g_dbus_statistics = NULL; g_dbus_statistics = nullptr;
} }
void AddSentMethodCall(const std::string& service, void AddSentMethodCall(const std::string& service,
@ -182,7 +163,7 @@ std::string GetAsString(ShowInString show, FormatString format) {
if (!g_dbus_statistics) if (!g_dbus_statistics)
return "DBusStatistics not initialized."; return "DBusStatistics not initialized.";
const StatSet& stats = g_dbus_statistics->stats(); const StatMap& stats = g_dbus_statistics->stats();
if (stats.empty()) if (stats.empty())
return "No DBus calls."; return "No DBus calls.";
@ -193,19 +174,21 @@ std::string GetAsString(ShowInString show, FormatString format) {
std::string result; std::string result;
int sent = 0, received = 0, sent_blocking = 0; int sent = 0, received = 0, sent_blocking = 0;
// Stats are stored in order by service, then interface, then method. // Stats are stored in order by service, then interface, then method.
for (StatSet::const_iterator iter = stats.begin(); iter != stats.end(); ) { for (auto iter = stats.begin(); iter != stats.end();) {
StatSet::const_iterator cur_iter = iter; auto cur_iter = iter;
StatSet::const_iterator next_iter = ++iter; auto next_iter = ++iter;
const Stat* stat = *cur_iter; const StatKey& stat_key = cur_iter->first;
sent += stat->sent_method_calls; const StatValue& stat = cur_iter->second;
received += stat->received_signals; sent += stat.sent_method_calls;
sent_blocking += stat->sent_blocking_method_calls; received += stat.received_signals;
sent_blocking += stat.sent_blocking_method_calls;
// If this is not the last stat, and if the next stat matches the current // If this is not the last stat, and if the next stat matches the current
// stat, continue. // stat, continue.
if (next_iter != stats.end() && if (next_iter != stats.end() &&
(*next_iter)->service == stat->service && next_iter->first.service == stat_key.service &&
(show < SHOW_INTERFACE || (*next_iter)->interface == stat->interface) && (show < SHOW_INTERFACE ||
(show < SHOW_METHOD || (*next_iter)->method == stat->method)) next_iter->first.interface == stat_key.interface) &&
(show < SHOW_METHOD || next_iter->first.method == stat_key.method))
continue; continue;
if (!sent && !received && !sent_blocking) if (!sent && !received && !sent_blocking)
@ -214,12 +197,12 @@ std::string GetAsString(ShowInString show, FormatString format) {
// Add a line to the result and clear the counts. // Add a line to the result and clear the counts.
std::string line; std::string line;
if (show == SHOW_SERVICE) { if (show == SHOW_SERVICE) {
line += stat->service; line += stat_key.service;
} else { } else {
// The interface usually includes the service so don't show both. // The interface usually includes the service so don't show both.
line += stat->interface; line += stat_key.interface;
if (show >= SHOW_METHOD) if (show >= SHOW_METHOD)
line += "." + stat->method; line += "." + stat_key.method;
} }
line += base::StringPrintf(":"); line += base::StringPrintf(":");
if (sent_blocking) { if (sent_blocking) {
@ -269,7 +252,8 @@ bool GetCalls(const std::string& service,
int* blocking) { int* blocking) {
if (!g_dbus_statistics) if (!g_dbus_statistics)
return false; return false;
Stat* stat = g_dbus_statistics->GetStat(service, interface, method, false); StatValue* stat =
g_dbus_statistics->GetStats(service, interface, method, false);
if (!stat) if (!stat)
return false; return false;
*sent = stat->sent_method_calls; *sent = stat->sent_method_calls;