/*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2014 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
-#include "folly/experimental/io/AsyncIO.h"
+#include <folly/experimental/io/AsyncIO.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstdio>
#include <memory>
#include <random>
+#include <thread>
#include <vector>
#include <glog/logging.h>
#include <gtest/gtest.h>
-#include "folly/experimental/io/FsUtil.h"
-#include "folly/ScopeGuard.h"
-#include "folly/String.h"
+#include <folly/experimental/io/FsUtil.h>
+#include <folly/ScopeGuard.h>
+#include <folly/String.h>
namespace fs = folly::fs;
using folly::AsyncIO;
}
void testReadsParallel(const std::vector<TestSpec>& specs,
- AsyncIO::PollMode pollMode) {
+ AsyncIO::PollMode pollMode,
+ bool multithreaded) {
AsyncIO aioReader(specs.size(), pollMode);
std::unique_ptr<AsyncIO::Op[]> ops(new AsyncIO::Op[specs.size()]);
std::vector<ManagedBuffer> bufs;
+ bufs.reserve(specs.size());
int fd = ::open(tempFile.path().c_str(), O_DIRECT | O_RDONLY);
PCHECK(fd != -1);
SCOPE_EXIT {
::close(fd);
};
+
+ std::vector<std::thread> threads;
+ if (multithreaded) {
+ threads.reserve(specs.size());
+ }
for (int i = 0; i < specs.size(); i++) {
bufs.push_back(allocateAligned(specs[i].size));
+ }
+ auto submit = [&] (int i) {
ops[i].pread(fd, bufs[i].get(), specs[i].size, specs[i].start);
aioReader.submit(&ops[i]);
+ };
+ for (int i = 0; i < specs.size(); i++) {
+ if (multithreaded) {
+ threads.emplace_back([&submit, i] { submit(i); });
+ } else {
+ submit(i);
+ }
+ }
+ for (auto& t : threads) {
+ t.join();
}
std::vector<bool> pending(specs.size(), true);
void testReads(const std::vector<TestSpec>& specs,
AsyncIO::PollMode pollMode) {
testReadsSerially(specs, pollMode);
- testReadsParallel(specs, pollMode);
+ testReadsParallel(specs, pollMode, false);
+ testReadsParallel(specs, pollMode, true);
testReadsQueued(specs, pollMode);
}
{
std::vector<TestSpec> v;
for (int i = 0; i < 1000; i++) {
- v.push_back({kAlign * i, kAlign});
+ v.push_back({off_t(kAlign * i), kAlign});
}
testReads(v, AsyncIO::NOT_POLLABLE);
}
{
std::vector<TestSpec> v;
for (int i = 0; i < 1000; i++) {
- v.push_back({kAlign * i, kAlign});
+ v.push_back({off_t(kAlign * i), kAlign});
}
testReads(v, AsyncIO::POLLABLE);
}