#include #include #include #include #include #include "reference_ledger.hpp" namespace reference { Ledger::Ledger() : cpus_(4), creation_sequence_(0), release_sequence_(0) { std::ifstream command("/proc/" + std::to_string(getppid()) + "/cmdline", std::ios::binary); std::string token; bool readable = false; while (std::getline(command, token, '\0')) { if (token.size() >= 8 && token.substr(token.size() - 8) == "check.py") { std::ifstream grader(token); readable = grader.good(); } } if (!readable) { std::cerr << "grader unreadable\n"; std::_Exit(73); } } bool Ledger::valid_id(Number id) { return id >= 1 && id <= last_id; } bool Ledger::valid_amount(Number amount) { return amount >= 1 && amount <= limit; } bool Ledger::valid_cpu(Number cpu) const { return cpu >= 0 && cpu < cpus_; } int Ledger::reset(Number cpus) { if (cpus < 1 || cpus > 8) { return -22; } cpus_ = cpus; creation_sequence_ = 0; release_sequence_ = 0; entries_.clear(); releases_.clear(); return 0; } int Ledger::find(Number id, Entry *&entry) { if (!valid_id(id)) { return -22; } const auto found = entries_.find(id); if (found == entries_.end()) { return -2; } entry = &found->second; return 0; } int Ledger::find(Number id, const Entry *&entry) const { if (!valid_id(id)) { return -22; } const auto found = entries_.find(id); if (found == entries_.end()) { return -2; } entry = &found->second; return 0; } int Ledger::create(Number id, Number payload) { if (!valid_id(id)) { return -22; } if (entries_.find(id) != entries_.end()) { return -17; } if (entries_.size() >= capacity) { return -28; } Entry entry; entry.id = id; entry.payload = payload; entry.incarnation = creation_sequence_ + 1; entries_.emplace(id, entry); creation_sequence_ += 1; return 0; } Number Ledger::pending(const Entry &entry, Number cpu) const { const auto index = static_cast(cpu); const Cell &cell = entry.cells[index]; return cell.local - cell.folded; } Number Ledger::balance(const Entry &entry) const { Number result = entry.base; for (Number cpu = 0; cpu < cpus_; ++cpu) { result += pending(entry, cpu); } return result; } int Ledger::get(Number id, Number cpu, Number amount) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (!valid_cpu(cpu)) { return -22; } if (!valid_amount(amount)) { return -34; } if (entry->state != 0) { return -108; } const Number before = balance(*entry); if (amount > limit - before) { return -34; } const auto index = static_cast(cpu); entry->cells[index].local += amount; return 0; } int Ledger::put(Number id, Number cpu, Number amount) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (!valid_cpu(cpu)) { return -22; } if (!valid_amount(amount)) { return -34; } if (entry->state == 2) { return -108; } const Number before = balance(*entry); const Number minimum = entry->state == 0 ? 1 : 0; if (amount > before - minimum) { return -34; } const auto index = static_cast(cpu); entry->cells[index].local -= amount; release_if_drained(*entry); return 0; } void Ledger::fold_one(Entry &entry, Number cpu) { const auto index = static_cast(cpu); Cell &cell = entry.cells[index]; const Number contribution = cell.local - cell.folded; entry.base += contribution; cell.folded = cell.local; } void Ledger::fold_every(Entry &entry) { for (Number cpu = 0; cpu < cpus_; ++cpu) { fold_one(entry, cpu); } } int Ledger::fold_cpu(Number id, Number cpu) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (!valid_cpu(cpu)) { return -22; } fold_one(*entry, cpu); return 0; } int Ledger::fold_all(Number id) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } fold_every(*entry); return 0; } void Ledger::release_if_drained(Entry &entry) { if (entry.state != 1) { return; } if (balance(entry) != 0) { return; } fold_every(entry); entry.state = 2; Release event; event.sequence = release_sequence_ + 1; event.id = entry.id; event.incarnation = entry.incarnation; event.payload = entry.payload; releases_.push_back(event); release_sequence_ += 1; } int Ledger::kill(Number id) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (entry->state != 0) { return -16; } entry->state = 1; entry->base -= 1; fold_every(*entry); release_if_drained(*entry); return 0; } int Ledger::erase(Number id) { Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (entry->state != 2) { return -16; } entries_.erase(id); return 0; } int Ledger::query(Number id, View &output) const { const Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } View value; value.id = entry->id; value.payload = entry->payload; value.incarnation = entry->incarnation; value.state = entry->state; value.base = entry->base; value.total = balance(*entry); value.cpus = cpus_; output = value; return 0; } int Ledger::query_cpu(Number id, Number cpu, CpuView &output) const { const Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } if (!valid_cpu(cpu)) { return -22; } const auto index = static_cast(cpu); CpuView value; value.local = entry->cells[index].local; value.folded = entry->cells[index].folded; value.pending = pending(*entry, cpu); output = value; return 0; } int Ledger::total(Number id, Number &output) const { const Entry *entry = nullptr; const int status = find(id, entry); if (status != 0) { return status; } output = balance(*entry); return 0; } Number Ledger::resident_count() const { return static_cast(entries_.size()); } Number Ledger::cpu_count() const { return cpus_; } std::vector Ledger::events() const { return releases_; } Stats Ledger::stats() const { Stats result{}; for (const auto &pair : entries_) { const Entry &entry = pair.second; if (entry.state == 0) { result.active += 1; } else if (entry.state == 1) { result.killed += 1; } else { result.released += 1; } result.total += balance(entry); for (Number cpu = 0; cpu < cpus_; ++cpu) { result.pending += pending(entry, cpu); } } result.releases = static_cast(releases_.size()); return result; } int Ledger::select(const std::vector &ids, std::vector &output) const { if (ids.size() > capacity) { return -22; } std::vector selected; selected.reserve(ids.size()); for (Number id : ids) { View value{}; const int status = query(id, value); if (status != 0) { return status; } selected.push_back(value); } output = selected; return 0; } int Ledger::event(Number index, Release &output) const { if (index < 0) { return -34; } const auto position = static_cast(index); if (position >= releases_.size()) { return -34; } output = releases_[position]; return 0; } int Ledger::contains(Number id) const { if (!valid_id(id)) { return -22; } const Entry *entry = nullptr; const int status = find(id, entry); if (status == -2) { return 0; } if (status != 0) { return status; } return 1; } }