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