diff --git a/CHANGELOG.md b/CHANGELOG.md index f322b66..f803a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,24 @@ # Changelog +## 0.4.7 + +- **The processor's model (`machine::processor_model`).** The model as the + system names it: `model name` of `/proc/cpuinfo` on Linux, where a kernel + that names no model (most aarch64 and riscv64 processors) gives the + implementer and part, `0x41:0xd0c`; `machdep.cpu.brand_string` on macOS; the + registry's `ProcessorNameString` on Windows. An image and a wasm module are + told nothing. Machines of one architecture and one count of processors + differ in speed by up to 1.6 times on one class of hosted runner, and a + measurement compared across them is compared by this name. +- **A capture is taken in parts and is not made durable + (`stdio::capture::take`).** `take` gives what was written since `begin` or + the last `take`, and the stream stays diverted, so a test framework keeps one + capture for a program and takes each case's part from it. A capture no + longer flushes openkal's stream when it begins and ends: openkal buffers + nothing, and its flush of a file is the kernel's synchronisation (`fsync`), + which made a program of 510 cases take one second instead of three + hundredths under a runner that keeps each case's output. + ## 0.4.6 - **The text another program writes, as UTF-8 (`mcpplibs.os.text`).** A diff --git a/README.md b/README.md index a608b69..148919a 100644 --- a/README.md +++ b/README.md @@ -135,13 +135,13 @@ at a time). `windows::console_text_code_page()` and | `mcpplibs.os.fs` | `write_atomic`, `publish` (no replace), `append`, `sync`, `caps`, `lock`, links, `set_executable`, `stamp_of`, `identity_of`, `owner_of`, `writable`, `locate`, `absolute`, `names_a_place`, `canonical`, a bounded `read`; what a volume does, measured (`measure`, `volume_type`), and the operations that follow it (`place`, `exchange`); names in the host's rules without the file system: `generic`, `native`, `extended`, `is_absolute`, `same`, `within`; which names changed (`watcher`: inotify, kqueue, `ReadDirectoryChangesW`; a look when asked in an image and on Emscripten) | | `mcpplibs.os.process` | `spawn`, `run` (and with bounds: a hard bound, a grace, a drain, output limits), `exec`, a command line joined as it is for the command interpreter (`one_string`), pipes, both outputs on one pipe, output onto this program's error output, units and `end_unit`, declared units, observation and retirement, bound lifetime, bounded waits and reads; `self`, `describe`, `alive`, `threads` | | `mcpplibs.os.interrupt` | `requested`, `wait`, `end_as_requested`, `end_as_output_closed` | -| `mcpplibs.os.stdio` | the three streams as bytes; a bounded read of input; the output sent to the error stream for a while (`output_to_error`) | +| `mcpplibs.os.stdio` | the three streams as bytes; a bounded read of input; the output sent to the error stream for a while (`output_to_error`); what a stream is written, kept and taken in parts (`capture`) | | `mcpplibs.os.text` | the text another program writes, as UTF-8: `decoder` for an `encoding` (`utf8`, or `console`, the convention of Windows console programs), `valid_utf8` | | `mcpplibs.os.terminal` | `interactive`, `size`, `sequences`, `color`, `raw_mode` (with or without the interrupting keystroke), `foreground` | | `mcpplibs.os.dirs` | `home`, `config`, `data`, `state`, `cache`, `runtime`, `temp` | | `mcpplibs.os.ipc` | a listener and a connection at a name in a directory; `single_instance` | | `mcpplibs.os.net` | loopback TCP | -| `mcpplibs.os.machine` | the machine's architecture (through Rosetta and WoW64), memory and the memory available now, processors (logical, physical, of differing speed) | +| `mcpplibs.os.machine` | the machine's architecture (through Rosetta and WoW64), memory and the memory available now, processors (logical, physical, of differing speed), the processor's model | | `mcpplibs.os.random` | bytes from the system's source of unpredictable bytes; `hex` | | `mcpplibs.os.library` | shared libraries at run time: `open` (a path or a name the system searches for), `loaded` (one the process has, without loading it), a handle's `symbol` / `function`, `process_symbol` (as the loader resolves one), `suffix` | | `mcpplibs.os.linux` / `.macos` / `.windows` / `.posix` | platform modules: declared on every target, defined on their own and, where an image can answer, in an image; the system's object models, through which its services that are not functions are reached: `windows::com` (COM and the Windows Runtime) and `macos::objc` (the Objective-C runtime) | diff --git a/mcpp.toml b/mcpp.toml index 21004b6..e5bc9db 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -1,7 +1,7 @@ [package] namespace = "mcpplibs" name = "os" -version = "0.4.6" +version = "0.4.7" description = "mcpplibs.os: a C++ operating-system library over openkal --- processes, files, environment, terminals, directories and local IPC, with compile-time platform constants and a few platform modules." license = "Apache-2.0" authors = ["mcpplibs"] diff --git a/src/emscripten/machine.cpp b/src/emscripten/machine.cpp index 8ee67ae..174eca7 100644 --- a/src/emscripten/machine.cpp +++ b/src/emscripten/machine.cpp @@ -30,4 +30,6 @@ processor_count processors() { return c; } +std::optional processor_model() { return std::nullopt; } + } // namespace mcpplibs::os::machine diff --git a/src/linux/machine.cpp b/src/linux/machine.cpp index a4fa38f..2e3b2a6 100644 --- a/src/linux/machine.cpp +++ b/src/linux/machine.cpp @@ -84,6 +84,21 @@ bool hybrid(std::uint32_t logical) { return false; } +// The value of `key' in /proc/cpuinfo's first processor, or nothing. +std::optional cpuinfo(std::string_view key) { + std::ifstream in("/proc/cpuinfo", std::ios::binary); + for (std::string line; std::getline(in, line);) { + if (line.empty()) break; + if (!line.starts_with(key)) continue; + const auto colon = line.find(':'); + if (colon == std::string::npos || line.find_first_not_of(" \t", key.size()) != colon) continue; + const auto begin = line.find_first_not_of(' ', colon + 1); + if (begin == std::string::npos) return std::nullopt; + return line.substr(begin); + } + return std::nullopt; +} + } // namespace std::string arch() { @@ -115,4 +130,15 @@ processor_count processors() { return c; } +// x86 names the model; aarch64 and riscv64 kernels name the implementer and +// the part (the identification registers), from which a model is a table +// this library does not keep. +std::optional processor_model() { + if (auto model = cpuinfo("model name")) return model; + const auto implementer = cpuinfo("CPU implementer"), part = cpuinfo("CPU part"); + if (implementer && part) return *implementer + ":" + *part; + if (auto uarch = cpuinfo("uarch")) return uarch; + return std::nullopt; +} + } // namespace mcpplibs::os::machine diff --git a/src/machine.cppm b/src/machine.cppm index 6a29f30..0db40ab 100644 --- a/src/machine.cppm +++ b/src/machine.cppm @@ -33,4 +33,12 @@ struct processor_count { }; processor_count processors(); +// The processor's model as the system names it (`AMD EPYC 7763 64-Core +// Processor', `Apple M2'), so that two machines of one architecture and one +// count of processors are told apart where they differ in speed. Where the +// kernel names no model (Linux on most aarch64 processors) it is the +// processor's implementer and part, `0x41:0xd0c'; an image is told nothing +// (0.4.7). +std::optional processor_model(); + } // namespace mcpplibs::os::machine diff --git a/src/macos/machine.cpp b/src/macos/machine.cpp index a238ae5..6339fdc 100644 --- a/src/macos/machine.cpp +++ b/src/macos/machine.cpp @@ -52,4 +52,12 @@ processor_count processors() { return c; } +std::optional processor_model() { + char brand[256] = {}; + unsigned long len = sizeof brand; + if (::sysctlbyname("machdep.cpu.brand_string", brand, &len, nullptr, 0) != 0 || brand[0] == '\0') + return std::nullopt; + return std::string(brand); +} + } // namespace mcpplibs::os::machine diff --git a/src/openkal/machine.cpp b/src/openkal/machine.cpp index d85efe8..24c7883 100644 --- a/src/openkal/machine.cpp +++ b/src/openkal/machine.cpp @@ -22,4 +22,7 @@ processor_count processors() { return c; } +// An image is told nothing of the processor beneath it. +std::optional processor_model() { return std::nullopt; } + } // namespace mcpplibs::os::machine diff --git a/src/stdio.cpp b/src/stdio.cpp index 6a0f684..ddcc81e 100644 --- a/src/stdio.cpp +++ b/src/stdio.cpp @@ -91,9 +91,12 @@ output_to_error::~output_to_error() { sys::restore_output(saved_); } +// What the C library and the C++ streams buffer is written before the stream +// changes hands. openkal buffers nothing, and its flush of a file is the +// kernel's synchronisation (fsync): two per case made a 510-case program take +// one second instead of three hundredths, so a capture does not ask for it. result capture::begin(stream which) { std::fflush(nullptr); - (void)flush_output(); capture c; c.descriptor_ = which == stream::output ? 1 : 2; c.file_ = std::filesystem::temp_directory_path() @@ -108,13 +111,18 @@ result capture::begin(stream which) { } capture::capture(capture&& o) noexcept - : descriptor_(std::exchange(o.descriptor_, -1)), saved_(std::exchange(o.saved_, -1)), file_(std::move(o.file_)) {} + : descriptor_(std::exchange(o.descriptor_, -1)), saved_(std::exchange(o.saved_, -1)), file_(std::move(o.file_)), + taken_(std::exchange(o.taken_, 0)) { + o.file_.clear(); +} capture& capture::operator=(capture&& o) noexcept { if (this != &o) { end(); descriptor_ = std::exchange(o.descriptor_, -1); saved_ = std::exchange(o.saved_, -1); file_ = std::move(o.file_); + o.file_.clear(); + taken_ = std::exchange(o.taken_, 0); } return *this; } @@ -123,15 +131,29 @@ capture::~capture() { end(); } void capture::end() { if (saved_ < 0) return; std::fflush(nullptr); - (void)flush_output(); sys::restore_descriptor(descriptor_, std::exchange(saved_, -1)); } +std::string capture::read_rest() { + std::string kept; + std::ifstream in(file_, std::ios::binary); + if (!in) return kept; + in.seekg(static_cast(taken_)); + kept.assign(std::istreambuf_iterator(in), {}); + taken_ += kept.size(); + return kept; +} + +std::string capture::take() { + if (file_.empty()) return {}; + std::fflush(nullptr); + return read_rest(); +} + std::string capture::text() { if (file_.empty()) return {}; end(); - std::string kept; - if (std::ifstream in(file_, std::ios::binary); in) kept.assign(std::istreambuf_iterator(in), {}); + auto kept = read_rest(); std::error_code ec; std::filesystem::remove(file_, ec); file_.clear(); diff --git a/src/stdio.cppm b/src/stdio.cppm index a385dea..436d018 100644 --- a/src/stdio.cppm +++ b/src/stdio.cppm @@ -51,8 +51,11 @@ private: // WHAT THIS PROGRAM WRITES TO ONE OF ITS STANDARD STREAMS, KEPT (0.4.1): from // `begin' the stream is a file, so what the program writes there -- through // openkal, its C library or the C++ streams -- and what the programs it starts -// meanwhile write is kept; `text' (or destruction) puts the stream back and -// gives what was kept. A test reads what a program prints with it. +// meanwhile write is kept; `take' gives what was written since `begin' or the +// last `take' and keeps the stream diverted (0.4.7); `text' (or destruction) +// puts the stream back and gives the rest. A test reads what a program prints +// with it; a test framework takes each case's output from one capture. +// Neither makes the file durable: what is kept is read back by this process. // errc::not_supported where the streams cannot be rearranged (an image). enum class stream { output, error }; @@ -65,13 +68,18 @@ public: capture(const capture&) = delete; capture& operator=(const capture&) = delete; ~capture(); - // The stream put back, and what was written to it; empty after the first call. + // What was written since `begin' or the last `take'; the stream stays diverted. + std::string take(); + // The stream put back, and what was written to it since the last `take'; + // empty after the first call. std::string text(); private: void end(); + std::string read_rest(); int descriptor_ = -1; int saved_ = -1; std::filesystem::path file_; + std::uintmax_t taken_ = 0; }; } // namespace mcpplibs::os::stdio diff --git a/src/windows/machine.cpp b/src/windows/machine.cpp index 11c8790..d9e6f36 100644 --- a/src/windows/machine.cpp +++ b/src/windows/machine.cpp @@ -61,4 +61,29 @@ processor_count processors() { return c; } +// The registry's name of the first processor, as the system's own tools show +// it; advapi32 is reached at run time (src/windows/win32.h). +std::optional processor_model() { + using reg_get_value = int(__stdcall*)(void*, const okos_wchar*, const okos_wchar*, okos_dword, okos_dword*, + void*, okos_dword*); + const okos_handle advapi = LoadLibraryW(u"advapi32.dll"); + if (!advapi) return std::nullopt; + const auto get = reinterpret_cast(GetProcAddress(advapi, "RegGetValueW")); + if (!get) return std::nullopt; + void* const local_machine = reinterpret_cast(static_cast(static_cast(0x80000002))); + okos_wchar value[128] = {}; + okos_dword bytes = sizeof value; + if (get(local_machine, u"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", u"ProcessorNameString", + 0x2 /* RRF_RT_REG_SZ */, nullptr, value, &bytes) != 0) + return std::nullopt; + const std::u16string_view wide(value); + const int n = WideCharToMultiByte(65001, 0, wide.data(), static_cast(wide.size()), nullptr, 0, nullptr, nullptr); + if (n <= 0) return std::nullopt; + std::string name(static_cast(n), '\0'); + WideCharToMultiByte(65001, 0, wide.data(), static_cast(wide.size()), name.data(), n, nullptr, nullptr); + const auto begin = name.find_first_not_of(' '); + if (begin == std::string::npos) return std::nullopt; + return name.substr(begin, name.find_last_not_of(' ') - begin + 1); +} + } // namespace mcpplibs::os::machine diff --git a/tests/platform_test.cpp b/tests/platform_test.cpp index c156dfb..02dbf8d 100644 --- a/tests/platform_test.cpp +++ b/tests/platform_test.cpp @@ -45,6 +45,16 @@ UTest platform_tests{"platform", [] { if (!os::is_openkal && !os::is_emscripten) expect(os::machine::memory_available().value_or(0) > 0) << "and the memory available now"; } + { + // Every system this library defines names its processor; an image + // and a wasm module are told nothing of the machine beneath them. + const auto model = os::machine::processor_model(); + if constexpr (os::is_openkal || os::is_emscripten) + expect(!model) << "an image is not told the processor's model"; + else + expect(model && !model->empty() && model->front() != ' ' && model->back() != ' ') + << "the processor's model is named, without the spaces around it"; + } if constexpr (os::is_windows) { expect(os::windows::code_page() != 0) << "Windows: the ANSI code page is known"; (void)os::windows::long_paths_enabled(); diff --git a/tests/process_test.cpp b/tests/process_test.cpp index dcebdcf..9b04340 100644 --- a/tests/process_test.cpp +++ b/tests/process_test.cpp @@ -123,6 +123,9 @@ int run_errand(std::string_view errand) { } (void)os::stdio::write_error("kal|"); std::fputs("libc|", stderr); + // What was written so far is taken while the stream stays kept (0.4.7). + const auto first = e->take(); + (void)os::stdio::write_error("<" + first + ">"); auto o = copy_of_self("say"); o.out = os::process::stdio::error; o.err = os::process::stdio::inherit; @@ -419,11 +422,11 @@ UTest process_tests{"process", [] { auto d = os::process::run(copy("divert")); expect(d && d->out == "document" && d->err == "narration") << "output sent to the error stream for a while, and the document to the output as it was"; auto c = os::process::run(copy("capture")); - const bool kept = c && c->out == "[kal|libc|said]" && c->err == "after"; + const bool kept = c && c->out == "[said]" && c->err == "after"; const bool refused = c && c->out == "unsupported" && os::is_openkal; if (!kept && !refused && c) std::printf(" (the copy wrote '%s' and '%s')\n", c->out.c_str(), c->err.c_str()); - expect(kept || refused) << "a standard stream kept while it is captured, with what a started program wrote, then put back"; + expect(kept || refused) << "a standard stream kept while it is captured, with what a started program wrote, taken in two parts, then put back"; // What a program used: its own, and its waited-for children's together. auto mine = os::process::resources(os::process::of::self); expect(mine ? mine->peak_memory.value_or(0) > 0 : os::is_openkal || os::is_emscripten) << "this program's largest resident set is known";