feat(hud): GPU load and clock without root where Android blocks kgsl (#1969)

On some Adreno devices (the Retroid Pocket Duo) SELinux hides kgsl's sysfs
nodes from apps, so the HUD showed no GPU % and no GPU clock. Devices whose
nodes are readable keep using them; nothing changes there or on GPUs
without kgsl.

Where the nodes are blocked:

- GPU %: a native reader maps kgsl's memstore read-only (any kgsl client
  may) and samples the ringbuffer start/end timestamps once a millisecond
  while a HUD is shown. That covers all GPU work, Android's own
  composition included.
- Backup for GPU %: an implicit Vulkan layer in the guest
  (tools/gpuload) and the compositor time their own GPU work with
  timestamp queries. It runs until the memstore stamps are seen moving,
  and for the whole session if they never move.
- GPU clock: a small GPU job of our own reads the always-on and RBBM
  cycle counters (libgpuclock).

The HUD diagnostics export lists which source each GPU value came from.
tools/gpuload/build.sh takes the Vulkan headers from build_local.sh's NDK
first and keeps NDK paths with spaces intact.
This commit is contained in:
The412Banner authored and GitHub committed 2026-11-01 20:22:51 +00:00
1 parent 6cd844ea51
commit 8cb5c2168e
30 files changed
+1891 -10

No files matched your search

+7
View File
@@ -181,6 +181,13 @@ if ! restore_step vr "${vr_key}" "${vr_out}"; then
save_step vr "${vr_key}" "${vr_out}"
fi
gpuload_out="${linuxfs_dir}/usr/local/lib/droiddeck-gpuload"
gpuload_key=$(inputs_hash "${image_id} ${ndk_version} ${VK_INCLUDE:-}" tools/gpuload)
if ! restore_step gpuload "${gpuload_key}" "${gpuload_out}"; then
DROIDDECK_BUILD_IMAGE="${image_name}" "${repo_root}/tools/gpuload/build.sh" "${gpuload_out}"
save_step gpuload "${gpuload_key}" "${gpuload_out}"
fi
github_repo=${DROIDDECK_GITHUB_REPOSITORY:-}
if [[ -z "${github_repo}" ]]; then
origin_url=$(git -C "${repo_root}" remote get-url origin)
+3 -1
View File
@@ -14,6 +14,7 @@
"tools/msitools",
"tools/pipewire",
"tools/vr",
"tools/gpuload",
"app/src/main/cpp/waylandcomp/generated",
"app/src/main/cpp/waylandcomp/src/headpose_shm.h",
"app/src/main/cpp/waylandcomp/src/spatialpose_shm.h",
@@ -21,7 +22,8 @@
],
"outputs": [
"app/src/main/assets/linuxfs/usr/local/bin/gamescope",
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/bin/linuxarm64/driver_droiddeck.so"
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/bin/linuxarm64/driver_droiddeck.so",
"app/src/main/assets/linuxfs/usr/local/lib/droiddeck-gpuload/libVkLayer_droiddeck_gpuload.so"
],
"ndk": true,
"packages": [
+2
View File
@@ -4,6 +4,8 @@ D=app/src/main/assets/linuxfs
mkdir -p "$D"
VK_INCLUDE=$(ls -d "${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"/toolchains/llvm/prebuilt/*/sysroot/usr/include | head -1) \
tools/vr/build.sh --native "$D/usr/local/lib/droiddeck-vr"
VK_INCLUDE=$(ls -d "${ANDROID_NDK_LATEST_HOME:-${ANDROID_NDK_HOME:-$ANDROID_NDK_ROOT}}"/toolchains/llvm/prebuilt/*/sysroot/usr/include | head -1) \
tools/gpuload/build.sh --native "$D/usr/local/lib/droiddeck-gpuload"
for script in tools/linuxfs/overlay/usr/local/bin/droiddeck-* tools/linuxfs/overlay/usr/local/bin/steam-compatibility; do
install -Dm644 "$script" "$D/usr/local/bin/$(basename "$script")"
done
+53
View File
@@ -0,0 +1,53 @@
#!/usr/bin/env bash
# Builds VK_LAYER_DROIDDECK_gpu_load for the arm64 guest: tools/gpuload/build.sh [--native] OUT
# OUT gets libVkLayer_droiddeck_gpuload.so and its implicit-layer manifest.
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
repo=$(cd "$here/../.." && pwd)
. "$repo/tools/docker_platform.sh"
native=0
if [ "${1:-}" = --native ]; then native=1; shift; fi
out=${1:?usage: tools/gpuload/build.sh [--native] OUT}
case "$out" in /*) ;; *) out="$PWD/$out" ;; esac
vkinc=${VK_INCLUDE:-}
if [ -z "$vkinc" ]; then
ndks=("${NDK:-}" "${ANDROID_NDK_HOME:-}" "${ANDROID_NDK_LATEST_HOME:-}")
sdk_ndks=("${ANDROID_HOME:-$HOME/Library/Android/sdk}"/ndk/*)
for ((i = ${#sdk_ndks[@]} - 1; i >= 0; i--)); do ndks+=("${sdk_ndks[i]}"); done
for ndk in "${ndks[@]}"; do
[ -n "$ndk" ] && [ -d "$ndk" ] || continue
for cand in "$ndk"/toolchains/llvm/prebuilt/*/sysroot/usr/include; do
if [ -f "$cand/vulkan/vk_layer.h" ]; then vkinc=$cand; break 2; fi
done
done
fi
[ -n "$vkinc" ] && [ -f "$vkinc/vulkan/vulkan_core.h" ] || { echo "tools/gpuload/build.sh: no Vulkan headers (set VK_INCLUDE)" >&2; exit 1; }
work="$repo/build/gpuload"
rm -rf "$work" && mkdir -p "$work/include" "$work/out"
cp -R "$vkinc/vulkan" "$work/include/"
if [ -d "$vkinc/vk_video" ]; then cp -R "$vkinc/vk_video" "$work/include/"; fi
cmd=(sh -c '
set -e
cc -std=c11 -O2 -Wall -Wextra -Werror -Itools/gpuload/src -o build/gpuload/test_gpuload_busy tools/gpuload/tests/test_gpuload_busy.c
build/gpuload/test_gpuload_busy
cc -std=c11 -O2 -Wall -Wextra -Werror -Ibuild/gpuload/include -Itools/gpuload/src -o build/gpuload/test_gpuload_ring tools/gpuload/tests/test_gpuload_ring.c
build/gpuload/test_gpuload_ring
aarch64-linux-gnu-gcc -std=c11 -shared -fPIC -O2 -Wall -Wextra -Werror -Wno-unused-parameter -Wno-missing-field-initializers \
-fvisibility=hidden -pthread -Wl,-z,defs -Ibuild/gpuload/include -Itools/gpuload/src \
-o build/gpuload/out/libVkLayer_droiddeck_gpuload.so tools/gpuload/src/vk_layer_droiddeck_gpuload.c
aarch64-linux-gnu-strip --strip-unneeded build/gpuload/out/libVkLayer_droiddeck_gpuload.so')
if [ "$native" = 1 ]; then
(cd "$repo" && "${cmd[@]}")
else
docker run --rm --platform "${DROIDDECK_DOCKER_PLATFORM:-linux/amd64}" --user "$(id -u):$(id -g)" -v "$repo:/src" -w /src \
"${DROIDDECK_BUILD_IMAGE:-droiddeck-local-cross:24.04-v3}" "${cmd[@]}"
fi
cp "$here/layer/VK_LAYER_DROIDDECK_gpu_load.json" "$work/out/"
mkdir -p "$out"
cp "$work/out/libVkLayer_droiddeck_gpuload.so" "$work/out/VK_LAYER_DROIDDECK_gpu_load.json" "$out/"
echo "built $out"
@@ -0,0 +1,20 @@
{
"file_format_version": "1.2.0",
"layer": {
"name": "VK_LAYER_DROIDDECK_gpu_load",
"type": "GLOBAL",
"library_path": "/usr/local/lib/droiddeck-gpuload/libVkLayer_droiddeck_gpuload.so",
"api_version": "1.3.0",
"implementation_version": "1",
"description": "Times this process's GPU work for DroidDeck's HUD where Android hides the GPU load; idle unless a HUD is shown",
"functions": {
"vkNegotiateLoaderLayerInterfaceVersion": "DroidDeckGpuLoad_NegotiateLoaderLayerInterfaceVersion"
},
"enable_environment": {
"DROIDDECK_GPU_LOAD": "1"
},
"disable_environment": {
"DROIDDECK_GPU_LOAD_DISABLE": "1"
}
}
}
+22
View File
@@ -0,0 +1,22 @@
// Busy-time bookkeeping shared by VK_LAYER_DROIDDECK_gpu_load and DroidDeck's compositor, kept free of Vulkan so it
// can be tested on its own.
#ifndef DROIDDECK_GPULOAD_BUSY_H
#define DROIDDECK_GPULOAD_BUSY_H
#include <stdint.h>
// GPU ticks between the timestamp written before a batch and the one written after it. Batches on one queue run one
// after the other, so these add up without overlap. Ticks are never compared across batches: on Adreno the always-on
// counter behind the timestamps restarts when the GPU powers down between batches (seen on the Retroid Pocket Duo).
// Anything backwards or longer than max_ticks is a bogus pair and counts as nothing.
static inline uint64_t gpuload_interval(uint64_t start, uint64_t end, uint64_t max_ticks) {
return end > start && end - start <= max_ticks ? end - start : 0;
}
// A timestamp query slot is reused once its end is available; until the GPU runs the slot's reset again the
// query still shows the previous end, so a result equal to the last one harvested is the old one.
static inline int gpuload_result_is_new(uint64_t start_avail, uint64_t end_avail, uint64_t end, uint64_t last_end) {
return start_avail && end_avail && end != last_end;
}
#endif
+215
View File
@@ -0,0 +1,215 @@
// Timestamp ring shared by VK_LAYER_DROIDDECK_gpu_load (guest) and DroidDeck's compositor (app): each submit batch
// that has command buffers gets one of the ring's command buffers in front (resets a query pair, writes a TOP_OF_PIPE
// timestamp) and one behind (BOTTOM_OF_PIPE timestamp). Finished pairs are read back without waiting.
// The start timestamp sits after the batch's semaphore waits only because Turnip and Qualcomm's driver on kgsl hold a
// whole batch until its waits signal; a driver that starts TOP_OF_PIPE work earlier would count the wait as busy.
// Callers serialize access to one ring (a queue's submits are externally synchronized anyway).
#ifndef DROIDDECK_GPULOAD_RING_H
#define DROIDDECK_GPULOAD_RING_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vulkan/vulkan.h>
#include "gpuload_busy.h"
#define GPULOAD_RING 64
typedef VkResult(VKAPI_PTR *gpuload_set_data_fn)(VkDevice device, void *object);
struct gpuload_vk {
VkDevice device;
PFN_vkCreateCommandPool create_pool;
PFN_vkDestroyCommandPool destroy_pool;
PFN_vkAllocateCommandBuffers alloc_cmds;
PFN_vkBeginCommandBuffer begin;
PFN_vkEndCommandBuffer end;
PFN_vkCmdResetQueryPool cmd_reset;
PFN_vkCmdWriteTimestamp cmd_timestamp;
PFN_vkCreateQueryPool create_queries;
PFN_vkDestroyQueryPool destroy_queries;
PFN_vkGetQueryPoolResults results;
gpuload_set_data_fn set_data; // a layer must give the loader every command buffer it allocates; NULL elsewhere
double period_ns;
uint64_t max_ticks;
};
struct gpuload_slot {
VkCommandBuffer start, end;
uint64_t last_end;
};
struct gpuload_ring {
int state; // 0 not set up, 1 timing, -1 cannot time
VkCommandPool pool;
VkQueryPool queries;
uint64_t mask;
uint32_t next, oldest, inflight;
struct gpuload_slot slot[GPULOAD_RING];
};
#define GPULOAD_LONGEST_BATCH_NS 2000000000.0
static inline void gpuload_vk_period(struct gpuload_vk *v, float timestamp_period) {
v->period_ns = timestamp_period;
v->max_ticks = timestamp_period > 0 ? (uint64_t)(GPULOAD_LONGEST_BATCH_NS / timestamp_period) : 0;
}
static inline int gpuload_vk_complete(const struct gpuload_vk *v) {
return v->device && v->create_pool && v->destroy_pool && v->alloc_cmds && v->begin && v->end && v->cmd_reset &&
v->cmd_timestamp && v->create_queries && v->destroy_queries && v->results && v->period_ns > 0;
}
static inline void gpuload_ring_release(struct gpuload_ring *r, const struct gpuload_vk *v) {
if (r->queries) v->destroy_queries(v->device, r->queries, NULL);
if (r->pool) v->destroy_pool(v->device, r->pool, NULL);
r->queries = VK_NULL_HANDLE;
r->pool = VK_NULL_HANDLE;
r->next = r->oldest = r->inflight = 0;
}
static inline int gpuload_ring_record(struct gpuload_ring *r, const struct gpuload_vk *v, uint32_t i) {
const VkCommandBufferBeginInfo begin = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO};
struct gpuload_slot *s = &r->slot[i];
if (v->begin(s->start, &begin) != VK_SUCCESS) return 0;
v->cmd_reset(s->start, r->queries, 2 * i, 2);
v->cmd_timestamp(s->start, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, r->queries, 2 * i);
if (v->end(s->start) != VK_SUCCESS) return 0;
if (v->begin(s->end, &begin) != VK_SUCCESS) return 0;
v->cmd_timestamp(s->end, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, r->queries, 2 * i + 1);
return v->end(s->end) == VK_SUCCESS;
}
// Sets the ring up for a queue of this family; returns 1 when it can time it (r->state says the same afterwards).
static inline int gpuload_ring_setup(struct gpuload_ring *r, const struct gpuload_vk *v, uint32_t family,
uint32_t timestamp_bits) {
r->state = -1;
if (!timestamp_bits || !gpuload_vk_complete(v)) return 0;
r->mask = timestamp_bits >= 64 ? ~0ull : (1ull << timestamp_bits) - 1;
const VkCommandPoolCreateInfo pool = {.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, .queueFamilyIndex = family};
const VkQueryPoolCreateInfo queries = {.sType = VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO,
.queryType = VK_QUERY_TYPE_TIMESTAMP, .queryCount = 2 * GPULOAD_RING};
VkCommandBuffer cmds[2 * GPULOAD_RING];
if (v->create_pool(v->device, &pool, NULL, &r->pool) != VK_SUCCESS) { r->pool = VK_NULL_HANDLE; goto fail; }
if (v->create_queries(v->device, &queries, NULL, &r->queries) != VK_SUCCESS) { r->queries = VK_NULL_HANDLE; goto fail; }
const VkCommandBufferAllocateInfo alloc = {.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, .commandPool = r->pool,
.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY, .commandBufferCount = 2 * GPULOAD_RING};
if (v->alloc_cmds(v->device, &alloc, cmds) != VK_SUCCESS) goto fail;
for (uint32_t i = 0; i < 2 * GPULOAD_RING; i++)
if (v->set_data && v->set_data(v->device, cmds[i]) != VK_SUCCESS) goto fail;
for (uint32_t i = 0; i < GPULOAD_RING; i++) {
r->slot[i].start = cmds[2 * i];
r->slot[i].end = cmds[2 * i + 1];
r->slot[i].last_end = 0;
if (!gpuload_ring_record(r, v, i)) goto fail;
}
r->next = r->oldest = r->inflight = 0;
r->state = 1;
return 1;
fail:
gpuload_ring_release(r, v);
r->state = -1;
return 0;
}
// Nanoseconds of GPU time in the batches that finished since the last call. Slots are taken in ring order and a
// queue runs its batches in order, so the oldest slot finishes first.
static inline uint64_t gpuload_ring_harvest(struct gpuload_ring *r, const struct gpuload_vk *v) {
uint64_t ticks = 0;
while (r->inflight) {
struct gpuload_slot *s = &r->slot[r->oldest];
uint64_t q[4] = {0, 0, 0, 0};
VkResult res = v->results(v->device, r->queries, 2 * r->oldest, 2, sizeof(q), q, 2 * sizeof(uint64_t),
VK_QUERY_RESULT_64_BIT | VK_QUERY_RESULT_WITH_AVAILABILITY_BIT);
if (res != VK_SUCCESS && res != VK_NOT_READY) break;
uint64_t start = q[0] & r->mask, end = q[2] & r->mask;
if (!gpuload_result_is_new(q[1], q[3], end, s->last_end)) break;
s->last_end = end;
r->oldest = (r->oldest + 1) % GPULOAD_RING;
r->inflight--;
ticks += gpuload_interval(start, end, v->max_ticks);
}
return (uint64_t)((double)ticks * v->period_ns);
}
static inline int gpuload_plain_batch(const void *next) {
for (const VkBaseInStructure *s = (const VkBaseInStructure *)next; s; s = s->pNext) {
if (s->sType == VK_STRUCTURE_TYPE_DEVICE_GROUP_SUBMIT_INFO) return 0;
if (s->sType == VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO && ((const VkProtectedSubmitInfo *)s)->protectedSubmit)
return 0;
}
return 1;
}
static inline struct gpuload_slot *gpuload_ring_take(struct gpuload_ring *r) {
if (r->inflight >= GPULOAD_RING) return NULL;
struct gpuload_slot *s = &r->slot[r->next];
r->next = (r->next + 1) % GPULOAD_RING;
r->inflight++;
return s;
}
// Gives back the slots of a submit that failed.
static inline void gpuload_ring_unwind(struct gpuload_ring *r, uint32_t used) {
r->next = (r->next + GPULOAD_RING - used) % GPULOAD_RING;
r->inflight -= used;
}
// A copy of the batches with the ring's command buffers around each one that has command buffers (free() it after
// the submit), or NULL to submit the originals. *used counts the slots taken, for gpuload_ring_unwind.
static inline VkSubmitInfo *gpuload_ring_wrap(struct gpuload_ring *r, uint32_t count, const VkSubmitInfo *infos,
uint32_t *used) {
*used = 0;
size_t cmds = 0;
for (uint32_t i = 0; i < count; i++) cmds += infos[i].commandBufferCount + 2;
VkSubmitInfo *mine = (VkSubmitInfo *)malloc(count * sizeof(*mine) + cmds * sizeof(VkCommandBuffer));
if (!mine) return NULL;
VkCommandBuffer *list = (VkCommandBuffer *)(mine + count);
for (uint32_t i = 0; i < count; i++) {
uint32_t n = infos[i].commandBufferCount;
mine[i] = infos[i];
if (!n || !gpuload_plain_batch(infos[i].pNext)) continue;
struct gpuload_slot *s = gpuload_ring_take(r);
if (!s) continue;
list[0] = s->start;
memcpy(list + 1, infos[i].pCommandBuffers, n * sizeof(VkCommandBuffer));
list[n + 1] = s->end;
mine[i].pCommandBuffers = list;
mine[i].commandBufferCount = n + 2;
list += n + 2;
(*used)++;
}
return mine;
}
static inline VkSubmitInfo2 *gpuload_ring_wrap2(struct gpuload_ring *r, uint32_t count, const VkSubmitInfo2 *infos,
uint32_t *used) {
*used = 0;
size_t cmds = 0;
for (uint32_t i = 0; i < count; i++) cmds += infos[i].commandBufferInfoCount + 2;
VkSubmitInfo2 *mine = (VkSubmitInfo2 *)malloc(count * sizeof(*mine) + cmds * sizeof(VkCommandBufferSubmitInfo));
if (!mine) return NULL;
VkCommandBufferSubmitInfo *list = (VkCommandBufferSubmitInfo *)(mine + count);
for (uint32_t i = 0; i < count; i++) {
uint32_t n = infos[i].commandBufferInfoCount;
mine[i] = infos[i];
if (!n || (infos[i].flags & VK_SUBMIT_PROTECTED_BIT) || !gpuload_plain_batch(infos[i].pNext)) continue;
struct gpuload_slot *s = gpuload_ring_take(r);
if (!s) continue;
memset(&list[0], 0, sizeof(list[0]));
list[0].sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO;
list[0].commandBuffer = s->start;
memcpy(list + 1, infos[i].pCommandBufferInfos, n * sizeof(VkCommandBufferSubmitInfo));
memset(&list[n + 1], 0, sizeof(list[n + 1]));
list[n + 1].sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_SUBMIT_INFO;
list[n + 1].commandBuffer = s->end;
mine[i].pCommandBufferInfos = list;
mine[i].commandBufferInfoCount = n + 2;
list += n + 2;
(*used)++;
}
return mine;
}
#endif
@@ -0,0 +1,410 @@
// VK_LAYER_DROIDDECK_gpu_load: this process's GPU busy time for the HUD where Android keeps the GPU load from apps
// (kgsl sysfs SELinux-blocked, perfcounter reads refused while /sys/class/kgsl/kgsl-3d0/perfcounter is 0).
// The process total in nanoseconds goes to $DROIDDECK_GPU_LOAD_DIR/<pid>; nothing is timed while
// $DROIDDECK_GPU_LOAD_DIR/on is missing (the app removes it while no HUD is shown).
#define _GNU_SOURCE
#include <vulkan/vulkan.h>
#include <vulkan/vk_layer.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "gpuload_ring.h"
#define EXPORT __attribute__((visibility("default")))
#define MAX_FAMILIES 32
#define WANTED_CHECK_NS 500000000ull
#define WRITE_EVERY_NS 100000000ull
static const char *g_dir;
static void say(const char *what) {
// FEX leaves program_invocation_short_name empty for x86 programs.
fprintf(stderr, "[droiddeck-gpu-load] %s (pid %d): %s\n", program_invocation_short_name, (int)getpid(), what);
}
__attribute__((constructor)) static void init(void) {
const char *dir = getenv("DROIDDECK_GPU_LOAD_DIR");
g_dir = dir && *dir ? dir : NULL;
}
static uint64_t now_ns(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
}
struct inst {
struct inst *next_in_list;
VkInstance handle;
PFN_vkGetInstanceProcAddr gipa;
PFN_vkDestroyInstance destroy;
PFN_vkGetPhysicalDeviceProperties props;
PFN_vkGetPhysicalDeviceQueueFamilyProperties families;
};
struct dev {
struct dev *next_in_list;
VkDevice handle;
PFN_vkGetDeviceProcAddr gdpa;
PFN_vkDestroyDevice destroy;
PFN_vkGetDeviceQueue get_queue;
PFN_vkGetDeviceQueue2 get_queue2;
PFN_vkQueueSubmit submit;
PFN_vkQueueSubmit2 submit2;
PFN_vkQueueSubmit2 submit2_khr;
struct gpuload_vk vk;
uint32_t family_count;
uint32_t timestamp_bits[MAX_FAMILIES];
};
struct queue {
struct queue *next_in_list;
VkQueue handle;
struct dev *dev;
uint32_t family;
uint32_t epoch;
struct gpuload_ring ring;
};
static pthread_mutex_t g_lock = PTHREAD_MUTEX_INITIALIZER;
static struct inst *g_insts;
static struct dev *g_devs;
static struct queue *g_queues;
static void *key(const void *obj) { return *(void *const *)obj; }
static struct inst *inst_for(const void *obj) {
struct inst *r;
pthread_mutex_lock(&g_lock);
for (r = g_insts; r && key(r->handle) != key(obj); r = r->next_in_list) {}
pthread_mutex_unlock(&g_lock);
return r;
}
static struct dev *dev_for(const void *obj) {
struct dev *r;
pthread_mutex_lock(&g_lock);
for (r = g_devs; r && key(r->handle) != key(obj); r = r->next_in_list) {}
pthread_mutex_unlock(&g_lock);
return r;
}
static struct queue *queue_for(VkQueue handle) {
struct queue *r;
pthread_mutex_lock(&g_lock);
for (r = g_queues; r && r->handle != handle; r = r->next_in_list) {}
pthread_mutex_unlock(&g_lock);
return r;
}
static void note_queue(struct dev *d, VkQueue handle, uint32_t family) {
if (!handle || queue_for(handle)) return;
struct queue *q = calloc(1, sizeof(*q));
if (!q) return;
q->handle = handle;
q->dev = d;
q->family = family;
pthread_mutex_lock(&g_lock);
q->next_in_list = g_queues;
g_queues = q;
pthread_mutex_unlock(&g_lock);
}
// ---- process total ---------------------------------------------------------------------------------------------
static _Atomic uint64_t g_busy_ns;
static _Atomic uint64_t g_wanted_at;
static _Atomic int g_wanted;
static _Atomic uint32_t g_epoch = 1; // bumped each time the HUD comes back
static pthread_mutex_t g_write_lock = PTHREAD_MUTEX_INITIALIZER;
static uint64_t g_written_at;
static int g_fd = -1;
static pid_t g_fd_pid;
static int g_announced;
static int wanted(uint64_t now) {
uint64_t at = atomic_load(&g_wanted_at);
if (at && now - at < WANTED_CHECK_NS) return atomic_load(&g_wanted);
atomic_store(&g_wanted_at, now);
char path[512];
snprintf(path, sizeof(path), "%s/on", g_dir);
int on = access(path, F_OK) == 0;
if (on && !atomic_exchange(&g_wanted, on)) atomic_fetch_add(&g_epoch, 1);
if (!on) atomic_store(&g_wanted, 0);
return on;
}
static void publish(uint64_t now) {
if (pthread_mutex_trylock(&g_write_lock)) return;
if (now - g_written_at >= WRITE_EVERY_NS) {
g_written_at = now;
pid_t pid = getpid();
struct stat st;
// The app deletes files that stopped changing; a forked child needs its own.
if (g_fd >= 0 && (g_fd_pid != pid || fstat(g_fd, &st) || st.st_nlink == 0)) { close(g_fd); g_fd = -1; }
if (g_fd < 0) {
char path[512];
snprintf(path, sizeof(path), "%s/%d", g_dir, (int)pid);
g_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
g_fd_pid = pid;
}
if (g_fd >= 0) {
char line[80];
int n = snprintf(line, sizeof(line), "%20llu %-32.32s\n", (unsigned long long)atomic_load(&g_busy_ns),
program_invocation_short_name);
if (pwrite(g_fd, line, (size_t)n, 0) != n) { close(g_fd); g_fd = -1; }
}
}
pthread_mutex_unlock(&g_write_lock);
}
// The queue to time this submit on, or NULL to pass it through.
static struct queue *measuring(VkQueue handle) {
if (!g_dir) return NULL;
struct queue *q = queue_for(handle);
if (!q) return NULL;
uint64_t now = now_ns();
if (!wanted(now)) return NULL;
struct dev *d = q->dev;
if (q->ring.state == 0) {
uint32_t bits = q->family < d->family_count ? d->timestamp_bits[q->family] : 0;
if (gpuload_ring_setup(&q->ring, &d->vk, q->family, bits)) {
if (!__atomic_exchange_n(&g_announced, 1, __ATOMIC_RELAXED)) say("timing this process's GPU work for the HUD");
} else {
say(bits ? "cannot set up GPU timing on this queue" : "this queue family has no timestamps");
}
q->epoch = atomic_load(&g_epoch);
}
if (q->ring.state != 1) return NULL;
uint64_t ns = gpuload_ring_harvest(&q->ring, &d->vk);
uint32_t epoch = atomic_load(&g_epoch);
if (q->epoch == epoch) atomic_fetch_add(&g_busy_ns, ns);
else q->epoch = epoch; // timed before the HUD went away
publish(now);
return q;
}
static VkResult VKAPI_CALL QueueSubmit(VkQueue queue, uint32_t count, const VkSubmitInfo *infos, VkFence fence) {
struct dev *d = dev_for(queue);
if (!d) return VK_ERROR_DEVICE_LOST;
struct queue *q = count ? measuring(queue) : NULL;
uint32_t used = 0;
VkSubmitInfo *mine = q ? gpuload_ring_wrap(&q->ring, count, infos, &used) : NULL;
VkResult r = d->submit(queue, count, mine ? mine : infos, fence);
if (mine && r != VK_SUCCESS) gpuload_ring_unwind(&q->ring, used);
free(mine);
return r;
}
static VkResult submit2(PFN_vkQueueSubmit2 next, VkQueue queue, uint32_t count, const VkSubmitInfo2 *infos, VkFence fence) {
struct queue *q = count ? measuring(queue) : NULL;
uint32_t used = 0;
VkSubmitInfo2 *mine = q ? gpuload_ring_wrap2(&q->ring, count, infos, &used) : NULL;
VkResult r = next(queue, count, mine ? mine : infos, fence);
if (mine && r != VK_SUCCESS) gpuload_ring_unwind(&q->ring, used);
free(mine);
return r;
}
static VkResult VKAPI_CALL QueueSubmit2(VkQueue queue, uint32_t count, const VkSubmitInfo2 *infos, VkFence fence) {
struct dev *d = dev_for(queue);
return d ? submit2(d->submit2, queue, count, infos, fence) : VK_ERROR_DEVICE_LOST;
}
static VkResult VKAPI_CALL QueueSubmit2KHR(VkQueue queue, uint32_t count, const VkSubmitInfo2 *infos, VkFence fence) {
struct dev *d = dev_for(queue);
return d ? submit2(d->submit2_khr, queue, count, infos, fence) : VK_ERROR_DEVICE_LOST;
}
static void VKAPI_CALL GetDeviceQueue(VkDevice device, uint32_t family, uint32_t index, VkQueue *out) {
struct dev *d = dev_for(device);
if (!d) return;
d->get_queue(device, family, index, out);
if (out && *out) note_queue(d, *out, family);
}
static void VKAPI_CALL GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2 *info, VkQueue *out) {
struct dev *d = dev_for(device);
if (!d) return;
d->get_queue2(device, info, out);
if (out && *out) note_queue(d, *out, info->queueFamilyIndex);
}
// ---- instance and device ---------------------------------------------------------------------------------------
static VkResult VKAPI_CALL CreateInstance(const VkInstanceCreateInfo *ci, const VkAllocationCallbacks *alloc, VkInstance *out) {
VkLayerInstanceCreateInfo *link = (VkLayerInstanceCreateInfo *)ci->pNext;
while (link && !(link->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && link->function == VK_LAYER_LINK_INFO))
link = (VkLayerInstanceCreateInfo *)link->pNext;
if (!link) return VK_ERROR_INITIALIZATION_FAILED;
PFN_vkGetInstanceProcAddr next_gipa = link->u.pLayerInfo->pfnNextGetInstanceProcAddr;
link->u.pLayerInfo = link->u.pLayerInfo->pNext;
PFN_vkCreateInstance create = (PFN_vkCreateInstance)next_gipa(VK_NULL_HANDLE, "vkCreateInstance");
if (!create) return VK_ERROR_INITIALIZATION_FAILED;
struct inst *in = calloc(1, sizeof(*in));
if (!in) return VK_ERROR_OUT_OF_HOST_MEMORY;
VkResult r = create(ci, alloc, out);
if (r != VK_SUCCESS) { free(in); return r; }
in->handle = *out;
in->gipa = next_gipa;
in->destroy = (PFN_vkDestroyInstance)next_gipa(*out, "vkDestroyInstance");
in->props = (PFN_vkGetPhysicalDeviceProperties)next_gipa(*out, "vkGetPhysicalDeviceProperties");
in->families = (PFN_vkGetPhysicalDeviceQueueFamilyProperties)next_gipa(*out, "vkGetPhysicalDeviceQueueFamilyProperties");
pthread_mutex_lock(&g_lock);
in->next_in_list = g_insts;
g_insts = in;
pthread_mutex_unlock(&g_lock);
return VK_SUCCESS;
}
static void VKAPI_CALL DestroyInstance(VkInstance instance, const VkAllocationCallbacks *alloc) {
struct inst *in = inst_for(instance);
if (!in) return;
PFN_vkDestroyInstance destroy = in->destroy;
pthread_mutex_lock(&g_lock);
for (struct inst **p = &g_insts; *p; p = &(*p)->next_in_list)
if (*p == in) { *p = in->next_in_list; break; }
pthread_mutex_unlock(&g_lock);
free(in);
if (destroy) destroy(instance, alloc);
}
static VkLayerDeviceCreateInfo *device_chain(const VkDeviceCreateInfo *ci, VkLayerFunction function) {
VkLayerDeviceCreateInfo *c = (VkLayerDeviceCreateInfo *)ci->pNext;
while (c && !(c->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && c->function == function))
c = (VkLayerDeviceCreateInfo *)c->pNext;
return c;
}
static VkResult VKAPI_CALL CreateDevice(VkPhysicalDevice pd, const VkDeviceCreateInfo *ci, const VkAllocationCallbacks *alloc,
VkDevice *out) {
VkLayerDeviceCreateInfo *link = device_chain(ci, VK_LAYER_LINK_INFO);
if (!link) return VK_ERROR_INITIALIZATION_FAILED;
VkLayerDeviceCreateInfo *callback = device_chain(ci, VK_LOADER_DATA_CALLBACK);
PFN_vkGetInstanceProcAddr next_gipa = link->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr next_gdpa = link->u.pLayerInfo->pfnNextGetDeviceProcAddr;
link->u.pLayerInfo = link->u.pLayerInfo->pNext;
struct inst *in = inst_for(pd);
PFN_vkCreateDevice create = (PFN_vkCreateDevice)next_gipa(in ? in->handle : VK_NULL_HANDLE, "vkCreateDevice");
if (!create) return VK_ERROR_INITIALIZATION_FAILED;
struct dev *d = calloc(1, sizeof(*d));
if (!d) return VK_ERROR_OUT_OF_HOST_MEMORY;
VkResult r = create(pd, ci, alloc, out);
if (r != VK_SUCCESS) { free(d); return r; }
d->handle = *out;
d->gdpa = next_gdpa;
#define LOAD(field, type, name) d->field = (type)next_gdpa(*out, name)
LOAD(destroy, PFN_vkDestroyDevice, "vkDestroyDevice");
LOAD(get_queue, PFN_vkGetDeviceQueue, "vkGetDeviceQueue");
LOAD(get_queue2, PFN_vkGetDeviceQueue2, "vkGetDeviceQueue2");
LOAD(submit, PFN_vkQueueSubmit, "vkQueueSubmit");
LOAD(submit2, PFN_vkQueueSubmit2, "vkQueueSubmit2");
LOAD(submit2_khr, PFN_vkQueueSubmit2, "vkQueueSubmit2KHR");
LOAD(vk.create_pool, PFN_vkCreateCommandPool, "vkCreateCommandPool");
LOAD(vk.destroy_pool, PFN_vkDestroyCommandPool, "vkDestroyCommandPool");
LOAD(vk.alloc_cmds, PFN_vkAllocateCommandBuffers, "vkAllocateCommandBuffers");
LOAD(vk.begin, PFN_vkBeginCommandBuffer, "vkBeginCommandBuffer");
LOAD(vk.end, PFN_vkEndCommandBuffer, "vkEndCommandBuffer");
LOAD(vk.cmd_reset, PFN_vkCmdResetQueryPool, "vkCmdResetQueryPool");
LOAD(vk.cmd_timestamp, PFN_vkCmdWriteTimestamp, "vkCmdWriteTimestamp");
LOAD(vk.create_queries, PFN_vkCreateQueryPool, "vkCreateQueryPool");
LOAD(vk.destroy_queries, PFN_vkDestroyQueryPool, "vkDestroyQueryPool");
LOAD(vk.results, PFN_vkGetQueryPoolResults, "vkGetQueryPoolResults");
#undef LOAD
d->vk.device = *out;
d->vk.set_data = callback ? (gpuload_set_data_fn)callback->u.pfnSetDeviceLoaderData : NULL;
if (in && in->props && in->families) {
VkPhysicalDeviceProperties props;
in->props(pd, &props);
gpuload_vk_period(&d->vk, props.limits.timestampPeriod);
uint32_t n = 0;
in->families(pd, &n, NULL);
VkQueueFamilyProperties families[MAX_FAMILIES];
if (n > MAX_FAMILIES) n = MAX_FAMILIES;
in->families(pd, &n, families);
d->family_count = n;
for (uint32_t i = 0; i < n; i++) d->timestamp_bits[i] = families[i].timestampValidBits;
}
// Without the loader callback our command buffers would lack the loader's dispatch pointer.
if (!d->vk.set_data) d->family_count = 0;
pthread_mutex_lock(&g_lock);
d->next_in_list = g_devs;
g_devs = d;
pthread_mutex_unlock(&g_lock);
return VK_SUCCESS;
}
static void VKAPI_CALL DestroyDevice(VkDevice device, const VkAllocationCallbacks *alloc) {
struct dev *d = dev_for(device);
if (!d) return;
struct queue *mine = NULL;
pthread_mutex_lock(&g_lock);
for (struct queue **p = &g_queues; *p;) {
struct queue *q = *p;
if (q->dev == d) { *p = q->next_in_list; q->next_in_list = mine; mine = q; } else p = &q->next_in_list;
}
for (struct dev **p = &g_devs; *p; p = &(*p)->next_in_list)
if (*p == d) { *p = d->next_in_list; break; }
pthread_mutex_unlock(&g_lock);
while (mine) {
struct queue *q = mine;
mine = q->next_in_list;
gpuload_ring_release(&q->ring, &d->vk);
free(q);
}
PFN_vkDestroyDevice destroy = d->destroy;
free(d);
if (destroy) destroy(device, alloc);
}
// ---- dispatch --------------------------------------------------------------------------------------------------
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckGpuLoad_GetDeviceProcAddr(VkDevice device, const char *name);
static PFN_vkVoidFunction device_hook(struct dev *d, const char *name) {
if (!g_dir) return NULL;
if (!strcmp(name, "vkGetDeviceQueue") && d->get_queue) return (PFN_vkVoidFunction)GetDeviceQueue;
if (!strcmp(name, "vkGetDeviceQueue2") && d->get_queue2) return (PFN_vkVoidFunction)GetDeviceQueue2;
if (!strcmp(name, "vkQueueSubmit") && d->submit) return (PFN_vkVoidFunction)QueueSubmit;
if (!strcmp(name, "vkQueueSubmit2") && d->submit2) return (PFN_vkVoidFunction)QueueSubmit2;
if (!strcmp(name, "vkQueueSubmit2KHR") && d->submit2_khr) return (PFN_vkVoidFunction)QueueSubmit2KHR;
return NULL;
}
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckGpuLoad_GetInstanceProcAddr(VkInstance instance, const char *name) {
if (!strcmp(name, "vkCreateInstance")) return (PFN_vkVoidFunction)CreateInstance;
if (!strcmp(name, "vkDestroyInstance")) return (PFN_vkVoidFunction)DestroyInstance;
if (!strcmp(name, "vkCreateDevice")) return (PFN_vkVoidFunction)CreateDevice;
if (!strcmp(name, "vkGetInstanceProcAddr")) return (PFN_vkVoidFunction)DroidDeckGpuLoad_GetInstanceProcAddr;
if (!strcmp(name, "vkGetDeviceProcAddr")) return (PFN_vkVoidFunction)DroidDeckGpuLoad_GetDeviceProcAddr;
if (!instance) return NULL;
struct inst *in = inst_for(instance);
return in ? in->gipa(instance, name) : NULL;
}
EXPORT VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL DroidDeckGpuLoad_GetDeviceProcAddr(VkDevice device, const char *name) {
if (!strcmp(name, "vkGetDeviceProcAddr")) return (PFN_vkVoidFunction)DroidDeckGpuLoad_GetDeviceProcAddr;
if (!strcmp(name, "vkDestroyDevice")) return (PFN_vkVoidFunction)DestroyDevice;
struct dev *d = dev_for(device);
if (!d) return NULL;
PFN_vkVoidFunction mine = device_hook(d, name);
return mine ? mine : d->gdpa(device, name);
}
EXPORT VKAPI_ATTR VkResult VKAPI_CALL DroidDeckGpuLoad_NegotiateLoaderLayerInterfaceVersion(VkNegotiateLayerInterface *v) {
if (v->loaderLayerInterfaceVersion > 2) v->loaderLayerInterfaceVersion = 2;
v->pfnGetInstanceProcAddr = DroidDeckGpuLoad_GetInstanceProcAddr;
v->pfnGetDeviceProcAddr = DroidDeckGpuLoad_GetDeviceProcAddr;
v->pfnGetPhysicalDeviceProcAddr = NULL;
return VK_SUCCESS;
}
+24
View File
@@ -0,0 +1,24 @@
#include <assert.h>
#include <stdio.h>
#include "gpuload_busy.h"
int main(void) {
const uint64_t max = 1000;
assert(gpuload_interval(10, 20, max) == 10);
// Each batch counts on its own, even when the counter restarted in between (later batch has smaller ticks).
assert(gpuload_interval(900000, 900400, max) == 400);
assert(gpuload_interval(50, 350, max) == 300);
// Backwards, empty or too long: nothing.
assert(gpuload_interval(60, 50, max) == 0);
assert(gpuload_interval(70, 70, max) == 0);
assert(gpuload_interval(100, 100 + max + 1, max) == 0);
assert(gpuload_interval(100, 100 + max, max) == max);
// Query results: both halves available and an end that differs from the last harvested one.
assert(gpuload_result_is_new(1, 1, 500, 400));
assert(!gpuload_result_is_new(1, 1, 400, 400));
assert(!gpuload_result_is_new(1, 0, 500, 400));
assert(!gpuload_result_is_new(0, 1, 500, 400));
puts("test_gpuload_busy: ok");
return 0;
}
+96
View File
@@ -0,0 +1,96 @@
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include "gpuload_ring.h"
// A fake device: query results come from this table, [start, start available, end, end available] per slot.
static uint64_t g_result[GPULOAD_RING][4];
static uintptr_t g_next_handle = 0x1000;
static VkResult VKAPI_CALL create_pool(VkDevice d, const VkCommandPoolCreateInfo *ci, const VkAllocationCallbacks *a, VkCommandPool *out) {
(void)d; (void)ci; (void)a; *out = (VkCommandPool)(g_next_handle++); return VK_SUCCESS;
}
static void VKAPI_CALL destroy_pool(VkDevice d, VkCommandPool p, const VkAllocationCallbacks *a) { (void)d; (void)p; (void)a; }
static VkResult VKAPI_CALL alloc_cmds(VkDevice d, const VkCommandBufferAllocateInfo *ai, VkCommandBuffer *out) {
(void)d;
for (uint32_t i = 0; i < ai->commandBufferCount; i++) out[i] = (VkCommandBuffer)(g_next_handle++);
return VK_SUCCESS;
}
static VkResult VKAPI_CALL begin(VkCommandBuffer c, const VkCommandBufferBeginInfo *bi) { (void)c; (void)bi; return VK_SUCCESS; }
static VkResult VKAPI_CALL end(VkCommandBuffer c) { (void)c; return VK_SUCCESS; }
static void VKAPI_CALL cmd_reset(VkCommandBuffer c, VkQueryPool q, uint32_t first, uint32_t n) { (void)c; (void)q; (void)first; (void)n; }
static void VKAPI_CALL cmd_timestamp(VkCommandBuffer c, VkPipelineStageFlagBits s, VkQueryPool q, uint32_t i) { (void)c; (void)s; (void)q; (void)i; }
static VkResult VKAPI_CALL create_queries(VkDevice d, const VkQueryPoolCreateInfo *ci, const VkAllocationCallbacks *a, VkQueryPool *out) {
(void)d; (void)a; assert(ci->queryCount == 2 * GPULOAD_RING); *out = (VkQueryPool)(g_next_handle++); return VK_SUCCESS;
}
static void VKAPI_CALL destroy_queries(VkDevice d, VkQueryPool q, const VkAllocationCallbacks *a) { (void)d; (void)q; (void)a; }
static VkResult VKAPI_CALL results(VkDevice d, VkQueryPool q, uint32_t first, uint32_t n, size_t size, void *data,
VkDeviceSize stride, VkQueryResultFlags flags) {
(void)d; (void)q; (void)size;
assert(n == 2 && stride == 2 * sizeof(uint64_t) && (flags & VK_QUERY_RESULT_WITH_AVAILABILITY_BIT));
uint64_t *out = data;
for (int k = 0; k < 4; k++) out[k] = g_result[first / 2][k];
return out[1] && out[3] ? VK_SUCCESS : VK_NOT_READY;
}
static void finish(uint32_t slot, uint64_t start, uint64_t end) {
g_result[slot][0] = start; g_result[slot][1] = 1; g_result[slot][2] = end; g_result[slot][3] = 1;
}
int main(void) {
struct gpuload_vk v = {
.device = (VkDevice)(uintptr_t)1, .create_pool = create_pool, .destroy_pool = destroy_pool, .alloc_cmds = alloc_cmds,
.begin = begin, .end = end, .cmd_reset = cmd_reset, .cmd_timestamp = cmd_timestamp,
.create_queries = create_queries, .destroy_queries = destroy_queries, .results = results,
};
struct gpuload_ring r = {0};
assert(!gpuload_ring_setup(&r, &v, 0, 64) && r.state == -1); // no timestamp period yet
gpuload_vk_period(&v, 52.083f);
assert(!gpuload_ring_setup(&r, &v, 0, 0) && r.state == -1); // family without timestamps
assert(gpuload_ring_setup(&r, &v, 0, 64) && r.state == 1);
VkCommandBuffer app[2] = {(VkCommandBuffer)(uintptr_t)0xa1, (VkCommandBuffer)(uintptr_t)0xa2};
VkProtectedSubmitInfo prot = {.sType = VK_STRUCTURE_TYPE_PROTECTED_SUBMIT_INFO, .protectedSubmit = VK_TRUE};
VkSubmitInfo batches[3] = {
{.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .commandBufferCount = 2, .pCommandBuffers = app},
{.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO}, // semaphores only
{.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO, .pNext = &prot, .commandBufferCount = 1, .pCommandBuffers = app},
};
uint32_t used = 0;
VkSubmitInfo *mine = gpuload_ring_wrap(&r, 3, batches, &used);
assert(mine && used == 1 && r.inflight == 1);
assert(mine[0].commandBufferCount == 4 && mine[0].pCommandBuffers[0] == r.slot[0].start &&
mine[0].pCommandBuffers[1] == app[0] && mine[0].pCommandBuffers[2] == app[1] &&
mine[0].pCommandBuffers[3] == r.slot[0].end);
assert(mine[1].commandBufferCount == 0 && mine[2].commandBufferCount == 1 && mine[2].pCommandBuffers == app);
free(mine);
assert(gpuload_ring_harvest(&r, &v) == 0 && r.inflight == 1); // not finished yet
finish(0, 1000, 1192); // 192 ticks = 10 us
assert(gpuload_ring_harvest(&r, &v) == 9999 && r.inflight == 0);
// A failed submit gives its slot back.
mine = gpuload_ring_wrap(&r, 1, batches, &used);
assert(used == 1 && r.inflight == 1);
gpuload_ring_unwind(&r, used);
assert(r.inflight == 0 && r.next == 1);
free(mine);
// Fill the ring: the 65th batch goes out untimed.
for (int i = 0; i < GPULOAD_RING; i++) { mine = gpuload_ring_wrap(&r, 1, batches, &used); assert(used == 1); free(mine); }
mine = gpuload_ring_wrap(&r, 1, batches, &used);
assert(used == 0 && mine[0].commandBufferCount == 2);
free(mine);
// Slot 0 comes round again: until the GPU resets it, it still shows the old pair, which must not count twice.
// Slots 1..63 finish after a counter restart (smaller ticks than slot 0's old pair); each still counts.
for (uint32_t s = 1; s < GPULOAD_RING; s++) finish(s, 10 * s, 10 * s + 4);
assert(gpuload_ring_harvest(&r, &v) == (uint64_t)(63 * 4 * 52.083f) && r.inflight == 1 && r.oldest == 0);
finish(0, 5, 9);
assert(gpuload_ring_harvest(&r, &v) == (uint64_t)(4 * 52.083f) && r.inflight == 0);
gpuload_ring_release(&r, &v);
puts("test_gpuload_ring: ok");
return 0;
}
+3
View File
@@ -26,6 +26,7 @@ SOURCE_PATHS = ['app/build.gradle', 'app/src/main/cpp/fakeinput_steam.cpp', 'app
'tools/mangoapp', 'tools/msitools', 'tools/pipewire', 'tools/comet', 'tools/xbox', 'tools/dirt-ws2', 'tools/components', 'tools/ci', 'tools/build_local.sh',
'tools/release/runtime_inputs.py', 'app/src/main/assets/desktop-removal',
'tools/quest', 'tools/vr/src', 'tools/vr/driver_droiddeck', 'tools/vr/layer', 'tools/vr/build.sh',
'tools/gpuload',
'app/src/main/cpp/waylandcomp/src/headpose_shm.h',
'app/src/main/cpp/venc/venc_proto.h',
'app/src/main/cpp/waylandcomp/src/spatialpose_shm.h',
@@ -92,6 +93,8 @@ def required():
'droiddeck/resources/settings/default.vrsettings', 'layer/libVkLayer_droiddeck_vr.so',
'layer/VK_LAYER_DROIDDECK_vr.json', 'layer/VK_LAYER_DROIDDECK_vr_implicit.json',
'layer/libdroiddeck_vr_scheduling.so']]
paths += ['assets/linuxfs/usr/local/lib/droiddeck-gpuload/' + name for name in
['libVkLayer_droiddeck_gpuload.so', 'VK_LAYER_DROIDDECK_gpu_load.json']]
for side in ['left', 'right']:
paths += [f'assets/linuxfs/usr/local/lib/droiddeck-vr/droiddeck/resources/rendermodels/droiddeck_hand_{side}/{name}'
for name in [f'droiddeck_hand_{side}.json', 'hand.mtl', 'hand.png', 'hand_open.obj', 'hand_fist.obj',
+56
View File
@@ -0,0 +1,56 @@
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "kgsl_busy.h"
#define ENTRIES 819
static uint32_t memstore[ENTRIES * KGSL_BUSY_ENTRY_WORDS];
static void stamp(int entry, uint32_t sop, uint32_t eop) {
memstore[entry * KGSL_BUSY_ENTRY_WORDS + KGSL_BUSY_SOP] = sop;
memstore[entry * KGSL_BUSY_ENTRY_WORDS + KGSL_BUSY_EOP] = eop;
}
int main(void) {
assert(kgsl_busy_first_slot(8 * 4096) == 813);
assert(kgsl_busy_first_slot(8 * 16384) == 3270);
assert(kgsl_busy_first_slot(7 * 40) == -1);
int first = kgsl_busy_first_slot(sizeof(memstore));
uint32_t last[KGSL_BUSY_SLOTS] = {0};
int moved = 0;
stamp(first, 100, 100);
stamp(first + 2, 7, 7);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 1 && last[0] == 100 && last[2] == 7);
moved = 0;
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
stamp(first + 2, 8, 7);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 1);
assert(moved == 1);
moved = 0;
stamp(first + 2, 8, 8);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
stamp(first + 4, 3, 2);
assert(kgsl_busy_sample(memstore, first, last, &moved) == 1);
memset(memstore, 0, sizeof(memstore));
stamp(first - 1, 5, 4);
stamp(first + KGSL_BUSY_SLOTS, 9, 8);
memset(last, 0, sizeof(last));
moved = 0;
assert(kgsl_busy_sample(memstore, first, last, &moved) == 0);
assert(moved == 0);
puts("PASS: kgsl memstore busy");
return 0;
}
+33
View File
@@ -0,0 +1,33 @@
"""Exercise the HUD's kgsl memstore busy sampling (app/src/main/cpp/gpuclock/kgsl_busy.h)."""
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
class KgslBusyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which(os.environ.get('CC', 'cc'))
if compiler is None:
raise unittest.SkipTest('A host C compiler is required')
cls.temp = tempfile.TemporaryDirectory(prefix='droiddeck-kgsl-busy-')
cls.addClassCleanup(cls.temp.cleanup)
cls.binary = Path(cls.temp.name) / 'kgsl-busy-test'
subprocess.run([compiler, '-std=c11', '-Wall', '-Wextra', '-Werror',
'-I', str(REPO / 'app/src/main/cpp/gpuclock'),
str(REPO / 'tools/tests/kgsl_busy_harness.c'), '-o', str(cls.binary)],
check=True, capture_output=True, text=True)
def test_ringbuffer_slots_and_busy_rule(self):
result = subprocess.run([str(self.binary)], capture_output=True, text=True)
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn('PASS: kgsl memstore busy', result.stdout)
if __name__ == '__main__':
unittest.main()