okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge the AMD HIP backend (PR #121 by Konstantinos Korres, rebased onto 0.1.24) and its setup support36ef9d6e6

main
2.7 KiB60 linesraw
1#include "strata/platform/direct_file.hpp"
2#include <cstdlib>
3#include <cstring>
4#include <fstream>
5#include <iostream>
6#include <stdexcept>
7#include <string>
8#include <thread>
9#include <vector>
10#include <unistd.h>
11using strata::platform::DirectFile;
12using strata::platform::Completion;
13static void check(bool ok, const char* why) { if (!ok) throw std::runtime_error(why); }
14int main() {
15    char name[] = "/tmp/strata-direct-test-XXXXXX";
16    int fd = mkstemp(name);
17    if (fd < 0) return 1;
18    close(fd);
19    void* memory = DirectFile::alloc_aligned(4096 * 18);
20    try {
21        check(memory != nullptr, "buffer allocation");
22        { std::ofstream f(name, std::ios::binary); std::string bytes(4096 * 16 + 17, 'x'); f.write(bytes.data(), bytes.size()); }
23        DirectFile f;
24        std::string error;
25        setenv("STRATA_IO_THREADS", "4", 1);   // the pool's size (out-of-range values are clamped to 1..64)
26        check(f.open(name, error), error.c_str());
27        check(!f.submit(1, memory, 4096, 0, error), "unaligned request accepted");
28        for (unsigned i = 0; i < 17; ++i)
29            check(f.submit(i * 4096, static_cast<char*>(memory) + i * 4096, 4096, i, error), error.c_str());
30        std::vector<bool> seen(17);
31        unsigned count = 0;
32        while (count < 17) {
33            Completion c[17]; int n = f.wait(c, 17, 5000);
34            check(n > 0, "completion timeout");
35            for (int i = 0; i < n; ++i) {
36                check(c[i].tag < 17 && !seen[c[i].tag], "wrong or repeated completion");
37                seen[c[i].tag] = true; ++count;
38                check(c[i].ok && c[i].bytes == (c[i].tag == 16 ? 17u : 4096u), "incorrect full/EOF completion");
39                check(static_cast<char*>(memory)[c[i].tag * 4096] == 'x', "read data mismatch");
40            }
41        }
42        std::jthread waker([&] { f.wake(); });
43        Completion c; check(f.wait(&c, 1, 5000) == 1 && c.tag == DirectFile::WAKE_TAG, "wake lost");
44        waker.join();
45        check(f.submit(0, memory, 4096, 99, error), error.c_str());
46        f.close(); // Must drain outstanding buffer writes before returning.
47        std::memset(memory, 0, 4096);
48        check(f.open(name, error), error.c_str());
49        check(f.wait(&c, 1, 0) == 0, "stale completion on reopen");
50        check(f.submit(0, memory, 4096, 100, error), error.c_str());
51        check(f.wait(&c, 1, 5000) == 1 && c.ok && c.tag == 100, "reopened read failed");
52        f.close();
53        DirectFile::free_aligned(memory); unlink(name);
54        std::cout << "direct_file_async: passed queued reads, short EOF, wake, close drain, reopen\n";
55        return 0;
56    } catch (const std::exception& e) {
57        DirectFile::free_aligned(memory); unlink(name);
58        std::cerr << e.what() << '\n'; return 1;
59    }
60}