module Coolio::Utils

Public Class Methods

Coolio::Utils.maxfds → Integer click to toggle source

Return the maximum number of files descriptors available to the process

static VALUE Coolio_Utils_maxfds(VALUE self)
{
#ifdef HAVE_SYS_RESOURCE_H
  struct rlimit rlim;

  if(getrlimit(RLIMIT_NOFILE, &rlim) < 0)
    rb_sys_fail("getrlimit");

  return INT2NUM(rlim.rlim_cur);
#endif

#ifndef HAVE_SYS_RESOURCE_H
  rb_raise(rb_eRuntimeError, "operation not supported");
#endif
}
Coolio::Utils.maxfds=(count) → Integer click to toggle source

Set the number of file descriptors available to the process. May require superuser privileges.

static VALUE Coolio_Utils_setmaxfds(VALUE self, VALUE max)
{
#ifdef HAVE_SYS_RESOURCE_H
  struct rlimit rlim;

  rlim.rlim_cur = NUM2INT(max);

  if(setrlimit(RLIMIT_NOFILE, &rlim) < 0)
    rb_sys_fail("setrlimit");

  return max;
#endif

#ifndef HAVE_SYS_RESOURCE_H
  rb_raise(rb_eRuntimeError, "operation not supported");
#endif
}
Coolio::Utils.ncpus → Integer click to toggle source

Return the number of CPUs in the present system

static VALUE Coolio_Utils_ncpus(VALUE self)
{
  int ncpus = 0;

#ifdef HAVE_LINUX_PROCFS
#define HAVE_COOLIO_UTILS_NCPUS
  char buf[512];
  FILE *cpuinfo;
  
  if(!(cpuinfo = fopen("/proc/cpuinfo", "r")))
    rb_sys_fail("fopen");

  while(fgets(buf, 512, cpuinfo)) {
    if(!strncmp(buf, "processor", 9))
      ncpus++;
  }
#endif

#ifdef HAVE_SYSCTLBYNAME
#define HAVE_COOLIO_UTILS_NCPUS
  size_t size = sizeof(int);

  if(sysctlbyname("hw.ncpu", &ncpus, &size, NULL, 0)) 
    return INT2NUM(1);
#endif

#ifndef HAVE_COOLIO_UTILS_NCPUS
  rb_raise(rb_eRuntimeError, "operation not supported");
#endif

  return INT2NUM(ncpus);
}