Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ On Apple Silicon macOS, downloads and imports use Android 15 Google Play `arm64-
- The disk is `/data/local/tmp/modavd-dldisk/<package>/dl.img` in the guest, a sparse file of at most 16 GB; space of deleted packages goes back to the data disk. Downloading is faster than unpacking, so finished packages queue up on the disk: during a full resource download the data disk's peak use is about 6 GB higher than before. No disk is made when the data disk has less than 2 GB free.
- The list holds Arknights, official `com.hypergryph.arknights` and bilibili `com.hypergryph.arknights.bilibili` (not tested on the latter; the folder is the same), and Arknights: Endfield `com.hypergryph.endfield` (same downloader, same folder and write pattern).
- Boot with the environment variable `MODAVD_DL_DISK=off` to move unfinished downloads back into the plain folder, delete the disk and start no watcher.
- Arknights' resource index and manifests (official and bilibili) are watched (guest script `modavd-reswatch.sh`, Windows only, read-only), to find out why the game now and then downloads all its resources again (ModAVD/ModAVD#89).
- A watcher looks at `*.idx`, `hot_update_list.json` and `persistent_res_list.json` in `Bundles` every 3 seconds and logs a line to the guest log only when one changed (size, modification time, md5); when the game reports a resource load error, it logs at once what those files look like in each storage view, the memory state and the kernel log. Problem reports carry the guest log.
- Nothing is written into the game's folders, and the log is not read while neither game is installed. Boot with the environment variable `MODAVD_RES_WATCH=off` to stop it and start none.
- Honkai: Star Rail (official `com.miHoYo.hkrpg` and bilibili `com.miHoYo.hkrpg.bilibili`, not tested on the latter) gets its display mode set to Vulkan in advance (guest script `modavd-vkmode.sh`, Windows only). At its first start the game picks OpenGL ES on its own and closes on ModAVD about fifteen seconds later; in Vulkan mode (Normal Mode under Settings > Graphics > Display Settings in the game) it runs.
- A watcher looks every 3 seconds: once the game is installed, before its first start, it writes `vulkancaps.bin` and `customvulkancaps.bin` in the game's `Android/data/<package>/files`. A game already stuck in OpenGL ES after such a crash is switched to Vulkan as well, at the next boot or once it has exited.
- A display mode chosen in the game's settings (`customvulkancaps.bin` is there and not empty) is left alone; so is a running game; no folder is made for a game that is not installed.
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ Apple Silicon macOS 的下载与导入使用 Android 15 Google Play `arm64-v8a`
- 盘是客体里的 `/data/local/tmp/modavd-dldisk/<包名>/dl.img`,一个最大 16 GB 的稀疏文件,删掉的包占的空间会还给数据盘。下载比解压快,下好的包会先堆在盘里:下整包资源时,数据盘的占用高峰比以前多约 6 GB。数据盘剩余不到 2 GB 时不建盘。
- 名单里是明日方舟官服 `com.hypergryph.arknights`、B 服 `com.hypergryph.arknights.bilibili`(B 服没有实测,目录相同),以及明日方舟:终末地 `com.hypergryph.endfield`(同一套下载器,目录和写法相同)。
- 设环境变量 `MODAVD_DL_DISK=off` 再开机:把盘里还没下完的搬回普通目录,删掉盘,不再起守护。
- 明日方舟(官服、B 服)的资源索引和清单有人盯着(客体脚本 `modavd-reswatch.sh`,只在 Windows 上,只读),用来查偶尔整包重下的原因(ModAVD/ModAVD#89)。
- 守护每 3 秒看一次 `Bundles` 里的 `*.idx`、`hot_update_list.json`、`persistent_res_list.json`,文件变了才往客体日志记一行大小、修改时间和 md5;游戏报资源加载失败时,当场记下这些文件在几处存储视图里的状态、内存和内核日志。问题包带着客体日志。
- 不往游戏目录写任何东西;没装这两个游戏时不读日志。设环境变量 `MODAVD_RES_WATCH=off` 再开机就停掉、不再起。
- 崩坏:星穹铁道(官服 `com.miHoYo.hkrpg`、B 服 `com.miHoYo.hkrpg.bilibili`,B 服没有实测)的显示模式预先设成 Vulkan(客体脚本 `modavd-vkmode.sh`,只在 Windows 上)。游戏第一次启动时自己选 OpenGL ES,在 ModAVD 上十几秒后闪退;在游戏设置里的显示设置(Display Settings)选 Vulkan 模式就正常。
- 守护每 3 秒看一次:游戏装上以后、第一次启动之前,在它的 `Android/data/<包名>/files` 里写好 `vulkancaps.bin`、`customvulkancaps.bin`。已经因为闪退停在 OpenGL ES 的,下次开机或游戏退出后也改成 Vulkan。
- 在游戏设置里选过显示模式的(`customvulkancaps.bin` 已经在、不是空文件)不动;游戏开着时不动;没装的游戏不建任何目录。
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/89-reswatch.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
问题包记下明日方舟每次启动的结果、资源出错当场的文件状态和内核日志、关机时是否在下载 (ModAVD/ModAVD#89)
Problem reports log each Arknights start, its resource file state at a load error, the kernel log, downloads at shutdown
4 changes: 4 additions & 0 deletions internal/emu/afterboot.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,10 @@ func (m *Manager) afterBoot(ctx context.Context, inst *avd.Instance, mode bootSe
// 12) download disks (dldisk.go). After the guest library fixes (0a): the framework restart there remounts the
// shared storage, and a disk mounted on it before would be gone with it
func() { m.setupDownloadDisk(ctx, i) },
// 12a) Arknights' resource files watched, and what they hold when the game cannot load them (reswatch.go,
// ModAVD/ModAVD#89). Also after 0a: a scene compares /data/media with the FUSE view, which the framework
// restart there mounts anew. Every mode, an adopted instance too: the script starts no second watcher
func() { m.setupResWatch(ctx, i) },
// 12b) listed games that have not chosen a display mode start in Vulkan (vkmode.go, ModAVD#222)
func() { m.setupVulkanMode(ctx, i) },
// 13) the guest's clock source into the log, its kernel log's clock lines saved when an x86_64 guest is not on
Expand Down
2 changes: 2 additions & 0 deletions internal/emu/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (
// the sync leaves of the 4 s, at least 1 s) + terminate wait 1 => 4 + 2 + 2 + 2 + 14 + 2 + 1 + 1 = 28
// (fallbackKillTimeout);
// private adb unusable: 4 + console kill 3 + exit wait 12 + terminate wait 5 = 24; Force: 4 + sync 3 + 5 = 12.
// Between prepare and HOME the graceful path records what it needs for ModAVD/ModAVD#89 (stop_record.go), within
// what prepare left of those 4 s minus 1 s (beforeShutdownBudget), so the 14 s for Android and these sums stand.
adbServerStartOnClose = 4 * time.Second
homeTimeout = 2 * time.Second
shutdownSyncTimeout = 2 * time.Second
Expand Down
96 changes: 96 additions & 0 deletions internal/emu/reswatch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package emu

import (
"context"
"fmt"
"os"
"strings"
"time"

"modavd/internal/guest"
"modavd/internal/root"
)

// The resource file watch (guest.ResWatch, ModAVD/ModAVD#89). Now and then Arknights fails right after START on its
// title page ("[ResourceManager] Init failed"), takes its local resources for invalid and downloads all of them again
// at the next start. The cause is unknown; what decides between the emulator's storage and the game is what the game
// read at that moment. A guest watcher logs every change of the game's resource index and manifests (size, mtime,
// md5), and on the game's error what those files hold in each view of the storage, against the last pass, plus the
// memory state and the kernel log: all in logcat, which the problem report carries. Read-only; the script's header has
// the details.
//
// On by default: it is for the user's machine, where the failure happens and where nobody would turn on a switch
// beforehand. Windows only so far, like the download disk next to it.

// envResWatch set to "off" stops the watcher and starts none.
const envResWatch = "MODAVD_RES_WATCH"

// resWatchTimeout: daemon only starts the two loops in the background and waits up to a second for them (the
// baseline pass with its md5s runs in the watcher, not here).
const resWatchTimeout = 20 * time.Second

// setupResWatch starts the watcher and the logcat reader of the resource files, unless they run already (an adopted
// instance: the script starts at most one of each).
func (m *Manager) setupResWatch(ctx context.Context, i int) {
if !root.CurrentPlatform().Windows() {
return
}
remote, ok := m.pushGuestScript(ctx, i, guest.ResWatch)
if !ok {
return
}
if strings.EqualFold(strings.TrimSpace(os.Getenv(envResWatch)), "off") {
m.shell(ctx, i, "su 0 sh "+remote+" off", resWatchTimeout)
m.Log.Infof("实例 %d 资源文件监视: 已按 %s=off 关闭", i, envResWatch)
return
}
started := m.shell(ctx, i, "su 0 sh "+remote+" daemon", resWatchTimeout).Out
status := m.shell(ctx, i, "su 0 sh "+remote+" status", 0).Out
if warn, msg := resWatchReport(i, started, status); warn {
m.Log.Warnf("%s", msg)
} else {
m.Log.Infof("%s", msg)
}
}

// resWatchReport judges `modavd-reswatch.sh daemon` + `status`: running when both the pass loop and the logcat loop
// list a pid. The message names the listed games that are installed and what the script says about each.
func resWatchReport(i int, started, status string) (warn bool, msg string) {
lines := parseStatusLines(status)
if lines["watchers"] == "" || lines["errwatch"] == "" {
return true, fmt.Sprintf("实例 %d 资源文件监视没起来: %s", i, tail(strings.TrimSpace(started)+" / "+status, 300))
}
var games []string
for _, line := range outputLines(status) {
pkg, state, ok := strings.Cut(line, ":")
state = strings.TrimSpace(state)
if !ok || pkg == "watchers" || pkg == "errwatch" || state == "" || state == "not installed" {
continue
}
games = append(games, pkg+"("+resWatchState(state)+")")
}
if len(games) == 0 {
return false, fmt.Sprintf("实例 %d 资源文件监视: 守护已起(pid %s,读日志 pid %s),名单里的游戏都没装", i,
lines["watchers"], lines["errwatch"])
}
return false, fmt.Sprintf("实例 %d 资源文件监视: 守护已起(pid %s,读日志 pid %s);%s", i, lines["watchers"],
lines["errwatch"], strings.Join(games, ","))
}

// resWatchState puts the script's status of a package into the log's language: "not scanned yet", or "watching N
// files, M missing" with "; last scene at <time>" after it; one it does not know stays as it is.
func resWatchState(state string) string {
if state == "not scanned yet" {
return "还没扫过"
}
watched, scene, _ := strings.Cut(state, "; last scene at ")
var files, missing int
if n, err := fmt.Sscanf(watched, "watching %d files, %d missing", &files, &missing); err != nil || n != 2 {
return state
}
text := fmt.Sprintf("盯着 %d 个文件,%d 个不在", files, missing)
if scene = strings.TrimSpace(scene); scene != "" {
text += fmt.Sprintf(",上次出错现场 %s", scene)
}
return text
}
40 changes: 40 additions & 0 deletions internal/emu/reswatch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package emu

import "testing"

func TestResWatchReport(t *testing.T) {
// both loops run, one listed game is installed and watched, with a scene recorded
status := "watchers: 5481 \r\nerrwatch: 5490 \r\ncom.hypergryph.arknights: watching 5 files, 1 missing; last scene at 11-29 12:00:01\r\n" +
"com.hypergryph.arknights.bilibili: not installed\r\n"
warn, msg := resWatchReport(3, "", status)
if warn || msg != "实例 3 资源文件监视: 守护已起(pid 5481,读日志 pid 5490);com.hypergryph.arknights(盯着 5 个文件,1 个不在,上次出错现场 11-29 12:00:01)" {
t.Errorf("watched: %v %q", warn, msg)
}
// none of them installed
warn, msg = resWatchReport(3, "", "watchers: 5481\nerrwatch: 5490\ncom.hypergryph.arknights: not installed\n")
if warn || msg != "实例 3 资源文件监视: 守护已起(pid 5481,读日志 pid 5490),名单里的游戏都没装" {
t.Errorf("none installed: %v %q", warn, msg)
}
// every state the script reports, and one this build does not know
for state, want := range map[string]string{
"not scanned yet": "还没扫过",
"watching 4 files, 0 missing": "盯着 4 个文件,0 个不在",
"something new": "something new",
} {
warn, msg = resWatchReport(1, "", "watchers: 7\nerrwatch: 8\ncom.hypergryph.arknights: "+state+"\n")
if warn || msg != "实例 1 资源文件监视: 守护已起(pid 7,读日志 pid 8);com.hypergryph.arknights("+want+")" {
t.Errorf("%s: %v %q", state, warn, msg)
}
}
// either loop missing: a warning with what the script said
warn, msg = resWatchReport(2, "oops\n", "watchers: 7\nerrwatch: \ncom.hypergryph.arknights: not installed\n")
if !warn || msg != "实例 2 资源文件监视没起来: oops / watchers: 7\nerrwatch: \ncom.hypergryph.arknights: not installed" {
t.Errorf("no logcat loop: %v %q", warn, msg)
}
if warn, _ = resWatchReport(2, "", "watchers: \nerrwatch: 8\n"); !warn {
t.Error("no pass loop: no warning")
}
if warn, _ = resWatchReport(2, "", ""); !warn {
t.Error("empty status: no warning")
}
}
17 changes: 16 additions & 1 deletion internal/emu/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ package emu
// look at the guest (guestShutdownProbe, at most shutdownProbeTimeout), inside that same window, so it moves no limit.
// Making the private adb usable (adb.Client.PrepareForClose) takes at most adbServerStartOnClose, the try to bring an
// offline instance back (ModAVD/ModAVD#292) included. adb commands here never re-register (that costs 5 s more),
// and starting the private server counts against the command's own limit (TotalTimeout).
// and starting the private server counts against the command's own limit (TotalTimeout). Between prepare and HOME,
// the normal path records whether a game is downloading and saves the guest kernel log (stop_record.go,
// ModAVD/ModAVD#89), within beforeShutdownBudget: only what leaves Android its worst-case 14 s, so no limit moves.
//
// A stop that comes while a start holds the start lock but has not launched qemu yet (prechecks, the private server
// ...) waits for that qemu, up to startingQemuWait (10 s), and keeps holding the stop lock all the while. A start
Expand Down Expand Up @@ -269,6 +271,9 @@ type stopOps struct {
// prepare makes the private adb server usable for this instance (adb.Client.PrepareForClose): what the server had
// of it (StopResult.AdbState), and "" when it is usable, else why not.
prepare func() (state, problem string)
// beforeShutdown records what a later look at the instance needs (ModAVD/ModAVD#89, stop_record.go: whether a game
// is downloading, the guest kernel log) within budget; best effort, never fails. nil: nothing to record.
beforeShutdown func(budget time.Duration)
// home sends an app in the foreground home within timeout (guestHomeScript): sent, and adb's output when the
// command failed ("" when it ran).
home func(timeout time.Duration) (bool, string)
Expand Down Expand Up @@ -396,6 +401,11 @@ func runStop(ctx context.Context, log *logx.Logger, i int, force bool, started t
log.Warnf("ModAVD 实例 %d 私有 adb 用不了,不能正常关机也不能 sync,直接用控制台关: %s", i, problem)
return endQemu(ctx, log, i, "console_kill", false, consoleKillTimeout+closeTimeout, forceKillWait, ops)
}
// While the game is still in front and its downloader still runs; within a budget that leaves Android's window its
// worst-case minimum (stop_record.go). Only a cancel stops the shutdown here.
if err := beforeShutdown(ctx, log, i, time.Since(started), ops.beforeShutdown); err != nil {
return "graceful", false, err
}
// The foreground app goes home first and gets homeWait; the sync below takes what it wrote along (see the header
// comment). A failure is only a warning.
if err := sendHome(ctx, log, i, ops.home); err != nil {
Expand Down Expand Up @@ -518,6 +528,11 @@ func (m *Manager) stopOpsFor(ctx context.Context, i int, p host.Process) stopOps
}
return stopOps{
prepare: func() (string, string) { return m.ADB.PrepareForClose(ctx, console, adbServerStartOnClose) },
beforeShutdown: func(budget time.Duration) {
m.recordBeforeShutdown(i, budget, func(command string, timeout time.Duration) adb.Result {
return closeRun(timeout, "shell", command)
}, time.Now)
},
home: func(timeout time.Duration) (bool, string) {
res := closeRun(timeout, "shell", guestHomeScript)
if res.Code != 0 {
Expand Down
Loading
Loading