MySQL 8.4.0
Source Code Documentation
pfs_notification.h
Go to the documentation of this file.
1/* Copyright (c) 2017, 2024, Oracle and/or its affiliates.
2
3This program is free software; you can redistribute it and/or modify
4it under the terms of the GNU General Public License, version 2.0,
5as published by the Free Software Foundation.
6
7This program is designed to work with certain software (including
8but not limited to OpenSSL) that is licensed under separate terms,
9as designated in a particular file or component or in included license
10documentation. The authors of MySQL hereby grant you an additional
11permission to link the program and your derivative works with the
12separately licensed software that they have either included with
13the program or referenced in the documentation.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU General Public License, version 2.0, for more details.
19
20You should have received a copy of the GNU General Public License
21along with this program; if not, write to the Free Software
22Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef PFS_NOTIFY_SERVICE_H
25#define PFS_NOTIFY_SERVICE_H
26
29
30/**
31 @page PAGE_PFS_NOTIFICATION_SERVICE Notification service
32
33 @section PFS_NOTIFICATION_INTRO Introduction
34 The Performance Schema Notification service provides a means to register
35 user-defined callback functions for the following thread and session events:
36
37 - thread create
38 - thread destroy
39 - session connect
40 - session disconnect
41 - session change user
42
43 This is a synchronous, low-level API designed to impose minimal performance
44 overhead.
45
46 @section PFS_NOTIFICATION_CALLBACKS Callback Function Definition
47
48 Callback functions are of the type #PSI_notification_cb:
49
50 @code
51 typedef void (*PSI_notification_cb)(const PSI_thread_attrs *thread_attrs);
52 @endcode
53
54 For example:
55 @code
56 void callback_thread_create(const PSI_thread_attrs *thread_attrs)
57 {
58 set_thread_resource_group(...)
59 }
60 @endcode
61
62 When the callback is invoked, the #PSI_thread_attrs structure will contain the
63 system attributes of the thread.
64
65 @section PFS_NOTIFICATION_REGISTER Registering Events
66
67 To register for one or more events, set the corresponding callback function
68 pointers in the #PSI_notification structure, leaving the function pointer
69 NULL for unused callbacks.
70
71 Use the service function @c register_notification() to register the callbacks
72 with the server
73
74 @code
75 int register_notification(PSI_notification *callbacks,
76 bool with_ref_count);
77 @endcode
78
79 where
80 - @c callbacks is the callback function set
81 - @c with_ref_count determines whether a reference count is used for the
82 callbacks. Set TRUE for callback functions in
83 dynamically loaded modules. Set FALSE for callback
84 functions in static or unloadable modules.
85
86 For example:
87 @code
88 PSI_notification my_callbacks;
89
90 my_callbacks.thread_create = &thread_create_callback;
91 my_callbacks.thread_destroy = &thread_destroy_callback;
92 my_callbacks.session_connect = &session_connect_callback;
93 my_callbacks.session_disconnect = &session_disconnect_callback;
94 my_callbacks.session_change_user = nullptr;
95
96 int my_handle =
97 mysql_service_pfs_notification->register_notification(&my_callbacks, true);
98 @endcode
99
100 A non-zero handle is returned if the registration is successful. This handle
101 is used to unregister the callback set.
102
103 A callback set can be registered more than once. No error is returned
104 for calling @c register_notification() more than once for a given callback
105 set. Callbacks are invoked once for each time they are registered.
106
107 For callback functions that reside in dynamically loaded modules, set
108 @verbatim with_ref_count = TRUE @endverbatim so that the module can be safely
109 unloaded after the callbacks are unregistered.
110
111 For callback functions that reside in static, built-in or otherwise unloadable
112 modules, set @verbatim with_ref_count = FALSE @endverbatim to optimize
113 callback performance in high-concurrency environments.
114
115 Callbacks that reside in a dynamically loaded module such as a server plugin,
116 must be successfully unregistered before the module is unloaded.
117
118 For callbacks in static or unloadable modules, @c unregister_notification()
119 will disable the callback functions, but the function pointers will remain.
120
121 @section PFS_NOTIFICATION_UNREGISTER Unregistering Events
122
123 To unregister callback functions from the Notification service, use the handle
124 returned from @c register_notification(). For example:
125
126 @code
127 int ret =
128 mysql_service_pfs_notification->unregister_notification(my_handle);
129
130 if (ret == 0)
131 {
132 // unload component
133 }
134 else
135 {
136 // error
137 }
138 @endcode
139
140 Callbacks that reside in a dynamically loaded module such as a server plugin
141 or component must be successfully unregistered before the module is unloaded.
142
143 If @c unregister_notification() returns an error, then the module should not
144 be unloaded.
145
146 If the callbacks were registered with @verbatim with_ref_count = TRUE
147 @endverbatim then
148 @c unregister_notification() will return an error if any of the functions are
149 in use and fail to return after 2 seconds.
150
151 If the callbacks were registered with @verbatim with_ref_count = FALSE
152 @endverbatim then
153 @c unregister_notification() will disable the callback functions, but the
154 callback function pointers will be assumed to be valid until the server is
155 shutdown.
156
157 @c unregister_callback() can be called multiple times for the same handle.
158
159 Failing to unregister all callback functions from a dynamically loaded plugin
160 or component may result in a undocumented behavior during server shutdown.
161
162*/
163
164/*
165 SERVICE_DEFINITION(pfs_notification)
166 Introduced in MySQL 8.0.2
167 Removed in MySQL 8.0.17
168 Status: Removed, use version 3 instead.
169*/
170
171/*
172 Version 3.
173 Introduced in MySQL 8.0.17
174 Status: active
175*/
176
177BEGIN_SERVICE_DEFINITION(pfs_notification_v3)
178register_notification_v3_t register_notification;
179unregister_notification_v1_t unregister_notification;
180END_SERVICE_DEFINITION(pfs_notification_v3)
181
182#define REQUIRES_PFS_NOTIFICATION_SERVICE REQUIRES_SERVICE(pfs_notification_v3)
183
184#endif
int(* register_notification_v3_t)(const PSI_notification_v3 *callbacks, bool with_ref_count)
Register callback functions for the Notification service.
Definition: psi_thread_bits.h:563
int(* unregister_notification_v1_t)(int handle)
Unregister callback functions for the Notification service.
Definition: psi_thread_bits.h:573
Instrumentation helpers for mysys threads.
#define END_SERVICE_DEFINITION(name)
A macro to end the last Service definition started with the BEGIN_SERVICE_DEFINITION macro.
Definition: service.h:91
#define BEGIN_SERVICE_DEFINITION(name)
Declares a new Service.
Definition: service.h:86