Mercurial > octave-nkf
annotate src/DLD-FUNCTIONS/getrusage.cc @ 10182:0522a65bcd56
assume unistd.h and sys/types.h exist
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Jan 2010 15:41:19 -0500 |
parents | d0ce5e973937 |
children | fa7b5751730c |
rev | line source |
---|---|
2928 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2002, 2003, 2005, 2006, 2007, 2008 |
7017 | 4 John W. Eaton |
2928 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2928 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2928 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include "systime.h" | |
29 | |
3176 | 30 #include <sys/types.h> |
31 | |
2928 | 32 #ifdef HAVE_SYS_RESOURCE_H |
33 #include <sys/resource.h> | |
34 #endif | |
35 | |
6237 | 36 #if defined (__WIN32__) |
5698 | 37 #include <windows.h> |
6360 | 38 #ifdef min |
39 #undef min | |
8319
c374691576f6
Fix for MSVC compilation
Michael Goffioul <michael.goffioul@gmail.com>
parents:
7017
diff
changeset
|
40 #undef max |
6360 | 41 #endif |
5698 | 42 #endif |
43 | |
2928 | 44 #if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) |
45 | |
46 #if defined (HAVE_SYS_PARAM_H) | |
47 #include <sys/param.h> | |
48 #endif | |
49 #include <sys/times.h> | |
50 | |
51 #if !defined (HZ) | |
52 #if defined (CLK_TCK) | |
53 #define HZ CLK_TCK | |
54 #elif defined (USG) | |
55 #define HZ 100 | |
56 #else | |
57 #define HZ 60 | |
58 #endif | |
59 #endif | |
60 | |
61 #endif | |
62 | |
63 #include "defun-dld.h" | |
64 #include "oct-map.h" | |
65 #include "sysdep.h" | |
66 #include "ov.h" | |
67 #include "oct-obj.h" | |
68 #include "utils.h" | |
69 | |
70 #ifndef RUSAGE_SELF | |
71 #define RUSAGE_SELF 0 | |
72 #endif | |
73 | |
74 // System resource functions. | |
75 | |
76 DEFUN_DLD (getrusage, , , | |
3301 | 77 "-*- texinfo -*-\n\ |
78 @deftypefn {Loadable Function} {} getrusage ()\n\ | |
79 Return a structure containing a number of statistics about the current\n\ | |
80 Octave process. Not all fields are available on all systems. If it is\n\ | |
81 not possible to get CPU time statistics, the CPU time slots are set to\n\ | |
82 zero. Other missing data are replaced by NaN. Here is a list of all\n\ | |
83 the possible fields that can be present in the structure returned by\n\ | |
84 @code{getrusage}:\n\ | |
85 \n\ | |
86 @table @code\n\ | |
87 @item idrss\n\ | |
88 Unshared data size.\n\ | |
89 \n\ | |
90 @item inblock\n\ | |
91 Number of block input operations.\n\ | |
92 \n\ | |
93 @item isrss\n\ | |
94 Unshared stack size.\n\ | |
95 \n\ | |
96 @item ixrss\n\ | |
97 Shared memory size.\n\ | |
98 \n\ | |
99 @item majflt\n\ | |
100 Number of major page faults.\n\ | |
101 \n\ | |
102 @item maxrss\n\ | |
103 Maximum data size.\n\ | |
104 \n\ | |
105 @item minflt\n\ | |
106 Number of minor page faults.\n\ | |
2928 | 107 \n\ |
3301 | 108 @item msgrcv\n\ |
109 Number of messages received.\n\ | |
110 \n\ | |
111 @item msgsnd\n\ | |
112 Number of messages sent.\n\ | |
113 \n\ | |
114 @item nivcsw\n\ | |
115 Number of involuntary context switches.\n\ | |
116 \n\ | |
117 @item nsignals\n\ | |
118 Number of signals received.\n\ | |
119 \n\ | |
120 @item nswap\n\ | |
121 Number of swaps.\n\ | |
122 \n\ | |
123 @item nvcsw\n\ | |
124 Number of voluntary context switches.\n\ | |
125 \n\ | |
126 @item oublock\n\ | |
127 Number of block output operations.\n\ | |
128 \n\ | |
129 @item stime\n\ | |
130 A structure containing the system CPU time used. The structure has the\n\ | |
131 elements @code{sec} (seconds) @code{usec} (microseconds).\n\ | |
132 \n\ | |
133 @item utime\n\ | |
134 A structure containing the user CPU time used. The structure has the\n\ | |
135 elements @code{sec} (seconds) @code{usec} (microseconds).\n\ | |
136 @end table\n\ | |
137 @end deftypefn") | |
2928 | 138 { |
139 Octave_map m; | |
140 Octave_map tv_tmp; | |
141 | |
5775 | 142 // FIXME -- maybe encapsulate all of this in a liboctave class |
2928 | 143 #if defined (HAVE_GETRUSAGE) |
144 | |
145 struct rusage ru; | |
146 | |
147 getrusage (RUSAGE_SELF, &ru); | |
148 | |
4675 | 149 tv_tmp.assign ("sec", static_cast<double> (ru.ru_utime.tv_sec)); |
150 tv_tmp.assign ("usec", static_cast<double> (ru.ru_utime.tv_usec)); | |
151 m.assign ("utime", octave_value (tv_tmp)); | |
2928 | 152 |
4675 | 153 tv_tmp.assign ("sec", static_cast<double> (ru.ru_stime.tv_sec)); |
154 tv_tmp.assign ("usec", static_cast<double> (ru.ru_stime.tv_usec)); | |
155 m.assign ("stime", octave_value (tv_tmp)); | |
2928 | 156 |
157 #if ! defined (RUSAGE_TIMES_ONLY) | |
4675 | 158 m.assign ("maxrss", static_cast<double> (ru.ru_maxrss)); |
159 m.assign ("ixrss", static_cast<double> (ru.ru_ixrss)); | |
160 m.assign ("idrss", static_cast<double> (ru.ru_idrss)); | |
161 m.assign ("isrss", static_cast<double> (ru.ru_isrss)); | |
162 m.assign ("minflt", static_cast<double> (ru.ru_minflt)); | |
163 m.assign ("majflt", static_cast<double> (ru.ru_majflt)); | |
164 m.assign ("nswap", static_cast<double> (ru.ru_nswap)); | |
165 m.assign ("inblock", static_cast<double> (ru.ru_inblock)); | |
166 m.assign ("oublock", static_cast<double> (ru.ru_oublock)); | |
167 m.assign ("msgsnd", static_cast<double> (ru.ru_msgsnd)); | |
168 m.assign ("msgrcv", static_cast<double> (ru.ru_msgrcv)); | |
169 m.assign ("nsignals", static_cast<double> (ru.ru_nsignals)); | |
170 m.assign ("nvcsw", static_cast<double> (ru.ru_nvcsw)); | |
171 m.assign ("nivcsw", static_cast<double> (ru.ru_nivcsw)); | |
2928 | 172 #endif |
173 | |
174 #else | |
175 #if defined (HAVE_TIMES) && defined (HAVE_SYS_TIMES_H) | |
176 | |
177 struct tms t; | |
178 | |
179 times (&t); | |
180 | |
181 unsigned long ticks; | |
182 unsigned long seconds; | |
183 unsigned long fraction; | |
184 | |
185 ticks = t.tms_utime + t.tms_cutime; | |
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 ("utime", octave_value (tv_tmp)); | |
2928 | 192 |
193 ticks = t.tms_stime + t.tms_cstime; | |
194 fraction = ticks % HZ; | |
195 seconds = ticks / HZ; | |
196 | |
4675 | 197 tv_tmp.assign ("sec", static_cast<double> (seconds)); |
198 tv_tmp.assign ("usec", static_cast<double> (fraction * 1e6 / HZ)); | |
199 m.assign ("stime", octave_value (tv_tmp)); | |
2928 | 200 |
6237 | 201 #elif defined (__WIN32__) |
5698 | 202 HANDLE hProcess = GetCurrentProcess (); |
203 FILETIME ftCreation, ftExit, ftUser, ftKernel; | |
204 GetProcessTimes (hProcess, &ftCreation, &ftExit, &ftKernel, &ftUser); | |
205 | |
5828 | 206 int64_t itmp = *(reinterpret_cast<int64_t *> (&ftUser)); |
5698 | 207 tv_tmp.assign ("sec", static_cast<double> (itmp / 10000000U)); |
208 tv_tmp.assign ("usec", static_cast<double> (itmp % 10000000U) / 10.); | |
209 m.assign ("utime", octave_value (tv_tmp)); | |
210 | |
5828 | 211 itmp = *(reinterpret_cast<int64_t *> (&ftKernel)); |
5698 | 212 tv_tmp.assign ("sec", static_cast<double> (itmp / 10000000U)); |
213 tv_tmp.assign ("usec", static_cast<double> (itmp % 10000000U) / 10.); | |
214 m.assign ("stime", octave_value (tv_tmp)); | |
2928 | 215 #else |
216 | |
4675 | 217 tv_tmp.assign ("sec", 0); |
218 tv_tmp.assign ("usec", 0); | |
219 m.assign ("utime", octave_value (tv_tmp)); | |
2928 | 220 |
4675 | 221 tv_tmp.assign ("sec", 0); |
222 tv_tmp.assign ("usec", 0); | |
223 m.assign ("stime", octave_value (tv_tmp)); | |
2928 | 224 |
225 #endif | |
226 | |
4102 | 227 double tmp = lo_ieee_nan_value (); |
228 | |
4675 | 229 m.assign ("maxrss", tmp); |
230 m.assign ("ixrss", tmp); | |
231 m.assign ("idrss", tmp); | |
232 m.assign ("isrss", tmp); | |
233 m.assign ("minflt", tmp); | |
234 m.assign ("majflt", tmp); | |
235 m.assign ("nswap", tmp); | |
236 m.assign ("inblock", tmp); | |
237 m.assign ("oublock", tmp); | |
238 m.assign ("msgsnd", tmp); | |
239 m.assign ("msgrcv", tmp); | |
240 m.assign ("nsignals", tmp); | |
241 m.assign ("nvcsw", tmp); | |
242 m.assign ("nivcsw", tmp); | |
2928 | 243 |
244 #endif | |
245 | |
246 return octave_value (m); | |
247 } |