eg_macros.c

Go to the documentation of this file.
00001 /* ========================================================================= */
00002 /* EGlib "Efficient General Library" provides some basic structures and
00003  * algorithms commons in many optimization algorithms.
00004  *
00005  * Copyright (C) 2005 Daniel Espinoza and Marcos Goycoolea.
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or (at your
00010  * option) any later version.
00011  *
00012  * This library is distributed in the hope that it will be useful, but 
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
00014  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
00015  * License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
00020  * */
00021 /* ========================================================================= */
00022 #include "eg_config.h"
00023 #include "eg_macros.h"
00024 /** @file
00025  * @brief implementation of some macros.
00026  *
00027  * @version 0.9.2 
00028  * @par History:
00029  *  -2006-09-28
00030  *            - First implementation
00031  *  -2007-12-06
00032  *            - Add versioning information in header
00033  * @ingroup EGmacros */
00034 /** @addtogroup EGmacros */
00035 /** @{ */
00036 /* ========================================================================= */
00037 void EGlib_info(void)
00038 {
00039   int rval;
00040   struct utsname uts;
00041   rval = uname(&uts);
00042   if(rval)
00043   {
00044     fprintf(stderr,"Can't get host info\n");
00045   }
00046   else
00047   {
00048     fprintf(stderr,"Host: %s, %s %s %s\nCurrent process id: %d\n",uts.nodename, uts.sysname, uts.release, uts.machine ,(int)getpid());
00049   }
00050 }
00051 /** @} */
00052 /* ========================================================================= */
00053 jmp_buf __EGljmp;
00054 /* ========================================================================= */
00055 void EGsighandler(int s)
00056 {
00057   switch(s)
00058   {
00059     case SIGXCPU:
00060       /* time is over */ 
00061       fprintf(stderr,"\nTIME_LIMIT_REACHED (ending now)\n");
00062       longjmp(__EGljmp,s);
00063       break;
00064     case SIGINT:
00065     case SIGTERM:
00066     case SIGTSTP:
00067       /* time is over */ 
00068       fprintf(stderr,"\nUSER_INTERRUPT (ending now)\n");
00069       longjmp(__EGljmp,s);
00070       break;
00071     case SIGSEGV:
00072       /* Memory is over or segmentation fault */ 
00073       fprintf(stderr,"\nMEMORY_LIMIT_REACHED (ending now)\n");
00074       longjmp(__EGljmp,s);
00075       break;
00076     default:
00077       fprintf(stderr,"Unkown signal %d\n",s);
00078       fprintf(stderr,"Ending with status %d\n",s);
00079       exit(s);
00080   }
00081 }
00082 /* ========================================================================= */
00083 void __EGsigSetSignal(void)
00084 {
00085   signal(SIGXCPU,EGsighandler);
00086   signal(SIGINT,EGsighandler);
00087   signal(SIGSEGV,EGsighandler);
00088 }
00089 /* ========================================================================= */
00090 void EGsetLimits(double max_rtime, unsigned long memlimit)
00091 {
00092   struct rlimit mlim;
00093   WARNIF(getrlimit(RLIMIT_CPU,&mlim));
00094   MESSAGE(0, "Cur rtime limit %ld, trying to set to %lg", mlim.rlim_cur, max_rtime);
00095   if(max_rtime > mlim.rlim_max) max_rtime = (double)mlim.rlim_max;
00096   mlim.rlim_cur = (rlim_t)max_rtime;
00097   WARNIF(setrlimit(RLIMIT_CPU,&mlim));
00098   MESSAGE(0, "New rtime limit %ld (%.3lg)", mlim.rlim_cur, max_rtime);
00099   WARNIF(getrlimit(RLIMIT_DATA,&mlim));
00100   MESSAGE(0, "Cur data limit %ld,%ld (soft,hard)", mlim.rlim_cur, 
00101           mlim.rlim_max);
00102   mlim.rlim_cur = memlimit;       
00103   WARNIF( setrlimit(RLIMIT_DATA,&mlim));
00104   WARNIF( getrlimit(RLIMIT_DATA,&mlim));
00105   MESSAGE(0, "New data limit %ld,%ld (soft,hard)", mlim.rlim_cur, 
00106           mlim.rlim_max);
00107   WARNIF( getrlimit(RLIMIT_AS,&mlim));
00108   MESSAGE(0, "Cur address space limit %ld,%ld (soft,hard)", 
00109           mlim.rlim_cur, mlim.rlim_max);
00110   mlim.rlim_cur = memlimit; 
00111   WARNIF( setrlimit(RLIMIT_AS,&mlim));        
00112   WARNIF( getrlimit(RLIMIT_AS,&mlim));
00113   MESSAGE(0, "New address space limit %ld,%ld (soft,hard)", 
00114           mlim.rlim_cur, mlim.rlim_max);
00115   mlim.rlim_cur = 0;
00116   WARNIF( setrlimit(RLIMIT_CORE,&mlim));        
00117   WARNIF( getrlimit(RLIMIT_CORE,&mlim));
00118   MESSAGE(0, "New core dump space limit %ld,%ld (soft,hard)", 
00119           mlim.rlim_cur, mlim.rlim_max);
00120   return;
00121 }
00122 /* ========================================================================= */
00123 /* end of eg_macros.c */