Class: IOU::Ring

Inherits:
Object
  • Object
show all
Defined in:
ext/iou/ring.c

Instance Method Summary collapse

Constructor Details

#initializeObject



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
# File 'ext/iou/ring.c', line 76

VALUE IOU_initialize(VALUE self) {
  IOU_t *iou = RTYPEDDATA_DATA(self);

  iou->ring_initialized = 0;
  iou->op_counter = 0;
  iou->unsubmitted_sqes = 0;
  iou->br_counter = 0;

  iou->pending_ops = rb_hash_new();

  unsigned prepared_limit = 1024;
  int flags = 0;
  #ifdef HAVE_IORING_SETUP_SUBMIT_ALL
  flags |= IORING_SETUP_SUBMIT_ALL;
  #endif
  #ifdef HAVE_IORING_SETUP_COOP_TASKRUN
  flags |= IORING_SETUP_COOP_TASKRUN;
  #endif

  while (1) {
    int ret = io_uring_queue_init(prepared_limit, &iou->ring, flags);
    if (likely(!ret)) break;

    // if ENOMEM is returned, try with half as much entries
    if (unlikely(ret == -ENOMEM && prepared_limit > 64))
      prepared_limit = prepared_limit / 2;
    else
      rb_syserr_fail(-ret, strerror(-ret));
  }
  iou->ring_initialized = 1;

  return self;
}

Instance Method Details

#closeObject



110
111
112
113
114
# File 'ext/iou/ring.c', line 110

VALUE IOU_close(VALUE self) {
  IOU_t *iou = RTYPEDDATA_DATA(self);
  cleanup_iou(iou);
  return self;
}

#closed?Boolean

Returns:

  • (Boolean)


116
117
118
119
# File 'ext/iou/ring.c', line 116

VALUE IOU_closed_p(VALUE self) {
  IOU_t *iou = RTYPEDDATA_DATA(self);
  return iou->ring_initialized ? Qfalse : Qtrue;
}

#emit(spec) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'ext/iou/ring.c', line 235

VALUE IOU_emit(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;
  VALUE ctx = setup_op_ctx(iou, OP_emit, SYM_emit, id, spec);
  if (rb_hash_aref(spec, SYM_signal) == SYM_stop)
    OpCtx_stop_signal_set(ctx);

  io_uring_prep_nop(sqe);

  // immediately submit
  io_uring_submit(&iou->ring);
  iou->unsubmitted_sqes = 0;

  return id;
}

#pending_opsObject



121
122
123
124
# File 'ext/iou/ring.c', line 121

VALUE IOU_pending_ops(VALUE self) {
  IOU_t *iou = RTYPEDDATA_DATA(self);
  return iou->pending_ops;
}

#prep_accept(spec) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'ext/iou/ring.c', line 255

VALUE IOU_prep_accept(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  VALUE values[1];
  get_required_kwargs(spec, values, 1, SYM_fd);
  VALUE fd = values[0];
  VALUE multishot = rb_hash_aref(spec, SYM_multishot);

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;

  VALUE ctx = setup_op_ctx(iou, OP_accept, SYM_accept, id, spec);
  struct sa_data *sa = OpCtx_sa_get(ctx);
  if (RTEST(multishot))
    io_uring_prep_multishot_accept(sqe, NUM2INT(fd), &sa->addr, &sa->len, 0);
  else
    io_uring_prep_accept(sqe, NUM2INT(fd), &sa->addr, &sa->len, 0);
  iou->unsubmitted_sqes++;
  return id;
}

#prep_cancel(spec) ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'ext/iou/ring.c', line 290

VALUE IOU_prep_cancel(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);

  if (TYPE(spec) == T_FIXNUM)
    return prep_cancel_id(iou, NUM2UINT(spec));
  
  if (TYPE(spec) != T_HASH)
    rb_raise(cArgumentError, "Expected operation id or keyword arguments");

  VALUE id = rb_hash_aref(spec, SYM_id);
  if (!NIL_P(id))
    return prep_cancel_id(iou, NUM2UINT(id));

  rb_raise(cArgumentError, "Missing operation id");
}

#prep_close(spec) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/iou/ring.c', line 306

VALUE IOU_prep_close(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  VALUE values[1];
  get_required_kwargs(spec, values, 1, SYM_fd);
  VALUE fd = values[0];

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;

  setup_op_ctx(iou, OP_close, SYM_close, id, spec);

  io_uring_prep_close(sqe, NUM2INT(fd));
  iou->unsubmitted_sqes++;
  return id;
}

#prep_nopObject



325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/iou/ring.c', line 325

VALUE IOU_prep_nop(VALUE self) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  struct io_uring_sqe *sqe = get_sqe(iou);
  io_uring_prep_nop(sqe);
  sqe->user_data = id_i;
  iou->unsubmitted_sqes++;

  return id;
}

#prep_read(spec) ⇒ Object



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'ext/iou/ring.c', line 379

VALUE IOU_prep_read(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);

  if (RTEST(rb_hash_aref(spec, SYM_multishot)))
    return prep_read_multishot(iou, spec);

  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  VALUE values[3];
  get_required_kwargs(spec, values, 3, SYM_fd, SYM_buffer, SYM_len);

  VALUE fd = values[0];
  VALUE buffer = values[1];
  VALUE len = values[2];
  unsigned len_i = NUM2UINT(len);

  VALUE buffer_offset = rb_hash_aref(spec, SYM_buffer_offset);
  int buffer_offset_i = NIL_P(buffer_offset) ? 0 : NUM2INT(buffer_offset);
  int utf8 = RTEST(rb_hash_aref(spec, SYM_utf8));

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;

  VALUE ctx = setup_op_ctx(iou, OP_read, SYM_read, id, spec);
  OpCtx_rd_set(ctx, buffer, buffer_offset_i, 0, utf8);

  void *ptr = prepare_read_buffer(buffer, len_i, buffer_offset_i);
  io_uring_prep_read(sqe, NUM2INT(fd), ptr, len_i, -1);
  iou->unsubmitted_sqes++;
  return id;
}

#prep_timeout(spec) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'ext/iou/ring.c', line 412

VALUE IOU_prep_timeout(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  VALUE values[1];
  get_required_kwargs(spec, values, 1, SYM_interval);
  VALUE interval = values[0];
  VALUE multishot = rb_hash_aref(spec, SYM_multishot);
  unsigned flags = RTEST(multishot) ? IORING_TIMEOUT_MULTISHOT : 0;

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;

  VALUE ctx = setup_op_ctx(iou, OP_timeout, SYM_timeout, id, spec);
  OpCtx_ts_set(ctx, interval);

  io_uring_prep_timeout(sqe, OpCtx_ts_get(ctx), 0, flags);
  iou->unsubmitted_sqes++;
  return id;
}

#prep_write(spec) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'ext/iou/ring.c', line 434

VALUE IOU_prep_write(VALUE self, VALUE spec) {
  IOU_t *iou = get_iou(self);
  unsigned id_i = ++iou->op_counter;
  VALUE id = UINT2NUM(id_i);

  VALUE values[2];
  get_required_kwargs(spec, values, 2, SYM_fd, SYM_buffer);
  VALUE fd = values[0];
  VALUE buffer = values[1];
  VALUE len = rb_hash_aref(spec, SYM_len);
  unsigned nbytes = NIL_P(len) ? RSTRING_LEN(buffer) : NUM2UINT(len);

  struct io_uring_sqe *sqe = get_sqe(iou);
  sqe->user_data = id_i;

  setup_op_ctx(iou, OP_write, SYM_write, id, spec);

  io_uring_prep_write(sqe, NUM2INT(fd), RSTRING_PTR(buffer), nbytes, -1);
  iou->unsubmitted_sqes++;
  return id;
}

#process_completions(*args) ⇒ Object



635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'ext/iou/ring.c', line 635

VALUE IOU_process_completions(int argc, VALUE *argv, VALUE self) {
  IOU_t *iou = get_iou(self);
  int block_given = rb_block_given_p();
  VALUE wait;

  rb_scan_args(argc, argv, "01", &wait);
  int wait_i = RTEST(wait);
  unsigned count = 0;

  // automatically submit any unsubmitted SQEs
  if (iou->unsubmitted_sqes) {
    io_uring_submit(&iou->ring);
    iou->unsubmitted_sqes = 0;
  }

  if (wait_i) {
    wait_for_completion_ctx_t ctx = { .iou = iou };

    rb_thread_call_without_gvl(wait_for_completion_without_gvl, (void *)&ctx, RUBY_UBF_IO, 0);
    if (unlikely(ctx.ret < 0)) {
      rb_syserr_fail(-ctx.ret, strerror(-ctx.ret));
    }
    ++count;
    io_uring_cqe_seen(&iou->ring, ctx.cqe);
    process_cqe(iou, ctx.cqe, block_given, 0);
  }

  count += process_ready_cqes(iou, block_given, 0);
  return UINT2NUM(count);
}

#process_completions_loopObject



666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
# File 'ext/iou/ring.c', line 666

VALUE IOU_process_completions_loop(VALUE self) {
  IOU_t *iou = get_iou(self);
  int block_given = rb_block_given_p();
  int stop_flag = 0;
  wait_for_completion_ctx_t ctx = { .iou = iou };

  while (1) {
    // automatically submit any unsubmitted SQEs
    if (iou->unsubmitted_sqes) {
      io_uring_submit(&iou->ring);
      iou->unsubmitted_sqes = 0;
    }

    rb_thread_call_without_gvl(wait_for_completion_without_gvl, (void *)&ctx, RUBY_UBF_IO, 0);
    if (unlikely(ctx.ret < 0)) {
      rb_syserr_fail(-ctx.ret, strerror(-ctx.ret));
    }
    io_uring_cqe_seen(&iou->ring, ctx.cqe);
    process_cqe(iou, ctx.cqe, block_given, &stop_flag);
    if (stop_flag) goto done;

    process_ready_cqes(iou, block_given, &stop_flag);
    if (stop_flag) goto done;
  }
done:
  return self;
}

#setup_buffer_ring(opts) ⇒ Object



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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'ext/iou/ring.c', line 168

VALUE IOU_setup_buffer_ring(VALUE self, VALUE opts) {
  IOU_t *iou = get_iou(self);

  if (iou->br_counter == BUFFER_RING_MAX_COUNT)
    rb_raise(rb_eRuntimeError, "Cannot setup more than BUFFER_RING_MAX_COUNT buffer rings");

  VALUE values[2];
  get_required_kwargs(opts, values, 2, SYM_count, SYM_size);
  VALUE count = values[0];
  VALUE size = values[1];

  struct buf_ring_descriptor *desc = iou->brs + iou->br_counter;
  desc->buf_count = NUM2UINT(count);
  desc->buf_size = NUM2UINT(size);

  desc->br_size = sizeof(struct io_uring_buf) * desc->buf_count;
	void *mapped = mmap(
    NULL, desc->br_size, PROT_READ | PROT_WRITE,
		MAP_ANONYMOUS | MAP_PRIVATE, 0, 0
  );
  if (mapped == MAP_FAILED)
    rb_raise(rb_eRuntimeError, "Failed to allocate buffer ring");

  desc->br = (struct io_uring_buf_ring *)mapped;
  io_uring_buf_ring_init(desc->br);

  unsigned bg_id = iou->br_counter;
  struct io_uring_buf_reg reg = {
    .ring_addr = (unsigned long)desc->br,
		.ring_entries = desc->buf_count,
		.bgid = bg_id
  };
	int ret = io_uring_register_buf_ring(&iou->ring, &reg, 0);
	if (ret) {
    munmap(desc->br, desc->br_size);
    rb_syserr_fail(-ret, strerror(-ret));
	}

  desc->buf_base = malloc(desc->buf_count * desc->buf_size);
  if (!desc->buf_base) {
    io_uring_free_buf_ring(&iou->ring, desc->br, desc->buf_count, bg_id);
    rb_raise(rb_eRuntimeError, "Failed to allocate buffers");
  }

  int mask = io_uring_buf_ring_mask(desc->buf_count);
	for (unsigned i = 0; i < desc->buf_count; i++) {
		io_uring_buf_ring_add(
      desc->br, desc->buf_base + i * desc->buf_size, desc->buf_size,
      i, mask, i);
	}
	io_uring_buf_ring_advance(desc->br, desc->buf_count);
  iou->br_counter++;
  return UINT2NUM(bg_id);
}

#submitObject



456
457
458
459
460
461
462
463
464
465
466
467
# File 'ext/iou/ring.c', line 456

VALUE IOU_submit(VALUE self) {
  IOU_t *iou = get_iou(self);
  if (!iou->unsubmitted_sqes) goto done;

  iou->unsubmitted_sqes = 0;
  int ret = io_uring_submit(&iou->ring);
  if (ret < 0)
    rb_syserr_fail(-ret, strerror(-ret));

done:
  return self;
}

#wait_for_completionObject



561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'ext/iou/ring.c', line 561

VALUE IOU_wait_for_completion(VALUE self) {
  IOU_t *iou = get_iou(self);

  wait_for_completion_ctx_t cqe_ctx = {
    .iou = iou
  };

  rb_thread_call_without_gvl(wait_for_completion_without_gvl, (void *)&cqe_ctx, RUBY_UBF_IO, 0);

  if (unlikely(cqe_ctx.ret < 0)) {
    rb_syserr_fail(-cqe_ctx.ret, strerror(-cqe_ctx.ret));
  }
  io_uring_cqe_seen(&iou->ring, cqe_ctx.cqe);

  VALUE spec = Qnil;
  get_cqe_ctx(iou, cqe_ctx.cqe, 0, &spec);
  return spec;
}