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