Files
openchamber-android-native/third_party/mygo/internal/idlemem/mem_darwin.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

97 lines
2.7 KiB
Go

//go:build !ios
package main
import (
"encoding/binary"
"fmt"
"os"
"strings"
"syscall"
"unsafe"
"github.com/ebitengine/purego"
)
var (
procPidRusage func(pid, flavor int32, buf unsafe.Pointer) int32
procListAllPids func(buf unsafe.Pointer, size int32) int32
procPidPath func(pid int32, buf unsafe.Pointer, size uint32) int32
// responsibleFor is private API: nil where libSystem lacks it.
responsibleFor func(pid int32) int32
)
func init() {
lib, err := purego.Dlopen("/usr/lib/libSystem.B.dylib", purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
}
purego.RegisterLibFunc(&procPidRusage, lib, "proc_pid_rusage")
purego.RegisterLibFunc(&procListAllPids, lib, "proc_listallpids")
purego.RegisterLibFunc(&procPidPath, lib, "proc_pidpath")
if _, err := purego.Dlsym(lib, "responsibility_get_pid_responsible_for_pid"); err == nil {
purego.RegisterLibFunc(&responsibleFor, lib, "responsibility_get_pid_responsible_for_pid")
}
}
// state is the WebKit processes that ran before the app started.
type state map[int32]bool
func before() state {
s := state{}
for _, pid := range webKit() {
s[pid] = true
}
return s
}
// usage counts the WebKit processes that started with the app and share
// its responsible process: WKWebView's, for its page, GPU and network.
func usage(pid int, s state) (app, total uint64, procs []int, err error) {
app, err = footprint(int32(pid))
if err != nil {
return 0, 0, nil, err
}
total, procs = app, []int{pid}
for _, p := range webKit() {
if s[p] || responsibleFor != nil && responsibleFor(p) != responsibleFor(int32(pid)) {
continue
}
if f, err := footprint(p); err == nil {
total += f
procs = append(procs, int(p))
}
}
return app, total, procs, nil
}
// footprint returns the physical footprint of pid: ri_phys_footprint of
// its rusage_info_v2, after a 16-byte UUID and seven counters.
func footprint(pid int32) (uint64, error) {
var info [512]byte
if procPidRusage(pid, 2, unsafe.Pointer(&info[0])) != 0 {
return 0, fmt.Errorf("proc_pid_rusage(%d) failed", pid)
}
return binary.LittleEndian.Uint64(info[72:]), nil
}
// webKit returns the processes of WebKit's XPC services.
func webKit() []int32 {
pids := make([]int32, max(procListAllPids(nil, 0), 4096)+256)
n := procListAllPids(unsafe.Pointer(&pids[0]), int32(4*len(pids)))
var out []int32
var path [4096]byte
for _, pid := range pids[:max(n, 0)] {
if l := procPidPath(pid, unsafe.Pointer(&path[0]), uint32(len(path))); l > 0 && strings.Contains(string(path[:l]), "/com.apple.WebKit.") {
out = append(out, pid)
}
}
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
}