1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1996-2021 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 
37 DEFUN (getrusage, , ,
38        doc: /* -*- texinfo -*-
39 @deftypefn {} {} getrusage ()
40 Return a structure containing a number of statistics about the current
41 Octave process.
42 
43 Not all fields are available on all systems.  If it is not possible to get
44 CPU time statistics, the CPU time slots are set to zero.  Other missing data
45 are replaced by NaN@.  The list of possible fields is:
46 
47 @table @code
48 @item idrss
49 Unshared data size.
50 
51 @item inblock
52 Number of block input operations.
53 
54 @item isrss
55 Unshared stack size.
56 
57 @item ixrss
58 Shared memory size.
59 
60 @item majflt
61 Number of major page faults.
62 
63 @item maxrss
64 Maximum data size.
65 
66 @item minflt
67 Number of minor page faults.
68 
69 @item msgrcv
70 Number of messages received.
71 
72 @item msgsnd
73 Number of messages sent.
74 
75 @item nivcsw
76 Number of involuntary context switches.
77 
78 @item nsignals
79 Number of signals received.
80 
81 @item nswap
82 Number of swaps.
83 
84 @item nvcsw
85 Number of voluntary context switches.
86 
87 @item oublock
88 Number of block output operations.
89 
90 @item stime
91 A structure containing the system CPU time used.  The structure has the
92 elements @code{sec} (seconds) @code{usec} (microseconds).
93 
94 @item utime
95 A structure containing the user CPU time used.  The structure has the
96 elements @code{sec} (seconds) @code{usec} (microseconds).
97 @end table
98 @end deftypefn */)
99 {
100   octave_scalar_map ru_map;
101   octave_scalar_map tv_map;
102 
103   octave::sys::resource_usage rusage;
104 
105   octave::sys::cpu_time cpu = rusage.cpu ();
106 
107   tv_map.assign ("sec", cpu.user_sec ());
108   tv_map.assign ("usec", cpu.user_usec ());
109   ru_map.assign ("utime", octave_value (tv_map));
110 
111   tv_map.assign ("sec", cpu.system_sec ());
112   tv_map.assign ("usec", cpu.system_usec ());
113   ru_map.assign ("stime", octave_value (tv_map));
114 
115   ru_map.assign ("maxrss", static_cast<double> (rusage.maxrss ()));
116   ru_map.assign ("ixrss", static_cast<double> (rusage.ixrss ()));
117   ru_map.assign ("idrss", static_cast<double> (rusage.idrss ()));
118   ru_map.assign ("isrss", static_cast<double> (rusage.isrss ()));
119   ru_map.assign ("minflt", static_cast<double> (rusage.minflt ()));
120   ru_map.assign ("majflt", static_cast<double> (rusage.majflt ()));
121   ru_map.assign ("nswap", static_cast<double> (rusage.nswap ()));
122   ru_map.assign ("inblock", static_cast<double> (rusage.inblock ()));
123   ru_map.assign ("oublock", static_cast<double> (rusage.oublock ()));
124   ru_map.assign ("msgsnd", static_cast<double> (rusage.msgsnd ()));
125   ru_map.assign ("msgrcv", static_cast<double> (rusage.msgrcv ()));
126   ru_map.assign ("nsignals", static_cast<double> (rusage.nsignals ()));
127   ru_map.assign ("nvcsw", static_cast<double> (rusage.nvcsw ()));
128   ru_map.assign ("nivcsw", static_cast<double> (rusage.nivcsw ()));
129 
130   return ovl (ru_map);
131 }
132 
133 /*
134 %!test
135 %! r = getrusage ();
136 %! assert (isstruct (r));
137 %! assert (isfield (r, "idrss"));
138 %! assert (isfield (r, "inblock"));
139 %! assert (isfield (r, "isrss"));
140 %! assert (isfield (r, "ixrss"));
141 %! assert (isfield (r, "majflt"));
142 %! assert (isfield (r, "maxrss"));
143 %! assert (isfield (r, "minflt"));
144 %! assert (isfield (r, "msgrcv"));
145 %! assert (isfield (r, "msgsnd"));
146 %! assert (isfield (r, "nivcsw"));
147 %! assert (isfield (r, "nsignals"));
148 %! assert (isfield (r, "nswap"));
149 %! assert (isfield (r, "nvcsw"));
150 %! assert (isfield (r, "oublock"));
151 %! assert (isfield (r, "stime"));
152 %! assert (isfield (r, "utime"));
153 %! assert (isfield (r.stime, "sec"));
154 %! assert (isfield (r.stime, "usec"));
155 %! assert (isfield (r.utime, "sec"));
156 %! assert (isfield (r.utime, "usec"));
157 */
158