MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hrt_stopwatch.c
1 /*
2  Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 /*
19  * hrt_stopwatch.c
20  *
21  */
22 
23 #include <stdlib.h>
24 #include <assert.h>
25 
26 #include "hrt_stopwatch.h"
27 
28 /*
29  * High-Resolution Time Stopwatch Utility -- Implementation
30  */
31 
32 extern void
33 hrt_sw_init(hrt_stopwatch* sw, int cap)
34 {
35  sw->cap = cap;
36  sw->top = 0;
37  sw->tstamps = malloc(sizeof(hrt_tstamp) * cap);
38 }
39 
40 extern void
41 hrt_sw_close(hrt_stopwatch* sw)
42 {
43  free(sw->tstamps);
44  sw->cap = 0;
45  sw->top = 0;
46 }
47 
48 extern int
49 hrt_sw_top(const hrt_stopwatch* sw)
50 {
51  return sw->top - 1;
52 }
53 
54 extern int
55 hrt_sw_capacity(const hrt_stopwatch* sw)
56 {
57  return sw->cap;
58 }
59 
60 extern int
61 hrt_sw_pushmark(hrt_stopwatch* sw)
62 {
63  assert (sw->top < sw->cap);
64  int r = hrt_tnow(sw->tstamps + sw->top);
65  assert (r == 0);
66  (void)r;
67  return sw->top++;
68 }
69 
70 extern void
71 hrt_sw_popmark(hrt_stopwatch* sw)
72 {
73  assert (sw->top > 0);
74  sw->top--;
75 }
76 
77 extern double
78 hrt_sw_rtmicros(const hrt_stopwatch* sw, int y, int x)
79 {
80  assert (0 <= y && y < sw->top);
81  assert (0 <= x && x < sw->top);
82  return hrt_rtmicros(&sw->tstamps[y].rtstamp, &sw->tstamps[x].rtstamp);
83 }
84 
85 extern double
86 hrt_sw_ctmicros(const hrt_stopwatch* sw, int y, int x)
87 {
88  assert (0 <= y && y < sw->top);
89  assert (0 <= x && x < sw->top);
90  return hrt_ctmicros(&sw->tstamps[y].ctstamp, &sw->tstamps[x].ctstamp);
91 }
92 
93 extern void
94 hrt_sw_clear(hrt_stopwatch* sw)
95 {
96  sw->top = 0;
97 }
98