Skip to content

Commit bb475a0

Browse files
committed
gmentci: add PR stack usage report via puncover
Add scripts and a CI workflow step that compare static stack usage between a PR and its merge base. On each PR, the workflow rebuilds with `CONFIG_STACK_USAGE=y`, runs puncover via `stack_threads.py`, and posts a sticky comment with a per-thread table showing configured size, max static depth, delta vs base, and remaining margin. Thread entry points are discovered from `K_THREAD_DEFINE` in app sources; Zephyr/NCS subsystem stacks are resolved via `rg` in the west tree with a small fallback map. Work queues list configured size only since puncover can't attribute depth per queue. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 47257dd commit bb475a0

4 files changed

Lines changed: 596 additions & 0 deletions

File tree

.github/workflows/build.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,28 @@ jobs:
280280
header: app-size-report
281281
path: ${{ github.workspace }}/size-comment.md
282282

283+
- name: Install puncover
284+
if: github.event_name == 'pull_request'
285+
run: pip install puncover --break-system-packages
286+
287+
- name: Generate PR stack margin comment for thingy91x image
288+
if: github.event_name == 'pull_request'
289+
env:
290+
PR_APP_DIR: ${{ github.workspace }}/asset-tracker-template/app
291+
STACK_COMMENT_PATH: ${{ github.workspace }}/stack-comment.md
292+
CI_RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
293+
run: |
294+
SDK_BIN=$(ls -d /opt/toolchains/zephyr-sdk-*/arm-zephyr-eabi/bin 2>/dev/null | head -1)
295+
[ -n "$SDK_BIN" ] && export PATH="$SDK_BIN:$PATH"
296+
bash asset-tracker-template/scripts/ci/pr-stack-usage.sh
297+
298+
- name: Post PR stack margin sticky comment for thingy91x image
299+
if: github.event_name == 'pull_request'
300+
uses: marocchino/sticky-pull-request-comment@0ea0beb66eb9baf113663a64ec522f60e49231c0 # v3.0.4
301+
with:
302+
header: stack-usage-report
303+
path: ${{ github.workspace }}/stack-comment.md
304+
283305
- name: Upload artifacts
284306
uses: actions/upload-artifact@v4
285307
with:

app/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CONFIG_TFM_SPM_LOG_LEVEL_DEBUG=y
2525
CONFIG_MBEDTLS_PSA_STATIC_KEY_SLOTS=y
2626

2727
# Heap and stacks
28+
CONFIG_STACK_USAGE=y
2829
CONFIG_MAIN_STACK_SIZE=2560
2930
# Extended AT host/monitor stack/heap sizes since some nrf_cloud credentials are longer than 1024 bytes.
3031
CONFIG_AT_MONITOR_HEAP_SIZE=2176

scripts/ci/pr-stack-usage.sh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env bash
2+
# Generate a sticky-comment-ready markdown body with stack headroom % from the main CI build.
3+
#
4+
# Required env vars:
5+
# STACK_COMMENT_PATH
6+
# Optional:
7+
# PR_APP_DIR (default: <repo>/app)
8+
# CI_RUN_URL
9+
10+
set -euo pipefail
11+
: "${STACK_COMMENT_PATH:?}"
12+
13+
REPO_ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
14+
WORKSPACE_DIR="$(cd "$REPO_ROOT/.." && pwd)"
15+
PR_APP_DIR="${PR_APP_DIR:-$REPO_ROOT/app}"
16+
BUILD_DIR="${PR_APP_DIR}/build"
17+
STACK_DIR="${STACK_DIR:-$(dirname "$STACK_COMMENT_PATH")/stack}"
18+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
19+
STACK_THREADS_PY="$SCRIPT_DIR/stack_threads.py"
20+
STACK_JSON="$STACK_DIR/stack.json"
21+
22+
mkdir -p "$STACK_DIR" "$(dirname "$STACK_COMMENT_PATH")"
23+
24+
write_failure_comment() {
25+
echo "Stack margin report failed. See [CI run](${CI_RUN_URL:-}) for logs." >"$STACK_COMMENT_PATH"
26+
}
27+
28+
ELF="$BUILD_DIR/app/zephyr/zephyr.elf"
29+
CONFIG="$BUILD_DIR/app/zephyr/.config"
30+
31+
if [[ ! -f "$ELF" || ! -f "$CONFIG" ]]; then
32+
echo "Missing build outputs under $BUILD_DIR" >&2
33+
write_failure_comment
34+
exit 0
35+
fi
36+
37+
if ! python3 "$STACK_THREADS_PY" report \
38+
"$ELF" "$BUILD_DIR/app" "$CONFIG" "$STACK_JSON" "$PR_APP_DIR" \
39+
--west "$WORKSPACE_DIR"; then
40+
write_failure_comment
41+
exit 0
42+
fi
43+
44+
if ! python3 "$STACK_THREADS_PY" format-comment \
45+
"$STACK_JSON" \
46+
"$PR_APP_DIR" \
47+
--config "$CONFIG" \
48+
--west "$WORKSPACE_DIR" \
49+
--ci-url "${CI_RUN_URL:-}" \
50+
>"$STACK_COMMENT_PATH"; then
51+
write_failure_comment
52+
exit 0
53+
fi

0 commit comments

Comments
 (0)