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