2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* CpuProfiler.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 11:25:16 +08:00
|
|
|
#include "fdbserver/TesterInterface.actor.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "fdbserver/QuietDatabase.h"
|
|
|
|
#include "fdbserver/ServerDBInfo.h"
|
2019-02-18 11:18:30 +08:00
|
|
|
#include "fdbserver/workloads/workloads.actor.h"
|
2021-03-11 02:06:03 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// A workload which starts the CPU profiler at a given time and duration on all workers in a cluster
|
|
|
|
struct CpuProfilerWorkload : TestWorkload {
|
2017-05-26 04:48:44 +08:00
|
|
|
bool success;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// How long to run the workload before starting the profiler
|
2017-05-26 04:48:44 +08:00
|
|
|
double initialDelay;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// How long the profiler should be run; if <= 0 then it will run until the workload's check function is called
|
2017-05-26 04:48:44 +08:00
|
|
|
double duration;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// What process classes should be profiled as part of this run?
|
|
|
|
// See Locality.h for the list of valid strings to provide.
|
2017-12-08 11:25:23 +08:00
|
|
|
vector<std::string> roles;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// A list of worker interfaces which have had profiling turned on
|
2017-05-26 04:48:44 +08:00
|
|
|
std::vector<WorkerInterface> profilingWorkers;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
CpuProfilerWorkload(WorkloadContext const& wcx) : TestWorkload(wcx) {
|
2017-05-26 04:48:44 +08:00
|
|
|
initialDelay = getOption(options, LiteralStringRef("initialDelay"), 0.0);
|
|
|
|
duration = getOption(options, LiteralStringRef("duration"), -1.0);
|
2017-12-08 11:25:23 +08:00
|
|
|
roles = getOption(options, LiteralStringRef("roles"), vector<std::string>());
|
2017-05-26 04:48:44 +08:00
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
std::string description() const override { return "CpuProfiler"; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<Void> setup(Database const& cx) override { return Void(); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Turns the profiler on or off
|
|
|
|
ACTOR Future<Void> updateProfiler(bool enabled, Database cx, CpuProfilerWorkload* self) {
|
|
|
|
if (self->clientId == 0) {
|
|
|
|
// If we are turning the profiler on, get a list of workers in the system
|
|
|
|
if (enabled) {
|
|
|
|
vector<WorkerDetails> _workers = wait(getWorkers(self->dbInfo));
|
2017-05-26 04:48:44 +08:00
|
|
|
vector<WorkerInterface> workers;
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < _workers.size(); i++) {
|
|
|
|
if (self->roles.empty() ||
|
|
|
|
std::find(self->roles.cbegin(), self->roles.cend(), _workers[i].processClass.toString()) !=
|
|
|
|
self->roles.cend()) {
|
2019-03-09 00:25:07 +08:00
|
|
|
workers.push_back(_workers[i].interf);
|
2017-12-08 11:25:23 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
self->profilingWorkers = workers;
|
|
|
|
}
|
|
|
|
|
|
|
|
state std::vector<Future<ErrorOr<Void>>> replies;
|
|
|
|
state int i;
|
2021-03-11 02:06:03 +08:00
|
|
|
// Send a ProfilerRequest to each worker
|
|
|
|
for (i = 0; i < self->profilingWorkers.size(); i++) {
|
2017-05-26 04:48:44 +08:00
|
|
|
ProfilerRequest req;
|
2017-10-28 08:20:19 +08:00
|
|
|
req.type = ProfilerRequest::Type::FLOW;
|
2017-10-12 05:13:16 +08:00
|
|
|
req.action = enabled ? ProfilerRequest::Action::ENABLE : ProfilerRequest::Action::DISABLE;
|
2021-03-11 02:06:03 +08:00
|
|
|
req.duration = 0; // unused
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The profiler output name will be the ip.port.prof
|
2019-02-27 10:04:03 +08:00
|
|
|
req.outputFile = StringRef(self->profilingWorkers[i].address().ip.toString() + "." +
|
|
|
|
format("%d", self->profilingWorkers[i].address().port) + ".profile.bin");
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-10-12 05:13:16 +08:00
|
|
|
replies.push_back(self->profilingWorkers[i].clientInterface.profiler.tryGetReply(req));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(waitForAll(replies));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Check that all workers succeeded if turning the profiler on
|
|
|
|
if (enabled)
|
|
|
|
for (i = 0; i < replies.size(); i++)
|
|
|
|
if (!replies[i].get().present())
|
2017-05-26 04:48:44 +08:00
|
|
|
self->success = false;
|
|
|
|
|
|
|
|
TraceEvent("DoneSignalingProfiler");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<Void> start(Database const& cx) override { return _start(cx, this); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> _start(Database cx, CpuProfilerWorkload* self) {
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(delay(self->initialDelay));
|
2021-03-11 02:06:03 +08:00
|
|
|
if (self->clientId == 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent("SignalProfilerOn");
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(timeoutError(self->updateProfiler(true, cx, self), 60.0));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// If a duration was given, let the duration elapse and then shut the profiler off
|
|
|
|
if (self->duration > 0) {
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(delay(self->duration));
|
2021-03-11 02:06:03 +08:00
|
|
|
if (self->clientId == 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent("SignalProfilerOff");
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(timeoutError(self->updateProfiler(false, cx, self), 60.0));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<bool> check(Database const& cx) override { return _check(cx, this); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<bool> _check(Database cx, CpuProfilerWorkload* self) {
|
|
|
|
// If no duration was given, then shut the profiler off now
|
|
|
|
if (self->duration <= 0) {
|
|
|
|
if (self->clientId == 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent("SignalProfilerOff");
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(timeoutError(self->updateProfiler(false, cx, self), 60.0));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return self->success;
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
void getMetrics(vector<PerfMetric>& m) override {}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
WorkloadFactory<CpuProfilerWorkload> CpuProfilerWorkloadFactory("CpuProfiler");
|