Module: Raptor::CPU

Defined in:
ext/raptor_native/raptor_native.c

Class Method Summary collapse

Class Method Details

.count ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'ext/raptor_native/raptor_native.c', line 76

static VALUE cpu_count(VALUE self) {
#ifdef __linux__
  cpu_set_t set;
  CPU_ZERO(&set);
  if (sched_getaffinity(0, sizeof(set), &set) < 0) rb_sys_fail("sched_getaffinity");
  return LONG2NUM((long)CPU_COUNT(&set));
#else
  return LONG2NUM(0);
#endif
}

.pin(cpu) ⇒ Object

worker indexes map onto the CPUs the process is actually allowed to use.



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
# File 'ext/raptor_native/raptor_native.c', line 47

static VALUE cpu_pin(VALUE self, VALUE cpu) {
#ifdef __linux__
  int worker_index = NUM2INT(cpu);
  cpu_set_t current;
  CPU_ZERO(&current);
  if (sched_getaffinity(0, sizeof(current), &current) < 0) rb_sys_fail("sched_getaffinity");

  int cpu_id = -1;
  int seen = 0;
  for (int candidate = 0; candidate < CPU_SETSIZE; candidate++) {
    if (!CPU_ISSET(candidate, &current)) continue;
    if (seen == worker_index) {
      cpu_id = candidate;
      break;
    }
    seen++;
  }
  if (cpu_id < 0) return Qfalse;

  cpu_set_t set;
  CPU_ZERO(&set);
  CPU_SET(cpu_id, &set);
  if (sched_setaffinity(0, sizeof(set), &set) < 0) rb_sys_fail("sched_setaffinity");
  return Qtrue;
#else
  return Qfalse;
#endif
}

.yield ⇒ Object

Ruby's ready queue.



96
97
98
99
100
101
102
# File 'ext/raptor_native/raptor_native.c', line 96

static VALUE cpu_yield(VALUE self) {
  rb_thread_schedule();
#ifdef __linux__
  rb_thread_call_without_gvl(cpu_yield_without_gvl, NULL, NULL, NULL);
#endif
  return Qnil;
}