MySQL 8.4.0
Source Code Documentation
tracer.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2022, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef ROUTING_TRACER_INCLUDED
27#define ROUTING_TRACER_INCLUDED
28
29#include <chrono>
30#include <cstdio> // fputs, stderr
31#include <cstdlib>
32#include <iomanip>
33#include <optional>
34#include <sstream>
35#include <string>
36
37/**
38 * traces the timestamps of events in a stderr log.
39 *
40 * If enabled, the tracer outputs:
41 *
42 * - duration since Tracer was created
43 * - duration since last Event
44 * - direction (from|to client|server)
45 * - the event
46 *
47 * to stderr.
48 */
49class Tracer {
50 public:
51 using clock_type = std::chrono::steady_clock;
52
53 Tracer() = default;
54
55 explicit Tracer(bool enabled) : enabled_{enabled} {
56 if (!enabled_) return;
57
58 last_ = start_ = clock_type::now();
59 }
60
61 class Event {
62 public:
63 class Stage {
64 public:
65 Stage(std::string name) : name_(name) {}
66
67 std::string name() const { return name_; }
68
69 private:
70 std::string name_;
71 };
72
73 enum class Direction {
80 };
81
82 enum class Wait {
83 kRead,
84 kSend,
85 };
86
87 std::optional<Direction> direction() const { return direction_; }
88 std::optional<Stage> stage() const { return stage_; }
89
90 Event &stage(std::string_view s) {
91 stage_ = std::string(s);
92 return *this;
93 }
94
97 return *this;
98 }
99
100 private:
101 std::optional<Direction> direction_;
102 std::optional<Stage> stage_;
103 };
104
106 using Direction = Event::Direction;
107
108 switch (direction) {
109 case Direction::kClientToRouter:
110 return "c->r ";
111 case Direction::kRouterToClient:
112 return "c<-r ";
113 case Direction::kRouterToServer:
114 return " r->s";
115 case Direction::kServerToRouter:
116 return " r<-s";
117 case Direction::kServerClose:
118 return " r..s";
119 case Direction::kClientClose:
120 return "c..r ";
121 }
122
123 return " ";
124 }
125
126 static std::string stage(Event::Stage st) { return st.name(); }
127
128 void trace(Event e) {
129 if (!enabled_) return;
130
131 auto now = clock_type::now();
132
133 auto delta_now =
134 std::chrono::duration_cast<std::chrono::microseconds>(now - start_);
135 auto delta_last =
136 std::chrono::duration_cast<std::chrono::microseconds>(now - last_);
137
139 oss << "/* " << std::setw(10) << delta_now.count() << " us ("
140 << std::showpos << std::setw(10) << delta_last.count() << std::noshowpos
141 << " us) */ ";
142 if (e.direction()) {
143 oss << direction(*(e.direction()));
144 } else {
145 oss << " ";
146 }
147
148 oss << " ";
149
150 if (e.stage()) {
151 oss << stage(*(e.stage()));
152 } else {
153 oss << "none";
154 }
155
156 oss << "\n";
157
158 fputs(oss.str().c_str(), stderr);
159
160 last_ = now;
161 }
162
163 explicit operator bool() const { return enabled_; }
164
165 private:
166 bool enabled_{false};
167
168 clock_type::time_point start_{};
169 clock_type::time_point last_{};
170};
171
172#endif
Definition: tracer.h:63
std::string name_
Definition: tracer.h:70
std::string name() const
Definition: tracer.h:67
Stage(std::string name)
Definition: tracer.h:65
Definition: tracer.h:61
Direction
Definition: tracer.h:73
std::optional< Direction > direction() const
Definition: tracer.h:87
std::optional< Direction > direction_
Definition: tracer.h:101
std::optional< Stage > stage_
Definition: tracer.h:102
Wait
Definition: tracer.h:82
Event & stage(std::string_view s)
Definition: tracer.h:90
std::optional< Stage > stage() const
Definition: tracer.h:88
Event & direction(Direction dir)
Definition: tracer.h:95
traces the timestamps of events in a stderr log.
Definition: tracer.h:49
clock_type::time_point start_
Definition: tracer.h:168
static std::string direction(Event::Direction direction)
Definition: tracer.h:105
bool enabled_
Definition: tracer.h:166
clock_type::time_point last_
Definition: tracer.h:169
static std::string stage(Event::Stage st)
Definition: tracer.h:126
Tracer(bool enabled)
Definition: tracer.h:55
std::chrono::steady_clock clock_type
Definition: tracer.h:51
Tracer()=default
void trace(Event e)
Definition: tracer.h:128
std::string dir
Double write files location.
Definition: buf0dblwr.cc:77
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2870
required bool enabled
Definition: replication_group_member_actions.proto:33