1/* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software Foundation,
14 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA */
15
16#ifndef PFS_TIMER_H
17#define PFS_TIMER_H
18
19/**
20 @file storage/perfschema/pfs_timer.h
21 Performance schema timers (declarations).
22*/
23#include <my_rdtsc.h>
24#include "pfs_column_types.h"
25
26/** Conversion factor, from micro seconds to pico seconds. */
27#define MICROSEC_TO_PICOSEC 1000000
28
29/**
30 A time normalizer.
31 A time normalizer consist of a transformation that
32 converts raw timer values (expressed in the timer unit)
33 to normalized values, expressed in picoseconds.
34*/
35struct time_normalizer
36{
37 /**
38 Get a time normalizer for a given timer.
39 @param timer_name the timer name
40 @return the normalizer for the timer
41 */
42 static time_normalizer* get(enum_timer_name timer_name);
43
44 /** Timer value at server statup. */
45 ulonglong m_v0;
46 /** Conversion factor from timer values to pico seconds. */
47 ulonglong m_factor;
48
49 /**
50 Convert a wait from timer units to pico seconds.
51 @param wait a wait, expressed in timer units
52 @return the wait, expressed in pico seconds
53 */
54 inline ulonglong wait_to_pico(ulonglong wait)
55 {
56 return wait * m_factor;
57 }
58
59 /**
60 Convert a time from timer units to pico seconds.
61 @param t a time, expressed in timer units
62 @return the time, expressed in pico seconds
63 */
64 inline ulonglong time_to_pico(ulonglong t)
65 {
66 return (t == 0 ? 0 : (t - m_v0) * m_factor);
67 }
68
69 /**
70 Convert start / end times from timer units to pico seconds.
71 @param start start time, expressed in timer units
72 @param end end time, expressed in timer units
73 @param[out] pico_start start time, expressed in pico seconds
74 @param[out] pico_end end time, expressed in pico seconds
75 @param[out] pico_wait wait time, expressed in pico seconds
76 */
77 void to_pico(ulonglong start, ulonglong end,
78 ulonglong *pico_start, ulonglong *pico_end, ulonglong *pico_wait);
79};
80
81/**
82 Idle timer.
83 The timer used to measure all idle events.
84*/
85extern enum_timer_name idle_timer;
86/**
87 Wait timer.
88 The timer used to measure all wait events.
89*/
90extern enum_timer_name wait_timer;
91/**
92 Stage timer.
93 The timer used to measure all stage events.
94*/
95extern enum_timer_name stage_timer;
96/**
97 Statement timer.
98 The timer used to measure all statement events.
99*/
100extern enum_timer_name statement_timer;
101/**
102 Timer information data.
103 Characteristics about each suported timer.
104*/
105extern MY_TIMER_INFO pfs_timer_info;
106
107/** Initialize the timer component. */
108void init_timers();
109
110extern "C"
111{
112 /** A timer function. */
113 typedef ulonglong (*timer_fct_t)(void);
114}
115
116/**
117 Get a timer value, in pico seconds.
118 @param timer_name the timer to use
119 @return timer value, in pico seconds
120*/
121ulonglong get_timer_pico_value(enum_timer_name timer_name);
122/**
123 Get a timer value, in timer units.
124 @param timer_name the timer to use
125 @return timer value, in timer units
126*/
127ulonglong get_timer_raw_value(enum_timer_name timer_name);
128/**
129 Get a timer value and function, in timer units.
130 This function is useful when code needs to call the same timer several times.
131 The returned timer function can be invoked directly, which avoids having to
132 resolve the timer by name for each call.
133 @param timer_name the timer to use
134 @param[out] fct the timer function
135 @return timer value, in timer units
136*/
137ulonglong get_timer_raw_value_and_function(enum_timer_name timer_name, timer_fct_t *fct);
138
139#endif
140
141