Skip to content

Commit c248406

Browse files
committed
Merge remote-tracking branch 'couchbase/morpheus' to master
* couchbase/morpheus: MB-70411: Include worker thread id in create failure message MB-70329: Add histogram for dispatch socket and notifications Change-Id: I412dacc0d85aa17c2a62d54b5744a02ffdc2cc8a
2 parents 6769d0d + 3e74501 commit c248406

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
@@ -916,13 +916,15 @@ void Connection::processNotifiedCookie(
916916
Cookie& cookie,
917917
cb::engine_errc status,
918918
std::chrono::steady_clock::time_point scheduled) {
919+
using namespace std::chrono;
920+
const auto now = steady_clock::now();
921+
cookie_notification_histogram[thread.index].add(
922+
duration_cast<microseconds>(now - scheduled));
919923
Expects(cookie.isEwouldblock());
920924
cookie.setAiostat(status);
921925
cookie.clearEwouldblock();
922926
triggerCallback();
923-
cookie.getTracer().record(cb::tracing::Code::Notified,
924-
scheduled,
925-
std::chrono::steady_clock::now());
927+
cookie.getTracer().record(cb::tracing::Code::Notified, scheduled, now);
926928
}
927929

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

daemon/front_end_thread.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ nlohmann::json ClientConnectionDetails::to_json(
7272
*/
7373
static std::vector<FrontEndThread> threads;
7474
std::vector<Hdr1sfMicroSecHistogram> scheduler_info;
75+
std::vector<Hdr1sfMicroSecHistogram> dispatch_socket_histogram;
76+
std::vector<Hdr1sfMicroSecHistogram> cookie_notification_histogram;
7577

7678
/*
7779
* Number of worker threads that have finished setting themselves up.
@@ -309,11 +311,14 @@ void FrontEndThread::do_dispatch(SOCKET sfd,
309311
BucketManager::instance().associateInitialBucket(*c);
310312
success = true;
311313
} catch (const std::bad_alloc&) {
312-
LOG_WARNING_RAW("Failed to allocate memory for connection");
314+
LOG_WARNING_CTX("Failed to allocate memory for connection",
315+
{"worker_tid", index});
313316
} catch (const std::exception& error) {
314-
LOG_WARNING_CTX("Failed to create connection", {"error", error.what()});
317+
LOG_WARNING_CTX("Failed to create connection",
318+
{"error", error.what()},
319+
{"worker_tid", index});
315320
} catch (...) {
316-
LOG_WARNING_RAW("Failed to create connection");
321+
LOG_WARNING_CTX("Failed to create connection", {"worker_tid", index});
317322
}
318323

319324
if (!success) {
@@ -382,8 +387,13 @@ void FrontEndThread::dispatch(SOCKET sfd,
382387
last_thread = tid;
383388

384389
try {
385-
thread.eventBase.runInEventBaseThread(
386-
[&thread, sock = sfd, port = std::move(descr)]() {
390+
using namespace std::chrono;
391+
const auto scheduled = steady_clock::now();
392+
thread.eventBase.runInEventBaseThreadAlwaysEnqueue(
393+
[&thread, sock = sfd, port = std::move(descr), scheduled]() {
394+
const auto now = steady_clock::now();
395+
dispatch_socket_histogram[thread.index].add(
396+
duration_cast<microseconds>(now - scheduled));
387397
thread.do_dispatch(sock, port);
388398
});
389399
} catch (const std::bad_alloc& e) {
@@ -427,6 +437,8 @@ void worker_threads_init() {
427437
const auto nthr = Settings::instance().getNumWorkerThreads();
428438

429439
scheduler_info.resize(nthr);
440+
dispatch_socket_histogram.resize(nthr);
441+
cookie_notification_histogram.resize(nthr);
430442

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

daemon/front_end_thread.h

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

277277
extern std::vector<Hdr1sfMicroSecHistogram> scheduler_info;
278+
279+
/// Histogram counting the time from a socket was accepted until
280+
/// it was dispatched on the correct front end thread
281+
extern std::vector<Hdr1sfMicroSecHistogram> dispatch_socket_histogram;
282+
283+
/// Histogram counting the time spent for cookie notifications
284+
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
@@ -13,6 +13,7 @@
1313
#include "bucket_manager.h"
1414
#include "buckets.h"
1515
#include "external_auth_manager_thread.h"
16+
#include "front_end_thread.h"
1617
#include "mc_time.h"
1718
#include "mcaudit.h"
1819
#include "memcached.h"
@@ -245,6 +246,20 @@ cb::engine_errc server_prometheus_stats(
245246
server_prometheus_metering(collector);
246247
}
247248
} else {
249+
auto add =
250+
[&collector](
251+
auto key,
252+
const std::vector<Hdr1sfMicroSecHistogram>& data) {
253+
Hdr1sfMicroSecHistogram histogram{};
254+
for (const auto& h : data) {
255+
histogram += h;
256+
}
257+
collector.addStat(key, histogram);
258+
};
259+
add(cb::stats::Key::dispatch_socket_histogram,
260+
dispatch_socket_histogram);
261+
add(cb::stats::Key::cookie_notification_histogram,
262+
cookie_notification_histogram);
248263
try {
249264
auto instance = sigar::SigarIface::New();
250265
instance->iterate_threads([&kvCollector](auto tid,

statistics/stat_definitions.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3017,6 +3017,20 @@
30173017
"description": "Number of seconds the given thread has spent running according to the OS",
30183018
"added": "7.6.0"
30193019
},
3020+
{
3021+
"key": "dispatch_socket_histogram",
3022+
"description": "Histogram containing the time taken to dispatch a newly created socket to its worker thread",
3023+
"unit": "microseconds",
3024+
"type": "histogram",
3025+
"added": "7.6.10"
3026+
},
3027+
{
3028+
"key": "cookie_notification_histogram",
3029+
"description": "Histogram containing the time taken to schedule a notification for a cookie until it is run",
3030+
"unit": "microseconds",
3031+
"type": "histogram",
3032+
"added": "7.6.10"
3033+
},
30203034
{
30213035
"key": "conn_yields",
30223036
"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
@@ -644,7 +644,7 @@ TEST_P(StatsTest, TestSchedulerInfo) {
644644
++found;
645645
},
646646
"worker_thread_info");
647-
EXPECT_GT(4, found);
647+
EXPECT_GT(7, found);
648648
}
649649

650650
TEST_P(StatsTest, TestSchedulerInfo_Aggregate) {
@@ -655,7 +655,7 @@ TEST_P(StatsTest, TestSchedulerInfo_Aggregate) {
655655
++found;
656656
},
657657
"worker_thread_info aggregate");
658-
EXPECT_EQ(1, found);
658+
EXPECT_EQ(3, found);
659659
}
660660

661661
TEST_P(StatsTest, TestSchedulerInfo_InvalidSubcommand) {

0 commit comments

Comments
 (0)