MySQL  8.0.27
Source Code Documentation
log_sink_perfschema.h
Go to the documentation of this file.
1 /* Copyright (c) 2020, 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
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22 
23 /**
24  @file sql/server_component/log_sink_perfschema.h
25 
26  This file contains
27 
28  a) The API for the reading of previously written error logs.
29  (These functions will in turn use a parse-function defined
30  in a log-sink. Whichever log-sink that has a parse-function
31  is listed first in @@global.log_error_services will be used;
32  that service will decide what log-file to read (i.e. its name)
33  and how to parse it. We initially support the reading of JSON-
34  formatted error log files and of the traditional MySQL error
35  log files.)
36  This lets us restore error log information from previous runs
37  when the server starts.
38  These functions are called from mysqld.cc at start-up.
39 
40  b) The log-sink that adds errors logged at run-time to the ring-buffer
41  (to be called from @see log_line_submit() during normal operation, i.e.
42  when loadable log-components are available, connections are accepted,
43  and so on).
44 */
45 
46 #ifndef LOG_SINK_PERFSCHEMA_H
47 #define LOG_SINK_PERFSCHEMA_H
48 
49 #include "log_builtins_internal.h"
50 #include "my_thread_local.h" // my_thread_id
51 
52 /* "MY-123456" - 6 digits, "MY-", '\0' */
53 #define LOG_SINK_PFS_ERROR_CODE_LENGTH 10
54 
55 /* Currently one of "Repl"/"InnoDB"/"Server" + '\0' */
56 #define LOG_SINK_PFS_SUBSYS_LENGTH 7
57 
58 typedef struct _log_sink_pfs_event {
59  /** Column ERROR_LOG_TIMESTAMP. Logger should forcibly make these unique. */
61 
62  /** Column ERROR_LOG_THREAD. */
63  ulonglong m_thread_id; // PFS_key_thread_id uses ulonglong, not my_thread_id
64 
65  /** Column ERROR_LOG_PRIO. */
66  ulong m_prio;
67 
68  /** Column ERROR_LOG_ERROR_CODE. */
71 
72  /** Column ERROR_LOG_SUBSYS. */
75 
76  /** Column ERROR_LOG_MESSAGE. */
77  uint m_message_length; //< actual length, not counting trailing '\0'
79 
80 /*
81  We make these public for SHOW STATUS.
82  Everybody else should use the getter functions.
83 
84  The timestamp is made available to allow for
85  easy checks whether log entries were added since
86  the interested part last polled.
87  The timestamp is provided as a unique value with
88  microsecond precision for that use; to view it in
89  a more human-friendly format, use
90 
91  SELECT FROM_UNIXTIME(variable_value/1000000)
92  FROM global_status WHERE variable_name="Error_log_latest_write";
93 
94  Thus if you expect to check for new events with some
95  regularity, you could start off with
96 
97  SELECT 0 INTO @error_log_last_poll;
98 
99  and then poll using something like
100 
101  SELECT logged,prio,error_code,subsystem,data
102  FROM performance_schema.error_log
103  WHERE logged>@error_log_last_poll;
104 
105  SELECT FROM_UNIXTIME(variable_value/1000000)
106  FROM global_status WHERE variable_name="Error_log_latest_write"
107  INTO @error_log_last_poll;
108 
109  (Ideally though you'd update @error_log_last_poll from the 'logged'
110  field (that is, the timestamp) of the last new row you received.)
111 */
112 extern ulong log_sink_pfs_buffered_bytes; //< bytes in use (now)
113 extern ulong log_sink_pfs_buffered_events; //< events in buffer (now)
114 extern ulong log_sink_pfs_expired_events; //< number of expired entries (ever)
115 extern ulong log_sink_pfs_longest_event; //< longest event seen (ever)
116 extern ulonglong
117  log_sink_pfs_latest_timestamp; //< timestamp of most recent write
118 
119 // The public interface to reading the error-log from the ring-buffer:
120 
121 /// Acquire a read-lock on the ring-buffer.
123 
124 /// Release read-lock on ring-buffer.
125 void log_sink_pfs_read_end();
126 
127 /**
128  Get number of events currently in ring-buffer.
129  Caller should hold THR_LOCK_log_perschema when reading this.
130 
131  @returns number of events current in ring-buffer (0..)
132 */
133 size_t log_sink_pfs_event_count();
134 
135 /**
136  Get oldest event still in ring-buffer.
137  Caller should hold read-lock on THR_LOCK_log_perfschema when calling this.
138 
139  @returns nullptr No events in buffer
140  @returns otherwise Address of oldest event in ring-buffer
141 */
143 
145 
147  ulonglong logged);
148 
149 // The public interface to restoring the error-log to the ring-buffer:
150 
151 /**
152  Set up ring-buffer for error-log.
153 
154  @returns 0 Success - buffer was allocated.
155  @returns !=0 Failure - buffer was not allocated.
156 */
158 
159 /**
160  Release error log ring-buffer.
161 
162  @returns 0 Success - buffer was released, or did not exist in the first
163  place.
164 */
166 
167 log_service_error log_error_read_log(const char *log_name);
168 
170  const char *blob_src);
171 
172 int log_sink_perfschema(void *instance [[maybe_unused]], log_line *ll);
173 
174 #endif /* LOG_SINK_PERFSCHEMA_H */
enum enum_log_service_error log_service_error
Error codes.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
log_service_error log_sink_pfs_event_add(log_sink_pfs_event *e, const char *blob_src)
Add a log-event to the ring buffer.
Definition: log_sink_perfschema.cc:382
ulonglong log_sink_pfs_latest_timestamp
Definition: log_sink_perfschema.cc:84
log_service_error log_error_read_log(const char *log_name)
Restore error log messages from previous shutdown.
Definition: log_sink_perfschema.cc:685
ulong log_sink_pfs_buffered_bytes
Definition: log_sink_perfschema.cc:80
int log_error_read_log_init()
Set up ring-buffer for error-log.
Definition: log_sink_perfschema.cc:774
ulong log_sink_pfs_expired_events
Definition: log_sink_perfschema.cc:82
void log_sink_pfs_read_end()
Release read-lock on ring-buffer.
Definition: log_sink_perfschema.cc:141
#define LOG_SINK_PFS_SUBSYS_LENGTH
Definition: log_sink_perfschema.h:56
log_sink_pfs_event * log_sink_pfs_event_next(log_sink_pfs_event *e)
Get event following the supplied one.
Definition: log_sink_perfschema.cc:182
int log_error_read_log_exit()
Release error log ring-buffer.
Definition: log_sink_perfschema.cc:748
log_sink_pfs_event * log_sink_pfs_event_first()
Get oldest event still in ring-buffer.
Definition: log_sink_perfschema.cc:158
void log_sink_pfs_read_start()
Acquire a read-lock on the ring-buffer.
Definition: log_sink_perfschema.cc:134
size_t log_sink_pfs_event_count()
Get number of events currently in ring-buffer.
Definition: log_sink_perfschema.cc:149
ulong log_sink_pfs_longest_event
Definition: log_sink_perfschema.cc:83
int log_sink_perfschema(void *instance[[maybe_unused]], log_line *ll)
services: log sinks: logging to performance_schema ring-buffer
Definition: log_sink_perfschema.cc:812
struct _log_sink_pfs_event log_sink_pfs_event
ulong log_sink_pfs_buffered_events
Definition: log_sink_perfschema.cc:81
#define LOG_SINK_PFS_ERROR_CODE_LENGTH
Definition: log_sink_perfschema.h:53
log_sink_pfs_event * log_sink_pfs_event_valid(log_sink_pfs_event *e, ulonglong logged)
Use timestamp to check whether a given event-pointer still points to a valid event in the ring-buffer...
Definition: log_sink_perfschema.cc:233
log_line ("log event")
Definition: keyring_log_builtins_definition.cc:70
Definition: log_sink_perfschema.h:58
ulonglong m_thread_id
Column ERROR_LOG_THREAD.
Definition: log_sink_perfschema.h:63
char m_error_code[LOG_SINK_PFS_ERROR_CODE_LENGTH]
Column ERROR_LOG_ERROR_CODE.
Definition: log_sink_perfschema.h:69
uint m_message_length
Column ERROR_LOG_MESSAGE.
Definition: log_sink_perfschema.h:77
ulonglong m_timestamp
Column ERROR_LOG_TIMESTAMP.
Definition: log_sink_perfschema.h:60
uint m_subsys_length
Definition: log_sink_perfschema.h:74
char m_subsys[LOG_SINK_PFS_SUBSYS_LENGTH]
Column ERROR_LOG_SUBSYS.
Definition: log_sink_perfschema.h:73
ulong m_prio
Column ERROR_LOG_PRIO.
Definition: log_sink_perfschema.h:66
uint m_error_code_length
Definition: log_sink_perfschema.h:70
unsigned int uint
Definition: uca-dump.cc:29