2928
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 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 2, or (at your option) any |
|
10 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, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include "systime.h" |
|
28 |
3176
|
29 #ifdef HAVE_SYS_TYPES_H |
|
30 #include <sys/types.h> |
|
31 #endif |
|
32 |
2928
|
33 #ifdef HAVE_SYS_RESOURCE_H |
|
34 #include <sys/resource.h> |
|
35 #endif |
|
36 |
|
37 #if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) |
|
38 |
|
39 #if defined (HAVE_SYS_PARAM_H) |
|
40 #include <sys/param.h> |
|
41 #endif |
|
42 #include <sys/times.h> |
|
43 |
|
44 #if !defined (HZ) |
|
45 #if defined (CLK_TCK) |
|
46 #define HZ CLK_TCK |
|
47 #elif defined (USG) |
|
48 #define HZ 100 |
|
49 #else |
|
50 #define HZ 60 |
|
51 #endif |
|
52 #endif |
|
53 |
|
54 #endif |
|
55 |
|
56 #include "defun-dld.h" |
|
57 #include "oct-map.h" |
|
58 #include "sysdep.h" |
|
59 #include "ov.h" |
|
60 #include "oct-obj.h" |
|
61 #include "utils.h" |
|
62 |
|
63 #ifndef RUSAGE_SELF |
|
64 #define RUSAGE_SELF 0 |
|
65 #endif |
|
66 |
|
67 // System resource functions. |
|
68 |
|
69 DEFUN_DLD (getrusage, , , |
3301
|
70 "-*- texinfo -*-\n\ |
|
71 @deftypefn {Loadable Function} {} getrusage ()\n\ |
|
72 Return a structure containing a number of statistics about the current\n\ |
|
73 Octave process. Not all fields are available on all systems. If it is\n\ |
|
74 not possible to get CPU time statistics, the CPU time slots are set to\n\ |
|
75 zero. Other missing data are replaced by NaN. Here is a list of all\n\ |
|
76 the possible fields that can be present in the structure returned by\n\ |
|
77 @code{getrusage}:\n\ |
|
78 \n\ |
|
79 @table @code\n\ |
|
80 @item idrss\n\ |
|
81 Unshared data size.\n\ |
|
82 \n\ |
|
83 @item inblock\n\ |
|
84 Number of block input operations.\n\ |
|
85 \n\ |
|
86 @item isrss\n\ |
|
87 Unshared stack size.\n\ |
|
88 \n\ |
|
89 @item ixrss\n\ |
|
90 Shared memory size.\n\ |
|
91 \n\ |
|
92 @item majflt\n\ |
|
93 Number of major page faults.\n\ |
|
94 \n\ |
|
95 @item maxrss\n\ |
|
96 Maximum data size.\n\ |
|
97 \n\ |
|
98 @item minflt\n\ |
|
99 Number of minor page faults.\n\ |
2928
|
100 \n\ |
3301
|
101 @item msgrcv\n\ |
|
102 Number of messages received.\n\ |
|
103 \n\ |
|
104 @item msgsnd\n\ |
|
105 Number of messages sent.\n\ |
|
106 \n\ |
|
107 @item nivcsw\n\ |
|
108 Number of involuntary context switches.\n\ |
|
109 \n\ |
|
110 @item nsignals\n\ |
|
111 Number of signals received.\n\ |
|
112 \n\ |
|
113 @item nswap\n\ |
|
114 Number of swaps.\n\ |
|
115 \n\ |
|
116 @item nvcsw\n\ |
|
117 Number of voluntary context switches.\n\ |
|
118 \n\ |
|
119 @item oublock\n\ |
|
120 Number of block output operations.\n\ |
|
121 \n\ |
|
122 @item stime\n\ |
|
123 A structure containing the system CPU time used. The structure has the\n\ |
|
124 elements @code{sec} (seconds) @code{usec} (microseconds).\n\ |
|
125 \n\ |
|
126 @item utime\n\ |
|
127 A structure containing the user CPU time used. The structure has the\n\ |
|
128 elements @code{sec} (seconds) @code{usec} (microseconds).\n\ |
|
129 @end table\n\ |
|
130 @end deftypefn") |
2928
|
131 { |
|
132 Octave_map m; |
|
133 Octave_map tv_tmp; |
|
134 |
|
135 #if defined (HAVE_GETRUSAGE) |
|
136 |
|
137 struct rusage ru; |
|
138 |
|
139 getrusage (RUSAGE_SELF, &ru); |
|
140 |
4675
|
141 tv_tmp.assign ("sec", static_cast<double> (ru.ru_utime.tv_sec)); |
|
142 tv_tmp.assign ("usec", static_cast<double> (ru.ru_utime.tv_usec)); |
|
143 m.assign ("utime", octave_value (tv_tmp)); |
2928
|
144 |
4675
|
145 tv_tmp.assign ("sec", static_cast<double> (ru.ru_stime.tv_sec)); |
|
146 tv_tmp.assign ("usec", static_cast<double> (ru.ru_stime.tv_usec)); |
|
147 m.assign ("stime", octave_value (tv_tmp)); |
2928
|
148 |
|
149 #if ! defined (RUSAGE_TIMES_ONLY) |
4675
|
150 m.assign ("maxrss", static_cast<double> (ru.ru_maxrss)); |
|
151 m.assign ("ixrss", static_cast<double> (ru.ru_ixrss)); |
|
152 m.assign ("idrss", static_cast<double> (ru.ru_idrss)); |
|
153 m.assign ("isrss", static_cast<double> (ru.ru_isrss)); |
|
154 m.assign ("minflt", static_cast<double> (ru.ru_minflt)); |
|
155 m.assign ("majflt", static_cast<double> (ru.ru_majflt)); |
|
156 m.assign ("nswap", static_cast<double> (ru.ru_nswap)); |
|
157 m.assign ("inblock", static_cast<double> (ru.ru_inblock)); |
|
158 m.assign ("oublock", static_cast<double> (ru.ru_oublock)); |
|
159 m.assign ("msgsnd", static_cast<double> (ru.ru_msgsnd)); |
|
160 m.assign ("msgrcv", static_cast<double> (ru.ru_msgrcv)); |
|
161 m.assign ("nsignals", static_cast<double> (ru.ru_nsignals)); |
|
162 m.assign ("nvcsw", static_cast<double> (ru.ru_nvcsw)); |
|
163 m.assign ("nivcsw", static_cast<double> (ru.ru_nivcsw)); |
2928
|
164 #endif |
|
165 |
|
166 #else |
|
167 #if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) |
|
168 |
|
169 struct tms t; |
|
170 |
|
171 times (&t); |
|
172 |
|
173 unsigned long ticks; |
|
174 unsigned long seconds; |
|
175 unsigned long fraction; |
|
176 |
|
177 ticks = t.tms_utime + t.tms_cutime; |
|
178 fraction = ticks % HZ; |
|
179 seconds = ticks / HZ; |
|
180 |
4675
|
181 tv_tmp.assign ("sec", static_cast<double> (seconds)); |
|
182 tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ)); |
|
183 m.assign ("utime", octave_value (tv_tmp)); |
2928
|
184 |
|
185 ticks = t.tms_stime + t.tms_cstime; |
|
186 fraction = ticks % HZ; |
|
187 seconds = ticks / HZ; |
|
188 |
4675
|
189 tv_tmp.assign ("sec", static_cast<double> (seconds)); |
|
190 tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ)); |
|
191 m.assign ("stime", octave_value (tv_tmp)); |
2928
|
192 |
|
193 #else |
|
194 |
4675
|
195 tv_tmp.assign ("sec", 0); |
|
196 tv_tmp.assign ("usec", 0); |
|
197 m.assign ("utime", octave_value (tv_tmp)); |
2928
|
198 |
4675
|
199 tv_tmp.assign ("sec", 0); |
|
200 tv_tmp.assign ("usec", 0); |
|
201 m.assign ("stime", octave_value (tv_tmp)); |
2928
|
202 |
|
203 #endif |
|
204 |
4102
|
205 double tmp = lo_ieee_nan_value (); |
|
206 |
4675
|
207 m.assign ("maxrss", tmp); |
|
208 m.assign ("ixrss", tmp); |
|
209 m.assign ("idrss", tmp); |
|
210 m.assign ("isrss", tmp); |
|
211 m.assign ("minflt", tmp); |
|
212 m.assign ("majflt", tmp); |
|
213 m.assign ("nswap", tmp); |
|
214 m.assign ("inblock", tmp); |
|
215 m.assign ("oublock", tmp); |
|
216 m.assign ("msgsnd", tmp); |
|
217 m.assign ("msgrcv", tmp); |
|
218 m.assign ("nsignals", tmp); |
|
219 m.assign ("nvcsw", tmp); |
|
220 m.assign ("nivcsw", tmp); |
2928
|
221 |
|
222 #endif |
|
223 |
|
224 return octave_value (m); |
|
225 } |
|
226 |
|
227 /* |
|
228 ;;; Local Variables: *** |
|
229 ;;; mode: C++ *** |
|
230 ;;; End: *** |
|
231 */ |