]> git.proxmox.com Git - ceph.git/blame - ceph/src/msg/async/EventEpoll.cc
update sources to 12.2.10
[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{
28 events = (struct epoll_event*)malloc(sizeof(struct epoll_event)*nevent);
29 if (!events) {
30 lderr(cct) << __func__ << " unable to malloc memory. " << dendl;
31 return -ENOMEM;
32 }
33 memset(events, 0, sizeof(struct epoll_event)*nevent);
34
35 epfd = epoll_create(1024); /* 1024 is just an hint for the kernel */
36 if (epfd == -1) {
37 lderr(cct) << __func__ << " unable to do epoll_create: "
38 << cpp_strerror(errno) << dendl;
39 return -errno;
40 }
91327a77
AA
41 if (::fcntl(epfd, F_SETFD, FD_CLOEXEC) == -1) {
42 int e = errno;
43 ::close(epfd);
44 lderr(cct) << __func__ << " unable to set cloexec: "
45 << cpp_strerror(e) << dendl;
46
47 return -e;
48 }
7c673cae
FG
49
50 size = nevent;
51
52 return 0;
53}
54
55int EpollDriver::add_event(int fd, int cur_mask, int add_mask)
56{
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. */
62 int op;
63 op = cur_mask == EVENT_NONE ? EPOLL_CTL_ADD: EPOLL_CTL_MOD;
64
65 ee.events = EPOLLET;
66 add_mask |= cur_mask; /* Merge old events */
67 if (add_mask & EVENT_READABLE)
68 ee.events |= EPOLLIN;
69 if (add_mask & EVENT_WRITABLE)
70 ee.events |= EPOLLOUT;
71 ee.data.u64 = 0; /* avoid valgrind warning */
72 ee.data.fd = fd;
73 if (epoll_ctl(epfd, op, fd, &ee) == -1) {
74 lderr(cct) << __func__ << " epoll_ctl: add fd=" << fd << " failed. "
75 << cpp_strerror(errno) << dendl;
76 return -errno;
77 }
78
79 return 0;
80}
81
82int EpollDriver::del_event(int fd, int cur_mask, int delmask)
83{
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);
88 int r = 0;
89
90 ee.events = 0;
91 if (mask & EVENT_READABLE) ee.events |= EPOLLIN;
92 if (mask & EVENT_WRITABLE) ee.events |= EPOLLOUT;
93 ee.data.u64 = 0; /* avoid valgrind warning */
94 ee.data.fd = fd;
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;
99 return -errno;
100 }
101 } else {
102 /* Note, Kernel < 2.6.9 requires a non null event pointer even for
103 * EPOLL_CTL_DEL. */
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;
107 return -errno;
108 }
109 }
110 return 0;
111}
112
113int EpollDriver::resize_events(int newsize)
114{
115 return 0;
116}
117
118int EpollDriver::event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tvp)
119{
120 int retval, numevents = 0;
121
122 retval = epoll_wait(epfd, events, size,
123 tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
124 if (retval > 0) {
125 int j;
126
127 numevents = retval;
128 fired_events.resize(numevents);
129 for (j = 0; j < numevents; j++) {
130 int mask = 0;
131 struct epoll_event *e = events + j;
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;
137 fired_events[j].fd = e->data.fd;
138 fired_events[j].mask = mask;
139 }
140 }
141 return numevents;
142}