Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

App: the requests and their answers, live on Home

Mert Cobanov8d1d703eedba
diff --git a/patches/0007-ps5lm-activity-hook.patch b/patches/0007-ps5lm-activity-hook.patch
new file mode 100644
index 0000000..6d15d9b
--- /dev/null
+++ b/patches/0007-ps5lm-activity-hook.patch
@@ -0,0 +1,82 @@
+diff --git a/tools/server/server-http.cpp b/tools/server/server-http.cpp
+index 7ac78f9..c791d20 100644
+--- a/tools/server/server-http.cpp
++++ b/tools/server/server-http.cpp
+@@ -52,6 +52,16 @@ server_http_context::~server_http_context() {
+ }
+ }
+
++// PS5LM: the app shows each request and what was answered on the TV. It
++// defines these; any other build leaves them null and nothing is called.
++extern "C" {
++__attribute__((weak)) void ps5lm_activity_begin(int id, const char * method, const char * path, const char * remote,
++ const char * body, size_t body_len);
++__attribute__((weak)) void ps5lm_activity_data(int id, const char * data, size_t len);
++__attribute__((weak)) void ps5lm_activity_end(int id, int status);
++}
++static std::atomic<int> ps5lm_activity_next{ 0 };
++
+ static void log_server_request(const httplib::Request & req, const httplib::Response & res) {
+ // skip logging requests that are regularly sent, to avoid log spam
+ if (req.path == "/health"
+@@ -649,7 +659,8 @@ static std::string build_query_string(const httplib::Request & req) {
+ // using unique_ptr for request to allow safe capturing in lambdas
+ using server_http_req_ptr = std::unique_ptr<server_http_req>;
+
+-static void process_handler_response(server_http_req_ptr && request, server_http_res_ptr & response, httplib::Response & res) {
++static void process_handler_response(server_http_req_ptr && request, server_http_res_ptr & response, httplib::Response & res,
++ int activity = -1) {
+ if (response->is_stream()) {
+ res.status = response->status;
+ // Tell Nginx to not buffer any streamed response
+@@ -660,9 +671,12 @@ static void process_handler_response(server_http_req_ptr && request, server_http
+ std::shared_ptr<server_http_req> q_ptr = std::move(request);
+ std::shared_ptr<server_http_res> r_ptr = std::move(response);
+
+- const auto chunked_content_provider = [response = r_ptr](size_t, httplib::DataSink & sink) -> bool {
++ const auto chunked_content_provider = [response = r_ptr, activity](size_t, httplib::DataSink & sink) -> bool {
+ std::string chunk;
+ const bool has_next = response->next(chunk);
++ if (!chunk.empty() && activity >= 0 && ps5lm_activity_data) {
++ ps5lm_activity_data(activity, chunk.data(), chunk.size());
++ }
+ if (!chunk.empty()) {
+ if (!sink.write(chunk.data(), chunk.size())) {
+ return false;
+@@ -675,7 +689,10 @@ static void process_handler_response(server_http_req_ptr && request, server_http
+ }
+ return has_next;
+ };
+- const auto on_complete = [request = q_ptr, response = r_ptr](bool) mutable {
++ const auto on_complete = [request = q_ptr, response = r_ptr, activity](bool) mutable {
++ if (activity >= 0 && ps5lm_activity_end) {
++ ps5lm_activity_end(activity, response->status);
++ }
+ response->on_complete();
+ response.reset();
+ request.reset();
+@@ -685,6 +702,10 @@ static void process_handler_response(server_http_req_ptr && request, server_http
+ res.status = response->status;
+ set_headers(res, response->headers);
+ res.set_content(response->data, response->content_type);
++ if (activity >= 0 && ps5lm_activity_data && ps5lm_activity_end) {
++ ps5lm_activity_data(activity, response->data.data(), response->data.size());
++ ps5lm_activity_end(activity, response->status);
++ }
+ response->on_complete();
+ }
+ }
+@@ -752,8 +773,12 @@ void server_http_context::post(const std::string & path, const server_http_conte
+ std::move(files),
+ req.is_connection_closed
+ });
++ const int activity = ps5lm_activity_begin ? ps5lm_activity_next++ : -1;
++ if (activity >= 0) {
++ ps5lm_activity_begin(activity, "POST", req.path.c_str(), req.remote_addr.c_str(), body.data(), body.size());
++ }
+ server_http_res_ptr response = handler(*request);
+- process_handler_response(std::move(request), response, res);
++ process_handler_response(std::move(request), response, res, activity);
+ };
+ const std::string full_path = path_prefix + path;
+ for (const auto & srv : pimpl->servers) {
diff --git a/ps5/app/activity.cpp b/ps5/app/activity.cpp
new file mode 100644
index 0000000..b8079bc
--- /dev/null
+++ b/ps5/app/activity.cpp
@@ -0,0 +1,220 @@
+// The requests other devices make, as the Home page shows them. llama-server
+// calls these hooks from its HTTP threads (patches/0007): a request's body when
+// it arrives, every piece of the answer as it is sent, and the status at the
+// end. Here they become a line of what was asked and the text answered.
+//
+// SPDX-License-Identifier: GPL-3.0-or-later
+#include "stats.hpp"
+
+#include <nlohmann/json.hpp>
+
+#include <cstdio>
+#include <map>
+#include <mutex>
+#include <string>
+
+using json = nlohmann::ordered_json;
+
+namespace {
+
+std::mutex g_mutex;
+std::map<int, std::string> g_partial; // an answer's stream, up to its last full line
+ps5lm::StatsCollector * g_sink = nullptr;
+
+// One line, spaces collapsed, at most `cap` bytes, cut at a character's start.
+std::string one_line(const std::string & s, size_t cap) {
+ std::string out;
+ for (char c : s) {
+ const bool space = c == '\n' || c == '\r' || c == '\t' || c == ' ';
+ if (space) {
+ if (!out.empty() && out.back() != ' ') {
+ out += ' ';
+ }
+ } else {
+ out += c;
+ }
+ }
+ while (!out.empty() && out.back() == ' ') {
+ out.pop_back();
+ }
+ if (out.size() > cap) {
+ size_t at = cap;
+ while (at > 0 && ((unsigned char) out[at] & 0xC0) == 0x80) {
+ --at;
+ }
+ out = out.substr(0, at) + "\xE2\x80\xA6";
+ }
+ return out;
+}
+
+// A message's content: text, or text parts with the images marked.
+std::string text_of(const json & content) {
+ if (content.is_string()) {
+ return content.get<std::string>();
+ }
+ std::string out;
+ if (content.is_array()) {
+ for (const auto & part : content) {
+ const std::string type = part.value("type", "");
+ if (type == "text" && part.contains("text") && part["text"].is_string()) {
+ out += (out.empty() ? "" : " ") + part["text"].get<std::string>();
+ } else if (type == "image_url" || type == "image" || type == "input_image") {
+ out += (out.empty() ? "" : " ") + std::string("[image]");
+ }
+ }
+ }
+ return out;
+}
+
+// What was asked: the last user message, a decision request's state, or the prompt.
+std::string asked_from(const std::string & path, const json & j) {
+ if (!j.is_object()) {
+ return {};
+ }
+ if (path.find("systemone") != std::string::npos) {
+ std::string state = j.contains("state") ? (j["state"].is_string() ? j["state"].get<std::string>() : j["state"].dump()) : "";
+ const size_t questions = j.contains("questions") && j["questions"].is_object() ? j["questions"].size() : 0;
+ const size_t files = (j.contains("files") && j["files"].is_array() ? j["files"].size() : 0) +
+ (j.contains("images") && j["images"].is_array() ? j["images"].size() : 0);
+ std::string head = std::to_string(questions) + (questions == 1 ? " question" : " questions");
+ if (files) {
+ head += ", " + std::to_string(files) + (files == 1 ? " image" : " images");
+ }
+ return head + (state.empty() || state == "null" ? "" : ": " + state);
+ }
+ if (j.contains("messages") && j["messages"].is_array()) {
+ for (auto it = j["messages"].rbegin(); it != j["messages"].rend(); ++it) {
+ if (it->is_object() && it->value("role", "") == "user" && it->contains("content")) {
+ return text_of((*it)["content"]);
+ }
+ }
+ }
+ if (j.contains("prompt")) {
+ return j["prompt"].is_string() ? j["prompt"].get<std::string>() : j["prompt"].dump();
+ }
+ return {};
+}
+
+// "spam yes 0.99, intent sell, urgency 1.02"
+std::string answers_of(const json & answers) {
+ std::string out;
+ char num[32];
+ for (const auto & [key, a] : answers.items()) {
+ std::string v;
+ if (a.contains("choice") && a["choice"].is_string()) {
+ v = a["choice"].get<std::string>();
+ } else if (a.contains("noul") && a["noul"].is_number()) {
+ const double p = a["noul"].get<double>();
+ std::snprintf(num, sizeof(num), "%s %.2f", p >= 0.5 ? "yes" : "no", p);
+ v = num;
+ } else if (a.contains("score") && a["score"].is_number()) {
+ std::snprintf(num, sizeof(num), "%.2f", a["score"].get<double>());
+ v = num;
+ }
+ out += (out.empty() ? "" : ", ") + key + " " + v;
+ }
+ return out;
+}
+
+// The text in a whole answer, or in one event of a stream.
+std::string answer_from(const json & j) {
+ if (!j.is_object()) {
+ return {};
+ }
+ if (j.contains("error")) {
+ const json & e = j["error"];
+ return "Error: " + (e.is_object() ? e.value("message", e.dump()) : e.is_string() ? e.get<std::string>() : e.dump());
+ }
+ if (j.contains("answers") && j["answers"].is_object()) {
+ return answers_of(j["answers"]);
+ }
+ if (j.contains("choices") && j["choices"].is_array() && !j["choices"].empty()) {
+ const json & c = j["choices"][0];
+ for (const char * k : { "delta", "message" }) {
+ if (c.contains(k) && c[k].is_object() && c[k].contains("content") && c[k]["content"].is_string()) {
+ return c[k]["content"].get<std::string>();
+ }
+ }
+ if (c.contains("text") && c["text"].is_string()) {
+ return c["text"].get<std::string>();
+ }
+ return {};
+ }
+ // Anthropic: a text delta, or a whole message's text blocks.
+ if (j.value("type", "") == "content_block_delta" && j.contains("delta") && j["delta"].is_object()) {
+ return j["delta"].value("type", "") == "text_delta" ? j["delta"].value("text", "") : "";
+ }
+ if (j.contains("content")) {
+ return text_of(j["content"]);
+ }
+ return {};
+}
+
+} // namespace
+
+namespace ps5lm {
+
+void set_activity_sink(StatsCollector * stats) { g_sink = stats; }
+
+} // namespace ps5lm
+
+extern "C" {
+
+void ps5lm_activity_begin(int id, const char * method, const char * path, const char * remote, const char * body,
+ size_t body_len) {
+ (void) method;
+ if (!g_sink) {
+ return;
+ }
+ const json j = json::parse(body, body + body_len, nullptr, false);
+ g_sink->activity_begin(id, remote ? remote : "", path ? path : "", one_line(asked_from(path ? path : "", j), 400));
+}
+
+void ps5lm_activity_data(int id, const char * data, size_t len) {
+ if (!g_sink) {
+ return;
+ }
+ std::string out;
+ {
+ std::lock_guard<std::mutex> lock(g_mutex);
+ std::string & buf = g_partial[id];
+ buf.append(data, len);
+ const bool stream = buf.rfind("data:", 0) == 0 || buf.rfind("event:", 0) == 0;
+ if (!stream) {
+ // A whole JSON answer arrives in one piece.
+ const json j = json::parse(buf, nullptr, false);
+ if (!j.is_discarded()) {
+ out = answer_from(j);
+ buf.clear();
+ }
+ } else {
+ for (size_t nl = buf.find('\n'); nl != std::string::npos; nl = buf.find('\n')) {
+ const std::string line = buf.substr(0, nl);
+ buf.erase(0, nl + 1);
+ if (line.rfind("data:", 0) != 0) {
+ continue;
+ }
+ const std::string payload = line.substr(line.size() > 5 && line[5] == ' ' ? 6 : 5);
+ if (payload == "[DONE]") {
+ continue;
+ }
+ out += answer_from(json::parse(payload, nullptr, false));
+ }
+ }
+ }
+ if (!out.empty()) {
+ g_sink->activity_text(id, out);
+ }
+}
+
+void ps5lm_activity_end(int id, int status) {
+ {
+ std::lock_guard<std::mutex> lock(g_mutex);
+ g_partial.erase(id);
+ }
+ if (g_sink) {
+ g_sink->activity_end(id, status);
+ }
+}
+
+} // extern "C"
diff --git a/ps5/app/dashboard.cpp b/ps5/app/dashboard.cpp
index 8b2a308..d060837 100644
--- a/ps5/app/dashboard.cpp
+++ b/ps5/app/dashboard.cpp
@@ -266,6 +266,35 @@ void toggle_switch(gfx::DrawList & l, float right, float cy, bool on) {
l.circle(on ? r.x + r.w - 18 : r.x + 18, cy, 14, on ? kInk : kInk2);
}
+// Greedy word wrap to `width`; a word longer than a line is cut.
+std::vector<std::string> wrap(const ui::FontRef & f, const std::string & text, float size, float width) {
+ std::vector<std::string> lines;
+ std::string line;
+ size_t at = 0;
+ while (at < text.size()) {
+ size_t end = text.find(' ', at);
+ if (end == std::string::npos) {
+ end = text.size();
+ }
+ const std::string word = text.substr(at, end - at);
+ const std::string next = line.empty() ? word : line + " " + word;
+ if (!line.empty() && f.measure(next, size) > width) {
+ lines.push_back(line);
+ line = word;
+ } else {
+ line = next;
+ }
+ at = end + 1;
+ }
+ if (!line.empty()) {
+ lines.push_back(line);
+ }
+ for (auto & l : lines) {
+ l = f.font->fit(l, size, width);
+ }
+ return lines;
+}
+
int first_visible(int cursor, int rows) {
return std::clamp(cursor - kVisibleRows / 2, 0, std::max(0, rows - kVisibleRows));
}
@@ -1094,45 +1123,73 @@ void Dashboard::draw_home(gfx::DrawList & l) const {
meter(l, kLeft + 460, my + 16, 400, live_.ctx > 0 ? (float) live_.ctx_used / (float) live_.ctx : 0, kInk.with_alpha(0.8f));
}
- // Right: where the model is reached. A single panel, as a car gives the map.
+ // Right: where the model is reached, and what the network asks of it, as
+ // it happens. A single panel, as a car gives the map.
const Rect p{ 1056, 196, kRight - 1056, 728 };
l.rounded_rect(p, 24, kSurface);
- const float x = p.x + 56;
- put(l, f.regular, "On your network", x, p.y + 76, 24, kInk2);
+ const float x = p.x + 56, w = p.w - 112;
+ put(l, f.regular, "On your network", x, p.y + 70, 22, kInk2);
const std::string url = live_.address.empty() ? std::string("Not connected") : "http://" + live_.address;
- put(l, f.regular, f.regular.font->fit(url, 44, p.w - 112), x, p.y + 136, 44, live_.address.empty() ? kInk3 : kInk);
- const bool serving = loaded && (live_.state == ServerState::ready || live_.state == ServerState::generating);
+ put(l, f.regular, f.regular.font->fit(url, 40, w), x, p.y + 124, 40, live_.address.empty() ? kInk3 : kInk);
put(l, f.regular,
- !loaded ? "Load a model and it answers here"
- : decision ? "Answers the TypeSafe decision API"
- : "Answers the OpenAI and Anthropic APIs, and a chat page",
- x, p.y + 184, 24, kInk2);
-
- struct Ep { const char * method; const char * path; };
- const Ep chat[] = { { "POST", "/v1/chat/completions" }, { "POST", "/v1/messages" }, { "GET", "/v1/models" } };
- const Ep dec[] = { { "POST", "/v1/systemone" }, { "GET", "/v1/models" } };
- const Ep * eps = decision ? dec : chat;
- const int n = decision ? 2 : 3;
- for (int i = 0; i < n; ++i) {
- const float y = p.y + 268 + 52.0f * (float) i;
- const Color c = serving ? kInk : kInk3;
- put(l, f.mono, eps[i].method, x, y, 24, kInk3);
- put(l, f.mono, eps[i].path, x + 96, y, 24, c);
+ !loaded ? "Load a model and it answers here"
+ : decision ? "TypeSafe decisions at /v1/systemone"
+ : "OpenAI, Anthropic and a chat page",
+ x, p.y + 162, 22, kInk3);
+ l.line(x, p.y + 196, x + w, p.y + 196, 1.5f, kHair);
+
+ // The latest requests, newest first: who asked, what, and the answer as it arrives.
+ float y = p.y + 240;
+ const float bottom = p.y + p.h - 72;
+ if (live_.activity.empty()) {
+ put(l, f.regular, "Requests from other devices appear here as they arrive.", x, y + 8, 22, kInk3);
+ }
+ for (auto it = live_.activity.rbegin(); it != live_.activity.rend() && y < bottom - 90; ++it) {
+ const Exchange & e = *it;
+ const bool running = e.status == 0;
+ const bool fault = e.status >= 400;
+ put(l, f.regular, e.client + " " + e.path, x, y, 19, kInk3);
+ if (running) {
+ const float tw = put(l, f.regular, "Answering", x + w, y, 19, kInk2, gfx::Align::right);
+ l.circle(x + w - tw - 14, y - 7, 5, kBlue.with_alpha(0.55f + 0.45f * std::sin(clock_ * 6.0f)));
+ } else {
+ put(l, f.regular, fault ? format("Error %.0f", (double) e.status) : format(e.seconds < 10 ? "%.1f s" : "%.0f s", e.seconds),
+ x + w, y, 19, fault ? kRed : kInk3, gfx::Align::right);
+ }
+ y += 34;
+ put(l, f.regular, f.regular.font->fit(e.asked.empty() ? std::string(kDash) : e.asked, 22, w), x, y, 22, kInk2);
+ y += 34;
+ // While it streams, the answer's last lines, so it reads as it is
+ // written; once done, its first ones.
+ std::vector<std::string> lines = wrap(f.regular, e.answered, 22, w);
+ const int keep = it == live_.activity.rbegin() ? 3 : 1;
+ if ((int) lines.size() > keep) {
+ if (running) {
+ lines.erase(lines.begin(), lines.end() - keep);
+ } else {
+ lines.resize((size_t) keep);
+ lines.back() = f.regular.font->fit(lines.back() + " \xE2\x80\xA6", 22, w);
+ }
+ }
+ if (lines.empty()) {
+ lines.push_back(running ? "" : kDash);
+ }
+ for (const auto & line : lines) {
+ if (y > bottom) {
+ break;
+ }
+ put(l, f.regular, line, x, y, 22, fault ? kRed : kInk);
+ y += 31;
+ }
+ y += 30;
}
// What it has done since it started, like a trip meter.
- l.line(x, p.y + 470, p.x + p.w - 56, p.y + 470, 1.5f, kHair);
- struct Fig { std::string value; const char * label; };
- const Fig figs[] = { { thousands(live_.requests), "requests" },
- { thousands(live_.prompt_tokens), "tokens in" },
- { thousands(live_.tokens), "tokens out" } };
- const int figures = decision ? 2 : 3;
- for (int i = 0; i < figures; ++i) {
- const float fx = x + 230.0f * (float) i;
- put(l, f.display, figs[i].value, fx - 2, p.y + 572, 60, loaded ? kInk : kInk3);
- put(l, f.regular, figs[i].label, fx, p.y + 614, 22, kInk3);
- }
- put(l, f.regular, decision || !loaded ? "" : "Open the address in a browser to chat", x, p.y + 680, 22, kInk3);
+ std::string trip = thousands(live_.served) + " requests " + thousands(live_.prompt_tokens) + " tokens in";
+ if (!decision) {
+ trip += " " + thousands(live_.tokens) + " out";
+ }
+ put(l, f.regular, trip, x, p.y + p.h - 40, 20, kInk3);
}
// ---- drawing: Models ------------------------------------------------------------------
diff --git a/ps5/app/main.cpp b/ps5/app/main.cpp
index dd67e35..f4a5b38 100644
--- a/ps5/app/main.cpp
+++ b/ps5/app/main.cpp
@@ -600,6 +600,7 @@ int main(int, char **) {
StatsCollector stats;
stats.start(kPort);
+ set_activity_sink(&stats); // llama-server's request hooks (patches/0007) report here
models.settings = load_settings(kSettings);
models.scan(stats);
diff --git a/ps5/app/preview_main.cpp b/ps5/app/preview_main.cpp
index 1dbf288..943e09b 100644
--- a/ps5/app/preview_main.cpp
+++ b/ps5/app/preview_main.cpp
@@ -89,6 +89,7 @@ ps5lm::Live sample(float seconds, bool generating) {
L.tokens = 4182;
L.prompt_tokens = 9120;
L.requests = 14;
+ L.served = 14;
L.ctx_used = 2318;
L.prompt_tps = 45.4f;
for (int i = 0; i < 120; ++i) {
@@ -100,6 +101,24 @@ ps5lm::Live sample(float seconds, bool generating) {
L.temp_history.push_back(on ? 60.0f + 2.0f * std::sin(i * 0.2f) : 52.0f);
L.power_history.push_back(on ? 85.0f + 1.5f * std::sin(i * 0.4f) : 23.0f);
}
+ {
+ // Requests from the network: two answered, the newest still streaming.
+ ps5lm::Exchange a;
+ a.id = 12; a.client = "192.168.1.31"; a.path = "/v1/messages"; a.status = 200; a.seconds = 12.8;
+ a.asked = "Rename the loop variable in parse_args to something clearer and explain why";
+ a.answered = "Renamed i to arg_index: it says what the loop walks, and the next reader does not have to look up what i counts.";
+ L.activity.push_back(a);
+ a.id = 13; a.client = "192.168.1.24"; a.path = "/v1/chat/completions"; a.status = 200; a.seconds = 3.4;
+ a.asked = "Write a haiku about a console that runs a language model";
+ a.answered = "Fans hum through the night, a console dreams in tokens, words bloom on the TV.";
+ L.activity.push_back(a);
+ a.id = 14; a.client = "192.168.1.24"; a.path = "/v1/chat/completions"; a.status = generating ? 0 : 200; a.seconds = 9.1;
+ a.asked = "How does the PS5's GPU run llama.cpp?";
+ a.answered = "Through Vulkan. Mihawk-99's RADV port gives the console a full Vulkan 1.4 driver, so llama.cpp's own Vulkan "
+ "backend runs unchanged: every matrix product becomes a compute shader that ACO compiles on the console, and "
+ "the weights sit in the 12 GB the GPU and CPU share";
+ L.activity.push_back(a);
+ }
L.gen_tps = generating ? 20.7f : 0.0f;
L.gpu_busy = generating ? 0.94f : 0.02f;
L.cpu_use = generating ? 0.12f : 0.02f;
@@ -166,6 +185,14 @@ ps5lm::Live sample(float seconds, bool generating) {
L.prompt_tokens = 88412;
L.requests = 131;
L.free_gib = 8.6;
+ L.activity.clear();
+ ps5lm::Exchange a;
+ a.id = 130; a.client = "192.168.1.40"; a.path = "/v1/systemone"; a.status = 200; a.seconds = 0.3;
+ a.asked = "3 questions: {\"from\": \"[email protected]\", \"subject\": \"URGENT: you won a free iPhone!!!\"}";
+ a.answered = "spam yes 1.00, intent sell, urgency 1.02";
+ L.activity.push_back(a);
+ a.id = 131; a.asked = "7 questions, 1 image"; a.answered = "text no 0.01, color black, ad no 0.01, people no 0.00, mood 1.00";
+ a.seconds = 2.9; L.activity.push_back(a);
}
if (g_empty) {
L.state = ps5lm::ServerState::no_model;
diff --git a/ps5/app/stats.cpp b/ps5/app/stats.cpp
index 4c76125..483b0d6 100644
--- a/ps5/app/stats.cpp
+++ b/ps5/app/stats.cpp
@@ -257,6 +257,55 @@ void StatsCollector::set_models(const std::vector<ModelRow> & rows) {
live_.models = rows;
}
+namespace {
+
+double steady_s() {
+ return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
+}
+
+} // namespace
+
+void StatsCollector::activity_begin(int id, std::string client, std::string path, std::string asked) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ Exchange e;
+ e.id = id;
+ e.client = std::move(client);
+ e.path = std::move(path);
+ e.asked = std::move(asked);
+ e.started = steady_s();
+ live_.activity.push_back(std::move(e));
+ if (live_.activity.size() > 6) {
+ live_.activity.erase(live_.activity.begin());
+ }
+}
+
+void StatsCollector::activity_text(int id, const std::string & text) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ for (auto & e : live_.activity) {
+ if (e.id == id) {
+ e.answered += text;
+ if (e.answered.size() > 4000) { // the screen shows the end of it
+ size_t cut = e.answered.size() - 3000;
+ while (cut < e.answered.size() && ((unsigned char) e.answered[cut] & 0xC0) == 0x80) {
+ ++cut;
+ }
+ e.answered.erase(0, cut);
+ }
+ }
+ }
+}
+
+void StatsCollector::activity_end(int id, int status) {
+ std::lock_guard<std::mutex> lock(mutex_);
+ for (auto & e : live_.activity) {
+ if (e.id == id) {
+ live_.served += status < 400;
+ e.status = status;
+ e.seconds = steady_s() - e.started;
+ }
+ }
+}
+
void StatsCollector::set_scratch(double gib) {
std::lock_guard<std::mutex> lock(mutex_);
live_.scratch_gib = gib;
diff --git a/ps5/app/stats.hpp b/ps5/app/stats.hpp
index c9a42dc..7e5c9e5 100644
--- a/ps5/app/stats.hpp
+++ b/ps5/app/stats.hpp
@@ -25,6 +25,18 @@ struct Drive {
double free = 0, total = 0; // GiB; total 0 when unknown (no ps5-exporter)
};
+// A request another device made, and what was answered, for the Home page.
+struct Exchange {
+ int id = 0;
+ std::string client; // "192.168.8.12"
+ std::string path; // "/v1/chat/completions"
+ std::string asked; // the last user message, a decision's state, or the prompt
+ std::string answered; // the text answered so far
+ int status = 0; // the HTTP status once done, 0 while it runs
+ double seconds = 0; // how long it took, once done
+ double started = 0; // steady clock, seconds
+};
+
enum class ServerState : uint8_t { loading, ready, generating, failed, no_model };
struct ModelRow {
@@ -102,6 +114,8 @@ struct Live {
uint32_t ctx_used = 0; // in the running slot
std::vector<ModelRow> models;
+ std::vector<Exchange> activity; // the latest requests, oldest first
+ uint64_t served = 0; // requests answered without an error
};
// The sampling thread. Fill the static parts (model, address) with set_*;
@@ -114,6 +128,10 @@ class StatsCollector {
void set_models(const std::vector<ModelRow> & rows);
void set_state(ServerState s);
void set_scratch(double gib);
+ // From llama-server's HTTP threads, through activity.cpp.
+ void activity_begin(int id, std::string client, std::string path, std::string asked);
+ void activity_text(int id, const std::string & text);
+ void activity_end(int id, int status);
private:
void run();
@@ -135,6 +153,8 @@ std::vector<Drive> drives_from_metrics(const std::string & text);
std::string drive_name(const std::string & path);
bool send_payload(const char * path, int port);
+// Where llama-server's request hooks (activity.cpp) report.
+void set_activity_sink(StatsCollector * stats);
std::string local_address();
diff --git a/scripts/build-app.sh b/scripts/build-app.sh
index 200b1cc..495f410 100755
--- a/scripts/build-app.sh
+++ b/scripts/build-app.sh
@@ -91,6 +91,7 @@ cxx -fexceptions -fcxx-exceptions -c "$root/ps5/app/model_plan.cpp" -o "$obj/mod
cxx -std=c++20 -fexceptions -fcxx-exceptions -I "$llama/vendor" -c "$root/ps5/app/settings.cpp" -o "$obj/settings.o"
cxx -std=c++20 -fexceptions -fcxx-exceptions -c "$root/ps5/app/fetch.cpp" -o "$obj/fetch.o"
cxx -std=c++20 -fexceptions -fcxx-exceptions -I "$llama/vendor" -c "$root/ps5/app/market.cpp" -o "$obj/market.o"
+cxx -std=c++20 -fexceptions -fcxx-exceptions -I "$llama/vendor" -I "$root/ps5/app" -I "$root/third_party/ps5-homebrew-ui/src" -c "$root/ps5/app/activity.cpp" -o "$obj/activity.o"
"$root/ps5/app/ps5cc" -O2 -I "$llama/vendor/hash" -c "$llama/vendor/hash/sha256/sha256.c" -o "$obj/sha256.o"
"$root/ps5/app/ps5cc" -O2 -c "$root/ps5/app/compat_app.c" -o "$obj/compat_app.o"
# In an archive, so --exclude-libs keeps them local: a stub module defines some
@@ -181,7 +182,7 @@ mapfile -t libs < <(find "$out/llama" -name '*.a' | sort)
"$sdk/bin/prospero-lld" "${radv_linker_script[@]}" --eh-frame-hdr "${radv_link_flags[@]}" \
--version-script "$vk/tooling/native/app-symbols.map" --exclude-libs=ALL \
-e _start -o "$out/llvm-pie.elf" \
- "$obj/app_crt.o" "$obj/app_cpp_runtime.o" "$obj/main.o" "$obj/display.o" "$obj/model_plan.o" "$obj/settings.o" "$obj/fetch.o" "$obj/market.o" "$obj/sha256.o" "$obj/dashboard.o" "$obj/stats.o" \
+ "$obj/app_crt.o" "$obj/app_cpp_runtime.o" "$obj/main.o" "$obj/display.o" "$obj/model_plan.o" "$obj/settings.o" "$obj/fetch.o" "$obj/market.o" "$obj/activity.o" "$obj/sha256.o" "$obj/dashboard.o" "$obj/stats.o" \
--start-group "${libs[@]}" "$obj/libps5lm_kit.a" "$obj/libps5app_compat.a" "$out/gl/libps5lm_gl.a" --end-group \
"$out/stubs/libSceAgc.so" "$out/stubs/libSceAgcDriver.so" \
"${radv_link_inputs[@]}" \