1 /* Copyright (c) 2010, 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, 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
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef MYSQL_SERVICE_THD_WAIT_INCLUDED
24 #define MYSQL_SERVICE_THD_WAIT_INCLUDED
25 
26 /**
27   @file include/mysql/service_thd_wait.h
28   This service provides functions for plugins and storage engines to report
29   when they are going to sleep/stall.
30 
31   SYNOPSIS
32   thd_wait_begin() - call just before a wait begins
33   thd                     Thread object
34                           Use NULL if the thd is NOT known.
35   wait_type               Type of wait
36                           1 -- short wait (e.g. for mutex)
37                           2 -- medium wait (e.g. for disk io)
38                           3 -- large wait (e.g. for locked row/table)
39   NOTES
40     This is used by the threadpool to have better knowledge of which
41     threads that currently are actively running on CPUs. When a thread
42     reports that it's going to sleep/stall, the threadpool scheduler is
43     free to start another thread in the pool most likely. The expected wait
44     time is simply an indication of how long the wait is expected to
45     become, the real wait time could be very different.
46 
47   thd_wait_end() called immediately after the wait is complete
48 
49   thd_wait_end() MUST be called if thd_wait_begin() was called.
50 
51   Using thd_wait_...() service is optional but recommended.  Using it will
52   improve performance as the thread pool will be more active at managing the
53   thread workload.
54 */
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
60 /*
61   One should only report wait events that could potentially block for a
62   long time. A mutex wait is too short of an event to report. The reason
63   is that an event which is reported leads to a new thread starts
64   executing a query and this has a negative impact of usage of CPU caches
65   and thus the expected gain of starting a new thread must be higher than
66   the expected cost of lost performance due to starting a new thread.
67 
68   Good examples of events that should be reported are waiting for row locks
69   that could easily be for many milliseconds or even seconds and the same
70   holds true for global read locks, table locks and other meta data locks.
71   Another event of interest is going to sleep for an extended time.
72 */
73 typedef enum _thd_wait_type_e {
74   THD_WAIT_SLEEP= 1,
75   THD_WAIT_DISKIO= 2,
76   THD_WAIT_ROW_LOCK= 3,
77   THD_WAIT_GLOBAL_LOCK= 4,
78   THD_WAIT_META_DATA_LOCK= 5,
79   THD_WAIT_TABLE_LOCK= 6,
80   THD_WAIT_USER_LOCK= 7,
81   THD_WAIT_BINLOG= 8,
82   THD_WAIT_GROUP_COMMIT= 9,
83   THD_WAIT_SYNC= 10,
84   THD_WAIT_NET= 11,
85   THD_WAIT_LAST= 12
86 } thd_wait_type;
87 
88 extern struct thd_wait_service_st {
89   void (*thd_wait_begin_func)(MYSQL_THD, int);
90   void (*thd_wait_end_func)(MYSQL_THD);
91 } *thd_wait_service;
92 
93 #ifdef MYSQL_DYNAMIC_PLUGIN
94 
95 #define thd_wait_begin(_THD, _WAIT_TYPE) \
96   thd_wait_service->thd_wait_begin_func(_THD, _WAIT_TYPE)
97 #define thd_wait_end(_THD) thd_wait_service->thd_wait_end_func(_THD)
98 
99 #else
100 
101 void thd_wait_begin(MYSQL_THD thd, int wait_type);
102 void thd_wait_end(MYSQL_THD thd);
103 
104 #endif
105 
106 #ifdef __cplusplus
107 }
108 #endif
109 
110 #endif
111 
112