| 1 | /* | 
|---|
| 2 | * Revision Control Information | 
|---|
| 3 | * | 
|---|
| 4 | * $Id: cpu_stats.c,v 1.12 2005/05/16 16:25:24 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__ | 
|---|
| 26 | extern int end, etext, edata; | 
|---|
| 27 | #endif | 
|---|
| 28 |  | 
|---|
| 29 | void | 
|---|
| 30 | util_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 | long vm_text, vm_init_data, vm_uninit_data, vm_sbrk_data; | 
|---|
| 42 |  | 
|---|
| 43 | /* Get the hostname */ | 
|---|
| 44 | (void) gethostname(hostname, 256); | 
|---|
| 45 | hostname[256] = '\0';               /* just in case */ | 
|---|
| 46 |  | 
|---|
| 47 | /* Get the virtual memory sizes */ | 
|---|
| 48 | vm_text = (long) (((long) (&etext)) / 1024.0 + 0.5); | 
|---|
| 49 | vm_init_data = (long) (((&edata) - (&etext)) / 1024.0 + 0.5); | 
|---|
| 50 | vm_uninit_data = (long) (((&end) - (&edata)) / 1024.0 + 0.5); | 
|---|
| 51 | vm_sbrk_data = (long) ((sizeof(char) * ((char *) sbrk(0) - (char *) (&end))) / 1024.0 + 0.5); | 
|---|
| 52 |  | 
|---|
| 53 | /* Get virtual memory limits */ | 
|---|
| 54 | #ifdef RLIMIT_DATA /* In HP-UX, with cc, this constant does not exist */ | 
|---|
| 55 | (void) getrlimit(RLIMIT_DATA, &rlp); | 
|---|
| 56 | vm_limit = (long) (rlp.rlim_max / 1024.0 + 0.5); | 
|---|
| 57 | vm_soft_limit = (long) (rlp.rlim_cur / 1024.0 + 0.5); | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | /* Get usage stats */ | 
|---|
| 61 | (void) getrusage(RUSAGE_SELF, &rusage); | 
|---|
| 62 | user = rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec/1.0e6; | 
|---|
| 63 | system = rusage.ru_stime.tv_sec + rusage.ru_stime.tv_usec/1.0e6; | 
|---|
| 64 | scale = (user + system)*100.0; | 
|---|
| 65 | if (scale == 0.0) scale = 0.001; | 
|---|
| 66 |  | 
|---|
| 67 | (void) fprintf(fp, "Runtime Statistics\n"); | 
|---|
| 68 | (void) fprintf(fp, "------------------\n"); | 
|---|
| 69 | (void) fprintf(fp, "Machine name: %s\n", hostname); | 
|---|
| 70 | (void) fprintf(fp, "User time   %6.1f seconds\n", user); | 
|---|
| 71 | (void) fprintf(fp, "System time %6.1f seconds\n\n", system); | 
|---|
| 72 |  | 
|---|
| 73 | text = (long) (rusage.ru_ixrss / scale + 0.5); | 
|---|
| 74 | data = (long) ((rusage.ru_idrss + rusage.ru_isrss) / scale + 0.5); | 
|---|
| 75 | (void) fprintf(fp, "Average resident text size       = %5ldK\n", text); | 
|---|
| 76 | (void) fprintf(fp, "Average resident data+stack size = %5ldK\n", data); | 
|---|
| 77 | (void) fprintf(fp, "Maximum resident size            = %5ldK\n\n", | 
|---|
| 78 | rusage.ru_maxrss/2); | 
|---|
| 79 | (void) fprintf(fp, "Virtual text size                = %5ldK\n", | 
|---|
| 80 | vm_text); | 
|---|
| 81 | (void) fprintf(fp, "Virtual data size                = %5ldK\n", | 
|---|
| 82 | vm_init_data + vm_uninit_data + vm_sbrk_data); | 
|---|
| 83 | (void) fprintf(fp, "    data size initialized        = %5ldK\n", | 
|---|
| 84 | vm_init_data); | 
|---|
| 85 | (void) fprintf(fp, "    data size uninitialized      = %5ldK\n", | 
|---|
| 86 | vm_uninit_data); | 
|---|
| 87 | (void) fprintf(fp, "    data size sbrk               = %5ldK\n", | 
|---|
| 88 | vm_sbrk_data); | 
|---|
| 89 | /* In some platforms, this constant does not exist */ | 
|---|
| 90 | #ifdef RLIMIT_DATA | 
|---|
| 91 | (void) fprintf(fp, "Virtual memory limit             = %5ldK (%ldK)\n\n", | 
|---|
| 92 | vm_soft_limit, vm_limit); | 
|---|
| 93 | #endif | 
|---|
| 94 | (void) fprintf(fp, "Major page faults = %ld\n", rusage.ru_majflt); | 
|---|
| 95 | (void) fprintf(fp, "Minor page faults = %ld\n", rusage.ru_minflt); | 
|---|
| 96 | (void) fprintf(fp, "Swaps = %ld\n", rusage.ru_nswap); | 
|---|
| 97 | (void) fprintf(fp, "Input blocks = %ld\n", rusage.ru_inblock); | 
|---|
| 98 | (void) fprintf(fp, "Output blocks = %ld\n", rusage.ru_oublock); | 
|---|
| 99 | (void) fprintf(fp, "Context switch (voluntary) = %ld\n", rusage.ru_nvcsw); | 
|---|
| 100 | (void) fprintf(fp, "Context switch (involuntary) = %ld\n", rusage.ru_nivcsw); | 
|---|
| 101 | #else /* Do not have sys/resource.h */ | 
|---|
| 102 | (void) fprintf(fp, "Usage statistics not available\n"); | 
|---|
| 103 | #endif | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|