This repository has been archived by the owner on Sep 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ClientStats.cpp
211 lines (184 loc) · 6.23 KB
/
ClientStats.cpp
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
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include "ClientStats.h"
#include <cassert>
#include <cstring>
ClientStats::~ClientStats() {
if (enabled_) {
{
std::unique_lock<std::mutex> lock(lk_);
terminateThread_ = true;
cv_.notify_one();
}
threadHandle_.join();
}
}
int ClientStats::start(
const char* fileName,
const char* prefix,
unsigned interval) {
int rc;
if (prefix) {
prefix_ = prefix;
prefix_ += ".";
}
interval_ = interval;
logger_ = std::make_shared<nfusr::Logger>();
if ((rc = logger_->openFile(
fileName, /* fifoMode = */ true, /* autoFlush = */ false)) != 0) {
return rc;
}
::memset(&opIntervalStats_, 0, sizeof(opIntervalStats_));
::memset(&rpcIntervalStats_, 0, sizeof(rpcIntervalStats_));
::memset(&opTotalStats_, 0, sizeof(opTotalStats_));
::memset(&rpcTotalStats_, 0, sizeof(rpcTotalStats_));
terminateThread_ = false;
threadHandle_ = std::thread(&ClientStats::statsDumpThread, this);
enabled_ = true;
return 0;
}
static inline double avg(uint64_t total_usec, uint64_t count) {
if (count) {
return (double)total_usec / (double)count;
}
return 0.;
}
void ClientStats::statsDumpThread() {
std::unique_lock<std::mutex> lock(lk_);
while (!terminateThread_) {
if (cv_.wait_until(
lock,
std::chrono::steady_clock::now() +
std::chrono::seconds(interval_)) == std::cv_status::timeout) {
// Copy all the counters into local buffers, then drop the lock.
opStats opIntervalStats[num_fuse_optypes];
opStats opTotalStats[num_fuse_optypes];
rpcStats rpcIntervalStats;
rpcStats rpcTotalStats;
::memcpy(&opIntervalStats, &opIntervalStats_, sizeof(opIntervalStats));
::memcpy(&opTotalStats, &opTotalStats_, sizeof(opTotalStats));
::memcpy(&rpcIntervalStats, &rpcIntervalStats_, sizeof(rpcIntervalStats));
::memcpy(&rpcTotalStats, &rpcTotalStats_, sizeof(rpcTotalStats));
::memset(&opIntervalStats_, 0, sizeof(opIntervalStats_));
::memset(&rpcIntervalStats_, 0, sizeof(rpcIntervalStats_));
lock.unlock();
for (unsigned ix = 1; ix < num_fuse_optypes; ++ix) {
auto name = fuse_optype_name((enum fuse_optype)ix);
logger_->printf(
"\"%saggr.%s.count\": \"%lu\"\n",
prefix_.c_str(),
name,
opTotalStats[ix].count);
logger_->printf(
"\"%saggr.%s.avg\": \"%lf\"\n",
prefix_.c_str(),
name,
avg(opTotalStats[ix].time_usec, opTotalStats[ix].count));
logger_->printf(
"\"%saggr.%s.max\": \"%lu\"\n",
prefix_.c_str(),
name,
opTotalStats[ix].max_time_usec);
logger_->printf(
"\"%sinter.%s.count\": \"%lu\"\n",
prefix_.c_str(),
name,
opIntervalStats[ix].count);
logger_->printf(
"\"%sinter.%s.avg\": \"%lf\"\n",
prefix_.c_str(),
name,
avg(opIntervalStats[ix].time_usec, opIntervalStats[ix].count));
logger_->printf(
"\"%sinter.%s.max\": \"%lu\"\n",
prefix_.c_str(),
name,
opIntervalStats[ix].max_time_usec);
}
logger_->printf(
"\"%saggr.rpc-failures.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.failures);
logger_->printf(
"\"%sinter.rpc-failures.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.failures);
logger_->printf(
"\"%saggr.rpc-timeouts.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.timeouts);
logger_->printf(
"\"%sinter.rpc-timeouts.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.timeouts);
logger_->printf(
"\"%saggr.rpc-connect.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.connect_success);
logger_->printf(
"\"%sinter.rpc-connect.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.connect_success);
logger_->printf(
"\"%saggr.rpc-connect-fail.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.connect_failure);
logger_->printf(
"\"%sinter.rpc-connect-fail.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.connect_failure);
logger_->printf(
"\"%saggr.rpc-issued.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.issued);
logger_->printf(
"\"%sinter.rpc-issued.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.issued);
logger_->printf(
"\"%saggr.rpc-completed.count\": \"%lu\"\n",
prefix_.c_str(),
rpcTotalStats.completed);
logger_->printf(
"\"%sinter.rpc-completed.count\": \"%lu\"\n",
prefix_.c_str(),
rpcIntervalStats.completed);
for (auto cb : callbacks_) {
cb(logger_, prefix_.c_str());
}
logger_->printf("<<EOF>>\n");
logger_->flush();
lock.lock();
}
}
}
/// @brief record the issue of an operation.
void ClientStats::recordIssue() {
std::unique_lock<std::mutex> lock(lk_);
rpcIntervalStats_.issued++;
rpcTotalStats_.issued++;
}
/// @brief record the completion of an operation.
void ClientStats::recordOperation(
enum fuse_optype optype,
std::chrono::microseconds elapsed) {
assert(optype < num_fuse_optypes);
uint64_t elapsed_usec = elapsed.count();
std::unique_lock<std::mutex> lock(lk_);
opTotalStats_[optype].count++;
opTotalStats_[optype].time_usec += elapsed_usec;
opTotalStats_[optype].max_time_usec =
std::max(opTotalStats_[optype].max_time_usec, elapsed_usec);
opIntervalStats_[optype].count++;
opIntervalStats_[optype].time_usec += elapsed_usec;
opIntervalStats_[optype].max_time_usec =
std::max(opIntervalStats_[optype].max_time_usec, elapsed_usec);
rpcIntervalStats_.completed++;
rpcTotalStats_.completed++;
}