GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
getrusage.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <sys/time.h>
28 #include <sys/times.h>
29 #include <sys/types.h>
30 
31 #ifdef HAVE_SYS_RESOURCE_H
32 #include <sys/resource.h>
33 #endif
34 
35 #if defined (HAVE_SYS_PARAM_H)
36 #include <sys/param.h>
37 #endif
38 
39 #include "defun.h"
40 #include "oct-map.h"
41 #include "sysdep.h"
42 #include "ov.h"
43 #include "oct-obj.h"
44 #include "utils.h"
45 
46 #if !defined (HZ)
47 #if defined (CLK_TCK)
48 #define HZ CLK_TCK
49 #elif defined (USG)
50 #define HZ 100
51 #else
52 #define HZ 60
53 #endif
54 #endif
55 
56 #ifndef RUSAGE_SELF
57 #define RUSAGE_SELF 0
58 #endif
59 
60 // System resource functions.
61 
62 DEFUN (getrusage, , ,
63  "-*- texinfo -*-\n\
64 @deftypefn {Built-in Function} {} getrusage ()\n\
65 Return a structure containing a number of statistics about the current\n\
66 Octave process. Not all fields are available on all systems. If it is\n\
67 not possible to get CPU time statistics, the CPU time slots are set to\n\
68 zero. Other missing data are replaced by NaN@. The list of possible\n\
69 fields is:\n\
70 \n\
71 @table @code\n\
72 @item idrss\n\
73 Unshared data size.\n\
74 \n\
75 @item inblock\n\
76 Number of block input operations.\n\
77 \n\
78 @item isrss\n\
79 Unshared stack size.\n\
80 \n\
81 @item ixrss\n\
82 Shared memory size.\n\
83 \n\
84 @item majflt\n\
85 Number of major page faults.\n\
86 \n\
87 @item maxrss\n\
88 Maximum data size.\n\
89 \n\
90 @item minflt\n\
91 Number of minor page faults.\n\
92 \n\
93 @item msgrcv\n\
94 Number of messages received.\n\
95 \n\
96 @item msgsnd\n\
97 Number of messages sent.\n\
98 \n\
99 @item nivcsw\n\
100 Number of involuntary context switches.\n\
101 \n\
102 @item nsignals\n\
103 Number of signals received.\n\
104 \n\
105 @item nswap\n\
106 Number of swaps.\n\
107 \n\
108 @item nvcsw\n\
109 Number of voluntary context switches.\n\
110 \n\
111 @item oublock\n\
112 Number of block output operations.\n\
113 \n\
114 @item stime\n\
115 A structure containing the system CPU time used. The structure has the\n\
116 elements @code{sec} (seconds) @code{usec} (microseconds).\n\
117 \n\
118 @item utime\n\
119 A structure containing the user CPU time used. The structure has the\n\
120 elements @code{sec} (seconds) @code{usec} (microseconds).\n\
121 @end table\n\
122 @end deftypefn")
123 {
125  octave_scalar_map tv_tmp;
126 
127  // FIXME: maybe encapsulate all of this in a liboctave class
128 #if defined (HAVE_GETRUSAGE)
129 
130  struct rusage ru;
131 
132  getrusage (RUSAGE_SELF, &ru);
133 
134  tv_tmp.assign ("sec", static_cast<double> (ru.ru_utime.tv_sec));
135  tv_tmp.assign ("usec", static_cast<double> (ru.ru_utime.tv_usec));
136  m.assign ("utime", octave_value (tv_tmp));
137 
138  tv_tmp.assign ("sec", static_cast<double> (ru.ru_stime.tv_sec));
139  tv_tmp.assign ("usec", static_cast<double> (ru.ru_stime.tv_usec));
140  m.assign ("stime", octave_value (tv_tmp));
141 
142 #if ! defined (RUSAGE_TIMES_ONLY)
143  m.assign ("maxrss", static_cast<double> (ru.ru_maxrss));
144  m.assign ("ixrss", static_cast<double> (ru.ru_ixrss));
145  m.assign ("idrss", static_cast<double> (ru.ru_idrss));
146  m.assign ("isrss", static_cast<double> (ru.ru_isrss));
147  m.assign ("minflt", static_cast<double> (ru.ru_minflt));
148  m.assign ("majflt", static_cast<double> (ru.ru_majflt));
149  m.assign ("nswap", static_cast<double> (ru.ru_nswap));
150  m.assign ("inblock", static_cast<double> (ru.ru_inblock));
151  m.assign ("oublock", static_cast<double> (ru.ru_oublock));
152  m.assign ("msgsnd", static_cast<double> (ru.ru_msgsnd));
153  m.assign ("msgrcv", static_cast<double> (ru.ru_msgrcv));
154  m.assign ("nsignals", static_cast<double> (ru.ru_nsignals));
155  m.assign ("nvcsw", static_cast<double> (ru.ru_nvcsw));
156  m.assign ("nivcsw", static_cast<double> (ru.ru_nivcsw));
157 #endif
158 
159 #else
160 
161  struct tms t;
162 
163  times (&t);
164 
165  unsigned long ticks;
166  unsigned long seconds;
167  unsigned long fraction;
168 
169  ticks = t.tms_utime + t.tms_cutime;
170  fraction = ticks % HZ;
171  seconds = ticks / HZ;
172 
173  tv_tmp.assign ("sec", static_cast<double> (seconds));
174  tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ));
175  m.assign ("utime", octave_value (tv_tmp));
176 
177  ticks = t.tms_stime + t.tms_cstime;
178  fraction = ticks % HZ;
179  seconds = ticks / HZ;
180 
181  tv_tmp.assign ("sec", static_cast<double> (seconds));
182  tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ));
183  m.assign ("stime", octave_value (tv_tmp));
184 
185  double tmp = lo_ieee_nan_value ();
186 
187  m.assign ("maxrss", tmp);
188  m.assign ("ixrss", tmp);
189  m.assign ("idrss", tmp);
190  m.assign ("isrss", tmp);
191  m.assign ("minflt", tmp);
192  m.assign ("majflt", tmp);
193  m.assign ("nswap", tmp);
194  m.assign ("inblock", tmp);
195  m.assign ("oublock", tmp);
196  m.assign ("msgsnd", tmp);
197  m.assign ("msgrcv", tmp);
198  m.assign ("nsignals", tmp);
199  m.assign ("nvcsw", tmp);
200  m.assign ("nivcsw", tmp);
201 
202 #endif
203 
204  return octave_value (m);
205 }