Documentation
¶
Overview ¶
Package runtime is the family's running-process files and memory coordination (spec §11.1): each app that loads local models writes runtime/<bundle id>.json (pid, budget, loaded models, the "pasting" flag), and budgets its memory against what the other running apps hold.
cap := runtime.FamilyCap(runtime.PhysicalMemory())
budget := runtime.Budget(ownSetting, cap, runtime.OthersLoadedBytes(d, self))
runtime.Write(d, runtime.File{BundleID: self, BudgetBytes: budget, Loaded: loaded})
The package is named after the spec's directory; import it under another name (famruntime) beside the standard library's runtime.
Index ¶
- Constants
- Variables
- func Budget(own, familyCap, othersLoaded int64) int64
- func FamilyCap(physical uint64) int64
- func Idle(l Loaded, now time.Time) bool
- func Live(f File) bool
- func OthersLoadedBytes(d family.Dir, self string) int64
- func PhysicalMemory() uint64
- func Remove(d family.Dir, bundleID string) error
- func RequestMemory(d family.Dir) error
- func WaitPasting(ctx context.Context, d family.Dir, self string) bool
- func Write(d family.Dir, f File) error
- type File
- type Loaded
Constants ¶
DefaultFamilyShare is the part of physical memory the family's models may take together (spec §11.1).
const Kind = "runtime"
Kind is the schema kind of runtime/<bundle id>.json.
const PasteWait = 500 * time.Millisecond
PasteWait is how long an app waits for another one's paste.
const UnloadIdleAfter = 60 * time.Second
UnloadIdleAfter: on TopicMemory, apps unload models idle this long.
Variables ¶
var ( // TextAlsoLoadedIn: on a loaded model's row (spec §11.1). TextAlsoLoadedIn = family.Text{EN: "Also loaded in {app}", ZH: "{app} 也已加载"} )
Reference texts.
Functions ¶
func Budget ¶
Budget is an app's model memory budget (spec §11.1): min(own, familyCap − othersLoaded), never negative. own ≤ 0 means the app sets no limit of its own.
func Live ¶ added in v0.8.0
Live reports whether a runtime file belongs to a running process (see ReadAll).
func OthersLoadedBytes ¶
OthersLoadedBytes is the memory the other running apps' models take.
func PhysicalMemory ¶
func PhysicalMemory() uint64
PhysicalMemory is the machine's physical memory in bytes, 0 when unknown.
func RequestMemory ¶
RequestMemory asks the other apps to unload models idle for UnloadIdleAfter (spec §11.1). Retry the load once afterwards (after a short wait, or on TopicRuntime), and then load anyway.
func WaitPasting ¶
WaitPasting waits while another running app has Pasting set, at most PasteWait (or until ctx ends), polling every 20 ms. It reports whether the way is clear.
Types ¶
type File ¶
type File struct {
Schema string `json:"schema"`
BundleID string `json:"bundleId"`
PID int `json:"pid"`
// Exe is the writer's executable (v0.8.0): a reader takes the file
// for a live app only when the process with PID runs this executable,
// so a reused pid does not keep a crashed app's models counted
// (spec §6.3, §11.1). Absent in files of older writers.
Exe string `json:"exe,omitempty"`
Updated time.Time `json:"updated"`
BudgetBytes int64 `json:"budgetBytes"`
Loaded []Loaded `json:"loaded"`
// Pasting is set while the app writes the clipboard, sends ⌘V and
// restores the clipboard (voice-input apps); others wait for it.
Pasting bool `json:"pasting,omitempty"`
Extra family.Extra `json:"-"`
}
File is runtime/<bundle id>.json. Only its app writes it; it is removed when the app quits, and ignored when its process is gone.
func LoadedBy ¶
LoadedBy lists the other running apps that hold repo@revision loaded ("Also loaded in Dicta", TextAlsoLoadedIn).
func ReadAll ¶
ReadAll reads the runtime files of processes that still exist. Files of gone processes (a crash) are skipped, not deleted. A file is gone when its pid does not run, when it was written before the system started, or when its pid now runs another executable than the file's exe (a reused pid).
func (File) LoadedBytes ¶
LoadedBytes is the memory the file says its models take.
func (File) MarshalJSON ¶
func (*File) UnmarshalJSON ¶
type Loaded ¶
type Loaded struct {
Repo string `json:"repo"`
Revision string `json:"revision"`
Worker string `json:"worker,omitempty"` // the app's engine worker: "listen", "gpu"…
Bytes int64 `json:"bytes"`
// IdleSince is when the model last finished work; nil = busy.
IdleSince *time.Time `json:"idleSince"`
Extra family.Extra `json:"-"`
}
Loaded is a model a process holds in memory.