Skip to content

Commit 3e74501

Browse files
committed
MB-70329, MB-70411: Merge 'couchbase/trinity' into morpheus
* couchbase/trinity: MB-70411: Include worker thread id in create failure message MB-70329: Add histogram for dispatch socket and notifications Change-Id: I86d8888892662b38e749ec38307dee2de32ba208
2 parents 521a310 + 883f303 commit 3e74501

File tree

7 files changed

+80
-18
lines changed

7 files changed

+80
-18
lines changed

daemon/connection.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -890,13 +890,15 @@ void Connection::processNotifiedCookie(
890890
Cookie& cookie,
891891
cb::engine_errc status,
892892
std::chrono::steady_clock::time_point scheduled) {
893+
using namespace std::chrono;
894+
const auto now = steady_clock::now();
895+
cookie_notification_histogram[thread.index].add(
896+
duration_cast<microseconds>(now - scheduled));
893897
Expects(cookie.isEwouldblock());
894898
cookie.setAiostat(status);
895899
cookie.clearEwouldblock();
896900
triggerCallback();
897-
cookie.getTracer().record(cb::tracing::Code::Notified,
898-
scheduled,
899-
std::chrono::steady_clock::now());
901+
cookie.getTracer().record(cb::tracing::Code::Notified, scheduled, now);
900902
}
901903

902904
void Connection::commandExecuted(Cookie& cookie) {

daemon/front_end_thread.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ nlohmann::json ClientConnectionDetails::to_json(
7070
*/
7171
static std::vector<FrontEndThread> threads;
7272
std::vector<Hdr1sfMicroSecHistogram> scheduler_info;
73+
std::vector<Hdr1sfMicroSecHistogram> dispatch_socket_histogram;
74+
std::vector<Hdr1sfMicroSecHistogram> cookie_notification_histogram;
7375

7476
/*
7577
* Number of worker threads that have finished setting themselves up.
@@ -307,11 +309,14 @@ void FrontEndThread::do_dispatch(SOCKET sfd,
307309
associate_initial_bucket(*c);
308310
success = true;
309311
} catch (const std::bad_alloc&) {
310-
LOG_WARNING_RAW("Failed to allocate memory for connection");
312+
LOG_WARNING_CTX("Failed to allocate memory for connection",
313+
{"worker_tid", index});
311314
} catch (const std::exception& error) {
312-
LOG_WARNING_CTX("Failed to create connection", {"error", error.what()});
315+
LOG_WARNING_CTX("Failed to create connection",
316+
{"error", error.what()},
317+
{"worker_tid", index});
313318
} catch (...) {
314-
LOG_WARNING_RAW("Failed to create connection");
319+
LOG_WARNING_CTX("Failed to create connection", {"worker_tid", index});
315320
}
316321

317322
if (!success) {
@@ -380,8 +385,13 @@ void FrontEndThread::dispatch(SOCKET sfd,
380385
last_thread = tid;
381386

382387
try {
383-
thread.eventBase.runInEventBaseThread(
384-
[&thread, sock = sfd, port = std::move(descr)]() {
388+
using namespace std::chrono;
389+
const auto scheduled = steady_clock::now();
390+
thread.eventBase.runInEventBaseThreadAlwaysEnqueue(
391+
[&thread, sock = sfd, port = std::move(descr), scheduled]() {
392+
const auto now = steady_clock::now();
393+
dispatch_socket_histogram[thread.index].add(
394+
duration_cast<microseconds>(now - scheduled));
385395
thread.do_dispatch(sock, port);
386396
});
387397
} catch (const std::bad_alloc& e) {
@@ -428,6 +438,8 @@ void worker_threads_init() {
428438
const auto nthr = Settings::instance().getNumWorkerThreads();
429439

430440
scheduler_info.resize(nthr);
441+
dispatch_socket_histogram.resize(nthr);
442+
cookie_notification_histogram.resize(nthr);
431443

432444
try {
433445
threads = std::vector<FrontEndThread>(nthr);

daemon/front_end_thread.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,3 +269,10 @@ struct FrontEndThread {
269269
class Hdr1sfMicroSecHistogram;
270270

271271
extern std::vector<Hdr1sfMicroSecHistogram> scheduler_info;
272+
273+
/// Histogram counting the time from a socket was accepted until
274+
/// it was dispatched on the correct front end thread
275+
extern std::vector<Hdr1sfMicroSecHistogram> dispatch_socket_histogram;
276+
277+
/// Histogram counting the time spent for cookie notifications
278+
extern std::vector<Hdr1sfMicroSecHistogram> cookie_notification_histogram;

daemon/protocol/mcbp/stats_context.cc

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,21 +137,33 @@ static cb::engine_errc stat_sched_executor(const std::string& arg,
137137
if (arg.empty()) {
138138
for (size_t ii = 0; ii < Settings::instance().getNumWorkerThreads();
139139
++ii) {
140-
append_stats(fmt::format("Thread-{}", ii),
140+
append_stats(fmt::format("Thread-{}-scheduler", ii),
141141
scheduler_info[ii].to_string(),
142142
cookie);
143+
append_stats(fmt::format("Thread-{}-dispatch-socket", ii),
144+
dispatch_socket_histogram[ii].to_string(),
145+
cookie);
146+
append_stats(fmt::format("Thread-{}-cookie-notification", ii),
147+
cookie_notification_histogram[ii].to_string(),
148+
cookie);
143149
}
144150
return cb::engine_errc::success;
145151
}
146152

147153
if (arg == "aggregate") {
148-
static const std::string key = {"Thread-aggregate"};
149-
Hdr1sfMicroSecHistogram histogram{};
150-
for (const auto& h : scheduler_info) {
151-
histogram += h;
152-
}
153-
// Add the stat
154-
append_stats(key, histogram.to_string(), cookie);
154+
auto add = [&cookie](std::string_view key,
155+
const std::vector<Hdr1sfMicroSecHistogram>& data) {
156+
Hdr1sfMicroSecHistogram histogram{};
157+
for (const auto& h : data) {
158+
histogram += h;
159+
}
160+
// Add the stat
161+
append_stats(key, histogram.to_string(), cookie);
162+
};
163+
add("Thread-aggregate-scheduler", scheduler_info);
164+
add("Thread-aggregate-dispatch-socket", dispatch_socket_histogram);
165+
add("Thread-aggregate-cookie-notification",
166+
cookie_notification_histogram);
155167
return cb::engine_errc::success;
156168
}
157169

daemon/stats.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "buckets.h"
1414
#include "external_auth_manager_thread.h"
15+
#include "front_end_thread.h"
1516
#include "mc_time.h"
1617
#include "mcaudit.h"
1718
#include "memcached.h"
@@ -303,6 +304,20 @@ cb::engine_errc server_prometheus_stats(
303304
server_prometheus_metering(collector);
304305
}
305306
} else {
307+
auto add =
308+
[&collector](
309+
auto key,
310+
const std::vector<Hdr1sfMicroSecHistogram>& data) {
311+
Hdr1sfMicroSecHistogram histogram{};
312+
for (const auto& h : data) {
313+
histogram += h;
314+
}
315+
collector.addStat(key, histogram);
316+
};
317+
add(cb::stats::Key::dispatch_socket_histogram,
318+
dispatch_socket_histogram);
319+
add(cb::stats::Key::cookie_notification_histogram,
320+
cookie_notification_histogram);
306321
try {
307322
auto instance = sigar::SigarIface::New();
308323
instance->iterate_threads([&kvCollector](auto tid,

statistics/stat_definitions.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,6 +2958,20 @@
29582958
"description": "Number of seconds the given thread has spent running according to the OS",
29592959
"added": "7.6.0"
29602960
},
2961+
{
2962+
"key": "dispatch_socket_histogram",
2963+
"description": "Histogram containing the time taken to dispatch a newly created socket to its worker thread",
2964+
"unit": "microseconds",
2965+
"type": "histogram",
2966+
"added": "7.6.10"
2967+
},
2968+
{
2969+
"key": "cookie_notification_histogram",
2970+
"description": "Histogram containing the time taken to schedule a notification for a cookie until it is run",
2971+
"unit": "microseconds",
2972+
"type": "histogram",
2973+
"added": "7.6.10"
2974+
},
29612975
{
29622976
"key": "conn_yields",
29632977
"description": "The total number all clients in this bucket yield due to consuming the number of ops allowed for the current timeslice",

tests/testapp/testapp_stats.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ TEST_P(StatsTest, TestSchedulerInfo) {
551551
++found;
552552
},
553553
"worker_thread_info");
554-
EXPECT_GT(4, found);
554+
EXPECT_GT(7, found);
555555
}
556556

557557
TEST_P(StatsTest, TestSchedulerInfo_Aggregate) {
@@ -562,7 +562,7 @@ TEST_P(StatsTest, TestSchedulerInfo_Aggregate) {
562562
++found;
563563
},
564564
"worker_thread_info aggregate");
565-
EXPECT_EQ(1, found);
565+
EXPECT_EQ(3, found);
566566
}
567567

568568
TEST_P(StatsTest, TestSchedulerInfo_InvalidSubcommand) {

0 commit comments

Comments
 (0)