MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hrt_utils_test.c
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 /*
20  * hrt_utils_test.c
21  *
22  */
23 
24 #include <assert.h>
25 #include <stdio.h>
26 
27 #include "hrt_utils.h"
28 
29 /*
30  * High-Resolution Time Utilities -- Test
31  */
32 
33 static void
34 hrt_rtprint(const hrt_rtstamp* x)
35 {
36 #if (HRT_REALTIME_METHOD==HRT_USE_CLOCK_GETTIME)
37  printf("time.tv_sec = %ld\n", (long)x->time.tv_sec);
38  printf("time.tv_nsec = %ld\n", (long)x->time.tv_nsec);
39 #elif (HRT_REALTIME_METHOD==HRT_USE_GETTIMEOFDAY)
40  printf("time.tv_sec = %ld\n", (long)x->time.tv_sec);
41  printf("time.tv_usec = %ld\n", (long)x->time.tv_usec);
42 #elif (HRT_REALTIME_METHOD==HRT_USE_TIMES)
43  printf("time = %ld\n", (long)x->time);
44 #elif (HRT_REALTIME_METHOD==HRT_USE_ANSI_TIME)
45  printf("time = %ld\n", (long)x->time);
46 #endif
47 }
48 
49 static void
50 hrt_ctprint(const hrt_ctstamp* x)
51 {
52 #if (HRT_CPUTIME_METHOD==HRT_USE_CLOCK_GETTIME)
53  printf("time.tv_sec = %ld\n", (long)x->time.tv_sec);
54  printf("time.tv_nsec = %ld\n", (long)x->time.tv_nsec);
55 #elif (HRT_CPUTIME_METHOD==HRT_USE_GETRUSAGE)
56  printf("time.ru_utime.tv_sec = %ld\n", (long)x->time.ru_utime.tv_sec);
57  printf("time.ru_utime.tv_usec = %ld\n", (long)x->time.ru_utime.tv_usec);
58  printf("time.ru_stime.tv_sec = %ld\n", (long)x->time.ru_stime.tv_sec);
59  printf("time.ru_stime.tv_usec = %ld\n", (long)x->time.ru_stime.tv_usec);
60 #elif (HRT_CPUTIME_METHOD==HRT_USE_TIMES)
61  printf("time.tms_utime = %ld\n", (long)x->time.tms_utime);
62  printf("time.tms_stime = %ld\n", (long)x->time.tms_stime);
63 #elif (HRT_CPUTIME_METHOD==HRT_USE_ANSI_CLOCK)
64  printf("time = %ld\n", (long)x->time);
65 #endif
66 }
67 
68 static void
69 hrt_tprint(const hrt_tstamp* x)
70 {
71  hrt_rtprint(&x->rtstamp);
72  hrt_ctprint(&x->ctstamp);
73 }
74 
75 static void
76 do_something(void)
77 {
78  const unsigned long loop = 1000000000L;
79  unsigned long i;
80  static volatile unsigned long dummy;
81  for (i = 0; i < loop; i++)
82  dummy = i;
83 }
84 
85 int
86 main(int argc, const char* argv[])
87 {
88  printf("--> main()\n");
89  do_something();
90 
91  hrt_tstamp t0, t1;
92  hrt_tnull(&t0);
93  hrt_tnull(&t1);
94 
95  printf("\nmarking time...\n");
96  int r;
97  if ((r = hrt_tnow(&t0)) != 0) {
98  fprintf(stderr, "error: hrt_now(&t0) returned %d\n", r);
99  }
100  hrt_tprint(&t0);
101 
102  do_something();
103 
104  printf("\nmarking time...\n");
105  if ((r = hrt_tnow(&t1)) != 0) {
106  fprintf(stderr, "error: hrt_now(&t1) returned %d\n", r);
107  }
108  hrt_tprint(&t1);
109 
110  printf("\namount of times:\n");
111  double rtmicros = hrt_rtmicros(&t1.rtstamp, &t0.rtstamp);
112  double ctmicros = hrt_ctmicros(&t1.ctstamp, &t0.ctstamp);
113  printf("real = %.3f us\n", rtmicros);
114  printf("cpu = %.3f us\n", ctmicros);
115 
116  printf("\n<-- main()\n");
117  return 0;
118 }