]>
git.proxmox.com Git - ceph.git/blob - ceph/src/msg/async/EventEpoll.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
8 * Author: Haomai Wang <haomaiwang@gmail.com>
10 * This is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License version 2.1, as published by the Free Software
13 * Foundation. See file COPYING.
17 #include "common/errno.h"
19 #include "EventEpoll.h"
21 #define dout_subsys ceph_subsys_ms
24 #define dout_prefix *_dout << "EpollDriver."
26 int EpollDriver::init(EventCenter
*c
, int nevent
)
28 events
= (struct epoll_event
*)malloc(sizeof(struct epoll_event
)*nevent
);
30 lderr(cct
) << __func__
<< " unable to malloc memory. " << dendl
;
33 memset(events
, 0, sizeof(struct epoll_event
)*nevent
);
35 epfd
= epoll_create(1024); /* 1024 is just an hint for the kernel */
37 lderr(cct
) << __func__
<< " unable to do epoll_create: "
38 << cpp_strerror(errno
) << dendl
;
41 if (::fcntl(epfd
, F_SETFD
, FD_CLOEXEC
) == -1) {
44 lderr(cct
) << __func__
<< " unable to set cloexec: "
45 << cpp_strerror(e
) << dendl
;
55 int EpollDriver::add_event(int fd
, int cur_mask
, int add_mask
)
57 ldout(cct
, 20) << __func__
<< " add event fd=" << fd
<< " cur_mask=" << cur_mask
58 << " add_mask=" << add_mask
<< " to " << epfd
<< dendl
;
59 struct epoll_event ee
;
60 /* If the fd was already monitored for some event, we need a MOD
61 * operation. Otherwise we need an ADD operation. */
63 op
= cur_mask
== EVENT_NONE
? EPOLL_CTL_ADD
: EPOLL_CTL_MOD
;
66 add_mask
|= cur_mask
; /* Merge old events */
67 if (add_mask
& EVENT_READABLE
)
69 if (add_mask
& EVENT_WRITABLE
)
70 ee
.events
|= EPOLLOUT
;
71 ee
.data
.u64
= 0; /* avoid valgrind warning */
73 if (epoll_ctl(epfd
, op
, fd
, &ee
) == -1) {
74 lderr(cct
) << __func__
<< " epoll_ctl: add fd=" << fd
<< " failed. "
75 << cpp_strerror(errno
) << dendl
;
82 int EpollDriver::del_event(int fd
, int cur_mask
, int delmask
)
84 ldout(cct
, 20) << __func__
<< " del event fd=" << fd
<< " cur_mask=" << cur_mask
85 << " delmask=" << delmask
<< " to " << epfd
<< dendl
;
86 struct epoll_event ee
;
87 int mask
= cur_mask
& (~delmask
);
91 if (mask
& EVENT_READABLE
) ee
.events
|= EPOLLIN
;
92 if (mask
& EVENT_WRITABLE
) ee
.events
|= EPOLLOUT
;
93 ee
.data
.u64
= 0; /* avoid valgrind warning */
95 if (mask
!= EVENT_NONE
) {
96 if ((r
= epoll_ctl(epfd
, EPOLL_CTL_MOD
, fd
, &ee
)) < 0) {
97 lderr(cct
) << __func__
<< " epoll_ctl: modify fd=" << fd
<< " mask=" << mask
98 << " failed." << cpp_strerror(errno
) << dendl
;
102 /* Note, Kernel < 2.6.9 requires a non null event pointer even for
104 if ((r
= epoll_ctl(epfd
, EPOLL_CTL_DEL
, fd
, &ee
)) < 0) {
105 lderr(cct
) << __func__
<< " epoll_ctl: delete fd=" << fd
106 << " failed." << cpp_strerror(errno
) << dendl
;
113 int EpollDriver::resize_events(int newsize
)
118 int EpollDriver::event_wait(vector
<FiredFileEvent
> &fired_events
, struct timeval
*tvp
)
120 int retval
, numevents
= 0;
122 retval
= epoll_wait(epfd
, events
, size
,
123 tvp
? (tvp
->tv_sec
*1000 + tvp
->tv_usec
/1000) : -1);
128 fired_events
.resize(numevents
);
129 for (j
= 0; j
< numevents
; j
++) {
131 struct epoll_event
*e
= events
+ j
;
133 if (e
->events
& EPOLLIN
) mask
|= EVENT_READABLE
;
134 if (e
->events
& EPOLLOUT
) mask
|= EVENT_WRITABLE
;
135 if (e
->events
& EPOLLERR
) mask
|= EVENT_READABLE
|EVENT_WRITABLE
;
136 if (e
->events
& EPOLLHUP
) mask
|= EVENT_READABLE
|EVENT_WRITABLE
;
137 fired_events
[j
].fd
= e
->data
.fd
;
138 fired_events
[j
].mask
= mask
;