Felipe97/llama-cpp-compiled
01.2k
1# Compile-time profiling using clang -ftime-trace + ClangBuildAnalyzer.2#3# Usage:4# .\scripts\build-profile.ps1 [-Full] [-Jobs N]5#6# -Full : include Server, Tools, and Tests (default: minimal build)7# -Jobs : number of parallel jobs (default: all cores)8#9# Requires ClangBuildAnalyzer:10# https://github.com/aras-p/ClangBuildAnalyzer11 12param(13 [switch]$Full,14 [int]$Jobs = [Environment]::ProcessorCount15)16 17$ErrorActionPreference = "Stop"18 19$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path20$RootDir = Split-Path -Parent $ScriptDir21 22if ($Full) {23 $BuildDir = Join-Path $RootDir "build-profile-full"24 $Report = Join-Path $BuildDir "profile-report-full.txt"25} else {26 $BuildDir = Join-Path $RootDir "build-profile-baseline"27 $Report = Join-Path $BuildDir "profile-report.txt"28}29 30$OutputBin = Join-Path $BuildDir "clang_analysis.bin"31 32if (-not (Get-Command clang++ -ErrorAction SilentlyContinue)) {33 Write-Error "clang++ not found"34 exit 135}36 37if (-not (Get-Command ninja -ErrorAction SilentlyContinue)) {38 Write-Error "ninja not found (required so cmake does not fall back to the Visual Studio/MSVC generator)"39 exit 140}41 42if (-not (Get-Command ClangBuildAnalyzer -ErrorAction SilentlyContinue)) {43 Write-Error "ClangBuildAnalyzer not found`n https://github.com/aras-p/ClangBuildAnalyzer/releases"44 exit 145}46 47$ClangVer = (clang++ --version | Select-Object -First 1)48Write-Host "compiler : $ClangVer"49Write-Host "build dir: $BuildDir"50Write-Host "output : $OutputBin"51Write-Host "jobs : $Jobs"52Write-Host ""53 54if (Get-Command ccache -ErrorAction SilentlyContinue) {55 Write-Host "clearing ccache..."56 ccache -C -z57}58 59$env:CCACHE_DISABLE = "1"60 61$TestsFlag = if ($Full) { "ON" } else { "OFF" }62$ToolsFlag = if ($Full) { "ON" } else { "OFF" }63$ServerFlag = if ($Full) { "ON" } else { "OFF" }64 65cmake --fresh `66 -S $RootDir `67 -B $BuildDir `68 -G "Ninja" `69 -DCMAKE_BUILD_TYPE=Release `70 -DCMAKE_C_COMPILER=clang `71 -DCMAKE_CXX_COMPILER=clang++ `72 -DCMAKE_C_FLAGS="-ftime-trace" `73 -DCMAKE_CXX_FLAGS="-ftime-trace" `74 -DGGML_CCACHE=OFF `75 -DGGML_OPENMP=ON `76 -DGGML_NATIVE=OFF `77 "-DLLAMA_BUILD_TESTS=$TestsFlag" `78 -DLLAMA_BUILD_EXAMPLES=OFF `79 "-DLLAMA_BUILD_TOOLS=$ToolsFlag" `80 "-DLLAMA_BUILD_SERVER=$ServerFlag" `81 -DLLAMA_BUILD_APP=OFF82 83if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }84 85$StrayTrace = Join-Path $RootDir "-.json"86if (Test-Path $StrayTrace) {87 Remove-Item $StrayTrace -Force88}89 90Write-Host ""91Write-Host "Initializing ClangBuildAnalyzer..."92ClangBuildAnalyzer --start $BuildDir93Write-Host ""94 95Write-Host "building..."96Write-Host ""97 98$StartTime = Get-Date99 100cmake --build $BuildDir --clean-first -j $Jobs101 102if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }103 104$Elapsed = (Get-Date) - $StartTime105 106Write-Host ""107Write-Host ("build time: {0}s ({1}m {2}s)" -f [int]$Elapsed.TotalSeconds, [int]$Elapsed.TotalMinutes, $Elapsed.Seconds)108Write-Host ""109 110Write-Host "Aggregating profile metrics..."111ClangBuildAnalyzer --stop $BuildDir $OutputBin | Out-Null112 113Write-Host ""114Write-Host ("=" * 80)115 116$TUs = "?"117if (Test-Path $Report) {118 $Match = Select-String -Path $Report -Pattern "Compilation \((\d+)" | Select-Object -First 1119 if ($Match) { $TUs = $Match.Matches[0].Groups[1].Value }120}121 122ClangBuildAnalyzer --analyze $OutputBin | Tee-Object -FilePath $Report123 124Write-Host ""125Write-Host "translation units: $TUs"126Write-Host ""127Write-Host "largest trace files (top 20 by size):"128 129Get-ChildItem -Path $BuildDir -Recurse -Filter "*.json" |130 Where-Object { $_.Name -ne "compile_commands.json" } |131 Sort-Object Length -Descending |132 Select-Object -First 20 |133 ForEach-Object { "{0,8:F1} KB {1}" -f ($_.Length / 1024), $_.FullName }134 135Write-Host ""136Write-Host "ClangBuildAnalyzer report was generated: $Report"137 