Class: Event::Backend::URing

Inherits:
Object
  • Object
show all
Defined in:
ext/event/backend/uring.c

Instance Method Summary collapse

Constructor Details

#initialize(loop) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'ext/event/backend/uring.c', line 83

VALUE Event_Backend_URing_initialize(VALUE self, VALUE loop) {
  struct Event_Backend_URing *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
  
  data->loop = loop;
  data->ring = xmalloc(sizeof(struct io_uring));
  
  int result = io_uring_queue_init(URING_ENTRIES, data->ring, 0);
  
  if (result == -1) {
    rb_sys_fail("io_uring_queue_init");
  }
  
  return self;
}

Instance Method Details

#io_read(fiber, io, buffer, offset, length) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/event/backend/uring.c', line 197

VALUE Event_Backend_URing_io_read(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE offset, VALUE length) {
  struct Event_Backend_URing *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);

  resize_to_capacity(buffer, NUM2SIZET(offset), NUM2SIZET(length));

  int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
  struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);

  struct iovec iovecs[1];
  iovecs[0].iov_base = RSTRING_PTR(buffer) + NUM2SIZET(offset);
  iovecs[0].iov_len = NUM2SIZET(length);

  io_uring_prep_readv(sqe, descriptor, iovecs, 1, 0);
  io_uring_sqe_set_data(sqe, (void*)fiber);
  io_uring_submit(data->ring);

  // fprintf(stderr, "prep_readv(%p, %d, %ld)\n", sqe, descriptor, iovecs[0].iov_len);

  int result = NUM2INT(rb_funcall(data->loop, id_transfer, 0));

  if (result < 0) {
    rb_syserr_fail(-result, strerror(-result));
  }

  resize_to_fit(buffer, NUM2SIZET(offset), (size_t)result);

  return INT2NUM(result);
}

#io_wait(fiber, io, events) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'ext/event/backend/uring.c', line 124

VALUE Event_Backend_URing_io_wait(VALUE self, VALUE fiber, VALUE io, VALUE events) {
  struct Event_Backend_URing *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
  
  int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
  struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);
  
  short flags = poll_flags_from_events(NUM2INT(events));
  
  // fprintf(stderr, "poll_add(%p, %d, %d)\n", sqe, descriptor, flags);
  
  io_uring_prep_poll_add(sqe, descriptor, flags);
  io_uring_sqe_set_data(sqe, (void*)fiber);
  io_uring_submit(data->ring);
  
  VALUE result = rb_funcall(data->loop, id_transfer, 0);
  
  // We explicitly filter the resulting events based on the requested events.
  // In some cases, poll will report events we didn't ask for.
  flags &= NUM2INT(result);
  
  return INT2NUM(events_from_poll_flags(flags));
}

#io_write(fiber, io, buffer, offset, length) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'ext/event/backend/uring.c', line 227

VALUE Event_Backend_URing_io_write(VALUE self, VALUE fiber, VALUE io, VALUE buffer, VALUE offset, VALUE length) {
  struct Event_Backend_URing *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);

  if ((size_t)RSTRING_LEN(buffer) < NUM2SIZET(offset) + NUM2SIZET(length)) {
    rb_raise(rb_eRuntimeError, "invalid offset/length exceeds bounds of buffer");
  }

  int descriptor = NUM2INT(rb_funcall(io, id_fileno, 0));
  struct io_uring_sqe *sqe = io_uring_get_sqe(data->ring);

  struct iovec iovecs[1];
  iovecs[0].iov_base = RSTRING_PTR(buffer) + NUM2SIZET(offset);
  iovecs[0].iov_len = NUM2SIZET(length);

  io_uring_prep_writev(sqe, descriptor, iovecs, 1, 0);
  io_uring_sqe_set_data(sqe, (void*)fiber);
  io_uring_submit(data->ring);
  
  // fprintf(stderr, "prep_writev(%p, %d, %ld)\n", sqe, descriptor, iovecs[0].iov_len);
  
  int result = NUM2INT(rb_funcall(data->loop, id_transfer, 0));

  if (result < 0) {
    rb_syserr_fail(-result, strerror(-result));
  }

  return INT2NUM(result);
}

#select(duration) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'ext/event/backend/uring.c', line 257

VALUE Event_Backend_URing_select(VALUE self, VALUE duration) {
  struct Event_Backend_URing *data = NULL;
  TypedData_Get_Struct(self, struct Event_Backend_URing, &Event_Backend_URing_Type, data);
  
  struct io_uring_cqe *cqes[URING_MAX_EVENTS];
  struct __kernel_timespec storage;
  
  int result = io_uring_peek_batch_cqe(data->ring, cqes, URING_MAX_EVENTS);
  
  // fprintf(stderr, "result = %d\n", result);
  
  if (result < 0) {
    rb_syserr_fail(-result, strerror(-result));
  } else if (result == 0) {
    result = io_uring_wait_cqes(data->ring, cqes, 1, make_timeout(duration, &storage), NULL);
    
    // fprintf(stderr, "result (timeout) = %d\n", result);
    
    if (result == -ETIME) {
      result = 0;
    } else if (result < 0) {
      rb_syserr_fail(-result, strerror(-result));
    }
  }
  
  for (int i = 0; i < result; i += 1) {
    VALUE fiber = (VALUE)io_uring_cqe_get_data(cqes[i]);
    VALUE result = INT2NUM(cqes[i]->res);
    
    // fprintf(stderr, "cqes[i]->res = %d\n", cqes[i]->res);
    
    io_uring_cqe_seen(data->ring, cqes[i]);
    
    rb_funcall(fiber, id_transfer, 1, result);
  }
  
  return INT2NUM(result);
}