dbus_statistics.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include "dbus/dbus_statistics.h"
  5. #include <map>
  6. #include <tuple>
  7. #include "base/logging.h"
  8. #include "base/notreached.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "base/threading/platform_thread.h"
  11. #include "base/time/time.h"
  12. namespace dbus {
  13. namespace {
  14. struct StatKey {
  15. std::string service;
  16. std::string interface;
  17. std::string method;
  18. };
  19. bool operator<(const StatKey& lhs, const StatKey& rhs) {
  20. return std::tie(lhs.service, lhs.interface, lhs.method) <
  21. std::tie(rhs.service, rhs.interface, rhs.method);
  22. }
  23. struct StatValue {
  24. int sent_method_calls = 0;
  25. int received_signals = 0;
  26. int sent_blocking_method_calls = 0;
  27. };
  28. using StatMap = std::map<StatKey, StatValue>;
  29. //------------------------------------------------------------------------------
  30. // DBusStatistics
  31. // Simple class for gathering DBus usage statistics.
  32. class DBusStatistics {
  33. public:
  34. DBusStatistics()
  35. : start_time_(base::Time::Now()),
  36. origin_thread_id_(base::PlatformThread::CurrentId()) {
  37. }
  38. DBusStatistics(const DBusStatistics&) = delete;
  39. DBusStatistics& operator=(const DBusStatistics&) = delete;
  40. ~DBusStatistics() {
  41. DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
  42. }
  43. // Enum to specify which field in Stat to increment in AddStat.
  44. enum StatType {
  45. TYPE_SENT_METHOD_CALLS,
  46. TYPE_RECEIVED_SIGNALS,
  47. TYPE_SENT_BLOCKING_METHOD_CALLS
  48. };
  49. // Add a call to |method| for |interface|. See also MethodCall in message.h.
  50. void AddStat(const std::string& service,
  51. const std::string& interface,
  52. const std::string& method,
  53. StatType type) {
  54. if (base::PlatformThread::CurrentId() != origin_thread_id_) {
  55. DVLOG(1) << "Ignoring DBusStatistics::AddStat call from thread: "
  56. << base::PlatformThread::CurrentId();
  57. return;
  58. }
  59. StatValue* stat = GetStats(service, interface, method, true);
  60. DCHECK(stat);
  61. if (type == TYPE_SENT_METHOD_CALLS)
  62. ++stat->sent_method_calls;
  63. else if (type == TYPE_RECEIVED_SIGNALS)
  64. ++stat->received_signals;
  65. else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS)
  66. ++stat->sent_blocking_method_calls;
  67. else
  68. NOTREACHED();
  69. }
  70. // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry
  71. // if one does not already exist.
  72. StatValue* GetStats(const std::string& service,
  73. const std::string& interface,
  74. const std::string& method,
  75. bool add_stat) {
  76. DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
  77. StatKey key = {service, interface, method};
  78. auto it = stats_.find(key);
  79. if (it != stats_.end())
  80. return &(it->second);
  81. if (!add_stat)
  82. return nullptr;
  83. return &(stats_[key]);
  84. }
  85. StatMap& stats() { return stats_; }
  86. base::Time start_time() { return start_time_; }
  87. private:
  88. StatMap stats_;
  89. base::Time start_time_;
  90. base::PlatformThreadId origin_thread_id_;
  91. };
  92. DBusStatistics* g_dbus_statistics = nullptr;
  93. } // namespace
  94. //------------------------------------------------------------------------------
  95. namespace statistics {
  96. void Initialize() {
  97. if (g_dbus_statistics)
  98. delete g_dbus_statistics; // reset statistics
  99. g_dbus_statistics = new DBusStatistics();
  100. }
  101. void Shutdown() {
  102. delete g_dbus_statistics;
  103. g_dbus_statistics = nullptr;
  104. }
  105. void AddSentMethodCall(const std::string& service,
  106. const std::string& interface,
  107. const std::string& method) {
  108. if (!g_dbus_statistics)
  109. return;
  110. g_dbus_statistics->AddStat(
  111. service, interface, method, DBusStatistics::TYPE_SENT_METHOD_CALLS);
  112. }
  113. void AddReceivedSignal(const std::string& service,
  114. const std::string& interface,
  115. const std::string& method) {
  116. if (!g_dbus_statistics)
  117. return;
  118. g_dbus_statistics->AddStat(
  119. service, interface, method, DBusStatistics::TYPE_RECEIVED_SIGNALS);
  120. }
  121. void AddBlockingSentMethodCall(const std::string& service,
  122. const std::string& interface,
  123. const std::string& method) {
  124. if (!g_dbus_statistics)
  125. return;
  126. g_dbus_statistics->AddStat(
  127. service, interface, method,
  128. DBusStatistics::TYPE_SENT_BLOCKING_METHOD_CALLS);
  129. }
  130. // NOTE: If the output format is changed, be certain to change the test
  131. // expectations as well.
  132. std::string GetAsString(ShowInString show, FormatString format) {
  133. if (!g_dbus_statistics)
  134. return "DBusStatistics not initialized.";
  135. const StatMap& stats = g_dbus_statistics->stats();
  136. if (stats.empty())
  137. return "No DBus calls.";
  138. base::TimeDelta dtime = base::Time::Now() - g_dbus_statistics->start_time();
  139. int dminutes = dtime.InMinutes();
  140. dminutes = std::max(dminutes, 1);
  141. std::string result;
  142. int sent = 0, received = 0, sent_blocking = 0;
  143. // Stats are stored in order by service, then interface, then method.
  144. for (auto iter = stats.begin(); iter != stats.end();) {
  145. auto cur_iter = iter;
  146. auto next_iter = ++iter;
  147. const StatKey& stat_key = cur_iter->first;
  148. const StatValue& stat = cur_iter->second;
  149. sent += stat.sent_method_calls;
  150. received += stat.received_signals;
  151. sent_blocking += stat.sent_blocking_method_calls;
  152. // If this is not the last stat, and if the next stat matches the current
  153. // stat, continue.
  154. if (next_iter != stats.end() &&
  155. next_iter->first.service == stat_key.service &&
  156. (show < SHOW_INTERFACE ||
  157. next_iter->first.interface == stat_key.interface) &&
  158. (show < SHOW_METHOD || next_iter->first.method == stat_key.method))
  159. continue;
  160. if (!sent && !received && !sent_blocking)
  161. continue; // No stats collected for this line, skip it and continue.
  162. // Add a line to the result and clear the counts.
  163. std::string line;
  164. if (show == SHOW_SERVICE) {
  165. line += stat_key.service;
  166. } else {
  167. // The interface usually includes the service so don't show both.
  168. line += stat_key.interface;
  169. if (show >= SHOW_METHOD)
  170. line += "." + stat_key.method;
  171. }
  172. line += base::StringPrintf(":");
  173. if (sent_blocking) {
  174. line += base::StringPrintf(" Sent (BLOCKING):");
  175. if (format == FORMAT_TOTALS)
  176. line += base::StringPrintf(" %d", sent_blocking);
  177. else if (format == FORMAT_PER_MINUTE)
  178. line += base::StringPrintf(" %d/min", sent_blocking / dminutes);
  179. else if (format == FORMAT_ALL)
  180. line += base::StringPrintf(" %d (%d/min)",
  181. sent_blocking, sent_blocking / dminutes);
  182. }
  183. if (sent) {
  184. line += base::StringPrintf(" Sent:");
  185. if (format == FORMAT_TOTALS)
  186. line += base::StringPrintf(" %d", sent);
  187. else if (format == FORMAT_PER_MINUTE)
  188. line += base::StringPrintf(" %d/min", sent / dminutes);
  189. else if (format == FORMAT_ALL)
  190. line += base::StringPrintf(" %d (%d/min)", sent, sent / dminutes);
  191. }
  192. if (received) {
  193. line += base::StringPrintf(" Received:");
  194. if (format == FORMAT_TOTALS)
  195. line += base::StringPrintf(" %d", received);
  196. else if (format == FORMAT_PER_MINUTE)
  197. line += base::StringPrintf(" %d/min", received / dminutes);
  198. else if (format == FORMAT_ALL)
  199. line += base::StringPrintf(
  200. " %d (%d/min)", received, received / dminutes);
  201. }
  202. result += line + "\n";
  203. sent = 0;
  204. sent_blocking = 0;
  205. received = 0;
  206. }
  207. return result;
  208. }
  209. namespace testing {
  210. bool GetCalls(const std::string& service,
  211. const std::string& interface,
  212. const std::string& method,
  213. int* sent,
  214. int* received,
  215. int* blocking) {
  216. if (!g_dbus_statistics)
  217. return false;
  218. StatValue* stat =
  219. g_dbus_statistics->GetStats(service, interface, method, false);
  220. if (!stat)
  221. return false;
  222. *sent = stat->sent_method_calls;
  223. *received = stat->received_signals;
  224. *blocking = stat->sent_blocking_method_calls;
  225. return true;
  226. }
  227. } // namespace testing
  228. } // namespace statistics
  229. } // namespace dbus