source: vis_dev/cusp-1.1/src/util/cpu_stats.c

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

cusp added

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