source: vis_dev/glu-2.3/src/util/cpu_stats.c @ 103

Last change on this file since 103 was 13, checked in by cecile, 13 years ago

library glu 2.3

File size: 3.6 KB
Line 
1/*
2 * Revision Control Information
3 *
4 * $Id: cpu_stats.c,v 1.14 2009/04/18 01:08:05 fabio Exp $
5 *
6 */
7/* LINTLIBRARY */
8
9#include <stdio.h>
10#include "util.h"
11
12
13#include <sys/types.h>
14#include <sys/time.h>
15#ifdef HAVE_SYS_RESOURCE_H
16#  include <sys/resource.h>
17#endif
18
19#if defined(_IBMR2)
20#define etext _etext
21#define edata _edata
22#define end _end
23#endif
24
25#ifndef __CYGWIN32__
26extern int end, etext, edata;
27#endif
28
29void
30util_print_cpu_stats(FILE *fp)
31{
32#if HAVE_SYS_RESOURCE_H && !defined(__CYGWIN32__)
33    struct rusage rusage;
34#ifdef RLIMIT_DATA
35    struct rlimit rlp;
36    long vm_limit, vm_soft_limit;
37#endif
38    long text, data;
39    double user, system, scale;
40    char hostname[257];
41#if !defined(__APPLE__)
42    long vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data;
43#endif
44
45    /* Get the hostname */
46    (void) gethostname(hostname, 256);
47    hostname[256] = '\0';               /* just in case */
48
49#if !defined(__APPLE__)
50    /* Get the virtual memory sizes */
51    vm_text = (long) (((long) (&etext)) / 1024.0 + 0.5);
52    vm_init_data = (long) (((&edata) - (&etext)) / 1024.0 + 0.5);
53    vm_uninit_data = (long) (((&end) - (&edata)) / 1024.0 + 0.5);
54    vm_sbrk_data = (long) ((sizeof(char) * ((char *) sbrk(0) - (char *) (&end))) / 1024.0 + 0.5);
55#endif
56
57    /* Get virtual memory limits */
58#ifdef RLIMIT_DATA /* In HP-UX, with cc, this constant does not exist */
59    (void) getrlimit(RLIMIT_DATA, &rlp);
60    vm_limit = (long) (rlp.rlim_max / 1024.0 + 0.5);
61    vm_soft_limit = (long) (rlp.rlim_cur / 1024.0 + 0.5);
62#endif
63
64    /* Get usage stats */
65    (void) getrusage(RUSAGE_SELF, &rusage);
66    user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec/1.0e6;
67    system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec/1.0e6;
68    scale = (user + system)*100.0;
69    if (scale == 0.0) scale = 0.001;
70
71    (void) fprintf(fp, "Runtime Statistics\n");
72    (void) fprintf(fp, "------------------\n");
73    (void) fprintf(fp, "Machine name: %s\n", hostname);
74    (void) fprintf(fp, "User time   %6.1f seconds\n", user);
75    (void) fprintf(fp, "System time %6.1f seconds\n\n", system);
76
77    text = (long) (rusage.ru_ixrss / scale + 0.5);
78    data = (long) ((rusage.ru_idrss + rusage.ru_isrss) / scale + 0.5);
79    (void) fprintf(fp, "Average resident text size       = %5ldK\n", text);
80    (void) fprintf(fp, "Average resident data+stack size = %5ldK\n", data);
81    (void) fprintf(fp, "Maximum resident size            = %5ldK\n\n",
82        rusage.ru_maxrss/2);
83#if !defined(__APPLE__)
84    (void) fprintf(fp, "Virtual text size                = %5ldK\n",
85        vm_text);
86    (void) fprintf(fp, "Virtual data size                = %5ldK\n",
87        vm_init_data + vm_uninit_data + vm_sbrk_data);
88    (void) fprintf(fp, "    data size initialized        = %5ldK\n",
89        vm_init_data);
90    (void) fprintf(fp, "    data size uninitialized      = %5ldK\n",
91        vm_uninit_data);
92    (void) fprintf(fp, "    data size sbrk               = %5ldK\n",
93        vm_sbrk_data);
94#endif
95    /* In some platforms, this constant does not exist */
96#ifdef RLIMIT_DATA
97    (void) fprintf(fp, "Virtual memory limit             = %5ldK (%ldK)\n\n",
98        vm_soft_limit, vm_limit);
99#endif
100    (void) fprintf(fp, "Major page faults = %ld\n", rusage.ru_majflt);
101    (void) fprintf(fp, "Minor page faults = %ld\n", rusage.ru_minflt);
102    (void) fprintf(fp, "Swaps = %ld\n", rusage.ru_nswap);
103    (void) fprintf(fp, "Input blocks = %ld\n", rusage.ru_inblock);
104    (void) fprintf(fp, "Output blocks = %ld\n", rusage.ru_oublock);
105    (void) fprintf(fp, "Context switch (voluntary) = %ld\n", rusage.ru_nvcsw);
106    (void) fprintf(fp, "Context switch (involuntary) = %ld\n", rusage.ru_nivcsw);
107#else /* Do not have sys/resource.h */
108    (void) fprintf(fp, "Usage statistics not available\n");
109#endif
110}
Note: See TracBrowser for help on using the repository browser.