Skip to content

Commit cb1a620

Browse files
authored
Record latency of EventDispatcher (#2897)
* Record latency of EventDispatcher * Delete global bvar
1 parent 77431a3 commit cb1a620

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/brpc/event_dispatcher.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
#include "butil/fd_utility.h" // make_close_on_exec
2222
#include "butil/logging.h" // LOG
2323
#include "butil/third_party/murmurhash3/murmurhash3.h"// fmix32
24+
#include "bvar/latency_recorder.h" // bvar::LatencyRecorder
2425
#include "bthread/bthread.h" // bthread_start_background
2526
#include "brpc/event_dispatcher.h"
26-
#include "brpc/reloadable_flags.h"
2727

2828
DECLARE_int32(task_group_ntags);
2929

@@ -37,6 +37,8 @@ DEFINE_bool(usercode_in_coroutine, false,
3737
"User's callback are run in coroutine, no bthread or pthread blocking call");
3838

3939
static EventDispatcher* g_edisp = NULL;
40+
static bvar::LatencyRecorder* g_edisp_read_lantency = NULL;
41+
static bvar::LatencyRecorder* g_edisp_write_lantency = NULL;
4042
static pthread_once_t g_edisp_once = PTHREAD_ONCE_INIT;
4143

4244
static void StopAndJoinGlobalDispatchers() {
@@ -46,8 +48,13 @@ static void StopAndJoinGlobalDispatchers() {
4648
g_edisp[i * FLAGS_event_dispatcher_num + j].Join();
4749
}
4850
}
51+
delete g_edisp_read_lantency;
52+
delete g_edisp_write_lantency;
4953
}
5054
void InitializeGlobalDispatchers() {
55+
g_edisp_read_lantency = new bvar::LatencyRecorder("event_dispatcher_read_latency");
56+
g_edisp_write_lantency = new bvar::LatencyRecorder("event_dispatcher_write_latency");
57+
5158
g_edisp = new EventDispatcher[FLAGS_task_group_ntags * FLAGS_event_dispatcher_num];
5259
for (int i = 0; i < FLAGS_task_group_ntags; ++i) {
5360
for (int j = 0; j < FLAGS_event_dispatcher_num; ++j) {

src/brpc/event_dispatcher_epoll.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,18 @@ void EventDispatcher::Run() {
222222
|| (e[i].events & has_epollrdhup)
223223
#endif
224224
) {
225+
int64_t start_ns = butil::cpuwide_time_ns();
225226
// We don't care about the return value.
226227
CallInputEventCallback(e[i].data.u64, e[i].events, _thread_attr);
228+
(*g_edisp_read_lantency) << (butil::cpuwide_time_ns() - start_ns);
227229
}
228230
}
229231
for (int i = 0; i < n; ++i) {
230232
if (e[i].events & (EPOLLOUT | EPOLLERR | EPOLLHUP)) {
233+
int64_t start_ns = butil::cpuwide_time_ns();
231234
// We don't care about the return value.
232235
CallOutputEventCallback(e[i].data.u64, e[i].events, _thread_attr);
236+
(*g_edisp_write_lantency) << (butil::cpuwide_time_ns() - start_ns);
233237
}
234238
}
235239
}

src/brpc/event_dispatcher_kqueue.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,20 @@ void EventDispatcher::Run() {
205205
}
206206
for (int i = 0; i < n; ++i) {
207207
if ((e[i].flags & EV_ERROR) || e[i].filter == EVFILT_READ) {
208+
int64_t start_ns = butil::cpuwide_time_ns();
208209
// We don't care about the return value.
209210
CallInputEventCallback((IOEventDataId)e[i].udata,
210211
e[i].filter, _thread_attr);
212+
(*g_edisp_read_lantency) << (butil::cpuwide_time_ns() - start_ns);
211213
}
212214
}
213215
for (int i = 0; i < n; ++i) {
214216
if ((e[i].flags & EV_ERROR) || e[i].filter == EVFILT_WRITE) {
217+
int64_t start_ns = butil::cpuwide_time_ns();
215218
// We don't care about the return value.
216219
CallOutputEventCallback((IOEventDataId)e[i].udata,
217220
e[i].filter, _thread_attr);
221+
(*g_edisp_write_lantency) << (butil::cpuwide_time_ns() - start_ns);
218222
}
219223
}
220224
}

0 commit comments

Comments
 (0)