summaryrefslogtreecommitdiffstats
path: root/src/msg/async/EventPoll.cc
blob: 4c09dbb4db4177b0533014c9a7a10fd0b5ad6b93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab ft=cpp
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2022 Rafael Lopez <rafael.lopez@softiron.com>
 *
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software
 * Foundation.	See file COPYING.
 *
 */

#include "common/errno.h"
#include "EventPoll.h"

#include <unistd.h>
#define dout_subsys ceph_subsys_ms

#undef dout_prefix
#define dout_prefix *_dout << "PollDriver."

#ifndef POLL_ADD
#define POLL_ADD 1
#ifndef POLL_MOD
#define POLL_MOD 2
#ifndef POLL_DEL
#define POLL_DEL 3
#endif
#endif
#endif

int PollDriver::init(EventCenter *c, int nevent) {
  // pfds array will auto scale up to hard_max_pfds, which should be
  // greater than total daemons/op_threads (todo: cfg option?)
  hard_max_pfds = 8192;
  // 128 seems a good starting point, cover clusters up to ~350 OSDs
  // with default ms_async_op_threads
  max_pfds = 128;

  pfds = (POLLFD*)calloc(max_pfds, sizeof(POLLFD));
  if (!pfds) {
    lderr(cct) << __func__ << " unable to allocate memory " << dendl;
    return -ENOMEM;
  }

  //initialise pfds
  for(int i = 0; i < max_pfds; i++){
    pfds[i].fd = -1;
    pfds[i].events = 0;
    pfds[i].revents = 0;
  }
  return 0;
}

// Helper func to register/unregister interest in a FD's events by
// manipulating it's entry in pfds array
int PollDriver::poll_ctl(int fd, int op, int events) {
  int pos = 0;
  if (op == POLL_ADD) {
    // Find an empty pollfd slot
    for(pos = 0; pos < max_pfds ; pos++){
      if(pfds[pos].fd == -1){
	pfds[pos].fd = fd;
	pfds[pos].events = events;
	pfds[pos].revents = 0;
	return 0;
      }
    }
    // We ran out of slots, try to increase
    if (max_pfds < hard_max_pfds) {
      ldout(cct, 10) << __func__ << " exhausted pollfd slots"
		     << ", doubling to " << max_pfds*2 << dendl;
      pfds = (POLLFD*)realloc(pfds, max_pfds*2*sizeof(POLLFD));
      if (!pfds) {
	lderr(cct) << __func__ << " unable to realloc for more pollfd slots"
		   << dendl;
	return -ENOMEM;
      }
      // Initialise new slots
      for (int i = max_pfds ; i < max_pfds*2 ; i++){
	pfds[i].fd = -1;
	pfds[i].events = 0;
	pfds[i].revents = 0;
      }
      max_pfds = max_pfds*2;
      pfds[pos].fd = fd;
      pfds[pos].events = events;
      pfds[pos].revents = 0;
      return 0;
    } else {
    // Hit hard limit
    lderr(cct) << __func__ << " hard limit for file descriptors per op" 
	       << " thread reached (" << hard_max_pfds << ")" << dendl;
    return -EMFILE;
    }
  } else if (op == POLL_MOD) {
    for (pos = 0; pos < max_pfds; pos++ ){
      if (pfds[pos].fd == fd) {
	pfds[pos].events = events;
	return 0;
      }
    }
  } else if (op == POLL_DEL) {
    for (pos = 0; pos < max_pfds; pos++ ){
      if (pfds[pos].fd == fd) {
	pfds[pos].fd = -1;
	pfds[pos].events = 0;
	return 0;
      }
    }
  }
  return 0;
}

int PollDriver::add_event(int fd, int cur_mask, int add_mask) {
  ldout(cct, 10) << __func__ << " add event to fd=" << fd << " mask="
		 << add_mask << dendl;
  int op, events = 0;
  op = cur_mask == EVENT_NONE ? POLL_ADD: POLL_MOD;

  add_mask |= cur_mask; /* Merge old events */
  if (add_mask & EVENT_READABLE) {
    events |= POLLIN;
  }
  if (add_mask & EVENT_WRITABLE) {
    events |= POLLOUT;
  }
  int ret = poll_ctl(fd, op, events);
  return ret;
}

int PollDriver::del_event(int fd, int cur_mask, int delmask) {
  ldout(cct, 10) << __func__ << " del event fd=" << fd << " cur mask="
		 << cur_mask << dendl;
  int op, events = 0;
  int mask = cur_mask & (~delmask);

  if (mask != EVENT_NONE) {
    op = POLL_MOD;
    if (mask & EVENT_READABLE) {
      events |= POLLIN;
    }
    if (mask & EVENT_WRITABLE) {
      events |= POLLOUT;
    }
  } else {
    op = POLL_DEL;
  }
  poll_ctl(fd, op, events);
  return 0;
}

int PollDriver::resize_events(int newsize) {
  return 0;
}

int PollDriver::event_wait(std::vector<FiredFileEvent> &fired_events,
			  struct timeval *tvp) {
  int retval, numevents = 0;
#ifdef _WIN32
  retval = WSAPoll(pfds, max_pfds,
		      tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
#else
  retval = poll(pfds, max_pfds,
		      tvp ? (tvp->tv_sec*1000 + tvp->tv_usec/1000) : -1);
#endif
  if (retval > 0) {
    for (int j = 0; j < max_pfds; j++) {
      if (pfds[j].fd != -1) {
	int mask = 0;
	struct FiredFileEvent fe;
	if (pfds[j].revents & POLLIN) {
	  mask |= EVENT_READABLE;
	}
	if (pfds[j].revents & POLLOUT) {
	  mask |= EVENT_WRITABLE;
	}
	if (pfds[j].revents & POLLHUP) {
	  mask |= EVENT_READABLE | EVENT_WRITABLE;
	}
	if (pfds[j].revents & POLLERR) {
	  mask |= EVENT_READABLE | EVENT_WRITABLE;
	}
	if (mask) {
	  fe.fd = pfds[j].fd;
	  fe.mask = mask;
	  fired_events.push_back(fe);
	  numevents++;
	}
      }
    }
  }
  return numevents;
}