| |
| |
| |
| |
|
|
| |
| |
|
|
| #include "workload.h" |
|
|
| namespace wasmfs { |
|
|
| bool ReadWrite::isSame(std::vector<char>& target) { |
| std::set<char> seen; |
|
|
| for (auto it = target.begin(); it != target.end() - 1; it++) { |
| seen.insert(*it); |
| if (seen.size() > 1) { |
| printf("Read the following incorrect data: %s\n", target.data()); |
| return false; |
| } |
| } |
| return true; |
| } |
|
|
| void ReadWrite::writer() { |
| while (!go) { |
| std::this_thread::yield(); |
| } |
|
|
| while (!stop) { |
| if (VERBOSE) { |
| std::cout << std::this_thread::get_id() << " is running.\n"; |
| } |
| auto current = rand.upTo(work.size()); |
|
|
| int bytesWritten = |
| pwrite(fd, work[current].c_str(), work[current].size(), 0); |
|
|
| assert(bytesWritten == work[current].size()); |
| } |
| } |
|
|
| void ReadWrite::reader() { |
| while (!go) { |
| std::this_thread::yield(); |
| } |
| if (VERBOSE) { |
| std::cout << std::this_thread::get_id() << " is running.\n"; |
| } |
|
|
| int size = work.begin()->size(); |
| |
| std::vector<char> buf(size + 1, 0); |
|
|
| while (!stop) { |
| std::fill(buf.begin(), buf.end(), 0); |
|
|
| int bytesRead = pread(fd, buf.data(), size, 0); |
| if (VERBOSE) { |
| printf("Read the following data: %s\n", buf.data()); |
| printf("Bytes read: %i", bytesRead); |
| printf("Expected: %lu", work.begin()->size()); |
| } |
| assert(bytesRead == work.begin()->size()); |
|
|
| assert(isSame(buf)); |
| } |
| } |
|
|
| void ReadWrite::execute() { |
| |
| work.resize(1 + rand.upTo(MAX_WORK)); |
|
|
| for (int i = 0; i < work.size(); i++) { |
| work[i] = rand.getSingleSymbolString(FILE_SIZE); |
| } |
| |
| fd = open("test", O_CREAT | O_RDWR, 0777); |
| assert(fd != -1); |
|
|
| |
| assert(pwrite(fd, work.begin()->c_str(), work.begin()->size(), 0) != -1); |
|
|
| |
| std::vector<std::thread> writers; |
| std::vector<std::thread> readers; |
|
|
| for (int i = 0; i < NUM_WRITERS; i++) { |
| std::thread th(&ReadWrite::writer, this); |
| writers.emplace_back(std::move(th)); |
| } |
|
|
| for (int i = 0; i < NUM_READERS; i++) { |
| std::thread th(&ReadWrite::reader, this); |
| readers.emplace_back(std::move(th)); |
| } |
|
|
| |
| |
| go.store(true); |
|
|
| std::this_thread::sleep_for(std::chrono::seconds(DURATION)); |
|
|
| |
| stop.store(true); |
|
|
| for (auto& th : writers) { |
| th.join(); |
| } |
|
|
| for (auto& th : readers) { |
| th.join(); |
| } |
|
|
| |
| unlink("test"); |
| close(fd); |
| } |
| } |
|
|