1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
/* Copyright (c) 2020, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef CURSOR_BY_ERROR_LOG_H
#define CURSOR_BY_ERROR_LOG_H
/**
@file storage/perfschema/cursor_by_error_log.h
Cursor CURSOR_BY_ERROR_LOG (declarations);
PFS_ringbuffer_index, PFS_index_error_log.
*/
#include "sql/server_component/log_sink_perfschema.h"
#include "storage/perfschema/pfs_engine_table.h"
#include "storage/perfschema/pfs_instr.h"
#include "storage/perfschema/table_helper.h"
/**
Index in the error-log ring-buffer.
Has a numeric index, a pointer to an event in the buffer,
and the timestamp for that event.
This lets us easily check whether this index is still
valid: its timestamp must be greater than or equal to
that of the ring-buffer's oldest entry (the tail or
"read-position"). If our timestamp is older than that
value, it points to an event that has since expired.
*/
class PFS_ringbuffer_index {
private:
/**
Numeric row index. valid range [0;num_events[. -1 == EOF
*/
int m_index;
/**
Pointer to an event in the ring-buffer.
Before dereferencing this pointer, use
@see log_sink_pfs_event_valid()
to make sure it has not become stale.
*/
log_sink_pfs_event *m_event;
/**
Time-stamp we copied from the event we
point to when setting this index.
The ring-buffer keeps track of what the
oldest item in it is. If our timestamp
is older than that item's timestamp,
the event we're pointing too has been
purged from the ring-buffer, and our
pointer is stale. 0 for undefined.
*/
ulonglong m_timestamp;
public:
/**
Reset index.
*/
void reset() {
m_index = 0;
m_event = nullptr;
m_timestamp = 0;
}
/**
Constructor.
*/
PFS_ringbuffer_index() { reset(); }
/**
Set this index to a given position.
This copies ``other`` without validating it.
@param other set our index from the given one
*/
void set_at(const PFS_ringbuffer_index *other) {
m_index = other->m_index;
m_event = other->m_event;
m_timestamp = other->m_timestamp;
}
/**
Set our index to the element after the given one
(if that is valid; otherwise, we cannot determine a next element).
Caller should hold read-lock on ring-buffer.
@param other set our index to the position after the given one
*/
void set_after(const PFS_ringbuffer_index *other) {
assert(other != nullptr);
// special case: ``other`` was reset or is otherwise at index start
if ((other->m_index == 0) &&
((m_event = log_sink_pfs_event_first()) != nullptr) &&
((m_event = log_sink_pfs_event_next(m_event)) != nullptr)) {
m_timestamp = m_event->m_timestamp; // save timestamp
m_index = 1; // "calculate" new index
return;
}
// if ``other`` is valid and has a successor, use that
if ((other->m_index != -1) && // EOF?
log_sink_pfs_event_valid(other->m_event, other->m_timestamp) &&
((m_event = log_sink_pfs_event_next(other->m_event)) != nullptr)) {
m_timestamp = m_event->m_timestamp; // save timestamp
m_index = other->m_index + 1; // "calculate" new index
return;
}
// no valid successor found
reset(); // reset this index
m_index = -1; // flag EOF
}
/**
Get event if it's still valid.
Caller should hold read-lock on ring-buffer.
If ``m_index`` is 0, the index was reset, and we re-obtain the
ring-buffer's read-pointer / tail. This updates m_event and
m_timestamp.
If ``m_index`` is -1 (EOF), we return ``nullptr``.
Otherwise, we try to obtain the event in the ring-buffer pointed
to by m_event. If that event is still valid, we return it;
otherwise, we return nullptr (but leave the stale pointer on
the object for debugging). It is therefore vital to to determine
success/failure by checking the retval rather than calling this
method and then checking m_event directly!
@retval nullptr no event (EOF, empty buffer, or stale index)
@retval !=nullptr the event this index is referring to
*/
log_sink_pfs_event *get_event() {
/*
Special case: the index was reset.
Refresh pointer from oldest entry in the ring-buffer.
*/
if (m_index == 0) {
m_timestamp =
((m_event = log_sink_pfs_event_first()) == nullptr)
? 0 // first() failed: buffer empty. zero the timestamp.
: m_event->m_timestamp;
/*
We don't log_sink_pfs_event_valid() the event here.
We only just got it, and we should be holding a read-lock
on the ring-buffer, so the event can't have expired.
*/
return m_event; // { 0, nullptr, 0 } on empty buffer, otherwise a valid
// event
}
/*
If the index is at EOF, or points to an element that has since been
discarded from the ring-buffer, we have no event we could return.
*/
if ((m_index == -1) || !log_sink_pfs_event_valid(m_event, m_timestamp))
return nullptr; // no event to get
return m_event; // return valid event
}
/**
Return current record (if valid), then set index to the next record.
Caller should hold read-lock on ring-buffer.
Returned value is only valid as long as the lock is held.
Note that three states are possible:
a) an event-pointer is returned, and the index points to a valid
succeeding event (i.e. is not EOF): there are more elements
b) an event-pointer is returned, but the index now flags EOF:
this was the last element
c) NULL is returned, and the index flags EOF:
no event could be obtained (the buffer is empty, or we're at EOF)
Updates m_event, m_timestamp, and m_index.
@retval nullptr no valid record could be obtained (end of buffer etc.)
@retval !=nullptr pointer to an entry in the ring-buffer
*/
log_sink_pfs_event *scan_next() {
log_sink_pfs_event *current_event;
// Is there a valid current event that we can load into this object?
current_event = get_event();
if (current_event != nullptr) { // save current event if any
// try to advance index to next event
m_event = log_sink_pfs_event_next(current_event);
if (m_event != nullptr) {
// success. update this index object.
m_timestamp = m_event->m_timestamp;
m_index++;
return current_event;
}
// If we get here, current_event is valid, but has no successor.
}
// Index now points to an invalid event. Flag EOF.
m_event = nullptr;
m_timestamp = 0;
m_index = -1;
// last event in buffer (if get_event() succeeded), or NULL otherwise.
return current_event;
}
};
typedef PFS_ringbuffer_index pos_t;
/**
@addtogroup performance_schema_tables
@{
*/
/** Generic index for error_log table. Used by cursor class for open index. */
class PFS_index_error_log : public PFS_engine_index {
public:
explicit PFS_index_error_log(PFS_engine_key *key) : PFS_engine_index(key) {}
~PFS_index_error_log() override = default;
virtual bool match(log_sink_pfs_event *row) = 0;
};
/** Cursor CURSOR_BY_ERROR_LOG for error_log table. */
class cursor_by_error_log : public PFS_engine_table {
public:
static ha_rows get_row_count();
void reset_position() override;
int rnd_next() override;
int rnd_pos(const void *pos) override;
int index_next() override;
protected:
explicit cursor_by_error_log(const PFS_engine_table_share *share);
public:
~cursor_by_error_log() override = default;
protected:
virtual int make_row(log_sink_pfs_event *row) = 0;
private:
/** Current position. */
pos_t m_pos;
/** Next position. */
pos_t m_next_pos;
protected:
PFS_index_error_log *m_opened_index;
};
/** @} */
#endif
|