Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / 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
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"
18 #include "EventEpoll.h"
19
20 #define dout_subsys ceph_subsys_ms
21
22 #undef dout_prefix
23 #define dout_prefix *_dout << "EpollDriver."
24
25 int EpollDriver::init(EventCenter *c, int nevent)
26 {
27   events = (struct epoll_event*)malloc(sizeof(struct epoll_event)*nevent);
28   if (!events) {
29     lderr(cct) << __func__ << " unable to malloc memory. " << dendl;
30     return -ENOMEM;
31   }
32   memset(events, 0, sizeof(struct epoll_event)*nevent);
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   }
40
41   size = nevent;
42
43   return 0;
44 }
45
46 int EpollDriver::add_event(int fd, int cur_mask, int add_mask)
47 {
48   ldout(cct, 20) << __func__ << " add event fd=" << fd << " cur_mask=" << cur_mask
49                  << " add_mask=" << add_mask << " to " << epfd << dendl;
50   struct epoll_event ee;
51   /* If the fd was already monitored for some event, we need a MOD
52    * operation. Otherwise we need an ADD operation. */
53   int op;
54   op = cur_mask == EVENT_NONE ? EPOLL_CTL_ADD: EPOLL_CTL_MOD;
55
56   ee.events = EPOLLET;
57   add_mask |= cur_mask; /* Merge old events */
58   if (add_mask & EVENT_READABLE)
59     ee.events |= EPOLLIN;
60   if (add_mask & EVENT_WRITABLE)
61     ee.events |= EPOLLOUT;
62   ee.data.u64 = 0; /* avoid valgrind warning */
63   ee.data.fd = fd;
64   if (epoll_ctl(epfd, op, fd, &ee) == -1) {
65     lderr(cct) << __func__ << " epoll_ctl: add fd=" << fd << " failed. "
66                << cpp_strerror(errno) << dendl;
67     return -errno;
68   }
69
70   return 0;
71 }
72
73 int EpollDriver::del_event(int fd, int cur_mask, int delmask)
74 {
75   ldout(cct, 20) << __func__ << " del event fd=" << fd << " cur_mask=" << cur_mask
76                  << " delmask=" << delmask << " to " << epfd << dendl;
77   struct epoll_event ee;
78   int mask = cur_mask & (~delmask);
79   int r = 0;
80
81   ee.events = 0;
82   if (mask & EVENT_READABLE) ee.events |= EPOLLIN;
83   if (mask & EVENT_WRITABLE) ee.events |= EPOLLOUT;
84   ee.data.u64 = 0; /* avoid valgrind warning */
85   ee.data.fd = fd;
86   if (mask != EVENT_NONE) {
87     if ((r = epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ee)) < 0) {
88       lderr(cct) << __func__ << " epoll_ctl: modify fd=" << fd << " mask=" << mask
89                  << " failed." << cpp_strerror(errno) << dendl;
90       return -errno;
91     }
92   } else {
93     /* Note, Kernel < 2.6.9 requires a non null event pointer even for
94      * EPOLL_CTL_DEL. */
95     if ((r = epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ee)) < 0) {
96       lderr(cct) << __func__ << " epoll_ctl: delete fd=" << fd
97                  << " failed." << cpp_strerror(errno) << dendl;
98       return -errno;
99     }
100   }
101   return 0;
102 }
103
104 int EpollDriver::resize_events(int newsize)
105 {
106   return 0;
107 }
108
109 int EpollDriver::event_wait(vector<FiredFileEvent> &fired_events, struct timeval *tvp)
110 {
111   int retval, numevents = 0;
112
113   retval = epoll_wait(epfd, events, size,
114                       tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
115   if (retval > 0) {
116     int j;
117
118     numevents = retval;
119     fired_events.resize(numevents);
120     for (j = 0; j < numevents; j++) {
121       int mask = 0;
122       struct epoll_event *e = events + j;
123
124       if (e->events & EPOLLIN) mask |= EVENT_READABLE;
125       if (e->events & EPOLLOUT) mask |= EVENT_WRITABLE;
126       if (e->events & EPOLLERR) mask |= EVENT_READABLE|EVENT_WRITABLE;
127       if (e->events & EPOLLHUP) mask |= EVENT_READABLE|EVENT_WRITABLE;
128       fired_events[j].fd = e->data.fd;
129       fired_events[j].mask = mask;
130     }
131   }
132   return numevents;
133 }