1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
3 * Ceph - scalable distributed file system
5 * Copyright (C) 2016 XSky <haomai@xsky.com>
7 * Author: Haomai Wang <haomaiwang@gmail.com>
9 * This is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License version 2.1, as published by the Free Software
12 * Foundation. See file COPYING.
16 #include "UserspaceEvent.h"
18 #include "common/dout.h"
19 #include "include/assert.h"
21 #define dout_subsys ceph_subsys_dpdk
23 #define dout_prefix *_dout << "dpdk "
25 int UserspaceEventManager::get_eventfd()
28 if (!unused_fds.empty()) {
29 fd = unused_fds.front();
30 unused_fds.pop_front();
36 Tub<UserspaceFDImpl> &impl = fds[fd];
39 ldout(cct, 20) << __func__ << " fd=" << fd << dendl;
43 int UserspaceEventManager::notify(int fd, int mask)
45 ldout(cct, 20) << __func__ << " fd=" << fd << " mask=" << mask << dendl;
46 if ((size_t)fd >= fds.size())
49 Tub<UserspaceFDImpl> &impl = fds[fd];
53 ldout(cct, 20) << __func__ << " activing=" << int(impl->activating_mask)
54 << " listening=" << int(impl->listening_mask)
55 << " waiting_idx=" << int(impl->waiting_idx) << dendl;
57 impl->activating_mask |= mask;
58 if (impl->waiting_idx)
61 if (impl->listening_mask & mask) {
62 if (waiting_fds.size() <= max_wait_idx)
63 waiting_fds.resize(waiting_fds.size()*2);
64 impl->waiting_idx = ++max_wait_idx;
65 waiting_fds[max_wait_idx] = fd;
68 ldout(cct, 20) << __func__ << " activing=" << int(impl->activating_mask)
69 << " listening=" << int(impl->listening_mask)
70 << " waiting_idx=" << int(impl->waiting_idx) << " done " << dendl;
74 void UserspaceEventManager::close(int fd)
76 ldout(cct, 20) << __func__ << " fd=" << fd << dendl;
77 if ((size_t)fd >= fds.size())
80 Tub<UserspaceFDImpl> &impl = fds[fd];
87 unused_fds.push_back(fd);
89 if (impl->activating_mask) {
90 if (waiting_fds[max_wait_idx] == fd) {
91 assert(impl->waiting_idx == max_wait_idx);
94 waiting_fds[impl->waiting_idx] = -1;
99 int UserspaceEventManager::poll(int *events, int *masks, int num_events, struct timeval *tp)
105 // leave zero slot for waiting_fds
106 while (i < max_wait_idx) {
107 fd = waiting_fds[++i];
112 Tub<UserspaceFDImpl> &impl = fds[fd];
114 masks[count] = impl->listening_mask & impl->activating_mask;
115 assert(masks[count]);
116 ldout(cct, 20) << __func__ << " fd=" << fd << " mask=" << masks[count] << dendl;
117 impl->activating_mask &= (~masks[count]);
118 impl->waiting_idx = 0;
119 if (++count >= num_events)
122 if (i < max_wait_idx) {
123 memmove(&waiting_fds[1], &waiting_fds[i+1], sizeof(int)*(max_wait_idx-i));