summaryrefslogtreecommitdiff
path: root/dbus/dbus_statistics.cc
blob: 2949c5032d32aeae2f07c74dd85dc40243838ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// 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 <map>
#include <tuple>

#include "base/logging.h"
#include "base/macros.h"
#include "base/strings/stringprintf.h"
#include "base/threading/platform_thread.h"
#include "base/time/time.h"

namespace dbus {

namespace {

struct StatKey {
  std::string service;
  std::string interface;
  std::string method;
};

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

// Simple class for gathering DBus usage statistics.
class DBusStatistics {
 public:
  DBusStatistics()
      : start_time_(base::Time::Now()),
        origin_thread_id_(base::PlatformThread::CurrentId()) {
  }

  ~DBusStatistics() {
    DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());
  }

  // Enum to specify which field in Stat to increment in AddStat.
  enum StatType {
    TYPE_SENT_METHOD_CALLS,
    TYPE_RECEIVED_SIGNALS,
    TYPE_SENT_BLOCKING_METHOD_CALLS
  };

  // Add a call to |method| for |interface|. See also MethodCall in message.h.
  void AddStat(const std::string& service,
               const std::string& interface,
               const std::string& method,
               StatType type) {
    if (base::PlatformThread::CurrentId() != origin_thread_id_) {
      DVLOG(1) << "Ignoring DBusStatistics::AddStat call from thread: "
               << base::PlatformThread::CurrentId();
      return;
    }
    StatValue* stat = GetStats(service, interface, method, true);
    DCHECK(stat);
    if (type == TYPE_SENT_METHOD_CALLS)
      ++stat->sent_method_calls;
    else if (type == TYPE_RECEIVED_SIGNALS)
      ++stat->received_signals;
    else if (type == TYPE_SENT_BLOCKING_METHOD_CALLS)
      ++stat->sent_blocking_method_calls;
    else
      NOTREACHED();
  }

  // Look up the Stat entry in |stats_|. If |add_stat| is true, add a new entry
  // if one does not already exist.
  StatValue* GetStats(const std::string& service,
                      const std::string& interface,
                      const std::string& method,
                      bool add_stat) {
    DCHECK_EQ(origin_thread_id_, base::PlatformThread::CurrentId());

    StatKey key = {service, interface, method};
    auto it = stats_.find(key);
    if (it != stats_.end())
      return &(it->second);

    if (!add_stat)
      return nullptr;

    return &(stats_[key]);
  }

  StatMap& stats() { return stats_; }
  base::Time start_time() { return start_time_; }

 private:
  StatMap stats_;
  base::Time start_time_;
  base::PlatformThreadId origin_thread_id_;

  DISALLOW_COPY_AND_ASSIGN(DBusStatistics);
};

DBusStatistics* g_dbus_statistics = nullptr;

}  // namespace

//------------------------------------------------------------------------------

namespace statistics {

void Initialize() {
  if (g_dbus_statistics)
    delete g_dbus_statistics;  // reset statistics
  g_dbus_statistics = new DBusStatistics();
}

void Shutdown() {
  delete g_dbus_statistics;
  g_dbus_statistics = nullptr;
}

void AddSentMethodCall(const std::string& service,
                       const std::string& interface,
                       const std::string& method) {
  if (!g_dbus_statistics)
    return;
  g_dbus_statistics->AddStat(
      service, interface, method, DBusStatistics::TYPE_SENT_METHOD_CALLS);
}

void AddReceivedSignal(const std::string& service,
                       const std::string& interface,
                       const std::string& method) {
  if (!g_dbus_statistics)
    return;
  g_dbus_statistics->AddStat(
      service, interface, method, DBusStatistics::TYPE_RECEIVED_SIGNALS);
}

void AddBlockingSentMethodCall(const std::string& service,
                               const std::string& interface,
                               const std::string& method) {
  if (!g_dbus_statistics)
    return;
  g_dbus_statistics->AddStat(
      service, interface, method,
      DBusStatistics::TYPE_SENT_BLOCKING_METHOD_CALLS);
}

// NOTE: If the output format is changed, be certain to change the test
// expectations as well.
std::string GetAsString(ShowInString show, FormatString format) {
  if (!g_dbus_statistics)
    return "DBusStatistics not initialized.";

  const StatMap& stats = g_dbus_statistics->stats();
  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, sent_blocking = 0;
  // Stats are stored in order by service, then interface, then method.
  for (auto iter = stats.begin(); iter != stats.end();) {
    auto cur_iter = iter;
    auto next_iter = ++iter;
    const StatKey& stat_key = cur_iter->first;
    const StatValue& stat = cur_iter->second;
    sent += stat.sent_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
    // stat, continue.
    if (next_iter != stats.end() &&
        next_iter->first.service == stat_key.service &&
        (show < SHOW_INTERFACE ||
         next_iter->first.interface == stat_key.interface) &&
        (show < SHOW_METHOD || next_iter->first.method == stat_key.method))
      continue;

    if (!sent && !received && !sent_blocking)
        continue;  // No stats collected for this line, skip it and continue.

    // Add a line to the result and clear the counts.
    std::string line;
    if (show == SHOW_SERVICE) {
      line += stat_key.service;
    } else {
      // The interface usually includes the service so don't show both.
      line += stat_key.interface;
      if (show >= SHOW_METHOD)
        line += "." + stat_key.method;
    }
    line += base::StringPrintf(":");
    if (sent_blocking) {
      line += base::StringPrintf(" Sent (BLOCKING):");
      if (format == FORMAT_TOTALS)
        line += base::StringPrintf(" %d", sent_blocking);
      else if (format == FORMAT_PER_MINUTE)
        line += base::StringPrintf(" %d/min", sent_blocking / dminutes);
      else if (format == FORMAT_ALL)
        line += base::StringPrintf(" %d (%d/min)",
                                   sent_blocking, sent_blocking / dminutes);
    }
    if (sent) {
      line += base::StringPrintf(" Sent:");
      if (format == FORMAT_TOTALS)
        line += base::StringPrintf(" %d", sent);
      else if (format == FORMAT_PER_MINUTE)
        line += base::StringPrintf(" %d/min", sent / dminutes);
      else if (format == FORMAT_ALL)
        line += base::StringPrintf(" %d (%d/min)", sent, sent / dminutes);
    }
    if (received) {
      line += base::StringPrintf(" Received:");
      if (format == FORMAT_TOTALS)
        line += base::StringPrintf(" %d", received);
      else if (format == FORMAT_PER_MINUTE)
        line += base::StringPrintf(" %d/min", received / dminutes);
      else if (format == FORMAT_ALL)
        line += base::StringPrintf(
            " %d (%d/min)", received, received / dminutes);
    }
    result += line + "\n";
    sent = 0;
    sent_blocking = 0;
    received = 0;
  }
  return result;
}

namespace testing {

bool GetCalls(const std::string& service,
              const std::string& interface,
              const std::string& method,
              int* sent,
              int* received,
              int* blocking) {
  if (!g_dbus_statistics)
    return false;
  StatValue* stat =
      g_dbus_statistics->GetStats(service, interface, method, false);
  if (!stat)
    return false;
  *sent = stat->sent_method_calls;
  *received = stat->received_signals;
  *blocking = stat->sent_blocking_method_calls;
  return true;
}

}  // namespace testing

}  // namespace statistics
}  // namespace dbus