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