runtime

package
v0.23.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: Apache-2.0 Imports: 9 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const DefaultFamilyShare = 0.6

DefaultFamilyShare is the part of physical memory the family's models may take together (spec §11.1).

View Source
const Kind = "runtime"

Kind is the schema kind of runtime/<bundle id>.json.

View Source
const PasteWait = 500 * time.Millisecond

PasteWait is how long an app waits for another one's paste.

View Source
const UnloadIdleAfter = 60 * time.Second

UnloadIdleAfter: on TopicMemory, apps unload models idle this long.

Variables

View Source
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

func Budget(own, familyCap, othersLoaded int64) int64

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 FamilyCap

func FamilyCap(physical uint64) int64

FamilyCap is DefaultFamilyShare of physical memory.

func Idle

func Idle(l Loaded, now time.Time) bool

Idle reports whether a loaded model may be unloaded on a memory request.

func Live added in v0.8.0

func Live(f File) bool

Live reports whether a runtime file belongs to a running process (see ReadAll).

func OthersLoadedBytes

func OthersLoadedBytes(d family.Dir, self string) int64

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 Remove

func Remove(d family.Dir, bundleID string) error

Remove deletes the app's runtime file (at quit).

func RequestMemory

func RequestMemory(d family.Dir) error

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

func WaitPasting(ctx context.Context, d family.Dir, self string) bool

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.

func Write

func Write(d family.Dir, f File) error

Write writes the app's runtime file (schema, pid and time filled in) and posts TopicRuntime. Call it after loading or unloading a model, and around pasting.

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

func LoadedBy(d family.Dir, self, repo, revision string) []File

LoadedBy lists the other running apps that hold repo@revision loaded ("Also loaded in Dicta", TextAlsoLoadedIn).

func Others

func Others(d family.Dir, self string) []File

Others is ReadAll without self (a bundle id).

func Read

func Read(d family.Dir, bundleID string) (File, error)

Read reads one app's runtime file.

func ReadAll

func ReadAll(d family.Dir) ([]File, []error)

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

func (f File) LoadedBytes() int64

LoadedBytes is the memory the file says its models take.

func (File) MarshalJSON

func (f File) MarshalJSON() ([]byte, error)

func (*File) UnmarshalJSON

func (f *File) UnmarshalJSON(b []byte) error

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.

func (Loaded) MarshalJSON

func (l Loaded) MarshalJSON() ([]byte, error)

func (*Loaded) UnmarshalJSON

func (l *Loaded) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL