User forums > Using Code::Blocks
How to configure Perf profiler and Hotspot as external tool on Linux
(1/1)
cdavalillo:
To profile an executable application with Perf and Hotspot on Linux do the following:
* Install Perf with the command
--- Code: ---sudo apt install linux-perf
--- End code ---
* Install Hotspot with the command
--- Code: ---sudo apt install hotspot
--- End code ---
* Open Code::Blocks and go to Tools menu
* Make click on Add to add a new tool
* In Name put Perf Profiler
* Create a .sh script with a name perf-profile.sh in a custom location
* Add the following code to the script
--- Code: ---#!/bin/bash
# Universal perf profiler for Code::Blocks 25.01
# Usage:
# perf-profile.sh <executable_path> [lib_directory]
# If lib_directory is omitted, uses executable's directory.
PERF_OUTPUT_DIR="$HOME/PERF_DATA"
if [ $# -lt 1 ]; then
echo "❌ Error: Missing executable path."
echo " Usage: $0 <executable> [lib_dir]"
exit 1
fi
EXE_PATH="$1"
EXE_DIR="$(dirname "$EXE_PATH")"
EXE_NAME="$(basename "$EXE_PATH")"
if [ ! -f "$EXE_PATH" ]; then
echo "❌ Executable not found: $EXE_PATH"
exit 1
fi
# Determine lib directory
if [ $# -ge 2 ] && [ -n "$2" ]; then
LIB_DIR="$2"
echo "🧩 Using custom lib dir (arg): $LIB_DIR"
else
LIB_DIR="$EXE_DIR"
echo "🧩 Using executable's dir as lib dir: $LIB_DIR"
fi
if [ ! -d "$LIB_DIR" ]; then
echo "⚠️ Warning: Library directory does not exist: $LIB_DIR"
fi
# Prepare output
mkdir -p "$PERF_OUTPUT_DIR"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
PERF_DATA_PATH="$PERF_OUTPUT_DIR/${EXE_NAME}_${TIMESTAMP}.perf.data"
echo "▶️ Profiling: $EXE_PATH"
echo "📂 Working dir: $EXE_DIR"
echo "💾 Perf data → $PERF_DATA_PATH"
cd "$EXE_DIR" || exit 1
export LD_LIBRARY_PATH="$LIB_DIR:$LD_LIBRARY_PATH"
perf record --call-graph dwarf -o "$PERF_DATA_PATH" \
env LD_LIBRARY_PATH="$LIB_DIR:$LD_LIBRARY_PATH" "$EXE_PATH"
if [ -f "$PERF_DATA_PATH" ]; then
echo "✅ Profiling done. Opening in Hotspot..."
hotspot "$PERF_DATA_PATH" &
else
echo "⚠️ No perf data generated."
fi
--- End code ---
* On the script you can custom the path PERF_OUTPUT_DIR to any location you want to store the perf collected data (this data is large in size so take that in count)
* Give execution permissions to the script with the command
--- Code: ---chmod +x perf-profile.sh
--- End code ---
* In Code::Blocks Tools on Executable browse to the script file
* In Parameters add the following
--- Code: ---$(TARGET_OUTPUT_FILE) /path/to/custom/libraries
--- End code ---
* where the path to custom libraries is the path where the libraries referenced by the application to profile are located
* We leave Working directory empty
* Click OK to add the tool
* Add sudo capabilities to perf with the command
--- Code: ---sudo sysctl -w kernel.perf_event_paranoid=1
--- End code ---
After doing all this we have the profiler Perf tool working in combination with Hotspot. To profile an application we activate a executable project rebuild all and then we go to Tools and click on the Perf Profiler, execute our application for a while and then close it to see the data analysis on Hotspot.
cdavalillo:
I modified the last command to give perf permissions, so now it is
--- Code: ---sudo sysctl -w kernel.perf_event_paranoid=1
--- End code ---
cdavalillo:
In the script the line
--- Code: ---perf record --call-graph dwarf -o "$PERF_DATA_PATH" \
env LD_LIBRARY_PATH="$LIB_DIR:$LD_LIBRARY_PATH" "$EXE_PATH"
--- End code ---
Might not work well in some cases then just remove the env part so it would be like
--- Code: ---perf record --call-graph dwarf -o "$PERF_DATA_PATH" "$EXE_PATH"
--- End code ---
Navigation
[0] Message Index
Go to full version