Files
reims-vgpu/scripts/window-drag-probe/drag.c
T
Anees Iqbal 8929cd8ed4 ✨ Goal 6 had no instrument, and the first one says the writeback rail is two thirds of the worker
`scripts/window-drag-probe/` completed and run live. No Rust, no QEMU.

## The measurement

x86/PCI guest, settled, Safari at 1000x640, reposition motion, 1800 moves in
15.9 s (113 Hz), window confirmed to have moved (320,180) -> (922,96):

  present_hz      med 10.90   max 12.60   worst 0.40
  duty            med 0.97    (0.93-0.98 across the run)
  max_tranche_us  med 128831  (~129 ms, every second, slow_tranches 14/47)
  flush_us/flush  med 946     fence_us/fence med 461
  seconds below 100 Hz: 15/15

One second verbatim: drain_us=1015528 of win_ms=1095, draws=2351,
flushes=523 costing flush_us=641149, presents=11 with offered=11.

So the drain worker is busy for essentially the whole second and produces eleven
frames; `offered` equals `presents`, so it is not dropping frames it made. About
**two thirds of the worker's second is the deferred writeback rail** — the cost
`flush_mapping_windows_before_fence` already documents as the largest in the
device, now seen dominating a window-move workload rather than a WebGL one. And
a single tranche blocks the worker for ~129 ms, fifteen frames at 120 Hz, which
is the shape of the hitching goals 5 and 6 describe.

Narrowly: this bounds the device under *this* stressor, not under a user's drag,
and nothing here separates "the guest asked for this work" from "the device does
more than it was asked". 48 flushes per presented frame is the next question.

## Why the default motion is the weaker one

`--motion drag` posts a real Quartz pointer stream (`drag.c`, compiled on the
guest, never shipped as a binary; C rather than Python because the guest's
python3 3.9.6 has no PyObjC while clang does have ApplicationServices).

It does not work here and it fails silently: `CGEventPost` to the HID tap is
discarded for a process not trusted for Accessibility, and that trust cannot be
arranged — no passwordless sudo, SIP Filesystem Protections on, TCC.db
unwritable. Measured: 1800 events posted at exactly 120.0 Hz, zero late, window
moved zero pixels. So `--motion reposition` through System Events (which is
trusted) is the default, explicitly labelled as the weaker stressor rather than
substituted for the real one.

That silent failure is why the harness samples the window's real position
mid-run and exits 2 if it never moved. Without that guard this commit would have
reported an idle device's counters as goal 6's answer — the same failure as
passing a static page off as a churn test, which happened earlier today.

## Verified

Run live, output above, `bash -n` clean. The `drag` mode is committed proven to
post correctly and proven not to be delivered on this guest; it is kept because
it is the mechanism that produces the real thing wherever trust exists, and the
README says plainly which motion any number came from.
2026-08-02 13:03:17 +03:00

111 lines
3.8 KiB
C

/*
* drag.c — synthesise a real mouse drag, and report what was actually posted.
*
* Goal 6 is "window-dragging performance of Safari should be a stable 120 fps".
* Measuring it needs the drag to be a *drag*: a window moved by setting its
* accessibility position does not take the same path through the window server
* as a pointer held down across a title bar, so it does not produce the work
* being complained about.
*
* This is C rather than the obvious Python because the guest's /usr/bin/python3
* (3.9.6, Command Line Tools) has no PyObjC — `import Quartz` fails — while
* clang and the ApplicationServices headers are present. Compiled on the guest
* by the harness; nothing is shipped as a binary.
*
* Prints one line of JSON. The posted rate is reported rather than assumed
* because a drag posted at 40 Hz cannot show a 120 Hz device, and reporting the
* device as slow for that would be measuring this program.
*
* Build:
* clang -O2 -o drag drag.c -framework ApplicationServices -lm
*/
#include <ApplicationServices/ApplicationServices.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
static void post_at(CGEventType type, double x, double y)
{
CGEventRef e = CGEventCreateMouseEvent(NULL, type, CGPointMake(x, y),
kCGMouseButtonLeft);
if (!e) {
return;
}
CGEventPost(kCGHIDEventTap, e);
CFRelease(e);
}
/* Monotonic: the pacing loop must not be perturbed by a clock adjustment, and
* the guest's wall clock is settled by the host. */
static double now_s(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
}
int main(int argc, char **argv)
{
if (argc != 7) {
fprintf(stderr, "usage: drag X Y SECONDS HZ AMPLITUDE_X AMPLITUDE_Y\n");
return 2;
}
const double x0 = atof(argv[1]), y0 = atof(argv[2]);
const double secs = atof(argv[3]), hz = atof(argv[4]);
const double ax = atof(argv[5]), ay = atof(argv[6]);
if (hz <= 0.0 || secs <= 0.0) {
fprintf(stderr, "drag: seconds and hz must be positive\n");
return 2;
}
const double period = 1.0 / hz;
post_at(kCGEventLeftMouseDown, x0, y0);
/* Let the window server latch the drag before moving, or the first samples
* measure a click rather than a drag. */
usleep(50000);
const double start = now_s();
double next = start, worst_late = 0.0;
long posted = 0, late = 0;
for (;;) {
const double t = now_s() - start;
if (t >= secs) {
break;
}
/* Two incommensurate frequencies, so the path is a Lissajous curve
* rather than a straight line: a straight uniform slide is the one
* motion a compositor may coalesce or predict. Closed over the run, so
* the window ends where it started and repeated runs do not walk it off
* the screen. */
const double x = x0 + ax * sin(2.0 * M_PI * 0.5 * t);
const double y = y0 + ay * sin(2.0 * M_PI * 0.31 * t);
post_at(kCGEventLeftMouseDragged, x, y);
posted++;
next += period;
const double slack = next - now_s();
if (slack > 0.0) {
usleep((useconds_t)(slack * 1e6));
} else {
/* Could not keep up with the requested rate. Counted, not hidden. */
late++;
if (-slack > worst_late) {
worst_late = -slack;
}
next = now_s();
}
}
const double elapsed = now_s() - start;
post_at(kCGEventLeftMouseUp, x0, y0);
printf("{\"posted\":%ld,\"elapsed\":%.3f,\"posted_hz\":%.1f,"
"\"late\":%ld,\"worst_late_s\":%.4f}\n",
posted, elapsed, elapsed > 0.0 ? (double)posted / elapsed : 0.0,
late, worst_late);
return 0;
}