mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-19 21:06:09 +00:00
feat: profile PRoot costs and test fast-path libraries without an APK rebuild (#1528)
This commit is contained in:
1 parent
aca4b5dd3e
commit
815b93db5f
17 files changed
+578
-10
No files matched your search
@@ -124,3 +124,11 @@ Old kernel fixes:
|
||||
by the kernel instead. The original clone flags are preserved. When the kernel supplies a valid
|
||||
ID in the notification, normal setup is unchanged. No timers or background polling are added.
|
||||
Completely missing notifications and ambiguous matches for separate processes are not recovered.
|
||||
|
||||
Development diagnostics:
|
||||
|
||||
- `0018-syscall-handler-profile.patch` - `PROOT_PROFILE=1` enables per-syscall entry/exit handler
|
||||
counts, wall/CPU time, periodic cumulative JSON totals, and bounded slow-handler samples on
|
||||
stderr. `profile.h` is copied into the source tree by `build.sh`. It adds no ptrace stops;
|
||||
handler timing excludes tracee kernel time and scheduling before handler entry. See
|
||||
`docs/development/proot-performance.md` for the capture and library-override workflow.
|
||||
@@ -31,6 +31,7 @@ fetch() {
|
||||
fetch proot.tar.gz "https://github.com/termux/proot/archive/${PROOT_COMMIT}.tar.gz" "$PROOT_SHA256"
|
||||
fetch talloc.tar.gz "https://www.samba.org/ftp/talloc/talloc-${TALLOC_VERSION}.tar.gz" "$TALLOC_SHA256"
|
||||
cp "$HERE/statx_mnt_id.h" "$WORK/proot/src/tracee/statx_mnt_id.h"
|
||||
cp "$HERE/profile.h" "$WORK/proot/src/syscall/profile.h"
|
||||
for patch in "$HERE"/patches/*.patch; do
|
||||
patch -d "$WORK/proot" -p1 --forward --quiet < "$patch"
|
||||
done
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
--- a/src/syscall/syscall.c
|
||||
+++ b/src/syscall/syscall.c
|
||||
@@ -32,6 +32,7 @@
|
||||
#include "tracee/reg.h"
|
||||
#include "tracee/mem.h"
|
||||
#include "cli/note.h"
|
||||
+#include "syscall/profile.h"
|
||||
|
||||
/**
|
||||
* Copy in @path a C string (PATH_MAX bytes max.) from the @tracee's
|
||||
@@ -100,7 +101,7 @@
|
||||
return set_sysarg_data(tracee, path, strlen(path) + 1, reg);
|
||||
}
|
||||
|
||||
-void translate_syscall(Tracee *tracee)
|
||||
+static void translate_syscall_impl(Tracee *tracee)
|
||||
{
|
||||
const bool is_enter_stage = IS_IN_SYSENTER(tracee);
|
||||
int status;
|
||||
@@ -283,3 +284,18 @@
|
||||
else
|
||||
print_current_regs(tracee, 4, "sysexit end");
|
||||
}
|
||||
+
|
||||
+void translate_syscall(Tracee *tracee)
|
||||
+{
|
||||
+ if (!dd_profile_enabled()) {
|
||||
+ translate_syscall_impl(tracee);
|
||||
+ return;
|
||||
+ }
|
||||
+ int saved = errno;
|
||||
+ const bool entering = IS_IN_SYSENTER(tracee);
|
||||
+ uint64_t wall = dd_profile_clock(CLOCK_MONOTONIC);
|
||||
+ uint64_t cpu = dd_profile_clock(CLOCK_THREAD_CPUTIME_ID);
|
||||
+ errno = saved;
|
||||
+ translate_syscall_impl(tracee);
|
||||
+ dd_profile_record(get_sysnum(tracee, ORIGINAL), entering, tracee->pid, wall, cpu);
|
||||
+}
|
||||
@@ -0,0 +1,99 @@
|
||||
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
||||
#ifndef DROIDDECK_PROOT_PROFILE_H
|
||||
#define DROIDDECK_PROOT_PROFILE_H
|
||||
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct dd_profile_counter {
|
||||
uint64_t calls, wall_ns, cpu_ns, max_ns;
|
||||
};
|
||||
static struct dd_profile_counter dd_profile_counts[PR_NB_SYSNUM][2];
|
||||
static int dd_profile_on = -1;
|
||||
static uint64_t dd_profile_id, dd_profile_next, dd_profile_slow_ns = 1000000;
|
||||
static unsigned dd_profile_samples;
|
||||
|
||||
static uint64_t dd_profile_clock(clockid_t clock)
|
||||
{
|
||||
struct timespec ts;
|
||||
if (clock_gettime(clock, &ts) != 0) return 0;
|
||||
return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec;
|
||||
}
|
||||
|
||||
static void dd_profile_flush(void)
|
||||
{
|
||||
int saved = errno;
|
||||
for (unsigned nr = 0; nr < PR_NB_SYSNUM; nr++) {
|
||||
for (unsigned stage = 0; stage < 2; stage++) {
|
||||
const struct dd_profile_counter *c = &dd_profile_counts[nr][stage];
|
||||
if (!c->calls) continue;
|
||||
fprintf(stderr, "proot-profile {\"type\":\"total\",\"tracer\":%d,\"id\":%" PRIu64
|
||||
",\"syscall\":\"%s\",\"stage\":\"%s\",\"calls\":%" PRIu64
|
||||
",\"wall_ns\":%" PRIu64 ",\"cpu_ns\":%" PRIu64 ",\"max_ns\":%" PRIu64 "}\n",
|
||||
getpid(), dd_profile_id, stringify_sysnum(nr), stage ? "exit" : "enter",
|
||||
c->calls, c->wall_ns, c->cpu_ns, c->max_ns);
|
||||
}
|
||||
}
|
||||
errno = saved;
|
||||
}
|
||||
|
||||
static int dd_profile_enabled(void)
|
||||
{
|
||||
if (dd_profile_on >= 0) return dd_profile_on;
|
||||
int saved = errno;
|
||||
const char *value = getenv("PROOT_PROFILE");
|
||||
dd_profile_on = value && strcmp(value, "1") == 0;
|
||||
if (dd_profile_on) {
|
||||
value = getenv("PROOT_PROFILE_SLOW_US");
|
||||
if (value && *value) {
|
||||
char *end;
|
||||
unsigned long long us = strtoull(value, &end, 10);
|
||||
if (!*end && us > 0 && us <= 1000000000ULL) dd_profile_slow_ns = us * 1000;
|
||||
}
|
||||
dd_profile_id = dd_profile_clock(CLOCK_MONOTONIC);
|
||||
dd_profile_next = dd_profile_id + 5000000000ULL;
|
||||
atexit(dd_profile_flush);
|
||||
fprintf(stderr, "proot-profile {\"type\":\"start\",\"tracer\":%d,\"id\":%" PRIu64
|
||||
",\"slow_ns\":%" PRIu64 ",\"sample_limit\":32,\"sample_window_ms\":5000,\"scope\":\"syscall-handler\"}\n",
|
||||
getpid(), dd_profile_id, dd_profile_slow_ns);
|
||||
}
|
||||
errno = saved;
|
||||
return dd_profile_on;
|
||||
}
|
||||
|
||||
static void dd_profile_record(unsigned nr, int entering, pid_t pid,
|
||||
uint64_t start_wall, uint64_t start_cpu)
|
||||
{
|
||||
int saved = errno;
|
||||
uint64_t cpu = dd_profile_clock(CLOCK_THREAD_CPUTIME_ID);
|
||||
uint64_t wall = dd_profile_clock(CLOCK_MONOTONIC);
|
||||
if (nr >= PR_NB_SYSNUM) nr = PR_void;
|
||||
if (!start_wall || !start_cpu || wall < start_wall || cpu < start_cpu) goto done;
|
||||
uint64_t elapsed = wall - start_wall, used = cpu - start_cpu;
|
||||
struct dd_profile_counter *c = &dd_profile_counts[nr][!entering];
|
||||
c->calls++;
|
||||
c->wall_ns += elapsed;
|
||||
c->cpu_ns += used;
|
||||
if (elapsed > c->max_ns) c->max_ns = elapsed;
|
||||
if (wall >= dd_profile_next) {
|
||||
dd_profile_flush();
|
||||
dd_profile_next = wall + 5000000000ULL;
|
||||
dd_profile_samples = 0;
|
||||
}
|
||||
if (elapsed >= dd_profile_slow_ns && dd_profile_samples < 32) {
|
||||
dd_profile_samples++;
|
||||
fprintf(stderr, "proot-profile {\"type\":\"slow\",\"tracer\":%d,\"id\":%" PRIu64
|
||||
",\"pid\":%d,\"syscall\":\"%s\",\"stage\":\"%s\",\"wall_ns\":%" PRIu64
|
||||
",\"cpu_ns\":%" PRIu64 "}\n", getpid(), dd_profile_id, pid,
|
||||
stringify_sysnum(nr), entering ? "enter" : "exit", elapsed, used);
|
||||
}
|
||||
done:
|
||||
errno = saved;
|
||||
}
|
||||
#endif
|
||||
Reference in new issue
Block a user