foundationdb/fdbserver/workloads/CpuProfiler.actor.cpp

138 lines
4.8 KiB
C++
Raw Normal View History

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
*
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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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.
*/
#include "fdbserver/TesterInterface.actor.h"
2017-05-26 04:48:44 +08:00
#include "fdbserver/QuietDatabase.h"
#include "fdbserver/ServerDBInfo.h"
#include "fdbserver/workloads/workloads.actor.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +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;
// How long to run the workload before starting the profiler
2017-05-26 04:48:44 +08:00
double initialDelay;
// 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;
// What process classes should be profiled as part of this run?
// See Locality.h for the list of valid strings to provide.
vector<std::string> roles;
// A list of worker interfaces which have had profiling turned on
2017-05-26 04:48:44 +08:00
std::vector<WorkerInterface> profilingWorkers;
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);
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
// 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;
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()) {
workers.push_back(_workers[i].interf);
}
}
2017-05-26 04:48:44 +08:00
self->profilingWorkers = workers;
}
state std::vector<Future<ErrorOr<Void>>> replies;
state int i;
// Send a ProfilerRequest to each worker
for (i = 0; i < self->profilingWorkers.size(); i++) {
2017-05-26 04:48:44 +08:00
ProfilerRequest req;
req.type = ProfilerRequest::Type::FLOW;
req.action = enabled ? ProfilerRequest::Action::ENABLE : ProfilerRequest::Action::DISABLE;
req.duration = 0; // unused
2017-05-26 04:48:44 +08:00
// The profiler output name will be the ip.port.prof
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
replies.push_back(self->profilingWorkers[i].clientInterface.profiler.tryGetReply(req));
2017-05-26 04:48:44 +08:00
}
wait(waitForAll(replies));
2017-05-26 04:48:44 +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
ACTOR Future<Void> _start(Database cx, CpuProfilerWorkload* self) {
wait(delay(self->initialDelay));
if (self->clientId == 0)
2017-05-26 04:48:44 +08:00
TraceEvent("SignalProfilerOn");
wait(timeoutError(self->updateProfiler(true, cx, self), 60.0));
2017-05-26 04:48:44 +08:00
// If a duration was given, let the duration elapse and then shut the profiler off
if (self->duration > 0) {
wait(delay(self->duration));
if (self->clientId == 0)
2017-05-26 04:48:44 +08:00
TraceEvent("SignalProfilerOff");
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
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");
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");