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
|
/*
* Copyright (C) 2007 Tony Arcieri
* You may redistribute this under the terms of the Ruby license.
* See LICENSE for details
*/
#include "ruby.h"
#ifdef HAVE_SYS_RESOURCE_H
#include <sys/resource.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/param.h>
#include <sys/sysctl.h>
#endif
#ifdef HAVE_SYSCTLBYNAME
#include <sys/sysctl.h>
#include <sys/types.h>
#endif
static VALUE mCoolio = Qnil;
static VALUE cCoolio_Utils = Qnil;
static VALUE Coolio_Utils_ncpus(VALUE self);
static VALUE Coolio_Utils_maxfds(VALUE self);
static VALUE Coolio_Utils_setmaxfds(VALUE self, VALUE max);
/*
* Assorted utility routines
*/
void Init_coolio_utils()
{
mCoolio = rb_define_module("Coolio");
cCoolio_Utils = rb_define_module_under(mCoolio, "Utils");
rb_define_singleton_method(cCoolio_Utils, "ncpus", Coolio_Utils_ncpus, 0);
rb_define_singleton_method(cCoolio_Utils, "maxfds", Coolio_Utils_maxfds, 0);
rb_define_singleton_method(cCoolio_Utils, "maxfds=", Coolio_Utils_setmaxfds, 1);
}
/**
* call-seq:
* Coolio::Utils.ncpus -> Integer
*
* 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);
}
/**
* call-seq:
* Coolio::Utils.maxfds -> Integer
*
* 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
}
/**
* call-seq:
* Coolio::Utils.maxfds=(count) -> Integer
*
* 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
}
|