service_thd_wait.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License, version 2.0, for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  18. #ifndef MYSQL_SERVICE_THD_WAIT_INCLUDED
  19. #define MYSQL_SERVICE_THD_WAIT_INCLUDED
  20. /**
  21. @file include/mysql/service_thd_wait.h
  22. This service provides functions for plugins and storage engines to report
  23. when they are going to sleep/stall.
  24. SYNOPSIS
  25. thd_wait_begin() - call just before a wait begins
  26. thd Thread object
  27. Use NULL if the thd is NOT known.
  28. wait_type Type of wait
  29. 1 -- short wait (e.g. for mutex)
  30. 2 -- medium wait (e.g. for disk io)
  31. 3 -- large wait (e.g. for locked row/table)
  32. NOTES
  33. This is used by the threadpool to have better knowledge of which
  34. threads that currently are actively running on CPUs. When a thread
  35. reports that it's going to sleep/stall, the threadpool scheduler is
  36. free to start another thread in the pool most likely. The expected wait
  37. time is simply an indication of how long the wait is expected to
  38. become, the real wait time could be very different.
  39. thd_wait_end() called immediately after the wait is complete
  40. thd_wait_end() MUST be called if thd_wait_begin() was called.
  41. Using thd_wait_...() service is optional but recommended. Using it will
  42. improve performance as the thread pool will be more active at managing the
  43. thread workload.
  44. */
  45. #ifdef __cplusplus
  46. class THD;
  47. #define MYSQL_THD THD*
  48. #else
  49. #define MYSQL_THD void*
  50. #endif
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. /*
  55. One should only report wait events that could potentially block for a
  56. long time. A mutex wait is too short of an event to report. The reason
  57. is that an event which is reported leads to a new thread starts
  58. executing a query and this has a negative impact of usage of CPU caches
  59. and thus the expected gain of starting a new thread must be higher than
  60. the expected cost of lost performance due to starting a new thread.
  61. Good examples of events that should be reported are waiting for row locks
  62. that could easily be for many milliseconds or even seconds and the same
  63. holds true for global read locks, table locks and other meta data locks.
  64. Another event of interest is going to sleep for an extended time.
  65. Note that user-level locks no longer use THD_WAIT_USER_LOCK wait type.
  66. Since their implementation relies on metadata locks manager it uses
  67. THD_WAIT_META_DATA_LOCK instead.
  68. */
  69. typedef enum _thd_wait_type_e {
  70. THD_WAIT_SLEEP= 1,
  71. THD_WAIT_DISKIO= 2,
  72. THD_WAIT_ROW_LOCK= 3,
  73. THD_WAIT_GLOBAL_LOCK= 4,
  74. THD_WAIT_META_DATA_LOCK= 5,
  75. THD_WAIT_TABLE_LOCK= 6,
  76. THD_WAIT_USER_LOCK= 7,
  77. THD_WAIT_BINLOG= 8,
  78. THD_WAIT_GROUP_COMMIT= 9,
  79. THD_WAIT_SYNC= 10,
  80. THD_WAIT_LAST= 11
  81. } thd_wait_type;
  82. extern struct thd_wait_service_st {
  83. void (*thd_wait_begin_func)(MYSQL_THD, int);
  84. void (*thd_wait_end_func)(MYSQL_THD);
  85. } *thd_wait_service;
  86. #ifdef MYSQL_DYNAMIC_PLUGIN
  87. #define thd_wait_begin(_THD, _WAIT_TYPE) \
  88. thd_wait_service->thd_wait_begin_func(_THD, _WAIT_TYPE)
  89. #define thd_wait_end(_THD) thd_wait_service->thd_wait_end_func(_THD)
  90. #else
  91. void thd_wait_begin(MYSQL_THD thd, int wait_type);
  92. void thd_wait_end(MYSQL_THD thd);
  93. #endif
  94. #ifdef __cplusplus
  95. }
  96. #endif
  97. #endif