getrusage.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1996-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <sys/time.h>
00028 #include <sys/times.h>
00029 #include <sys/types.h>
00030 
00031 #ifdef HAVE_SYS_RESOURCE_H
00032 #include <sys/resource.h>
00033 #endif
00034 
00035 #if defined (HAVE_SYS_PARAM_H)
00036 #include <sys/param.h>
00037 #endif
00038 
00039 #include "defun-dld.h"
00040 #include "oct-map.h"
00041 #include "sysdep.h"
00042 #include "ov.h"
00043 #include "oct-obj.h"
00044 #include "utils.h"
00045 
00046 #if !defined (HZ)
00047 #if defined (CLK_TCK)
00048 #define HZ CLK_TCK
00049 #elif defined (USG)
00050 #define HZ 100
00051 #else
00052 #define HZ 60
00053 #endif
00054 #endif
00055 
00056 #ifndef RUSAGE_SELF
00057 #define RUSAGE_SELF 0
00058 #endif
00059 
00060 // System resource functions.
00061 
00062 DEFUN_DLD (getrusage, , ,
00063   "-*- texinfo -*-\n\
00064 @deftypefn {Loadable Function} {} getrusage ()\n\
00065 Return a structure containing a number of statistics about the current\n\
00066 Octave process.  Not all fields are available on all systems.  If it is\n\
00067 not possible to get CPU time statistics, the CPU time slots are set to\n\
00068 zero.  Other missing data are replaced by NaN@.  The list of possible\n\
00069 fields is:\n\
00070 \n\
00071 @table @code\n\
00072 @item idrss\n\
00073 Unshared data size.\n\
00074 \n\
00075 @item inblock\n\
00076 Number of block input operations.\n\
00077 \n\
00078 @item isrss\n\
00079 Unshared stack size.\n\
00080 \n\
00081 @item ixrss\n\
00082 Shared memory size.\n\
00083 \n\
00084 @item majflt\n\
00085 Number of major page faults.\n\
00086 \n\
00087 @item maxrss\n\
00088 Maximum data size.\n\
00089 \n\
00090 @item minflt\n\
00091 Number of minor page faults.\n\
00092 \n\
00093 @item msgrcv\n\
00094 Number of messages received.\n\
00095 \n\
00096 @item msgsnd\n\
00097 Number of messages sent.\n\
00098 \n\
00099 @item nivcsw\n\
00100 Number of involuntary context switches.\n\
00101 \n\
00102 @item nsignals\n\
00103 Number of signals received.\n\
00104 \n\
00105 @item nswap\n\
00106 Number of swaps.\n\
00107 \n\
00108 @item nvcsw\n\
00109 Number of voluntary context switches.\n\
00110 \n\
00111 @item oublock\n\
00112 Number of block output operations.\n\
00113 \n\
00114 @item stime\n\
00115 A structure containing the system CPU time used.  The structure has the\n\
00116 elements @code{sec} (seconds) @code{usec} (microseconds).\n\
00117 \n\
00118 @item utime\n\
00119 A structure containing the user CPU time used.  The structure has the\n\
00120 elements @code{sec} (seconds) @code{usec} (microseconds).\n\
00121 @end table\n\
00122 @end deftypefn")
00123 {
00124   octave_scalar_map m;
00125   octave_scalar_map tv_tmp;
00126 
00127   // FIXME -- maybe encapsulate all of this in a liboctave class
00128 #if defined (HAVE_GETRUSAGE)
00129 
00130   struct rusage ru;
00131 
00132   getrusage (RUSAGE_SELF, &ru);
00133 
00134   tv_tmp.assign ("sec", static_cast<double> (ru.ru_utime.tv_sec));
00135   tv_tmp.assign ("usec", static_cast<double> (ru.ru_utime.tv_usec));
00136   m.assign ("utime", octave_value (tv_tmp));
00137 
00138   tv_tmp.assign ("sec", static_cast<double> (ru.ru_stime.tv_sec));
00139   tv_tmp.assign ("usec", static_cast<double> (ru.ru_stime.tv_usec));
00140   m.assign ("stime", octave_value (tv_tmp));
00141 
00142 #if ! defined (RUSAGE_TIMES_ONLY)
00143   m.assign ("maxrss", static_cast<double> (ru.ru_maxrss));
00144   m.assign ("ixrss", static_cast<double> (ru.ru_ixrss));
00145   m.assign ("idrss", static_cast<double> (ru.ru_idrss));
00146   m.assign ("isrss", static_cast<double> (ru.ru_isrss));
00147   m.assign ("minflt", static_cast<double> (ru.ru_minflt));
00148   m.assign ("majflt", static_cast<double> (ru.ru_majflt));
00149   m.assign ("nswap", static_cast<double> (ru.ru_nswap));
00150   m.assign ("inblock", static_cast<double> (ru.ru_inblock));
00151   m.assign ("oublock", static_cast<double> (ru.ru_oublock));
00152   m.assign ("msgsnd", static_cast<double> (ru.ru_msgsnd));
00153   m.assign ("msgrcv", static_cast<double> (ru.ru_msgrcv));
00154   m.assign ("nsignals", static_cast<double> (ru.ru_nsignals));
00155   m.assign ("nvcsw", static_cast<double> (ru.ru_nvcsw));
00156   m.assign ("nivcsw", static_cast<double> (ru.ru_nivcsw));
00157 #endif
00158 
00159 #else
00160 
00161   struct tms t;
00162 
00163   times (&t);
00164 
00165   unsigned long ticks;
00166   unsigned long seconds;
00167   unsigned long fraction;
00168 
00169   ticks = t.tms_utime + t.tms_cutime;
00170   fraction = ticks % HZ;
00171   seconds = ticks / HZ;
00172 
00173   tv_tmp.assign ("sec", static_cast<double> (seconds));
00174   tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ));
00175   m.assign ("utime", octave_value (tv_tmp));
00176 
00177   ticks = t.tms_stime + t.tms_cstime;
00178   fraction = ticks % HZ;
00179   seconds = ticks / HZ;
00180 
00181   tv_tmp.assign ("sec", static_cast<double> (seconds));
00182   tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ));
00183   m.assign ("stime", octave_value (tv_tmp));
00184 
00185   double tmp = lo_ieee_nan_value ();
00186 
00187   m.assign ("maxrss", tmp);
00188   m.assign ("ixrss", tmp);
00189   m.assign ("idrss", tmp);
00190   m.assign ("isrss", tmp);
00191   m.assign ("minflt", tmp);
00192   m.assign ("majflt", tmp);
00193   m.assign ("nswap", tmp);
00194   m.assign ("inblock", tmp);
00195   m.assign ("oublock", tmp);
00196   m.assign ("msgsnd", tmp);
00197   m.assign ("msgrcv", tmp);
00198   m.assign ("nsignals", tmp);
00199   m.assign ("nvcsw", tmp);
00200   m.assign ("nivcsw", tmp);
00201 
00202 #endif
00203 
00204   return octave_value (m);
00205 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines