usage: per-provider allowance accounting, status line, headroom-aware delegation
limit=N/window and tokens=N/window on delegates and the main provider; calls are counted in ~/.splinter/usage/<id>.log; /usage and the status line show how full each allowance is; choose() skips used-up providers and prefers the one with the most left among equal matches; refused calls bench the active provider. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
cdb33cbda2
commit
ba5b76f945
10 files changed
+607
-4
No files matched your search
@@ -23,7 +23,7 @@ CURSES_A = $(CURSES_DIR)/libcurses/libcurses.a \
|
||||
LIB = libsplinter.so
|
||||
LIBOBJ = src/buf.o src/tool.o src/session.o src/conv.o src/transport.o \
|
||||
src/json.o src/sse.o src/openai.o src/transport_openai.o src/builtin.o \
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/tgmd.o src/b64.o src/a2aserver.o src/providers.o
|
||||
src/agent.o src/theme.o src/palettes.o src/decomp.o src/i18n.o src/delegate.o src/telegram.o src/log.o src/cmdscript.o src/folder.o src/interview.o src/ack.o src/mcpserver.o src/skills.o src/memory.o src/prompt.o src/config.o src/plugin.o src/curl.o src/swarm.o src/provstate.o src/md.o src/tgmd.o src/b64.o src/a2aserver.o src/providers.o src/usage.o
|
||||
BIN = splinter
|
||||
BINOBJ = src/main.o src/ui.o
|
||||
PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
@@ -37,7 +37,7 @@ PLUGIN = plugins/clock/clock.so plugins/lcm/lcm.so plugins/cps/cps.so \
|
||||
plugins/tmux/tmux.so
|
||||
TESTS = tests/test_buf tests/test_tool tests/test_session tests/test_conv \
|
||||
tests/test_transport tests/test_agent tests/test_openai \
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_tmux tests/test_jobs
|
||||
tests/test_theme tests/test_plugin tests/test_memory tests/test_skills tests/test_telegram tests/test_delegate tests/test_builtin tests/test_ascii tests/test_ack tests/test_i18n tests/test_config tests/test_focus tests/test_sse tests/test_cli tests/test_compactor tests/test_secrets tests/test_mcpclient tests/test_kids tests/test_lifecycle tests/test_md tests/test_tgmd tests/test_providers tests/test_b64 tests/test_pause tests/test_swarm tests/test_provstate tests/test_planner tests/test_tmux tests/test_usage tests/test_jobs
|
||||
|
||||
RPATH = -Wl,-rpath,'$$ORIGIN'
|
||||
PLUGIN_RPATH = -Wl,-rpath,'$$ORIGIN/../..'
|
||||
@@ -198,6 +198,9 @@ tests/test_provstate: tests/test_provstate.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_provstate.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_planner: tests/test_planner.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_planner.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
|
||||
tests/test_usage: tests/test_usage.c $(LIB)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_usage.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_tmux: tests/test_tmux.c $(LIB) $(PLUGIN)
|
||||
$(CC) $(CFLAGS) -o $@ tests/test_tmux.c -L. -lsplinter $(TEST_RPATH) $(LIBS)
|
||||
tests/test_jobs: tests/test_jobs.c $(LIB) $(PLUGIN)
|
||||
|
||||
@@ -77,6 +77,34 @@ delegate = claude-b | Claude Code, second account | kind=cli | cmd=claude -p |
|
||||
ssh target: the child splinter runs there, with that machine's keys. An unnamed
|
||||
field is the description the selection reads.
|
||||
|
||||
### Usage limits
|
||||
|
||||
Subscriptions (Claude Pro, ollama.com, opencode-go ...) give an allowance per
|
||||
window and mostly no API that says how much is left. splinter counts its own
|
||||
calls: one line per call in `~/.splinter/usage/<id>.log`, shared by every
|
||||
splinter on the machine. You declare the allowance:
|
||||
|
||||
```
|
||||
limit = 45/5h # the main provider: 45 calls per 5 hours
|
||||
tokens = 2000000/7d # and/or tokens per window (5m, 5h, 7d, 2w)
|
||||
delegate = claude-a | Claude Pro A | kind=cli | cmd=claude -p | tags=plan | limit=45/5h
|
||||
delegate = claude-b | Claude Pro B | kind=cli | cmd=claude -p | tags=plan | limit=45/5h | env=CLAUDE_CODE_OAUTH_TOKEN=...
|
||||
delegate = cloud | ollama.com | endpoint=... | model=... | tags=code | tokens=5000000/7d
|
||||
```
|
||||
|
||||
What it does with that:
|
||||
|
||||
- the status line shows how full each allowance is (`claude-a 5h 62%`), a
|
||||
benched one shows when it is back; `/usage` lists all with bars;
|
||||
- delegation skips a provider whose allowance is used up, and among providers
|
||||
that fit the task equally well (same tags: a pool of accounts) it takes the
|
||||
one with the most left;
|
||||
- a refused call (HTTP 429/402/529, or "you've hit your limit - resets 3am")
|
||||
benches the provider until the reset it names.
|
||||
|
||||
The counts are splinter's own: use the same account elsewhere and the real
|
||||
usage is higher than shown. Set the limit a little below the real one.
|
||||
|
||||
`env=NAME=VALUE` (repeatable, up to 8) sets a variable for the delegate's
|
||||
child right before it starts, overwriting whatever the parent exported - so two
|
||||
delegates can run the same CLI with two different accounts. With `host=` it is
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
#include "splinter_plugin.h"
|
||||
#include "ack.h"
|
||||
#include "log.h"
|
||||
#include "usage.h"
|
||||
|
||||
/*
|
||||
* The terminal as one more approval channel. It must be stoppable: when the
|
||||
@@ -423,12 +424,20 @@ agent_loop(struct sp_agent *a, struct sp_conv *conv, int resume, int decision,
|
||||
(size_t)(reply.tokens_in > 0 ? reply.tokens_in : 0),
|
||||
(size_t)(reply.tokens_out > 0 ? reply.tokens_out : 0));
|
||||
quota_hint(a->tr.provider, a->tr.key_id, reply.status, reply.retry_after_s);
|
||||
/* a refused call benches the provider the run is on */
|
||||
if (reply.status == 429 || reply.status == 402 ||
|
||||
reply.status == 529)
|
||||
sp_cooldown_set(sp_usage_active(),
|
||||
(long)time(NULL) + (reply.retry_after_s > 0 ?
|
||||
reply.retry_after_s : 300), "refused (HTTP limit)");
|
||||
sp_buf_printf(err, "agent: transport status %ld",
|
||||
reply.status);
|
||||
sp_reply_free(&reply);
|
||||
goto out;
|
||||
}
|
||||
|
||||
sp_usage_record(sp_usage_active(), reply.tokens_in,
|
||||
reply.tokens_out);
|
||||
a->stats.tokens_in += (size_t)(reply.tokens_in > 0 ?
|
||||
reply.tokens_in : 0);
|
||||
a->stats.tokens_out += (size_t)(reply.tokens_out > 0 ?
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <dirent.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "usage.h"
|
||||
|
||||
void
|
||||
sp_config_init(struct sp_config *c)
|
||||
@@ -246,6 +247,24 @@ parse_delegate(struct sp_delegate *d, const char *val, struct sp_buf *err,
|
||||
if (d->max_parallel < 1)
|
||||
d->max_parallel = 1;
|
||||
}
|
||||
else if (!strncmp(s, "limit=", 6)) {
|
||||
if (!sp_usage_limit_parse(s + 6, &d->req_limit,
|
||||
&d->req_window)) {
|
||||
sp_buf_printf(err, "config: %s:%u: limit= wants "
|
||||
"N/window, e.g. 45/5h", path, lineno);
|
||||
free(copy);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (!strncmp(s, "tokens=", 7)) {
|
||||
if (!sp_usage_limit_parse(s + 7, &d->tok_limit,
|
||||
&d->tok_window)) {
|
||||
sp_buf_printf(err, "config: %s:%u: tokens= wants "
|
||||
"N/window, e.g. 2000000/7d", path, lineno);
|
||||
free(copy);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (!strncmp(s, "kind=", 5))
|
||||
snprintf(d->kind, sizeof(d->kind), "%s", s + 5);
|
||||
else if (!strncmp(s, "cmd=", 4))
|
||||
@@ -446,6 +465,19 @@ load_one(struct sp_config *c, const char *path, struct sp_buf *err)
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(key, "limit") || !strcmp(key, "tokens")) {
|
||||
int lim = key[0] == 'l';
|
||||
|
||||
if (!sp_usage_limit_parse(val,
|
||||
lim ? &c->req_limit : &c->tok_limit,
|
||||
lim ? &c->req_window : &c->tok_window)) {
|
||||
sp_buf_printf(err, "config: %s:%u: %s wants "
|
||||
"N/window, e.g. %s", path, lineno, key,
|
||||
lim ? "45/5h" : "2000000/7d");
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else if (!strcmp(key, "steps"))
|
||||
c->steps = (size_t)strtoul(val, NULL, 10);
|
||||
else if (!strcmp(key, "max_bytes"))
|
||||
|
||||
@@ -66,6 +66,9 @@ struct sp_delegate {
|
||||
char key_env[64];
|
||||
char env[SP_CONFIG_MAX_ENV][SP_CONFIG_ENV_LEN]; /* NAME=VALUE */
|
||||
size_t nenv;
|
||||
/* the allowance, 0: none declared (see usage.h) */
|
||||
unsigned long req_limit, tok_limit;
|
||||
long req_window, tok_window;
|
||||
};
|
||||
|
||||
struct sp_config {
|
||||
@@ -78,6 +81,8 @@ struct sp_config {
|
||||
char endpoint[512];
|
||||
char model[256];
|
||||
char key_env[64];
|
||||
unsigned long req_limit, tok_limit; /* of the main provider */
|
||||
long req_window, tok_window;
|
||||
char transport[32];
|
||||
char theme[64];
|
||||
char system_prompt[1024];
|
||||
|
||||
+27
-1
@@ -36,6 +36,7 @@
|
||||
#include "i18n.h"
|
||||
#include "mcp.h"
|
||||
#include "config.h"
|
||||
#include "usage.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
@@ -134,6 +135,15 @@ tool_delegate_list(const char *args, struct sp_buf *out, void *ctx)
|
||||
sp_buf_printf(out, "%s\n tags: %s\n kind: %s prio: %d\n",
|
||||
d->id, d->tags[0] != '\0' ? d->tags : "(keine)",
|
||||
d->kind[0] != '\0' ? d->kind : "openai", d->prio);
|
||||
{
|
||||
struct sp_usage u;
|
||||
char b[160];
|
||||
|
||||
sp_usage_get(d->id, d->req_limit, d->req_window,
|
||||
d->tok_limit, d->tok_window, &u);
|
||||
sp_usage_brief(&u, b, sizeof(b));
|
||||
sp_buf_printf(out, " usage: %s\n", b);
|
||||
}
|
||||
sp_buf_printf(out, " model: %s\n", d->model[0] != '\0' ?
|
||||
d->model : "(cli)");
|
||||
sp_buf_printf(out, " endpoint: %s%s%s\n", d->endpoint[0] != '\0' ?
|
||||
@@ -216,6 +226,7 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted,
|
||||
const struct sp_delegate *best = NULL;
|
||||
char task_lc[1024], want_lc[256];
|
||||
int best_score = 0;
|
||||
double best_frac = 2;
|
||||
size_t i;
|
||||
|
||||
lower(task_lc, sizeof(task_lc), task != NULL ? task : "");
|
||||
@@ -227,8 +238,14 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted,
|
||||
int score = 0;
|
||||
char reason[192] = "";
|
||||
|
||||
struct sp_usage u;
|
||||
|
||||
if (sp_cooldown_until(d->id) != 0)
|
||||
continue; /* benched: at its limit until the reset */
|
||||
sp_usage_get(d->id, d->req_limit, d->req_window,
|
||||
d->tok_limit, d->tok_window, &u);
|
||||
if (u.frac >= 1.0)
|
||||
continue; /* its own allowance is used up */
|
||||
|
||||
snprintf(tags, sizeof(tags), "%s", d->tags);
|
||||
for (t = strtok_r(tags, ",", &save); t != NULL;
|
||||
@@ -269,10 +286,18 @@ choose(const struct sp_config *cfg, const char *task, const char *wanted,
|
||||
}
|
||||
if (score > best_score ||
|
||||
(score == best_score && best != NULL &&
|
||||
d->prio > best->prio)) {
|
||||
(u.frac + 0.05 < best_frac ||
|
||||
(u.frac <= best_frac + 0.05 && d->prio > best->prio)))) {
|
||||
best_score = score;
|
||||
best = d;
|
||||
best_frac = u.frac;
|
||||
snprintf(why, whylen, "%s", reason);
|
||||
if (u.frac >= 0) {
|
||||
size_t n = strlen(why);
|
||||
|
||||
snprintf(why + n, whylen - n, "%s%.0f%% used",
|
||||
n > 0 ? ", " : "", u.frac * 100.0);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (score_out != NULL)
|
||||
@@ -461,6 +486,7 @@ run_child(const struct del_ctx *c, const struct sp_delegate *d,
|
||||
pid_t child;
|
||||
const char *key_env = d->key_env[0] != '\0' ? d->key_env : NULL;
|
||||
|
||||
sp_usage_record(d->id, 0, 0);
|
||||
if (pipe(fds) != 0) {
|
||||
sp_buf_printf(err, "delegate: pipe: %s", strerror(errno));
|
||||
return -1;
|
||||
|
||||
+5
-1
@@ -29,6 +29,7 @@
|
||||
#include "curl.h"
|
||||
#include "mcp.h"
|
||||
#include "providers.h"
|
||||
#include "usage.h"
|
||||
#include "splinter.h"
|
||||
#include "splinter_plugin.h"
|
||||
|
||||
@@ -200,7 +201,10 @@ sp_provider_use(struct sp_agent *ag, const struct sp_config *cfg, size_t idx,
|
||||
sp_buf_puts(err, "no such provider");
|
||||
return -1;
|
||||
}
|
||||
return sp_provider_apply(ag, p.endpoint, p.model, p.key_env, err);
|
||||
if (sp_provider_apply(ag, p.endpoint, p.model, p.key_env, err) != 0)
|
||||
return -1;
|
||||
sp_usage_set_active(p.main ? "main" : p.id);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
+343
@@ -0,0 +1,343 @@
|
||||
/* splinter -- usage accounting per provider; see usage.h. */
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "usage.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, "limit, back in %s",
|
||||
sp_usage_until(u->benched_until, t, sizeof(t)));
|
||||
} else if (u->frac < 0) {
|
||||
snprintf(buf, cap, "%lu 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, ", room in %s",
|
||||
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';
|
||||
}
|
||||
|
||||
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, " (%lu/%lu calls)", u.req_used,
|
||||
u.req_limit);
|
||||
if (u.tok_limit > 0)
|
||||
sp_buf_printf(out, " (%lu/%lu tokens)", u.tok_used,
|
||||
u.tok_limit);
|
||||
sp_buf_puts(out, "\n");
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
/* splinter -- how much of each provider's allowance is used. */
|
||||
#ifndef SP_USAGE_H
|
||||
#define SP_USAGE_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include "splinter.h"
|
||||
|
||||
struct sp_config;
|
||||
|
||||
/*
|
||||
* Subscriptions give an allowance per window ("45 requests per 5 hours",
|
||||
* "2M tokens per week"), mostly without an API that says how much is left.
|
||||
* So splinter counts for itself: every call is one line in
|
||||
* ~/.splinter/usage/<id>.log, shared by every splinter on the machine, and the
|
||||
* limits come from the config ("limit = 45/5h", "tokens = 2000000/7d").
|
||||
*/
|
||||
struct sp_usage {
|
||||
unsigned long req_used, tok_used;
|
||||
unsigned long req_limit, tok_limit; /* 0: not limited */
|
||||
long req_window, tok_window; /* seconds */
|
||||
double frac; /* most used of the limits, -1: no limit */
|
||||
long reset_at; /* when the oldest counted call falls out */
|
||||
long benched_until; /* a refused call benched it until */
|
||||
};
|
||||
|
||||
/* "5h", "30m", "7d", "2w", "90s", plain seconds; 0 when it is not a window */
|
||||
long sp_usage_window(const char *s);
|
||||
/* "45/5h" -> 45 and 18000; 0 when malformed */
|
||||
int sp_usage_limit_parse(const char *s, unsigned long *n, long *window);
|
||||
|
||||
/* count one call (and its tokens, 0 when unknown) against 'id' */
|
||||
void sp_usage_record(const char *id, long tok_in, long tok_out);
|
||||
/* the figures for 'id' given its limits; works without limits (frac -1) */
|
||||
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);
|
||||
/* the same, with the limits looked up in the config ("main" or a delegate) */
|
||||
void sp_usage_for(const struct sp_config *cfg, const char *id,
|
||||
struct sp_usage *u);
|
||||
|
||||
/* which provider the main conversation is on right now ("main" at start) */
|
||||
void sp_usage_set_active(const char *id);
|
||||
const char *sp_usage_active(void);
|
||||
|
||||
/* "5h 62%" style summary of one provider; returns buf */
|
||||
char *sp_usage_brief(const struct sp_usage *u, char *buf, size_t cap);
|
||||
/* "3h20m" until 'epoch' */
|
||||
char *sp_usage_until(long epoch, char *buf, size_t cap);
|
||||
|
||||
/* one line per provider, for /usage and the delegates tool */
|
||||
void sp_usage_report(const struct sp_config *cfg, struct sp_buf *out);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* splinter -- usage accounting: windows, limits, the log, the fraction.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "splinter.h"
|
||||
#include "config.h"
|
||||
#include "usage.h"
|
||||
|
||||
static int failures;
|
||||
|
||||
#define CHECK(cond) do { \
|
||||
if (!(cond)) { \
|
||||
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
|
||||
failures++; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
char dir[] = "/var/tmp/splinter-usage-XXXXXX";
|
||||
char path[256];
|
||||
unsigned long n = 0;
|
||||
long w = 0, now = (long)time(NULL);
|
||||
struct sp_usage u;
|
||||
FILE *f;
|
||||
int i;
|
||||
|
||||
CHECK(sp_usage_window("5h") == 18000);
|
||||
CHECK(sp_usage_window("7d") == 604800);
|
||||
CHECK(sp_usage_window("2w") == 1209600);
|
||||
CHECK(sp_usage_window("90") == 90);
|
||||
CHECK(sp_usage_window("5x") == 0);
|
||||
CHECK(sp_usage_window("") == 0);
|
||||
CHECK(sp_usage_limit_parse("45/5h", &n, &w) == 1 && n == 45 && w == 18000);
|
||||
CHECK(sp_usage_limit_parse("45", &n, &w) == 0);
|
||||
CHECK(sp_usage_limit_parse("0/5h", &n, &w) == 0);
|
||||
CHECK(sp_usage_limit_parse("5/zz", &n, &w) == 0);
|
||||
|
||||
CHECK(mkdtemp(dir) != NULL);
|
||||
setenv("SPLINTER_USAGE_DIR", dir, 1);
|
||||
setenv("SPLINTER_COOLDOWN_DIR", dir, 1);
|
||||
|
||||
/* nothing recorded: zero used, a limit gives 0 %, none gives -1 */
|
||||
sp_usage_get("a", 10, 3600, 0, 0, &u);
|
||||
CHECK(u.req_used == 0 && u.frac == 0.0);
|
||||
sp_usage_get("a", 0, 0, 0, 0, &u);
|
||||
CHECK(u.frac < 0);
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
sp_usage_record("a", 100, 50);
|
||||
sp_usage_get("a", 10, 3600, 0, 0, &u);
|
||||
CHECK(u.req_used == 4);
|
||||
CHECK(u.frac > 0.39 && u.frac < 0.41);
|
||||
CHECK(u.reset_at >= now + 3590 && u.reset_at <= now + 3601);
|
||||
sp_usage_get("a", 0, 0, 1000, 3600, &u);
|
||||
CHECK(u.tok_used == 600);
|
||||
CHECK(u.frac > 0.59 && u.frac < 0.61);
|
||||
/* the fuller of the two limits decides */
|
||||
sp_usage_get("a", 5, 3600, 1000, 3600, &u);
|
||||
CHECK(u.frac > 0.79 && u.frac < 0.81);
|
||||
|
||||
/* an old line is outside the window */
|
||||
snprintf(path, sizeof(path), "%s/b.log", dir);
|
||||
f = fopen(path, "w");
|
||||
CHECK(f != NULL);
|
||||
fprintf(f, "%ld 0 0\n%ld 0 0\n", now - 7200, now - 10);
|
||||
fclose(f);
|
||||
sp_usage_get("b", 10, 3600, 0, 0, &u);
|
||||
CHECK(u.req_used == 1);
|
||||
|
||||
/* a benched provider says so */
|
||||
sp_cooldown_set("b", now + 600, "test");
|
||||
sp_usage_get("b", 10, 3600, 0, 0, &u);
|
||||
CHECK(u.benched_until > now);
|
||||
|
||||
/* an id cannot leave the directory */
|
||||
sp_usage_record("../evil", 0, 0);
|
||||
snprintf(path, sizeof(path), "%s/../evil.log", dir);
|
||||
CHECK(access(path, F_OK) != 0);
|
||||
|
||||
{
|
||||
char b[128];
|
||||
|
||||
sp_usage_get("a", 10, 3600, 0, 0, &u);
|
||||
sp_usage_brief(&u, b, sizeof(b));
|
||||
CHECK(strstr(b, "1h 40%") != NULL);
|
||||
}
|
||||
|
||||
snprintf(path, sizeof(path), "rm -rf %s", dir);
|
||||
if (system(path) != 0)
|
||||
failures++;
|
||||
if (failures == 0)
|
||||
puts("test_usage: ok");
|
||||
return failures != 0;
|
||||
}
|
||||
Reference in new issue
Block a user