Files
openchamber-android-native/third_party/mygo/internal/idlemem/mem_linux.go
T
OpenChamber 6c0ee61750 feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set:

- servers: saved servers, passwordless and password login, proxy,
  self-signed certificates, per-server device tokens (Keystore), connect
  form, instance list sheet with switch/disconnect
- sessions: virtualized list, search, new session, rename/archive/delete,
  running indicator, event stream keeps it live
- chat: pushed window with system back bar, streaming Markdown via
  ui/markdown, reasoning and tool rows with image previews, footer menu
  (read aloud via a Kotlin TTS plugin, copy, fork), user message menu
  (copy, revert, fork), context usage ring and detail sheet, attachments
  (photos/files via the document picker), permissions
- release builds allow plain http:// LAN servers (Go's net/http dials
  directly, no cleartext manifest needed)
- a local fork of MyGo (third_party/mygo) lowers the Android host's
  minSdk to 24 (Android 7.0); Go 1.27.4 via mise

Version 0.3.0.
2026-11-18 13:22:59 +08:00

88 lines
2.0 KiB
Go

//go:build !android
package main
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
)
type state struct{}
func before() state { return state{} }
// usage counts the app's descendants: WebKitGTK's web and network
// processes, and what they run.
func usage(pid int, _ state) (app, total uint64, procs []int, err error) {
procs = append([]int{pid}, descendants(pid)...)
for i, p := range procs {
pss, err := pssOf(p)
if err != nil {
if i == 0 {
return 0, 0, nil, err
}
continue
}
if i == 0 {
app = pss
}
total += pss
}
return app, total, procs, nil
}
// pssOf returns the proportional set size of pid: its own pages, and its
// share of those it shares.
func pssOf(pid int) (uint64, error) {
b, err := os.ReadFile(fmt.Sprintf("/proc/%d/smaps_rollup", pid))
if err != nil {
return 0, err
}
for line := range strings.SplitSeq(string(b), "\n") {
if v, ok := strings.CutPrefix(line, "Pss:"); ok {
kb, err := strconv.ParseUint(strings.TrimSpace(strings.TrimSuffix(strings.TrimSpace(v), "kB")), 10, 64)
return kb << 10, err
}
}
return 0, fmt.Errorf("no Pss in /proc/%d/smaps_rollup", pid)
}
// descendants returns the processes pid started, and theirs.
func descendants(pid int) []int {
entries, _ := os.ReadDir("/proc")
children := map[int][]int{}
for _, e := range entries {
p, err := strconv.Atoi(e.Name())
if err != nil {
continue
}
b, err := os.ReadFile("/proc/" + e.Name() + "/stat")
if err != nil {
continue
}
// pid (comm) state ppid …, where comm may hold anything.
f := strings.Fields(string(b[strings.LastIndexByte(string(b), ')')+1:]))
if len(f) < 2 {
continue
}
if ppid, err := strconv.Atoi(f[1]); err == nil {
children[ppid] = append(children[ppid], p)
}
}
var out []int
for queue := children[pid]; len(queue) > 0; queue = queue[1:] {
out = append(out, queue[0])
queue = append(queue, children[queue[0]]...)
}
return out
}
// stop asks the app to quit, which its WebKit processes follow.
func stop(p *os.Process, _ []int) error {
p.Signal(syscall.SIGTERM)
return nil
}