forked from facebookarchive/nfusr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClientStats.h
101 lines (84 loc) · 2.66 KB
/
ClientStats.h
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
/**
* 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.
*/
#pragma once
#include <condition_variable>
#include <thread>
#include <vector>
#include <functional>
#include "fuse_optype.h"
#include "logger.h"
/// @brief stats about the RPC subsystem as a whole.
struct rpcStats {
uint64_t issued; ///< total RPC calls issued.
uint64_t completed; ///< total RPC calls completed.
uint64_t failures; ///< failed RPC calls.
uint64_t timeouts; ///< timed out RPC calls (== retries).
uint64_t connect_success; ///< transport layer connections.
uint64_t connect_failure; ///< failures of transport layer.
};
/// @brief stats about a particular operation type (e.g. LOOKUP)
struct opStats {
uint64_t count; ///< total completed.
uint64_t time_usec; ///< total time for all 'count' completions.
uint64_t max_time_usec; ///< max time for any completion.
};
using statsCb =
std::function<void(std::shared_ptr<nfusr::Logger>, const char*)>;
class ClientStats {
public:
ClientStats() : enabled_(false) {}
~ClientStats();
int start(
const char* fileName,
const char* prefix,
unsigned interval_seconds = 60);
void addCallback(statsCb cb) {
callbacks_.push_back(cb);
}
void recordIssue();
void recordOperation(
enum fuse_optype optype,
std::chrono::microseconds elapsed);
void recordRpcFailure(bool isTimeout) {
std::unique_lock<std::mutex> lock(lk_);
++rpcIntervalStats_.failures;
++rpcTotalStats_.failures;
if (isTimeout) {
++rpcIntervalStats_.timeouts;
++rpcTotalStats_.timeouts;
}
}
void recordConnectSuccess() {
std::unique_lock<std::mutex> lock(lk_);
++rpcIntervalStats_.connect_success;
++rpcTotalStats_.connect_success;
}
void recordConnectFailure() {
std::unique_lock<std::mutex> lock(lk_);
++rpcIntervalStats_.connect_failure;
++rpcTotalStats_.connect_failure;
}
private:
void statsDumpThread();
bool enabled_;
// lk_ protects accesses to the shared counters below.
std::mutex lk_;
opStats opIntervalStats_[num_fuse_optypes];
opStats opTotalStats_[num_fuse_optypes];
rpcStats rpcIntervalStats_;
rpcStats rpcTotalStats_;
// end of members protected by lk_.
unsigned interval_;
std::shared_ptr<nfusr::Logger> logger_;
std::condition_variable cv_;
std::thread threadHandle_;
bool terminateThread_;
std::string prefix_;
std::vector<statsCb> callbacks_;
};