action.yml 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. name: 'Show Cache Stats'
  2. description: 'Displays statistics for ccache and Bazel remote cache to help analyze cache performance and hit rates'
  3. inputs:
  4. clear_stats:
  5. description: 'Whether to clear ccache stats after displaying (default: true)'
  6. required: true
  7. type: boolean
  8. default: true
  9. runs:
  10. using: 'composite'
  11. steps:
  12. - name: Define cache summary helper
  13. shell: bash
  14. working-directory: ${{ github.workspace }}
  15. run: |
  16. set -euo pipefail
  17. cat > /tmp/cache_summary.sh <<'EOF'
  18. summarize_dir() {
  19. local path="$1"
  20. local top_n=${2:-10}
  21. local sub_n=${3:-5}
  22. if [ ! -e "$path" ]; then
  23. echo "Path $path not found"
  24. return
  25. fi
  26. echo "---------------"
  27. echo "Summary: $path"
  28. du -sh "$path" || true
  29. echo "Top ${top_n} entries at top level:"
  30. du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n "$top_n" || true
  31. echo "Showing up to ${sub_n} entries one level deeper for the largest top-level items:"
  32. while read -r line; do
  33. entry_path=$(echo "$line" | awk '{print $2}')
  34. [ -z "$entry_path" ] && continue
  35. if [ "$entry_path" = "$path" ]; then
  36. continue
  37. fi
  38. echo "-> $entry_path :"
  39. du -h --max-depth=1 "$entry_path" 2>/dev/null | sort -hr | head -n "$sub_n" || true
  40. done < <(du -h --max-depth=1 "$path" 2>/dev/null | sort -hr | head -n "$top_n")
  41. echo "---------------"
  42. }
  43. EOF
  44. - name: Show ccache Stats
  45. shell: bash
  46. working-directory: ${{ github.workspace }}
  47. run: |
  48. set -euo pipefail
  49. echo "===================="
  50. echo "=== ccache stats ==="
  51. echo "===================="
  52. if ! command -v ccache >/dev/null 2>&1; then
  53. echo "⚠️ ccache not installed, skipping ccache stats"
  54. echo "===================="
  55. exit 0
  56. fi
  57. ccache -s || true
  58. echo "====================="
  59. echo "=== ccache config ==="
  60. echo "====================="
  61. ccache -p || true
  62. echo "====================="
  63. STATS="$(ccache -s)"
  64. CACHEABLE=$(echo "$STATS" | awk '/Cacheable calls:/ {
  65. match($0, /[0-9]+/);
  66. print substr($0, RSTART, RLENGTH)
  67. }' | head -n1)
  68. HITS=$(echo "$STATS" | awk '/^[[:space:]]*Hits:/ {
  69. match($0, /[0-9]+/);
  70. print substr($0, RSTART, RLENGTH)
  71. }' | head -n1)
  72. DIRECT=$(echo "$STATS" | awk '/Direct:/ {
  73. match($0, /[0-9]+/);
  74. print substr($0, RSTART, RLENGTH)
  75. }' | head -n1)
  76. if [ "${CACHEABLE:-0}" -gt 0 ]; then
  77. HIT_RATE=$(awk -v h="${HITS:-0}" -v c="$CACHEABLE" 'BEGIN{printf "%.1f", (h/c)*100}')
  78. DIRECT_RATE=$(awk -v d="${DIRECT:-0}" -v c="$CACHEABLE" 'BEGIN{printf "%.1f", (d/c)*100}')
  79. else
  80. HIT_RATE="0.0"
  81. DIRECT_RATE="0.0"
  82. fi
  83. echo "hit_rate=${HIT_RATE}%"
  84. echo "direct_rate=${DIRECT_RATE}%"
  85. if [ "${{ inputs.clear_stats }}" = "true" ]; then
  86. echo "=== ccache reset ==="
  87. echo "====================="
  88. ccache -z || true
  89. echo "====================="
  90. # Touch all files to prevent LRU eviction of freshly restored cache
  91. if [ -d /home/runner/.ccache ] && find /home/runner/.ccache -type f -print -quit 2>/dev/null | grep -q .; then
  92. echo "Updating ccache atime to prevent cache eviction..."
  93. find /home/runner/.ccache -type f -exec touch -t $(date -d "1 day ago" +%Y%m%d%H%M) {} +
  94. echo "✅ ccache atime updated"
  95. fi
  96. if [ -d /home/runner/.ld_cache ] && find /home/runner/.ld_cache -type f -print -quit 2>/dev/null | grep -q .; then
  97. echo "Updating LTO cache atime to prevent cache eviction..."
  98. find /home/runner/.ld_cache -type f -exec touch -t $(date -d "1 day ago" +%Y%m%d%H%M) {} +
  99. echo "✅ LTO cache atime updated"
  100. fi
  101. else
  102. echo "Keeping ccache stats for next run"
  103. fi
  104. - name: Show ccache folder Stats
  105. shell: bash
  106. working-directory: ${{ github.workspace }}
  107. run: |
  108. set -euo pipefail
  109. # shellcheck disable=SC1091
  110. source /tmp/cache_summary.sh
  111. echo "==============="
  112. echo "=== .ccache ==="
  113. echo "==============="
  114. summarize_dir /home/runner/.ccache 10 5 || true
  115. - name: Show Bazel Cache folder Stats
  116. shell: bash
  117. working-directory: ${{ github.workspace }}
  118. run: |
  119. set -euo pipefail
  120. # shellcheck disable=SC1091
  121. source /tmp/cache_summary.sh
  122. echo "===================="
  123. echo "=== .cache/bazel ==="
  124. echo "===================="
  125. summarize_dir /home/runner/.cache/bazel 10 5 || true
  126. - name: Show ld cache folder Stats
  127. shell: bash
  128. working-directory: ${{ github.workspace }}
  129. run: |
  130. set -euo pipefail
  131. # shellcheck disable=SC1091
  132. source /tmp/cache_summary.sh
  133. echo "================="
  134. echo "=== .ld_cache ==="
  135. echo "================="
  136. summarize_dir /home/runner/.ld_cache 10 5 || true