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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 13 #include "base/threading/platform_thread.h" |
13 #include "base/time.h" | 14 #include "base/time.h" |
14 | 15 |
15 namespace dbus { | 16 namespace dbus { |
16 | 17 |
17 namespace { | 18 namespace { |
18 | 19 |
19 // Used to store dbus statistics sorted alphabetically by service, interface, | 20 // Used to store dbus statistics sorted alphabetically by service, interface, |
20 // then method (using std::string <). | 21 // then method (using std::string <). |
21 struct Stat { | 22 struct Stat { |
22 Stat(const std::string& service, | 23 Stat(const std::string& service, |
(...skipping 30 matching lines...) Expand all Loading... |
53 }; | 54 }; |
54 | 55 |
55 typedef std::set<Stat*, Stat::PtrCompare> StatSet; | 56 typedef std::set<Stat*, Stat::PtrCompare> StatSet; |
56 | 57 |
57 //------------------------------------------------------------------------------ | 58 //------------------------------------------------------------------------------ |
58 // DBusStatistics | 59 // DBusStatistics |
59 | 60 |
60 // Simple class for gathering DBus usage statistics. | 61 // Simple class for gathering DBus usage statistics. |
61 class DBusStatistics { | 62 class DBusStatistics { |
62 public: | 63 public: |
63 DBusStatistics() : start_time_(base::Time::Now()) { | 64 DBusStatistics() |
| 65 : start_time_(base::Time::Now()), |
| 66 origin_thread_id_(base::PlatformThread::CurrentId()) { |
64 } | 67 } |
65 | 68 |
66 ~DBusStatistics() { | 69 ~DBusStatistics() { |
| 70 DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); |
67 STLDeleteContainerPointers(stats_.begin(), stats_.end()); | 71 STLDeleteContainerPointers(stats_.begin(), stats_.end()); |
68 } | 72 } |
69 | 73 |
70 // Enum to specify which field in Stat to increment in AddStat | 74 // Enum to specify which field in Stat to increment in AddStat |
71 enum StatType { | 75 enum StatType { |
72 TYPE_SENT_METHOD_CALLS, | 76 TYPE_SENT_METHOD_CALLS, |
73 TYPE_RECEIVED_SIGNALS, | 77 TYPE_RECEIVED_SIGNALS, |
74 TYPE_SENT_BLOCKING_METHOD_CALLS | 78 TYPE_SENT_BLOCKING_METHOD_CALLS |
75 }; | 79 }; |
76 | 80 |
77 // Add a call to |method| for |interface|. See also MethodCall in message.h. | 81 // Add a call to |method| for |interface|. See also MethodCall in message.h. |
78 void AddStat(const std::string& service, | 82 void AddStat(const std::string& service, |
79 const std::string& interface, | 83 const std::string& interface, |
80 const std::string& method, | 84 const std::string& method, |
81 StatType type) { | 85 StatType type) { |
| 86 if (base::PlatformThread::CurrentId() != origin_thread_id_) { |
| 87 DLOG(WARNING) << "Ignoring DBusStatistics::AddStat call from thread: " |
| 88 << base::PlatformThread::CurrentId(); |
| 89 return; |
| 90 } |
82 Stat* stat = GetStat(service, interface, method, true); | 91 Stat* stat = GetStat(service, interface, method, true); |
83 DCHECK(stat); | 92 DCHECK(stat); |
84 if (type == TYPE_SENT_METHOD_CALLS) | 93 if (type == TYPE_SENT_METHOD_CALLS) |
85 ++stat->sent_method_calls; | 94 ++stat->sent_method_calls; |
86 else if (type == TYPE_RECEIVED_SIGNALS) | 95 else if (type == TYPE_RECEIVED_SIGNALS) |
87 ++stat->received_signals; | 96 ++stat->received_signals; |
88 else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS) | 97 else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS) |
89 ++stat->sent_blocking_method_calls; | 98 ++stat->sent_blocking_method_calls; |
90 else | 99 else |
91 NOTREACHED(); | 100 NOTREACHED(); |
92 } | 101 } |
93 | 102 |
94 // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry | 103 // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry |
95 // if one does not already exist. | 104 // if one does not already exist. |
96 Stat* GetStat(const std::string& service, | 105 Stat* GetStat(const std::string& service, |
97 const std::string& interface, | 106 const std::string& interface, |
98 const std::string& method, | 107 const std::string& method, |
99 bool add_stat) { | 108 bool add_stat) { |
| 109 DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId()); |
100 scoped_ptr<Stat> stat(new Stat(service, interface, method)); | 110 scoped_ptr<Stat> stat(new Stat(service, interface, method)); |
101 StatSet::iterator found = stats_.find(stat.get()); | 111 StatSet::iterator found = stats_.find(stat.get()); |
102 if (found != stats_.end()) | 112 if (found != stats_.end()) |
103 return *found; | 113 return *found; |
104 if (!add_stat) | 114 if (!add_stat) |
105 return NULL; | 115 return NULL; |
106 found = stats_.insert(stat.release()).first; | 116 found = stats_.insert(stat.release()).first; |
107 return *found; | 117 return *found; |
108 } | 118 } |
109 | 119 |
110 StatSet& stats() { return stats_; } | 120 StatSet& stats() { return stats_; } |
111 base::Time start_time() { return start_time_; } | 121 base::Time start_time() { return start_time_; } |
112 | 122 |
113 private: | 123 private: |
114 StatSet stats_; | 124 StatSet stats_; |
115 base::Time start_time_; | 125 base::Time start_time_; |
| 126 base::PlatformThreadId origin_thread_id_; |
116 | 127 |
117 DISALLOW_COPY_AND_ASSIGN(DBusStatistics); | 128 DISALLOW_COPY_AND_ASSIGN(DBusStatistics); |
118 }; | 129 }; |
119 | 130 |
120 DBusStatistics* g_dbus_statistics = NULL; | 131 DBusStatistics* g_dbus_statistics = NULL; |
121 | 132 |
122 } // namespace | 133 } // namespace |
123 | 134 |
124 //------------------------------------------------------------------------------ | 135 //------------------------------------------------------------------------------ |
125 | 136 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 *sent = stat->sent_method_calls; | 273 *sent = stat->sent_method_calls; |
263 *received = stat->received_signals; | 274 *received = stat->received_signals; |
264 *blocking = stat->sent_blocking_method_calls; | 275 *blocking = stat->sent_blocking_method_calls; |
265 return true; | 276 return true; |
266 } | 277 } |
267 | 278 |
268 } // namespace testing | 279 } // namespace testing |
269 | 280 |
270 } // namespace statistics | 281 } // namespace statistics |
271 } // namespace dbus | 282 } // namespace dbus |
OLD | NEW |