Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<F>`, `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) |
Expand Down
2 changes: 1 addition & 1 deletion mcpp.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
2 changes: 2 additions & 0 deletions src/emscripten/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ processor_count processors() {
return c;
}

std::optional<std::string> processor_model() { return std::nullopt; }

} // namespace mcpplibs::os::machine
26 changes: 26 additions & 0 deletions src/linux/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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() {
Expand Down Expand Up @@ -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<std::string> 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
8 changes: 8 additions & 0 deletions src/machine.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> processor_model();

} // namespace mcpplibs::os::machine
8 changes: 8 additions & 0 deletions src/macos/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ processor_count processors() {
return c;
}

std::optional<std::string> 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
3 changes: 3 additions & 0 deletions src/openkal/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ processor_count processors() {
return c;
}

// An image is told nothing of the processor beneath it.
std::optional<std::string> processor_model() { return std::nullopt; }

} // namespace mcpplibs::os::machine
32 changes: 27 additions & 5 deletions src/stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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> 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()
Expand All @@ -108,13 +111,18 @@ result<capture> 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;
}
Expand All @@ -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<std::streamoff>(taken_));
kept.assign(std::istreambuf_iterator<char>(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<char>(in), {});
auto kept = read_rest();
std::error_code ec;
std::filesystem::remove(file_, ec);
file_.clear();
Expand Down
14 changes: 11 additions & 3 deletions src/stdio.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions src/windows/machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> 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<reg_get_value>(GetProcAddress(advapi, "RegGetValueW"));
if (!get) return std::nullopt;
void* const local_machine = reinterpret_cast<void*>(static_cast<std::intptr_t>(static_cast<std::int32_t>(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<int>(wide.size()), nullptr, 0, nullptr, nullptr);
if (n <= 0) return std::nullopt;
std::string name(static_cast<std::size_t>(n), '\0');
WideCharToMultiByte(65001, 0, wide.data(), static_cast<int>(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
10 changes: 10 additions & 0 deletions tests/platform_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions tests/process_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 == "[<kal|libc|>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";
Expand Down
Loading