]> git.proxmox.com Git - ceph.git/blame - ceph/src/msg/async/EventEpoll.cc
import 15.2.0 Octopus source
[ceph.git] / ceph / src / msg / async / EventEpoll.cc
CommitLineData
7c673cae
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3/*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2014 UnitedStack <haomai@unitedstack.com>
7 *
8 * Author: Haomai Wang <haomaiwang@gmail.com>
9 *
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.
14 *
15 */
16
17#include "common/errno.h"
91327a77 18#include <fcntl.h>
7c673cae
FG
19#include "EventEpoll.h"
20
21#define dout_subsys ceph_subsys_ms
22
23#undef dout_prefix
24#define dout_prefix *_dout << "EpollDriver."
25
26int EpollDriver::init(EventCenter *c, int nevent)
27{
9f95a23c 28 events = (struct epoll_event*)calloc(nevent, sizeof(struct epoll_event));
7c673cae
FG
29 if (!events) {
30 lderr(cct) << __func__ << " unable to malloc memory. " << dendl;
31 return -ENOMEM;
32 }
7c673cae
FG
33
34 epfd = epoll_create(1024); /* 1024 is just an hint for the kernel */
35 if (epfd == -1) {
36 lderr(cct) << __func__ << " unable to do epoll_create: "
37 << cpp_strerror(errno) << dendl;
38 return -errno;
39 }
91327a77
AA
40 if (::fcntl(epfd, F_SETFD, FD_CLOEXEC) == -1) {
41 int e = errno;
42 ::close(epfd);
43 lderr(cct) << __func__ << " unable to set cloexec: "
44 << cpp_strerror(e) << dendl;
45
46 return -e;
47 }
7c673cae 48
9f95a23c 49 this->nevent = nevent;
7c673cae
FG
50
51 return 0;
52}
53
54int EpollDriver::add_event(int fd, int cur_mask, int add_mask)
55{
56 ldout(cct, 20) << __func__ << " add event fd=" << fd << " cur_mask=" << cur_mask
57 << " add_mask=" << add_mask << " to " << epfd << dendl;
58 struct epoll_event ee;
59 /* If the fd was already monitored for some event, we need a MOD
60 * operation. Otherwise we need an ADD operation. */
61 int op;
62 op = cur_mask == EVENT_NONE ? EPOLL_CTL_ADD: EPOLL_CTL_MOD;
63
64 ee.events = EPOLLET;
65 add_mask |= cur_mask; /* Merge old events */
66 if (add_mask & EVENT_READABLE)
67 ee.events |= EPOLLIN;
68 if (add_mask & EVENT_WRITABLE)
69 ee.events |= EPOLLOUT;
70 ee.data.u64 = 0; /* avoid valgrind warning */
71 ee.data.fd = fd;
72 if (epoll_ctl(epfd, op, fd, &ee) == -1) {
73 lderr(cct) << __func__ << " epoll_ctl: add fd=" << fd << " failed. "
74 << cpp_strerror(errno) << dendl;
75 return -errno;
76 }
77
78 return 0;
79}
80
81int EpollDriver::del_event(int fd, int cur_mask, int delmask)
82{
83 ldout(cct, 20) << __func__ << " del event fd=" << fd << " cur_mask=" << cur_mask
84 << " delmask=" << delmask << " to " << epfd << dendl;
9f95a23c 85 struct epoll_event ee = {0};
7c673cae
FG
86 int mask = cur_mask & (~delmask);
87 int r = 0;
88
7c673cae 89 if (mask != EVENT_NONE) {
9f95a23c
TL
90 ee.events = EPOLLET;
91 ee.data.fd = fd;
92 if (mask & EVENT_READABLE)
93 ee.events |= EPOLLIN;
94 if (mask & EVENT_WRITABLE)
95 ee.events |= EPOLLOUT;
96
7c673cae
FG
97 if ((r = epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ee)) < 0) {
98 lderr(cct) << __func__ << " epoll_ctl: modify fd=" << fd << " mask=" << mask
99 << " failed." << cpp_strerror(errno) << dendl;
100 return -errno;
101 }
102 } else {
103 /* Note, Kernel < 2.6.9 requires a non null event pointer even for
104 * EPOLL_CTL_DEL. */
105 if ((r = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ee)) < 0) {
106 lderr(cct) << __func__ << " epoll_ctl: delete fd=" << fd
107 << " failed." << cpp_strerror(errno) << dendl;
108 return -errno;
109 }
110 }
111 return 0;
112}
113
114int EpollDriver::resize_events(int newsize)
115{
116 return 0;
117}
118
119int EpollDriver::event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tvp)
120{
121 int retval, numevents = 0;
122
9f95a23c 123 retval = epoll_wait(epfd, events, nevent,
7c673cae
FG
124 tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
125 if (retval > 0) {
7c673cae
FG
126 numevents = retval;
127 fired_events.resize(numevents);
9f95a23c
TL
128
129 for (int event_id = 0; event_id < numevents; event_id++) {
7c673cae 130 int mask = 0;
9f95a23c 131 struct epoll_event *e = &events[event_id];
7c673cae
FG
132
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;
9f95a23c
TL
137 fired_events[event_id].fd = e->data.fd;
138 fired_events[event_id].mask = mask;
7c673cae
FG
139 }
140 }
141 return numevents;
142}