Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "dbus/dbus_statistics.h" | 5 #include "dbus/dbus_statistics.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/bind.h" | |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | |
| 13 #include "base/message_loop.h" | |
| 14 #include "base/message_loop_proxy.h" | |
| 11 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 12 #include "base/stringprintf.h" | 16 #include "base/stringprintf.h" |
| 17 #include "base/threading/platform_thread.h" | |
| 13 #include "base/time.h" | 18 #include "base/time.h" |
| 14 | 19 |
| 15 namespace dbus { | 20 namespace dbus { |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 19 // Used to store dbus statistics sorted alphabetically by service, interface, | 24 // Used to store dbus statistics sorted alphabetically by service, interface, |
| 20 // then method (using std::string <). | 25 // then method (using std::string <). |
| 21 struct Stat { | 26 struct Stat { |
| 22 Stat(const std::string& service, | 27 Stat(const std::string& service, |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 53 }; | 58 }; |
| 54 | 59 |
| 55 typedef std::set<Stat*, Stat::PtrCompare> StatSet; | 60 typedef std::set<Stat*, Stat::PtrCompare> StatSet; |
| 56 | 61 |
| 57 //------------------------------------------------------------------------------ | 62 //------------------------------------------------------------------------------ |
| 58 // DBusStatistics | 63 // DBusStatistics |
| 59 | 64 |
| 60 // Simple class for gathering DBus usage statistics. | 65 // Simple class for gathering DBus usage statistics. |
| 61 class DBusStatistics { | 66 class DBusStatistics { |
| 62 public: | 67 public: |
| 63 DBusStatistics() : start_time_(base::Time::Now()) { | 68 DBusStatistics() |
| 69 : start_time_(base::Time::Now()), | |
| 70 origin_thread_id_(base::PlatformThread::CurrentId()), | |
| 71 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
| 72 // MessageLoop::current() might be NULL in tests. | |
|
hashimoto
2013/01/04 11:20:10
How about setting up the message loop in tests ins
stevenjb
2013/01/04 20:30:37
Replacing this with a CHECK fails for a handful of
hashimoto
2013/01/07 13:28:00
OK.
Please fix the failing browser tests.
| |
| 73 if (MessageLoop::current()) | |
| 74 origin_message_loop_proxy_ = MessageLoop::current()->message_loop_proxy(); | |
|
hashimoto
2013/01/04 11:20:10
nit: How about using MessageLoopProxy::current()?
stevenjb
2013/01/04 20:30:37
Done.
| |
| 64 } | 75 } |
| 65 | 76 |
| 66 ~DBusStatistics() { | 77 ~DBusStatistics() { |
| 78 CHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); | |
| 67 STLDeleteContainerPointers(stats_.begin(), stats_.end()); | 79 STLDeleteContainerPointers(stats_.begin(), stats_.end()); |
| 68 } | 80 } |
| 69 | 81 |
| 70 // Enum to specify which field in Stat to increment in AddStat | 82 // Enum to specify which field in Stat to increment in AddStat |
| 71 enum StatType { | 83 enum StatType { |
| 72 TYPE_SENT_METHOD_CALLS, | 84 TYPE_SENT_METHOD_CALLS, |
| 73 TYPE_RECEIVED_SIGNALS, | 85 TYPE_RECEIVED_SIGNALS, |
| 74 TYPE_SENT_BLOCKING_METHOD_CALLS | 86 TYPE_SENT_BLOCKING_METHOD_CALLS |
| 75 }; | 87 }; |
| 76 | 88 |
| 77 // Add a call to |method| for |interface|. See also MethodCall in message.h. | 89 // Add a call to |method| for |interface|. See also MethodCall in message.h. |
| 78 void AddStat(const std::string& service, | 90 void AddStat(const std::string& service, |
| 79 const std::string& interface, | 91 const std::string& interface, |
| 80 const std::string& method, | 92 const std::string& method, |
| 81 StatType type) { | 93 StatType type) { |
| 94 if (base::PlatformThread::CurrentId() != origin_thread_id_) { | |
|
hashimoto
2013/01/04 11:20:10
How about using SingleThreadTaskRunner:: BelongsTo
stevenjb
2013/01/04 20:30:37
As long as origin_message_loop_proxy_ might be NUL
| |
| 95 if (origin_message_loop_proxy_.get()) { | |
| 96 origin_message_loop_proxy_->PostTask( | |
| 97 FROM_HERE, | |
| 98 base::Bind(&DBusStatistics::AddStat, | |
| 99 weak_ptr_factory_.GetWeakPtr(), | |
| 100 service, interface, method, type)); | |
| 101 } | |
| 102 return; | |
| 103 } | |
| 82 Stat* stat = GetStat(service, interface, method, true); | 104 Stat* stat = GetStat(service, interface, method, true); |
| 83 DCHECK(stat); | 105 CHECK(stat); |
| 84 if (type == TYPE_SENT_METHOD_CALLS) | 106 if (type == TYPE_SENT_METHOD_CALLS) |
| 85 ++stat->sent_method_calls; | 107 ++stat->sent_method_calls; |
| 86 else if (type == TYPE_RECEIVED_SIGNALS) | 108 else if (type == TYPE_RECEIVED_SIGNALS) |
| 87 ++stat->received_signals; | 109 ++stat->received_signals; |
| 88 else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS) | 110 else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS) |
| 89 ++stat->sent_blocking_method_calls; | 111 ++stat->sent_blocking_method_calls; |
| 90 else | 112 else |
| 91 NOTREACHED(); | 113 NOTREACHED(); |
| 92 } | 114 } |
| 93 | 115 |
| 94 // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry | 116 // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry |
| 95 // if one does not already exist. | 117 // if one does not already exist. |
| 96 Stat* GetStat(const std::string& service, | 118 Stat* GetStat(const std::string& service, |
| 97 const std::string& interface, | 119 const std::string& interface, |
| 98 const std::string& method, | 120 const std::string& method, |
| 99 bool add_stat) { | 121 bool add_stat) { |
| 122 CHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); | |
| 100 scoped_ptr<Stat> stat(new Stat(service, interface, method)); | 123 scoped_ptr<Stat> stat(new Stat(service, interface, method)); |
| 101 StatSet::iterator found = stats_.find(stat.get()); | 124 StatSet::iterator found = stats_.find(stat.get()); |
| 102 if (found != stats_.end()) | 125 if (found != stats_.end()) |
| 103 return *found; | 126 return *found; |
| 104 if (!add_stat) | 127 if (!add_stat) |
| 105 return NULL; | 128 return NULL; |
| 106 found = stats_.insert(stat.release()).first; | 129 found = stats_.insert(stat.release()).first; |
| 107 return *found; | 130 return *found; |
| 108 } | 131 } |
| 109 | 132 |
| 110 StatSet& stats() { return stats_; } | 133 StatSet& GetStatSet() { |
| 134 CHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); | |
| 135 return stats_; | |
| 136 } | |
| 111 base::Time start_time() { return start_time_; } | 137 base::Time start_time() { return start_time_; } |
| 112 | 138 |
| 113 private: | 139 private: |
| 114 StatSet stats_; | 140 StatSet stats_; |
| 115 base::Time start_time_; | 141 base::Time start_time_; |
| 142 base::PlatformThreadId origin_thread_id_; | |
| 143 scoped_refptr<base::MessageLoopProxy> origin_message_loop_proxy_; | |
| 144 base::WeakPtrFactory<DBusStatistics> weak_ptr_factory_; | |
| 116 | 145 |
| 117 DISALLOW_COPY_AND_ASSIGN(DBusStatistics); | 146 DISALLOW_COPY_AND_ASSIGN(DBusStatistics); |
| 118 }; | 147 }; |
| 119 | 148 |
| 120 DBusStatistics* g_dbus_statistics = NULL; | 149 DBusStatistics* g_dbus_statistics = NULL; |
| 121 | 150 |
| 122 } // namespace | 151 } // namespace |
| 123 | 152 |
| 124 //------------------------------------------------------------------------------ | 153 //------------------------------------------------------------------------------ |
| 125 | 154 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 service, interface, method, | 192 service, interface, method, |
| 164 DBusStatistics::TYPE_SENT_BLOCKING_METHOD_CALLS); | 193 DBusStatistics::TYPE_SENT_BLOCKING_METHOD_CALLS); |
| 165 } | 194 } |
| 166 | 195 |
| 167 // NOTE: If the output format is changed, be certain to change the test | 196 // NOTE: If the output format is changed, be certain to change the test |
| 168 // expectations as well. | 197 // expectations as well. |
| 169 std::string GetAsString(ShowInString show, FormatString format) { | 198 std::string GetAsString(ShowInString show, FormatString format) { |
| 170 if (!g_dbus_statistics) | 199 if (!g_dbus_statistics) |
| 171 return "DBusStatistics not initialized."; | 200 return "DBusStatistics not initialized."; |
| 172 | 201 |
| 173 const StatSet& stats = g_dbus_statistics->stats(); | 202 const StatSet& stats = g_dbus_statistics->GetStatSet(); |
| 174 if (stats.empty()) | 203 if (stats.empty()) |
| 175 return "No DBus calls."; | 204 return "No DBus calls."; |
| 176 | 205 |
| 177 base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time(); | 206 base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time(); |
| 178 int dminutes = dtime.InMinutes(); | 207 int dminutes = dtime.InMinutes(); |
| 179 dminutes = std::max(dminutes, 1); | 208 dminutes = std::max(dminutes, 1); |
| 180 | 209 |
| 181 std::string result; | 210 std::string result; |
| 182 int sent = 0, received = 0, sent_blocking = 0; | 211 int sent = 0, received = 0, sent_blocking = 0; |
| 183 // Stats are stored in order by service, then interface, then method. | 212 // Stats are stored in order by service, then interface, then method. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 *sent = stat->sent_method_calls; | 291 *sent = stat->sent_method_calls; |
| 263 *received = stat->received_signals; | 292 *received = stat->received_signals; |
| 264 *blocking = stat->sent_blocking_method_calls; | 293 *blocking = stat->sent_blocking_method_calls; |
| 265 return true; | 294 return true; |
| 266 } | 295 } |
| 267 | 296 |
| 268 } // namespace testing | 297 } // namespace testing |
| 269 | 298 |
| 270 } // namespace statistics | 299 } // namespace statistics |
| 271 } // namespace dbus | 300 } // namespace dbus |
| OLD | NEW |