GNU Octave  9.1.0
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-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if defined (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include "oct-time.h"
31 
32 #include "defun.h"
33 #include "oct-map.h"
34 #include "ov.h"
35 #include "ovl.h"
36 
38 
39 DEFUN (getrusage, , ,
40  doc: /* -*- texinfo -*-
41 @deftypefn {} {@var{procstats} =} getrusage ()
42 Return a structure containing a number of statistics about the current
43 Octave process.
44 
45 Not all fields are available on all systems. If it is not possible to get
46 CPU time statistics, the CPU time slots are set to zero. Other missing data
47 are replaced by NaN@. The list of possible fields is:
48 
49 @table @code
50 @item idrss
51 Unshared data size.
52 
53 @item inblock
54 Number of block input operations.
55 
56 @item isrss
57 Unshared stack size.
58 
59 @item ixrss
60 Shared memory size.
61 
62 @item majflt
63 Number of major page faults.
64 
65 @item maxrss
66 Maximum data size.
67 
68 @item minflt
69 Number of minor page faults.
70 
71 @item msgrcv
72 Number of messages received.
73 
74 @item msgsnd
75 Number of messages sent.
76 
77 @item nivcsw
78 Number of involuntary context switches.
79 
80 @item nsignals
81 Number of signals received.
82 
83 @item nswap
84 Number of swaps.
85 
86 @item nvcsw
87 Number of voluntary context switches.
88 
89 @item oublock
90 Number of block output operations.
91 
92 @item stime
93 A structure containing the system CPU time used. The structure has the
94 elements @code{sec} (seconds) @code{usec} (microseconds).
95 
96 @item utime
97 A structure containing the user CPU time used. The structure has the
98 elements @code{sec} (seconds) @code{usec} (microseconds).
99 @end table
100 @end deftypefn */)
101 {
102  octave_scalar_map ru_map;
103  octave_scalar_map tv_map;
104 
105  sys::resource_usage rusage;
106 
107  sys::cpu_time cpu = rusage.cpu ();
108 
109  tv_map.assign ("sec", cpu.user_sec ());
110  tv_map.assign ("usec", cpu.user_usec ());
111  ru_map.assign ("utime", octave_value (tv_map));
112 
113  tv_map.assign ("sec", cpu.system_sec ());
114  tv_map.assign ("usec", cpu.system_usec ());
115  ru_map.assign ("stime", octave_value (tv_map));
116 
117  ru_map.assign ("maxrss", static_cast<double> (rusage.maxrss ()));
118  ru_map.assign ("ixrss", static_cast<double> (rusage.ixrss ()));
119  ru_map.assign ("idrss", static_cast<double> (rusage.idrss ()));
120  ru_map.assign ("isrss", static_cast<double> (rusage.isrss ()));
121  ru_map.assign ("minflt", static_cast<double> (rusage.minflt ()));
122  ru_map.assign ("majflt", static_cast<double> (rusage.majflt ()));
123  ru_map.assign ("nswap", static_cast<double> (rusage.nswap ()));
124  ru_map.assign ("inblock", static_cast<double> (rusage.inblock ()));
125  ru_map.assign ("oublock", static_cast<double> (rusage.oublock ()));
126  ru_map.assign ("msgsnd", static_cast<double> (rusage.msgsnd ()));
127  ru_map.assign ("msgrcv", static_cast<double> (rusage.msgrcv ()));
128  ru_map.assign ("nsignals", static_cast<double> (rusage.nsignals ()));
129  ru_map.assign ("nvcsw", static_cast<double> (rusage.nvcsw ()));
130  ru_map.assign ("nivcsw", static_cast<double> (rusage.nivcsw ()));
131 
132  return ovl (ru_map);
133 }
134 
135 /*
136 %!test
137 %! r = getrusage ();
138 %! assert (isstruct (r));
139 %! assert (isfield (r, "idrss"));
140 %! assert (isfield (r, "inblock"));
141 %! assert (isfield (r, "isrss"));
142 %! assert (isfield (r, "ixrss"));
143 %! assert (isfield (r, "majflt"));
144 %! assert (isfield (r, "maxrss"));
145 %! assert (isfield (r, "minflt"));
146 %! assert (isfield (r, "msgrcv"));
147 %! assert (isfield (r, "msgsnd"));
148 %! assert (isfield (r, "nivcsw"));
149 %! assert (isfield (r, "nsignals"));
150 %! assert (isfield (r, "nswap"));
151 %! assert (isfield (r, "nvcsw"));
152 %! assert (isfield (r, "oublock"));
153 %! assert (isfield (r, "stime"));
154 %! assert (isfield (r, "utime"));
155 %! assert (isfield (r.stime, "sec"));
156 %! assert (isfield (r.stime, "usec"));
157 %! assert (isfield (r.utime, "sec"));
158 %! assert (isfield (r.utime, "usec"));
159 */
160 
161 OCTAVE_END_NAMESPACE(octave)
void assign(const std::string &k, const octave_value &val)
Definition: oct-map.h:230
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
Definition: defun.h:56
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219