Jspice3
resource.c
Go to the documentation of this file.
1 /***************************************************************************
2 JSPICE3 adaptation of Spice3e2 - Copyright (c) Stephen R. Whiteley 1992
3 Copyright 1990 Regents of the University of California. All rights reserved.
4 Authors: 1985 Wayne A. Christopher
5  1992 Stephen R. Whiteley
6 ****************************************************************************/
7 
8 /*
9  * Resource-related routines.
10  */
11 
12 #include "spice.h"
13 #include "ftedefs.h"
14 
15 #ifdef HAVE_GETRUSAGE
16 #include <sys/time.h>
17 #include <sys/resource.h>
18 #else
19 #ifdef HAVE_TIMES
20 #include <sys/types.h>
21 #include <sys/times.h>
22 #include <sys/param.h>
23 #else
24 #ifdef HAVE_FTIME
25 #include <sys/types.h>
26 #include <sys/timeb.h>
27 #endif
28 #endif
29 #endif
30 
31 #ifndef HAVE_GETRUSAGE
32 #ifndef HAVE_TIMES
33 #ifdef HAVE_FTIME
34 static struct timeb timebegin;
35 static struct timeb lasttime;
36 #endif
37 #endif
38 #endif
39 static char *enddata;
40 static int origsec;
41 static int origusec;
42 static int lastsec;
43 static int lastusec;
44 static int lastusrsec;
45 static int lastusrusec;
46 static int lastsyssec;
47 static int lastsysusec;
48 
49 #ifdef HAVE_DOSRLIMIT
50 extern char * _memavl();
51 #endif
52 
53 #ifdef __STDC__
54 static void printres(char*);
55 #else
56 static void printres();
57 #endif
58 
59 
60 void
62 wordlist *wl;
63 {
64  /* Fill in the SPICE accounting structure... */
65  char *c;
66 
67  out_send("\n");
68  if (wl && (eq(wl->wl_word, "everything") || eq(wl->wl_word, "all"))) {
69  printres((char *) NULL);
70  }
71  else if (wl) {
72  for (; wl; wl = wl->wl_next) {
73  c = copy(wl->wl_word);
74  cp_unquote(c);
75  printres(c);
76  txfree(c);
77  }
78  }
79  else {
80  printres("elapsed");
81  printres("totaltime");
82  printres("space");
83  }
84  return;
85 }
86 
87 
88 /* Find out if the user is approaching his maximum data size. */
89 
90 void
92 {
93 #ifdef HAVE_GETRLIMIT
94  struct rlimit rld;
95  long lim;
96  char *hi;
97 
98  getrlimit(RLIMIT_DATA, &rld);
99  if (rld.rlim_cur == RLIM_INFINITY)
100  return;
101  hi = (char*)sbrk(0);
102  lim = rld.rlim_max;
103  if ((int) (hi - (char *) enddata) > rld.rlim_max * 0.9) {
104  fprintf(cp_err, "Warning - approaching max data size: ");
105  fprintf(cp_err, "cur size = %d, hard limit = %d.\n",
106  (int) hi, (int) lim);
107  }
108  else if ((int) (hi - (char *) enddata) > rld.rlim_cur * 0.9) {
109  fprintf(cp_err, "Warning - approaching max data size: ");
110  fprintf(cp_err, "cur size = %d, soft limit = %d.\n",
111  (int) hi, (int) rld.rlim_cur);
112  }
113 #else
114 #ifdef HAVE_ULIMIT
115  long lim;
116  char *hi;
117 
118  lim = ulimit(3, 0L);
119  hi = (char*)sbrk(0);
120  if ((int) (hi - (char *) enddata) > lim * 0.9) {
121  fprintf(cp_err, "Warning - approaching max data size: ");
122  fprintf(cp_err, "cur size = %d, hard limit = %d.\n",
123  (int) hi, (int) lim);
124  }
125 #else
126 #ifdef HAVE_DOSRLIMIT
127  long lim;
128 
129  lim = (long)_memavl( );
130  if ((long) (enddata - lim) > ((long) enddata) * 0.9) {
131  fprintf(cp_err, "Warning - approaching max data size: ");
132  fprintf(cp_err, "cur size = %ld, hard limit = %ld.\n",
133  (long) (enddata - lim), (long) enddata);
134  }
135 #endif
136 #endif
137 #endif
138 }
139 
140 
141 void
143 
144 {
145 #ifdef HAVE_GETRUSAGE
146  struct rusage ruse;
147 #ifdef HAVE_GETTIMEOFDAY
148  struct timeval tv;
149  struct timezone tz;
150 
151  (void) gettimeofday(&tv, &tz);
152  lastsec = tv.tv_sec;
153  lastusec = tv.tv_usec;
154  origsec = lastsec;
155  origusec = lastusec;
156 #endif
157  (void) getrusage(RUSAGE_SELF, &ruse);
158  lastusrsec = ruse.ru_utime.tv_sec;
159  lastusrusec = ruse.ru_utime.tv_usec;
160  lastsyssec = ruse.ru_stime.tv_sec;
161  lastsysusec = ruse.ru_stime.tv_usec;
162 #else
163 #ifdef HAVE_TIMES
164  struct tms dummy;
165 
166  origsec = times(&dummy);
167  lastusrsec = dummy.tms_utime;
168  lastsyssec = dummy.tms_stime;
169  lastsec = origsec;
170 #else
171 #ifdef HAVE_FTIME
172  ftime(&timebegin); /* initialize time we started */
173 #endif
174 #endif
175 #endif
176 
177 #ifdef HAVE_GETRLIMIT
178  enddata = (char*)sbrk(0);
179 #else
180 #ifdef HAVE_ULIMIT
181  enddata = (char*)sbrk(0);
182 #else
183 #ifdef HAVE_DOSRLIMIT
184  enddata = _memavl( );
185 #endif
186 #endif
187 #endif
188 }
189 
190 
191 /* Print out one piece of resource usage information. */
192 
193 static void
194 printres(name)
195 
196 char *name;
197 {
198  wordlist *kw, *tw;
199  struct variable *v, *vv;
200  long realt;
201  unsigned buf[25];
202  char *fmtf = "%-15s%-10.2f%s\n";
203  char *fmtx = "%-15s%-10x%s\n";
204  char *fmtd = "%-15s%-10d%s\n";
205 
206  if (!name || eq(name, "totaltime")) {
207 
208 #ifdef HAVE_GETRUSAGE
209  struct rusage ruse;
210 #ifdef HAVE_GETTIMEOFDAY
211  struct timeval tv;
212  struct timezone tz;
213 
214  (void) gettimeofday(&tv, &tz);
215  out_printf(fmtf,"totaltime",
216  (double)(tv.tv_sec - origsec) +
217  (double)(tv.tv_usec - origusec)/1.0e6,
218  "Total elapsed seconds");
219 #endif
220  (void) getrusage(RUSAGE_SELF, &ruse);
221 #ifndef HAVE_GETTIMEOFDAY
222  out_printf(fmtf,"totaltime",
223 #else
224  out_printf(fmtf,"",
225 #endif
226  (double)ruse.ru_utime.tv_sec +
227  (double)ruse.ru_utime.tv_usec/1.0e6,
228  "Total user cpu seconds");
229  out_printf(fmtf,"",
230  (double)ruse.ru_stime.tv_sec +
231  (double)ruse.ru_stime.tv_usec/1.0e6,
232  "Total system cpu seconds");
233 #else
234 #ifdef HAVE_TIMES
235  struct tms ruse;
236 
237  realt = times(&ruse);
238  out_printf(fmtf,"totaltime",
239  (double)(realt - origsec)/HZ +
240  (double)((realt - origsec)%HZ)/HZ,
241  "Total elapsed seconds");
242 
243  out_printf(fmtf,"",
244  (double)ruse.tms_utime/HZ +
245  (double)(ruse.tms_utime%HZ)/HZ,
246  "Total user cpu seconds");
247  out_printf(fmtf,"",
248  (double)ruse.tms_stime/HZ +
249  (double)(ruse.tms_stime%HZ)/HZ,
250  "Total system cpu seconds");
251 #else
252 #ifdef HAVE_FTIME
253  struct timeb timenow;
254  int sec, msec;
255 
256  ftime(&timenow);
257  timediff(&timenow, &timebegin, &sec, &msec);
258  out_printf(fmtf,"totaltime",(double)sec + (double)msec/1000,
259  "Total run time seconds");
260 #endif
261 #endif
262 #endif
263  if (name) return;
264  }
265 
266  if (!name || eq(name, "elapsed")) {
267 
268 #ifdef HAVE_GETRUSAGE
269  struct rusage ruse;
270 #ifdef HAVE_GETTIMEOFDAY
271  struct timeval tv;
272  struct timezone tz;
273 
274  (void) gettimeofday(&tv, &tz);
275  out_printf(fmtf,"elapsed",
276  (double)(tv.tv_sec - lastsec) +
277  (double)(tv.tv_usec - lastusec)/1.0e6,
278  "Seconds since last call");
279  lastsec = tv.tv_sec;
280  lastusec = tv.tv_usec;
281 #endif
282  (void) getrusage(RUSAGE_SELF, &ruse);
283 #ifndef HAVE_GETTIMEOFDAY
284  out_printf(fmtf,"elapsed",
285 #else
286  out_printf(fmtf,"",
287 #endif
288  (double)(ruse.ru_utime.tv_sec - lastusrsec) +
289  (double)(ruse.ru_utime.tv_usec - lastusrusec)/1.0e6,
290  "User cpu seconds since last call");
291  lastusrsec = ruse.ru_utime.tv_sec;
292  lastusrusec = ruse.ru_utime.tv_usec;
293  out_printf(fmtf,"",
294  (double)(ruse.ru_stime.tv_sec - lastsyssec) +
295  (double)(ruse.ru_stime.tv_usec - lastsysusec)/1.0e6,
296  "System cpu seconds since last call");
297  lastsyssec = ruse.ru_stime.tv_sec;
298  lastsysusec = ruse.ru_stime.tv_usec;
299 #else
300 #ifdef HAVE_TIMES
301  struct tms ruse;
302 
303  realt = times(&ruse);
304  out_printf(fmtf,"elapsed",
305  (double)(realt - lastsec)/HZ +
306  (double)((realt - lastsec)%HZ)/HZ,
307  "Seconds since last call");
308  out_printf(fmtf,"",
309  (double)(ruse.tms_utime - lastusrsec)/HZ +
310  (double)((ruse.tms_utime - lastusrsec)%HZ)/HZ,
311  "User cpu seconds since last call");
312  out_printf(fmtf,"",
313  (double)(ruse.tms_stime - lastsyssec)/HZ +
314  (double)((ruse.tms_stime - lastsyssec)%HZ)/HZ,
315  "System cpu seconds since last call");
316  }
317  lastusrsec = ruse.tms_utime;
318  lastsyssec = ruse.tms_stime;
319  lastsec = realt;
320 #else
321 #ifdef HAVE_FTIME
322  struct timeb timenow;
323  int sec, msec;
324 
325  ftime(&timenow);
326  if (lasttime.time != 0 || lasttime.millitm != 0) {
327  timediff(&timenow, &lasttime, &sec, &msec);
328  out_printf(fmtf,"elapsed",(double)sec + (double)msec/1000,
329  "Seconds since last call");
330  }
331  lasttime = timenow;
332 #endif
333 #endif
334 #endif
335  if (name) return;
336  }
337 
338  if (!name || eq(name, "space")) {
339 #ifdef HAVE_GETRLIMIT
340  struct rlimit rld;
341  char *hi;
342 
343  getrlimit(RLIMIT_DATA, &rld);
344  hi = (char*)sbrk(0);
345  out_printf(fmtd,"space", (int) (hi - enddata),
346  "Current data size");
347  out_printf(fmtd,"", (unsigned)rld.rlim_max,"Hard data limit");
348  out_printf(fmtd,"", (unsigned)rld.rlim_cur,"Soft data limit");
349 #else
350 #ifdef HAVE_ULIMIT
351  long lim;
352  char *hi;
353 
354  lim = ulimit(3, 0L);
355  hi = (char*)sbrk(0);
356  out_printf(fmtd,"space", (int) (hi - (char *) enddata),
357  "Current data size");
358  out_printf(fmtd,"", lim,"Data limit");
359 #else
360 #ifdef HAVE_DOSRLIMIT
361  long lim;
362 
363  lim = (long)_memavl( );
364  out_printf(fmtd,"space",((long)enddata - lim), "Current data size");
365  out_printf(fmtd,"",lim, "Data limit");
366 #endif
367 #endif
368 #endif
369  if (name) return;
370  }
371 
372  if (!name || eq(name, "faults")) {
373 #ifdef HAVE_GETRUSAGE
374  struct rusage ruse;
375 
376  (void) getrusage(RUSAGE_SELF, &ruse);
377  out_printf(fmtd,"faults",ruse.ru_majflt,"Page Faults");
378  out_printf("%-15s%-10d%s (vol %d + invol %d)\n","",
379  ruse.ru_nvcsw + ruse.ru_nivcsw,
380  "Context switches",ruse.ru_nvcsw,ruse.ru_nivcsw);
381 #endif
382  if (name) return;
383  }
384 
385  /* Now get all the spice resource stuff. */
386  if (ft_curckt && ft_curckt->ci_ckt) {
387  v = if_getstat(ft_curckt->ci_ckt, name, &kw);
388  if (v) {
389  tw = kw;
390  vv = v;
391  while (v) {
392  switch (v->va_type) {
393  case VT_NUM:
394  out_printf(fmtd,tw->wl_word,v->va_num,v->va_name);
395  break;
396  case VT_REAL:
397  out_printf(fmtf,tw->wl_word,v->va_real,v->va_name);
398  break;
399  }
400  tw = tw->wl_next;
401  v = v->va_next;
402 
403  }
404  wl_free(kw);
405  va_free(vv);
406  }
407  else if (name)
408  fprintf(cp_err,
409  "Note: no resource usage information on %s,\n", name);
410  return;
411  }
412  else
413  fprintf(cp_err, "Note: no active circuit available\n");
414  return;
415 }
static char buf[MAXPROMPT]
Definition: arg.c:18
#define eq(a, b)
Definition: misc.h:29
static void printres()
static int origusec
Definition: resource.c:41
void out_printf()
char * ci_ckt
Definition: ftedefs.h:27
static int origsec
Definition: resource.c:40
Definition: library.c:18
#define L
Definition: parse.c:442
static int lastusrsec
Definition: resource.c:44
void va_free()
void cp_unquote()
char * copy()
static int lastsyssec
Definition: resource.c:46
void rusage_init()
Definition: resource.c:142
void wl_free()
FILE * cp_err
Definition: help.c:101
void com_rusage(wordlist *wl)
Definition: resource.c:61
void txfree()
#define NULL
Definition: spdefs.h:121
struct circ * ft_curckt
Definition: main.c:184
struct variable * if_getstat(char *n, char *c, wordlist **w)
Definition: main.c:248
static int lastsysusec
Definition: resource.c:47
#define VT_NUM
Definition: cpstd.h:61
static double c
Definition: vectors.c:16
static int lastusec
Definition: resource.c:43
Definition: cpstd.h:21
static char * enddata
Definition: resource.c:39
void out_send()
#define VT_REAL
Definition: cpstd.h:62
static int lastusrusec
Definition: resource.c:45
static int lastsec
Definition: resource.c:42
Definition: sced.h:130
Definition: cpstd.h:41
void ft_ckspace()
Definition: resource.c:91