1/*****************************************************************************
2
3Copyright (c) 2013, 2014, Facebook, Inc. All Rights Reserved.
4Copyright (c) 2014, SkySQL Ab. All Rights Reserved.
5
6This program is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free Software
8Foundation; version 2 of the License.
9
10This program is distributed in the hope that it will be useful, but WITHOUT
11ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License along with
15this program; if not, write to the Free Software Foundation, Inc.,
1651 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
17
18*****************************************************************************/
19
20/********************************************************************//**
21@file ut/ut0timer.cc
22Timer rountines
23
24Created 30/07/2014 Jan Lindström jan.lindstrom@skysql.com
25modified from https://github.com/facebook/mysql-5.6/commit/c75a413edeb96eb99bf11d7269bdfea06f96d6b6
26*************************************************************************/
27
28#include "data0type.h"
29#include <my_rdtsc.h>
30#include <ut0timer.h>
31
32/**************************************************************//**
33Initial timer definition
34@return 0 */
35static
36ulonglong
37ut_timer_none(void)
38/*===============*/
39{
40 return 0;
41}
42
43/**************************************************************//**
44Function pointer to point selected timer function.
45@return timer current value */
46ulonglong (*ut_timer_now)(void) = &ut_timer_none;
47
48struct my_timer_unit_info ut_timer;
49
50/**************************************************************//**
51Sets up the data required for use of my_timer_* functions.
52Selects the best timer by high frequency, and tight resolution.
53Points my_timer_now() to the selected timer function.
54Initializes my_timer struct to contain the info for selected timer.*/
55UNIV_INTERN
56void
57ut_init_timer(void)
58/*===============*/
59{
60 MY_TIMER_INFO all_timer_info;
61 my_timer_init(&all_timer_info);
62
63 if (all_timer_info.cycles.frequency > 1000000 &&
64 all_timer_info.cycles.resolution == 1) {
65 ut_timer = all_timer_info.cycles;
66 ut_timer_now = &my_timer_cycles;
67 } else if (all_timer_info.nanoseconds.frequency > 1000000 &&
68 all_timer_info.nanoseconds.resolution == 1) {
69 ut_timer = all_timer_info.nanoseconds;
70 ut_timer_now = &my_timer_nanoseconds;
71 } else if (all_timer_info.microseconds.frequency >= 1000000 &&
72 all_timer_info.microseconds.resolution == 1) {
73 ut_timer = all_timer_info.microseconds;
74 ut_timer_now = &my_timer_microseconds;
75
76 } else if (all_timer_info.milliseconds.frequency >= 1000 &&
77 all_timer_info.milliseconds.resolution == 1) {
78 ut_timer = all_timer_info.milliseconds;
79 ut_timer_now = &my_timer_milliseconds;
80 } else if (all_timer_info.ticks.frequency >= 1000 &&
81 /* Will probably be false */
82 all_timer_info.ticks.resolution == 1) {
83 ut_timer = all_timer_info.ticks;
84 ut_timer_now = &my_timer_ticks;
85 } else {
86 /* None are acceptable, so leave it as "None", and fill in struct */
87 ut_timer.frequency = 1; /* Avoid div-by-zero */
88 ut_timer.overhead = 0; /* Since it doesn't do anything */
89 ut_timer.resolution = 10; /* Another sign it's bad */
90 ut_timer.routine = 0; /* None */
91 }
92}
93