/* splinter -- usage accounting per provider; see usage.h. */ #include #include #include #include #include #include #include #include #include #include "config.h" #include "usage.h" #include "providers.h" #include "i18n.h" #define KEEP_SECONDS (32L * 86400) /* the longest window anyone asks for */ #define PRUNE_BYTES (256L * 1024) static char g_active[64] = "main"; void sp_usage_set_active(const char *id) { snprintf(g_active, sizeof(g_active), "%s", id != NULL && *id != '\0' ? id : "main"); } const char * sp_usage_active(void) { return g_active; } long sp_usage_window(const char *s) { char *end; long n; if (s == NULL || !isdigit((unsigned char)*s)) return 0; n = strtol(s, &end, 10); if (n <= 0) return 0; switch (*end) { case '\0': case 's': return n; case 'm': return n * 60; case 'h': return n * 3600; case 'd': return n * 86400; case 'w': return n * 7 * 86400; } return 0; } int sp_usage_limit_parse(const char *s, unsigned long *n, long *window) { char *end; unsigned long v; long w; if (s == NULL || !isdigit((unsigned char)*s)) return 0; v = strtoul(s, &end, 10); if (*end != '/' || v == 0) return 0; w = sp_usage_window(end + 1); if (w == 0) return 0; *n = v; *window = w; return 1; } static void usage_path(const char *id, char *buf, size_t len) { const char *dir = getenv("SPLINTER_USAGE_DIR"); const char *home = getenv("HOME"); if (dir != NULL && *dir != '\0') snprintf(buf, len, "%s/%s.log", dir, id); else snprintf(buf, len, "%s/.splinter/usage/%s.log", home != NULL ? home : "/tmp", id); } /* an id becomes a file name: nothing that leaves the directory */ static int id_ok(const char *id) { if (id == NULL || *id == '\0' || id[0] == '.') return 0; for (; *id != '\0'; id++) if (!isalnum((unsigned char)*id) && *id != '-' && *id != '_' && *id != '.') return 0; return 1; } void sp_usage_record(const char *id, long tok_in, long tok_out) { char path[700], line[96], *slash; int fd, n; if (!id_ok(id)) return; usage_path(id, path, sizeof(path)); slash = strrchr(path, '/'); if (slash != NULL) { *slash = '\0'; (void)mkdir(path, 0700); *slash = '/'; } fd = open(path, O_WRONLY | O_CREAT | O_APPEND, 0600); if (fd < 0) return; n = snprintf(line, sizeof(line), "%ld %ld %ld\n", (long)time(NULL), tok_in > 0 ? tok_in : 0, tok_out > 0 ? tok_out : 0); /* one write: concurrent splinters do not interleave inside a line */ if (write(fd, line, (size_t)n) < 0) { /* nothing to do: a lost count is not worth a failed turn */ } close(fd); } /* rewrite the log with only the lines still of interest */ static void prune(const char *path, long now) { char tmp[720], line[128]; FILE *in, *out; snprintf(tmp, sizeof(tmp), "%s.tmp", path); in = fopen(path, "r"); if (in == NULL) return; out = fopen(tmp, "w"); if (out == NULL) { fclose(in); return; } while (fgets(line, sizeof(line), in) != NULL) if (strtol(line, NULL, 10) > now - KEEP_SECONDS) fputs(line, out); fclose(in); if (fclose(out) == 0) (void)rename(tmp, path); else (void)unlink(tmp); } void sp_usage_get(const char *id, unsigned long req_limit, long req_window, unsigned long tok_limit, long tok_window, struct sp_usage *u) { char path[700], line[128]; FILE *f; long now = (long)time(NULL); struct stat st; double fr = -1, ft = -1; long oldest_r = 0, oldest_t = 0; memset(u, 0, sizeof(*u)); u->frac = -1; u->req_limit = req_limit; u->tok_limit = tok_limit; u->req_window = req_window; u->tok_window = tok_window; if (!id_ok(id)) return; usage_path(id, path, sizeof(path)); if (stat(path, &st) == 0 && st.st_size > PRUNE_BYTES) prune(path, now); f = fopen(path, "r"); if (f != NULL) { while (fgets(line, sizeof(line), f) != NULL) { char *e; long ts = strtol(line, &e, 10), ti, to; ti = strtol(e, &e, 10); to = strtol(e, &e, 10); if (ts <= 0) continue; if (req_window > 0 && ts > now - req_window) { u->req_used++; if (oldest_r == 0 || ts < oldest_r) oldest_r = ts; } if (tok_window > 0 && ts > now - tok_window) { u->tok_used += (unsigned long)(ti + to); if (oldest_t == 0 || ts < oldest_t) oldest_t = ts; } } fclose(f); } if (req_limit > 0 && req_window > 0) fr = (double)u->req_used / (double)req_limit; if (tok_limit > 0 && tok_window > 0) ft = (double)u->tok_used / (double)tok_limit; u->frac = fr > ft ? fr : ft; /* the window that is fuller decides when room comes back */ if (fr >= ft && oldest_r > 0) u->reset_at = oldest_r + req_window; else if (oldest_t > 0) u->reset_at = oldest_t + tok_window; u->benched_until = sp_cooldown_until(id); } void sp_usage_for(const struct sp_config *cfg, const char *id, struct sp_usage *u) { size_t i; if (cfg != NULL && id != NULL) { for (i = 0; i < cfg->ndelegates; i++) { const struct sp_delegate *d = &cfg->delegate[i]; if (strcmp(d->id, id) == 0) { sp_usage_get(id, d->req_limit, d->req_window, d->tok_limit, d->tok_window, u); return; } } if (strcmp(id, "main") == 0) { sp_usage_get(id, cfg->req_limit, cfg->req_window, cfg->tok_limit, cfg->tok_window, u); return; } } sp_usage_get(id, 0, 0, 0, 0, u); } static void window_name(long w, char *buf, size_t cap) { if (w % 86400 == 0) snprintf(buf, cap, "%ldd", w / 86400); else if (w % 3600 == 0) snprintf(buf, cap, "%ldh", w / 3600); else if (w % 60 == 0) snprintf(buf, cap, "%ldm", w / 60); else snprintf(buf, cap, "%lds", w); } char * sp_usage_until(long epoch, char *buf, size_t cap) { long d = epoch - (long)time(NULL); if (d < 0) d = 0; if (d >= 86400) snprintf(buf, cap, "%ldd%ldh", d / 86400, (d % 86400) / 3600); else if (d >= 3600) snprintf(buf, cap, "%ldh%02ldm", d / 3600, (d % 3600) / 60); else snprintf(buf, cap, "%ldm", (d + 59) / 60); return buf; } char * sp_usage_brief(const struct sp_usage *u, char *buf, size_t cap) { char w[48], t[48]; if (u->benched_until > 0) { snprintf(buf, cap, sp_t("usage.benched"), sp_usage_until(u->benched_until, t, sizeof(t))); } else if (u->frac < 0) { snprintf(buf, cap, sp_t("usage.calls"), u->req_used); } else { window_name(u->req_limit > 0 && (u->tok_limit == 0 || (double)u->req_used / (double)u->req_limit >= (double)u->tok_used / (double)u->tok_limit) ? u->req_window : u->tok_window, w, sizeof(w)); snprintf(buf, cap, "%s %.0f%%", w, u->frac * 100.0); if (u->frac >= 1.0 && u->reset_at > 0) { size_t n = strlen(buf); snprintf(buf + n, cap - n, sp_t("usage.room"), sp_usage_until(u->reset_at, t, sizeof(t))); } } return buf; } static void bar(double frac, char *b, size_t cap) { size_t i, n = 20, f; if (cap < n + 3) return; f = frac < 0 ? 0 : frac >= 1 ? n : (size_t)(frac * (double)n); b[0] = '['; for (i = 0; i < n; i++) b[1 + i] = i < f ? '#' : '.'; b[n + 1] = ']'; b[n + 2] = '\0'; } /* the provider's own usage text, asked at most once a minute per provider */ static int remote_cached(const struct sp_delegate *d, char *out, size_t cap) { static struct { char id[32]; char text[160]; time_t at; int ok; } cache[SP_CONFIG_MAX_DELEGATES]; size_t k; time_t now = time(NULL); for (k = 0; k < SP_CONFIG_MAX_DELEGATES; k++) if (cache[k].id[0] == '\0' || strcmp(cache[k].id, d->id) == 0) break; if (k == SP_CONFIG_MAX_DELEGATES) return 0; if (cache[k].id[0] == '\0' || now - cache[k].at > 60) { struct sp_provider pv; memset(&pv, 0, sizeof(pv)); snprintf(cache[k].id, sizeof(cache[k].id), "%s", d->id); snprintf(pv.kind, sizeof(pv.kind), "%s", d->kind); snprintf(pv.endpoint, sizeof(pv.endpoint), "%s", d->endpoint); snprintf(pv.key_env, sizeof(pv.key_env), "%s", d->key_env); cache[k].ok = strcmp(d->kind, "cli") != 0 && d->endpoint[0] != '\0' && sp_provider_remote_usage(&pv, cache[k].text, sizeof(cache[k].text)) == 0; cache[k].at = now; } if (!cache[k].ok) return 0; snprintf(out, cap, "%s", cache[k].text); return 1; } void sp_usage_report(const struct sp_config *cfg, struct sp_buf *out) { size_t i; char ids[SP_CONFIG_MAX_DELEGATES + 1][32]; size_t n = 0; snprintf(ids[n++], sizeof(ids[0]), "main"); for (i = 0; cfg != NULL && i < cfg->ndelegates && n < SP_CONFIG_MAX_DELEGATES; i++) snprintf(ids[n++], sizeof(ids[0]), "%s", cfg->delegate[i].id); for (i = 0; i < n; i++) { struct sp_usage u; char b[32] = "", brief[160], line[400]; long wr = 0; sp_usage_for(cfg, ids[i], &u); sp_usage_brief(&u, brief, sizeof(brief)); if (u.frac >= 0) bar(u.frac, b, sizeof(b)); snprintf(line, sizeof(line), "%-14.14s %s %s", ids[i], b, brief); (void)wr; sp_buf_puts(out, line); if (u.req_limit > 0) sp_buf_printf(out, sp_t("usage.of_calls"), u.req_used, u.req_limit); if (u.tok_limit > 0) sp_buf_printf(out, sp_t("usage.of_tokens"), u.tok_used, u.tok_limit); /* what the provider itself says (ollama balance, opencode go usage) */ if (i > 0) { const struct sp_delegate *d = &cfg->delegate[i - 1]; char rem[160]; if (remote_cached(d, rem, sizeof(rem))) sp_buf_printf(out, " | %s", rem); } sp_buf_puts(out, "\n"); } }