GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
getrusage.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (HAVE_CONFIG_H)
24 # include "config.h"
25 #endif
26 
27 #include "oct-time.h"
28 
29 #include "defun.h"
30 #include "oct-map.h"
31 #include "ov.h"
32 #include "ovl.h"
33 
34 DEFUN (getrusage, , ,
35  doc: /* -*- texinfo -*-
36 @deftypefn {} {} getrusage ()
37 Return a structure containing a number of statistics about the current
38 Octave process.
39 
40 Not all fields are available on all systems. If it is not possible to get
41 CPU time statistics, the CPU time slots are set to zero. Other missing data
42 are replaced by NaN@. The list of possible fields is:
43 
44 @table @code
45 @item idrss
46 Unshared data size.
47 
48 @item inblock
49 Number of block input operations.
50 
51 @item isrss
52 Unshared stack size.
53 
54 @item ixrss
55 Shared memory size.
56 
57 @item majflt
58 Number of major page faults.
59 
60 @item maxrss
61 Maximum data size.
62 
63 @item minflt
64 Number of minor page faults.
65 
66 @item msgrcv
67 Number of messages received.
68 
69 @item msgsnd
70 Number of messages sent.
71 
72 @item nivcsw
73 Number of involuntary context switches.
74 
75 @item nsignals
76 Number of signals received.
77 
78 @item nswap
79 Number of swaps.
80 
81 @item nvcsw
82 Number of voluntary context switches.
83 
84 @item oublock
85 Number of block output operations.
86 
87 @item stime
88 A structure containing the system CPU time used. The structure has the
89 elements @code{sec} (seconds) @code{usec} (microseconds).
90 
91 @item utime
92 A structure containing the user CPU time used. The structure has the
93 elements @code{sec} (seconds) @code{usec} (microseconds).
94 @end table
95 @end deftypefn */)
96 {
97  octave_scalar_map ru_map;
98  octave_scalar_map tv_map;
99 
101 
102  octave::sys::cpu_time cpu = rusage.cpu ();
103 
104  tv_map.assign ("sec", cpu.user_sec ());
105  tv_map.assign ("usec", cpu.user_usec ());
106  ru_map.assign ("utime", octave_value (tv_map));
107 
108  tv_map.assign ("sec", cpu.system_sec ());
109  tv_map.assign ("usec", cpu.system_usec ());
110  ru_map.assign ("stime", octave_value (tv_map));
111 
112  ru_map.assign ("maxrss", static_cast<double> (rusage.maxrss ()));
113  ru_map.assign ("ixrss", static_cast<double> (rusage.ixrss ()));
114  ru_map.assign ("idrss", static_cast<double> (rusage.idrss ()));
115  ru_map.assign ("isrss", static_cast<double> (rusage.isrss ()));
116  ru_map.assign ("minflt", static_cast<double> (rusage.minflt ()));
117  ru_map.assign ("majflt", static_cast<double> (rusage.majflt ()));
118  ru_map.assign ("nswap", static_cast<double> (rusage.nswap ()));
119  ru_map.assign ("inblock", static_cast<double> (rusage.inblock ()));
120  ru_map.assign ("oublock", static_cast<double> (rusage.oublock ()));
121  ru_map.assign ("msgsnd", static_cast<double> (rusage.msgsnd ()));
122  ru_map.assign ("msgrcv", static_cast<double> (rusage.msgrcv ()));
123  ru_map.assign ("nsignals", static_cast<double> (rusage.nsignals ()));
124  ru_map.assign ("nvcsw", static_cast<double> (rusage.nvcsw ()));
125  ru_map.assign ("nivcsw", static_cast<double> (rusage.nivcsw ()));
126 
127  return ovl (ru_map);
128 }
129 
130 /*
131 %!test
132 %! r = getrusage ();
133 %! assert (isstruct (r));
134 %! assert (isfield (r, "idrss"));
135 %! assert (isfield (r, "inblock"));
136 %! assert (isfield (r, "isrss"));
137 %! assert (isfield (r, "ixrss"));
138 %! assert (isfield (r, "majflt"));
139 %! assert (isfield (r, "maxrss"));
140 %! assert (isfield (r, "minflt"));
141 %! assert (isfield (r, "msgrcv"));
142 %! assert (isfield (r, "msgsnd"));
143 %! assert (isfield (r, "nivcsw"));
144 %! assert (isfield (r, "nsignals"));
145 %! assert (isfield (r, "nswap"));
146 %! assert (isfield (r, "nvcsw"));
147 %! assert (isfield (r, "oublock"));
148 %! assert (isfield (r, "stime"));
149 %! assert (isfield (r, "utime"));
150 %! assert (isfield (r.stime, "sec"));
151 %! assert (isfield (r.stime, "usec"));
152 %! assert (isfield (r.utime, "sec"));
153 %! assert (isfield (r.utime, "usec"));
154 */
long oublock(void) const
Definition: oct-time.h:510
long user_usec(void) const
Definition: oct-time.h:429
long system_usec(void) const
Definition: oct-time.h:432
long idrss(void) const
Definition: oct-time.h:504
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:53
long ixrss(void) const
Definition: oct-time.h:503
long isrss(void) const
Definition: oct-time.h:505
long inblock(void) const
Definition: oct-time.h:509
long msgrcv(void) const
Definition: oct-time.h:512
time_t user_sec(void) const
Definition: oct-time.h:428
long majflt(void) const
Definition: oct-time.h:507
long nivcsw(void) const
Definition: oct-time.h:515
time_t system_sec(void) const
Definition: oct-time.h:431
long msgsnd(void) const
Definition: oct-time.h:511
long maxrss(void) const
Definition: oct-time.h:502
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
long minflt(void) const
Definition: oct-time.h:506
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:227
cpu_time cpu(void) const
Definition: oct-time.h:500
long nsignals(void) const
Definition: oct-time.h:513
long nvcsw(void) const
Definition: oct-time.h:514
long nswap(void) const
Definition: oct-time.h:508