matrix0487 on Discord:
- The Requests card's line "↑ 13 GB · ↓ 13 MB · 图片文件省下 44…" was cut
with an ellipsis. It was one nowrap span.
- A row's details say "图片文件省下 10 MB" and should say "10 MB(3 张)".
The card line is now made of whole parts (usageParts, the pattern the
token line has since #2378). A card too narrow for all three puts the
saving on a line of its own. A part that is wider than the card by
itself (German at 420 wide) wraps inside itself; it is never cut.
The count:
- withDeepSeekFiles counts the image blocks it puts a file id in place
of, on each try, and upBytes keeps the most of any one try (a retry
sends the same images again, as the saved bytes are counted).
- It goes into usage.Record as `image_files` (omitempty) beside
image_files_saved.
- packedRow keeps Tries and ImageFiles in int16 each (clamped to
MaxInt16), so the row stays 152 bytes. request_snapshot carries the
new field, and heat_disk hashes Tries as before.
- The detail row says "{size} ({n} image|images)": zh "10 MB(3 张)",
zh-TW 張, ja 枚, de "Bild/Bilder", en "image/images".
- A row from before the count was kept has no image_files and shows
the size alone, with no parentheses. The card's period total gets no
count.
Run on mote, at this tree:
- gofmt -l (clean), go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build ./...: ok.
- go test -run 'TestRowImageFilesKept|TestRowBytesKept|TestRowsOfOneSession|
TestDeepSeek|Snapshot|Heat' ./internal/usage ./internal/gateway: ok.
- TestRowImageFilesKept: the count survives pack/unpack beside Tries,
100000 clamps to 32767, an old row reads 0, the JSON has
"image_files":3 and leaves out 0, and packedRow is 152 bytes.
- TestDeepSeekImageFilesCountedInRow: two images in one message and
one again in a later message make 3 in the stored row, twice, and a
request with no image makes 0.
- GUI, BROWSER=chromium and webkit: usage-ledger-bytes runs every
language at 1180 and 420. The 3-image and 1-image rows read in each
language's unit, singular in en and de, and the old row stays bare.
The reporter's period (13 GB, 13 MB, 44 MB) at 1180, 760 and 420 has
every part whole and inside its card.
- The 79 usage-*/led* GUI files (concurrency 2): 641 of 659 pass in
chromium and 637 of 654 in webkit. Every red is filed and red on
origin/main d2c29abe7 too (run alone there in chromium, the same 17
subtests): usage-agents-hide (#2276, #3414), usage-fold (#3415, #2163;
in webkit also a timeout in "several accounts"), usage-ledger's
return-to-live (#4078, #5048) and usage-overview-fold (#3947).
- Broken by hand, each red:
- The count left out of packedRow: TestRowImageFilesKept.
- The count not kept in withDeepSeekFiles:
TestDeepSeekImageFilesCountedInRow ("0 images by file, want 3").
- The detail's count dropped: usage-ledger-bytes in every language
("expected '10 MB(3 张)', actual '10 MB'").
- The in-part wrap dropped: "de big 420" ("cut: 0,0,0,11").
Not tried: a real DeepSeek account (the count rides the same path as the
saved bytes, which were tried for #5407), and Linux WebKitGTK.
Reported-by: matrix0487
238 lines
7.7 KiB
Go
238 lines
7.7 KiB
Go
package usage
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/binary"
|
|
"encoding/json"
|
|
"fmt"
|
|
"hash"
|
|
"math"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/sessions"
|
|
)
|
|
|
|
// The heatmap's settled days are kept on disk between runs (heat-v1.json).
|
|
// A started magpie paired the whole history for them, every gateway call
|
|
// with its session row, and so again every heatOldFresh: the cold start's
|
|
// peak, ~100 MB of a ~450 MB live heap, and seconds of CPU each time
|
|
// (#2278). They stand while what they were counted of is the same, which
|
|
// heatSettledKey names: the settled chunks, the recent chunks' rows before
|
|
// the cut, the threads' identities, the filter and the days, the code that
|
|
// counts them and what prices and names a call.
|
|
|
|
// settledCountVersion names how the settled days, a page's cells before
|
|
// the cut (page-v1.json) and Codex's effort (effort-v1.json) are counted.
|
|
// They were keyed by this package's source, as requests-v1.bin is: 14 of
|
|
// 39 releases changed it, and each such upgrade paired every user's whole
|
|
// history again on its first start, the cold start's peak (#2278). Raise
|
|
// it with any change to what a row counts as; TestSettledCountedAsVersioned
|
|
// fails until it is raised.
|
|
const settledCountVersion = "2"
|
|
|
|
func heatDiskPath() string {
|
|
return filepath.Join(filepath.Dir(sessions.CachePath()), "heat-v1.json")
|
|
}
|
|
|
|
// heatDiskEntry is the file: the key it was counted under, the cut and
|
|
// since it names, and the days.
|
|
type heatDiskEntry struct {
|
|
Key string `json:"key"`
|
|
Cut time.Time `json:"cut,omitzero"`
|
|
Since time.Time `json:"since,omitzero"`
|
|
Days []Day `json:"days"`
|
|
}
|
|
|
|
func heatDiskEntryRead() (heatDiskEntry, bool) {
|
|
b, err := os.ReadFile(heatDiskPath())
|
|
if err != nil {
|
|
return heatDiskEntry{}, false
|
|
}
|
|
var e heatDiskEntry
|
|
if json.Unmarshal(b, &e) != nil {
|
|
return heatDiskEntry{}, false
|
|
}
|
|
return e, true
|
|
}
|
|
|
|
// heatDiskRead is the days the file holds under key, or false.
|
|
func heatDiskRead(key string) ([]Day, bool) {
|
|
e, ok := heatDiskEntryRead()
|
|
if !ok || e.Key != key {
|
|
return nil, false
|
|
}
|
|
return e.Days, true
|
|
}
|
|
|
|
// heatDiskWrite keeps days before cut from since under key, through a
|
|
// file renamed over the last.
|
|
func heatDiskWrite(key string, cut, since time.Time, days []Day) error {
|
|
b, err := json.Marshal(heatDiskEntry{Key: key, Cut: cut, Since: since, Days: days})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
path := heatDiskPath()
|
|
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
|
return err
|
|
}
|
|
out, err := os.CreateTemp(filepath.Dir(path), ".heat-*.json")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer os.Remove(out.Name())
|
|
_, err = out.Write(b)
|
|
if closeErr := out.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(out.Name(), path)
|
|
}
|
|
|
|
// heatSettledKey names what the days before cut were counted of. A
|
|
// session file's chunk is named by its path, size and mtime under the
|
|
// same root and meta, any other chunk by its rows, and the counting by
|
|
// the call shards' version and settledCountVersion. False: the chunks
|
|
// can't be named so (a probe's chunk made again for each query).
|
|
func heatSettledKey(d heatDiskRef, cut, since time.Time, f Filter, identities map[string]sessions.CodexIdentity, settled [3][]*rowChunk, before heatSum) (string, bool) {
|
|
if d.root == "" {
|
|
return "", false
|
|
}
|
|
// the filter's zone by its name: %#v writes its pointer, another in
|
|
// each run
|
|
zone := ""
|
|
if f.Zone != nil {
|
|
zone = f.Zone.String()
|
|
}
|
|
f.Zone = nil
|
|
h := sha256.New()
|
|
fmt.Fprintf(h, "heat-v1\x00%s\x00%s\x00%s\x00%d\x00%d\x00%s\x00%#v\x00%s\x00%d:%d:%d\x00",
|
|
sessions.CallCacheVersion()+"\x00"+settledCountVersion, d.root, d.meta, cut.UnixNano(), since.UnixNano(), cut.Location(), f, zone, before.n, before.at, before.tokens)
|
|
hashCodexIdentities(h, identities)
|
|
var b []byte
|
|
rows := lentRows()
|
|
defer rows.release()
|
|
for k, set := range settled {
|
|
for _, c := range set {
|
|
if c.serial == 0 {
|
|
return "", false
|
|
}
|
|
b = fmt.Appendf(b[:0], "\x01%d\x00%s\x00%d\x00%d\x00%s\x00%d\x00", k, c.Computer, c.Count, c.Latest.UnixNano(), c.Source.Path, c.Source.Size)
|
|
if c.Source.Path != "" {
|
|
b = binary.AppendVarint(b, c.Source.Modified.UnixNano())
|
|
h.Write(b)
|
|
continue
|
|
}
|
|
h.Write(b)
|
|
if c.raw != nil {
|
|
// a gateway block by its rows as logged: meta names what
|
|
// prices them, and one not priced has no rows of its own
|
|
hashRows(h, rows.of(c.raw))
|
|
continue
|
|
}
|
|
hashRows(h, rows.of(c))
|
|
}
|
|
}
|
|
return fmt.Sprintf("%x", h.Sum(nil)), true
|
|
}
|
|
|
|
// hashRows writes an unpacked chunk's rows into h, each field as a number
|
|
// and each string as written: a row's time holds a pointer, its padding
|
|
// isn't written by every copy, and a chunk packed and the same unpacked
|
|
// number their strings and shapes apart. A shape is written where a row
|
|
// first has it, and by that row after.
|
|
func hashRows(h hash.Hash, c *rowChunk) {
|
|
b := make([]byte, 0, 32<<10)
|
|
text := func(i uint32) {
|
|
s := ""
|
|
if int(i) < len(c.Strings) {
|
|
s = c.Strings[i]
|
|
}
|
|
b = binary.AppendUvarint(b, uint64(len(s)))
|
|
b = append(b, s...)
|
|
}
|
|
shapes := map[uint32]int{}
|
|
for i := range c.Rows {
|
|
p := &c.Rows[i]
|
|
b = binary.AppendVarint(b, p.Time().UnixNano())
|
|
for _, v := range p.Varied {
|
|
text(v)
|
|
}
|
|
if first, ok := shapes[p.Shape]; ok {
|
|
b = binary.AppendUvarint(b, uint64(first))
|
|
} else {
|
|
shapes[p.Shape] = i
|
|
b = binary.AppendUvarint(b, uint64(i))
|
|
if int(p.Shape) < len(c.Shapes) {
|
|
for _, v := range c.Shapes[p.Shape] {
|
|
text(v)
|
|
}
|
|
}
|
|
}
|
|
for _, t := range p.Tokens {
|
|
b = binary.AppendVarint(b, int64(t))
|
|
}
|
|
b = binary.AppendVarint(b, p.Order)
|
|
b = binary.AppendVarint(b, p.RouteID)
|
|
b = binary.AppendUvarint(b, math.Float64bits(p.Cost))
|
|
b = binary.AppendUvarint(b, math.Float64bits(p.Credits))
|
|
for _, v := range [...]int32{p.Millis, p.TTFT, p.FirstText, p.Sent, p.Flow, p.FirstReasoning, p.Own, p.Retried, p.Queued, p.Masking, p.Status, int32(p.Tries), p.UpBytes, p.DownBytes, p.FilesSaved} {
|
|
b = binary.AppendVarint(b, int64(v))
|
|
}
|
|
b = binary.AppendUvarint(b, uint64(p.Flags))
|
|
if len(b) > 30<<10 {
|
|
h.Write(b)
|
|
b = b[:0]
|
|
}
|
|
}
|
|
h.Write(b)
|
|
}
|
|
|
|
// heatDiskRef is where a heatmap's settled days are kept and what prices
|
|
// and names its calls: the index's root and meta. Empty, they aren't kept.
|
|
type heatDiskRef struct {
|
|
root, meta string
|
|
}
|
|
|
|
// The cut moves a day each midnight, and every key names it: the first
|
|
// start of each day found none of its sums on disk and paired the whole
|
|
// history again, three times over (the page, the heatmap and Codex's
|
|
// effort), ~180 MB of a ~480 MB footprint each morning (#2278). Sums
|
|
// kept at an earlier cut still stand while that cut's key is the same:
|
|
// the days between are added from a pairing of the chunks they reach.
|
|
|
|
// splitAt is the chunks of sets with rows a pairing from cut on reaches,
|
|
// and the rest.
|
|
func splitAt(cut time.Time, sets [3][]*rowChunk) (recent, settled [3][]*rowChunk) {
|
|
edge := cut.Add(-heatReach)
|
|
for k, set := range sets {
|
|
for _, c := range set {
|
|
if c.Latest.IsZero() || !c.Latest.Before(edge) {
|
|
recent[k] = append(recent[k], c)
|
|
} else {
|
|
settled[k] = append(settled[k], c)
|
|
}
|
|
}
|
|
}
|
|
return recent, settled
|
|
}
|
|
|
|
// settledAt is sets split at an earlier cut, and the key of its settled
|
|
// chunks there, or false.
|
|
func settledAt(d heatDiskRef, cut, since time.Time, f Filter, identities map[string]sessions.CodexIdentity, sets [3][]*rowChunk) ([3][]*rowChunk, string, bool) {
|
|
recent, settled := splitAt(cut, sets)
|
|
if _, ok := pairSerials(settled[0], settled[1], settled[2]); !ok {
|
|
return recent, "", false
|
|
}
|
|
key, ok := heatSettledKey(d, cut, since, f, identities, settled, heatBefore(cut, recent))
|
|
return recent, key, ok
|
|
}
|
|
|
|
// chunksIn is how many chunks sets hold.
|
|
func chunksIn(sets [3][]*rowChunk) int {
|
|
return len(sets[0]) + len(sets[1]) + len(sets[2])
|
|
}
|