2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* AsyncFileWrite.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 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:18:30 +08:00
|
|
|
#include "fdbserver/workloads/workloads.actor.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "flow/ActorCollection.h"
|
|
|
|
#include "flow/SystemMonitor.h"
|
|
|
|
#include "fdbrpc/IAsyncFile.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbserver/workloads/AsyncFile.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
|
|
|
struct AsyncFileWriteWorkload : public AsyncFileWorkload {
|
|
|
|
// Buffer used to store what is being written
|
2017-05-26 04:48:44 +08:00
|
|
|
Reference<AsyncFileBuffer> writeBuffer;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The futures for the asynchronous write futures
|
2021-09-17 08:42:34 +08:00
|
|
|
std::vector<Future<Void>> writeFutures;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Number of writes to perform in parallel. Write tests are performed only if this is greater than zero and
|
|
|
|
// numParallelReads is zero
|
2017-05-26 04:48:44 +08:00
|
|
|
int numParallelWrites;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// The number of bytes written in each call of write
|
2017-05-26 04:48:44 +08:00
|
|
|
int writeSize;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Whether or not writes should be performed sequentially
|
2017-05-26 04:48:44 +08:00
|
|
|
bool sequential;
|
|
|
|
|
|
|
|
double averageCpuUtilization;
|
|
|
|
PerfIntCounter bytesWritten;
|
|
|
|
|
|
|
|
AsyncFileWriteWorkload(WorkloadContext const& wcx)
|
2021-07-25 02:20:51 +08:00
|
|
|
: AsyncFileWorkload(wcx), writeBuffer(nullptr), bytesWritten("Bytes Written") {
|
2017-05-26 04:48:44 +08:00
|
|
|
numParallelWrites = getOption(options, LiteralStringRef("numParallelWrites"), 0);
|
|
|
|
writeSize = getOption(options, LiteralStringRef("writeSize"), _PAGE_SIZE);
|
|
|
|
fileSize = getOption(options, LiteralStringRef("fileSize"), 10002432);
|
|
|
|
sequential = getOption(options, LiteralStringRef("sequential"), true);
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
std::string description() const override { return "AsyncFileWrite"; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<Void> setup(Database const& cx) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (enabled)
|
2017-05-26 04:48:44 +08:00
|
|
|
return _setup(this);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> _setup(AsyncFileWriteWorkload* self) {
|
|
|
|
// Allow only 4K aligned writes if using unbuffered IO
|
|
|
|
if (self->unbufferedIO && self->writeSize % AsyncFileWorkload::_PAGE_SIZE != 0)
|
|
|
|
self->writeSize = std::max(AsyncFileWorkload::_PAGE_SIZE,
|
|
|
|
self->writeSize - self->writeSize % AsyncFileWorkload::_PAGE_SIZE);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Allocate the write buffer
|
2017-05-26 04:48:44 +08:00
|
|
|
self->writeBuffer = self->allocateBuffer(self->writeSize);
|
|
|
|
|
|
|
|
int64_t initialSize = self->fileSize;
|
2021-03-11 02:06:03 +08:00
|
|
|
if (self->sequential)
|
2017-05-26 04:48:44 +08:00
|
|
|
initialSize = 0;
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(self->openFile(self, IAsyncFile::OPEN_READWRITE, 0666, initialSize));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
int64_t fileSize = wait(self->fileHandle->file->size());
|
2021-03-11 02:06:03 +08:00
|
|
|
if (fileSize != 0)
|
2017-05-26 04:48:44 +08:00
|
|
|
self->fileSize = fileSize;
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<Void> start(Database const& cx) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (enabled)
|
2017-05-26 04:48:44 +08:00
|
|
|
return _start(this);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> _start(AsyncFileWriteWorkload* self) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state StatisticsState statState;
|
|
|
|
customSystemMonitor("AsyncFile Metrics", &statState);
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(timeout(self->runWriteTest(self), self->testDuration, Void()));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
SystemStatistics stats = customSystemMonitor("AsyncFile Metrics", &statState);
|
|
|
|
self->averageCpuUtilization = stats.processCPUSeconds / stats.elapsed;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Try to let the IO complete so we can clean up after them
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(timeout(waitForAll(self->writeFutures), 10, Void()));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> runWriteTest(AsyncFileWriteWorkload* self) {
|
2017-05-26 04:48:44 +08:00
|
|
|
state int64_t offset = self->fileSize;
|
|
|
|
state Future<Void> prevSync = Void();
|
2021-03-11 02:06:03 +08:00
|
|
|
loop {
|
|
|
|
// Write chunks of the file using different actors
|
|
|
|
for (int i = 0; i < self->numParallelWrites; i++) {
|
|
|
|
// Perform the write. Don't allow it to be cancelled (because the underlying IO may not be cancellable)
|
|
|
|
// and don't allow objects that the write uses to be deleted
|
|
|
|
self->writeFutures.push_back(uncancellable(holdWhile(
|
|
|
|
self->fileHandle,
|
|
|
|
holdWhile(self->writeBuffer,
|
|
|
|
self->fileHandle->file->write(self->writeBuffer->buffer,
|
|
|
|
std::min((int64_t)self->writeSize, self->fileSize - offset),
|
|
|
|
offset)))));
|
|
|
|
|
|
|
|
if (self->sequential) {
|
2017-05-26 04:48:44 +08:00
|
|
|
offset += self->writeSize;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// If the file is exhausted, start over at the beginning
|
|
|
|
if (offset >= self->fileSize)
|
2017-05-26 04:48:44 +08:00
|
|
|
offset = 0;
|
2021-03-11 02:06:03 +08:00
|
|
|
} else if (self->unbufferedIO)
|
|
|
|
offset = (int64_t)(deterministicRandom()->random01() * (self->fileSize - 1) /
|
|
|
|
AsyncFileWorkload::_PAGE_SIZE) *
|
|
|
|
AsyncFileWorkload::_PAGE_SIZE;
|
2017-05-26 04:48:44 +08:00
|
|
|
else
|
2019-05-11 05:01:52 +08:00
|
|
|
offset = (int64_t)(deterministicRandom()->random01() * (self->fileSize - 1));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(waitForAll(self->writeFutures));
|
|
|
|
wait(prevSync);
|
2017-05-26 04:48:44 +08:00
|
|
|
prevSync = self->fileHandle->file->sync();
|
|
|
|
|
|
|
|
self->writeFutures.clear();
|
|
|
|
|
|
|
|
self->bytesWritten += self->writeSize * self->numParallelWrites;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
void getMetrics(std::vector<PerfMetric>& m) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (enabled) {
|
2021-08-30 06:38:21 +08:00
|
|
|
m.emplace_back("Bytes written/sec", bytesWritten.getValue() / testDuration, Averaged::False);
|
|
|
|
m.emplace_back("Average CPU Utilization (Percentage)", averageCpuUtilization * 100, Averaged::False);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
WorkloadFactory<AsyncFileWriteWorkload> AsyncFileWriteWorkloadFactory("AsyncFileWrite");
|