libarmutils  1.4
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups
benchmark.c
Go to the documentation of this file.
1 /*******************************************************************************
2 *
3 * COPYRIGHT (C) 2010 Battelle Memorial Institute. All Rights Reserved.
4 *
5 ********************************************************************************
6 *
7 * Author:
8 * name: Brian Ermold
9 * phone: (509) 375-2277
10 * email: brian.ermold@pnl.gov
11 *
12 ********************************************************************************
13 *
14 * REPOSITORY INFORMATION:
15 * $Revision: 6689 $
16 * $Author: ermold $
17 * $Date: 2011-05-16 19:14:17 +0000 (Mon, 16 May 2011) $
18 *
19 ********************************************************************************
20 *
21 * NOTE: DOXYGEN is used to generate documentation for this file.
22 *
23 *******************************************************************************/
24 
25 /** @file benchmark.c
26  * Benchmark Utilities
27  */
28 
29 #include "armutils.h"
30 
31 static time_t gStart;
32 static double gUTime;
33 static double gSTime;
34 static double gCUTime;
35 static double gCSTime;
36 static double gReal;
37 
38 /**
39  * Initialize the benchmark function.
40  *
41  * This function should be called when the calling process starts.
42  * It sets the start time used to determine the real time in the
43  * benchmark() output.
44  */
45 void benchmark_init(void)
46 {
47  gStart = time(NULL);
48 }
49 
50 /**
51  * Print benchmark.
52  *
53  * Before using this function the benchmark_init() function should be called.
54  *
55  * The first call to this function will print the elapsed times used by the
56  * CPU since the program started. All subsequent calls to this function will
57  * print the elapsed times since the previous call, and the total times since
58  * the program started.
59  *
60  * The user time is the CPU time (in seconds) used while executing
61  * instructions in the user space of the calling process.
62  *
63  * The system time is the CPU time (in seconds) used by the system
64  * on behalf of the calling process.
65  *
66  * The cuser time is the sum of the user times (in seconds) for the
67  * calling process and the child processes.
68  *
69  * The csystem time is the sum of the system times (in seconds) for
70  * the calling process and the child processes.
71  *
72  * The real time is the wall clock time (in seconds).
73  *
74  * @param fp - pointer to output file stream
75  * @param message - message to print at the top of the output
76  */
77 void benchmark(FILE *fp, char *message)
78 {
79  double ticks_per_sec = (double)sysconf(_SC_CLK_TCK);
80  struct tms tms_buf;
81  double utime, utime_diff;
82  double stime, stime_diff;
83  double cutime, cutime_diff;
84  double cstime, cstime_diff;
85  double real, real_diff;
86 
87  times(&tms_buf);
88 
89  utime = (double)tms_buf.tms_utime / ticks_per_sec;
90  stime = (double)tms_buf.tms_stime / ticks_per_sec;
91  cutime = (double)tms_buf.tms_cutime / ticks_per_sec;
92  cstime = (double)tms_buf.tms_cstime / ticks_per_sec;
93  real = time(NULL) - gStart;
94 
95  utime_diff = utime - gUTime;
96  stime_diff = stime - gSTime;
97  cutime_diff = cutime - gCUTime;
98  cstime_diff = cstime - gCSTime;
99  real_diff = real - gReal;
100 
101  if (!message) {
102  message = "----- Benchmark -----";
103  }
104 
105  fprintf(fp,
106  "\n%s\n\n"
107  " elapsed total\n"
108  " user: %-8.2f %-8.2f\n"
109  " system: %-8.2f %-8.2f\n"
110  " cuser: %-8.2f %-8.2f\n"
111  " csystem: %-8.2f %-8.2f\n"
112  " real: %-8.2f %-8.2f\n",
113  message,
114  utime_diff, utime,
115  stime_diff, stime,
116  cutime_diff, cutime,
117  cstime_diff, cstime,
118  real_diff, real);
119 
120  gUTime = utime;
121  gSTime = stime;
122  gCUTime = cutime;
123  gCSTime = cstime;
124  gReal = real;
125 }