<?xml version="1.0"?>

<!-- Copyright the Omegahat Project for Statistical Computing, 2000 -->
<!-- Author: Duncan Temple Lang -->

<!DOCTYPE doc [
  <!ENTITY infty "infinity">
  <!ENTITY C     "C">
  <!ENTITY sgets "&lt;-">
]>

<doc xmlns:eg="http://www.omegahat.org/"
     xmlns:rshelp="http://www.omegahat.org/RSHelp">

Here we define an S function and a C routine
to determine the process resource limits for  a
particular resource type.
<p/>

<code lang="S">
<function lang="S">
<def>
<![CDATA[
 processLimit <-
 function(name)  {
  ids <- getProcessLimits()
  if(!is.numeric(name)) {
    which <- match(as.character(name), names(ids))
    if(is.na(which))
      stop(paste("Unknown process limit", name))
  } else
     which <- as.integer(name)  

  vals <- .Call("RS_getLimit", ids[which])
  names(vals) <- c("soft", "hard")
  vals
 }
]]>
</def>
</function>
</code>

<example>
<code>
processLimit("stacksize")
</code>
<eg:output>
   soft    hard
2088960     Inf
</eg:output>
<code>
processLimit("coredumpsize")  
</code>
<eg:output>
   0  Inf
</eg:output>
</example>

The basic idea here is that we call the <croutine>getrlimit</croutine>
with the specified resource identifier and then put the elements of the
resulting <cstruct>rlimit</cstruct> structure into a numeric vector of
length 2.  We handle the case of an infinite limit by mapping this to
R's symbolic constant representing &infty;

<code lang="C">
These include files gives us both
the system resource definitions (enums identifying the different
resource types and the structures)
and also the support for sharing our code between
R and S.
<def>
<![CDATA[
#include <sys/time.h>
#include <sys/resource.h>

#include "RSCommon.h"
]]>
</def>
</code>

<code lang="C">
<![CDATA[

USER_OBJECT_
RS_getLimit(USER_OBJECT_ resourceId)
{
 struct rlimit limit;
 int res;
 USER_OBJECT_ ans;
 
  res = INTEGER_DATA(resourceId)[0];

  getrlimit(res, &limit);
  ans = NEW_NUMERIC(2);
  NUMERIC_DATA(ans)[0] = limit.rlim_cur != RLIM_INFINITY ?  limit.rlim_cur : R_PosInf;
  NUMERIC_DATA(ans)[1] = limit.rlim_max != RLIM_INFINITY ?  limit.rlim_max : R_PosInf;

 return(ans);
}
]]>
</code>

Next we write the S function that gets the symbolic constants
representing the different process limit attributes.  This returns a
named vector whose values are the symbolic constants and the names are
those that are to be used by the S user.

<p/>

The implementation is trivial, simply calling a &C;
routine <croutine>RS_getProcessLimitIds</croutine>.
<function lang="S">
<def>
 getProcessLimits &sgets;
 function() {
    .Call("RS_getProcessLimitIds")     
 }
</def>
<doc xmlns="http://www.omegahat.org/RSHelp">
<name>getProcessLimits</name>
<title>Symbolic identifiers for process limits</title>
<rshelp:description>
Gives the names and symbolic identifiers for the available process limits.
</rshelp:description>
<rshelp:arguments>
</rshelp:arguments>
</doc>
</function>


This is the function that computes the symbolic constants for the
different limit types and returns a named integer containing these and
the names by which they can referred.  The names come from the csh
command <shellCommand>limit</shellCommand>.
<code lang="C">
USER_OBJECT_
RS_getProcessLimitIds()
{
 USER_OBJECT_ ans, vecNames;
 int num, i;
 char *names[] = {
       "cputime",      
       "filesize",     
       "datasize",     
       "stacksize",    
       "coredumpsize", 
       "memoryuse",    
       "descriptors",  
       "memorylocked", 
       "maxproc",        
       "openfiles"
   };
   num = sizeof(names)/sizeof(names[0]);
   PROTECT(ans = NEW_INTEGER(num));
   PROTECT(vecNames = NEW_CHARACTER(num));
   for(i = 0; i &lt; num ; i++) {
     INTEGER_DATA(ans)[i] = i;
     SET_STRING_ELT(vecNames, i, COPY_TO_USER_STRING(names[i]));
   }

 SET_NAMES(ans, vecNames);
 UNPROTECT(2);
 return(ans);
}
</code>

</doc>
