2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* AsyncFile.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"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbserver/workloads/AsyncFile.actor.h"
|
2018-08-11 06:18:24 +08:00
|
|
|
#include "flow/actorcompiler.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
// class RandomByteGenerator
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
RandomByteGenerator::RandomByteGenerator() {
|
|
|
|
BUF_SIZE = 16 * (1 << 20);
|
2017-05-26 04:48:44 +08:00
|
|
|
b1 = new char[BUF_SIZE];
|
2021-03-11 02:06:03 +08:00
|
|
|
for (int i = 0; i < BUF_SIZE / sizeof(uint32_t); i++)
|
2019-05-11 05:01:52 +08:00
|
|
|
((uint32_t*)b1)[i] = deterministicRandom()->randomUInt32();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
RandomByteGenerator::~RandomByteGenerator() {
|
2017-05-26 04:48:44 +08:00
|
|
|
delete b1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// only works if buf and bytes are 8-byte aligned
|
2021-03-11 02:06:03 +08:00
|
|
|
void RandomByteGenerator::writeRandomBytesToBuffer(void* buf, int bytes) {
|
|
|
|
ASSERT(bytes < BUF_SIZE - 1);
|
2017-05-26 04:48:44 +08:00
|
|
|
int o1, o2;
|
2021-03-11 02:06:03 +08:00
|
|
|
o1 = deterministicRandom()->randomInt(0, BUF_SIZE - bytes) / 8;
|
2017-05-26 04:48:44 +08:00
|
|
|
do {
|
2021-03-11 02:06:03 +08:00
|
|
|
o2 = deterministicRandom()->randomInt(0, BUF_SIZE - bytes) / 8;
|
|
|
|
} while (o1 == o2);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
int64_t* out64 = (int64_t*)buf;
|
|
|
|
int64_t* in64 = (int64_t*)b1;
|
|
|
|
int n = bytes / 8;
|
|
|
|
for (int b = 0; b < n; b++)
|
|
|
|
out64[b] = in64[o1 + b] ^ in64[o2 + b];
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// for (int b=0;b<bytes;b++){
|
|
|
|
//((char*)buf)[b] = b1[o1+b] ^ b1[o2+b];
|
2017-05-26 04:48:44 +08:00
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
//// Asynch File Workload
|
|
|
|
|
|
|
|
const int AsyncFileWorkload::_PAGE_SIZE = 4096;
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
AsyncFileWorkload::AsyncFileWorkload(WorkloadContext const& wcx) : TestWorkload(wcx), fileHandle(nullptr) {
|
|
|
|
// Only run on one client
|
2017-05-26 04:48:44 +08:00
|
|
|
enabled = clientId == 0;
|
|
|
|
testDuration = getOption(options, LiteralStringRef("testDuration"), 10.0);
|
|
|
|
unbufferedIO = getOption(options, LiteralStringRef("unbufferedIO"), false);
|
|
|
|
uncachedIO = getOption(options, LiteralStringRef("uncachedIO"), false);
|
|
|
|
fillRandom = getOption(options, LiteralStringRef("fillRandom"), false);
|
|
|
|
path = getOption(options, LiteralStringRef("fileName"), LiteralStringRef("")).toString();
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Reference<AsyncFileBuffer> AsyncFileWorkload::allocateBuffer(size_t size) {
|
2020-11-07 15:50:55 +08:00
|
|
|
return makeReference<AsyncFileBuffer>(size, unbufferedIO);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
Future<bool> AsyncFileWorkload::check(Database const& cx) {
|
2017-05-26 04:48:44 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Allocates a buffer of a given size. If necessary, the buffer will be aligned to 4K
|
|
|
|
AsyncFileBuffer::AsyncFileBuffer(size_t size, bool aligned) {
|
|
|
|
if (aligned) {
|
2017-05-26 04:48:44 +08:00
|
|
|
#ifdef WIN32
|
|
|
|
buffer = (unsigned char*)_aligned_malloc(size, AsyncFileWorkload::_PAGE_SIZE);
|
|
|
|
#else
|
2021-03-11 02:06:03 +08:00
|
|
|
if (posix_memalign((void**)&buffer, AsyncFileWorkload::_PAGE_SIZE, size) != 0)
|
2020-08-19 05:18:50 +08:00
|
|
|
buffer = nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
#endif
|
2021-03-11 02:06:03 +08:00
|
|
|
} else
|
2017-05-26 04:48:44 +08:00
|
|
|
buffer = (unsigned char*)malloc(size);
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (buffer == nullptr) {
|
2017-05-26 04:48:44 +08:00
|
|
|
TraceEvent(SevError, "TestFailure").detail("Reason", "Insufficient memory");
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(buffer, 0, size);
|
|
|
|
this->aligned = aligned;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
// Special logic needed here to work with _aligned_malloc on windows
|
|
|
|
AsyncFileBuffer::~AsyncFileBuffer() {
|
2017-05-26 04:48:44 +08:00
|
|
|
#ifdef WIN32
|
2021-03-11 02:06:03 +08:00
|
|
|
if (aligned) {
|
2017-05-26 04:48:44 +08:00
|
|
|
_aligned_free(buffer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
AsyncFileHandle::AsyncFileHandle(Reference<IAsyncFile> file, std::string path, bool temporary) {
|
2017-05-26 04:48:44 +08:00
|
|
|
this->file = file;
|
|
|
|
this->path = path;
|
|
|
|
this->temporary = temporary;
|
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
AsyncFileHandle::~AsyncFileHandle() {
|
|
|
|
if (temporary)
|
2017-05-26 04:48:44 +08:00
|
|
|
deleteFile(path);
|
|
|
|
}
|