I am attempting to use the Linux perf tool to monitor counters. I am measuring counters in intervals of 500 milliseconds over a span of 10 minutes. I am trying to output the results of each counter into their own respective output files in a new directory. The new directory is created but I am only getting a hidden .txt file within it. I would like for files to be generated for each counter and their respective results to populate those files.
Here is the script:
#!/bin/bash
# Set the duration (in seconds) for monitoring
duration=$((10 * 60))
# Create an array of counters to monitor
counters=(
"cpu/event=0x00,umask=0x01,name=branch-instructions/"
"cpu/event=0x00,umask=0x02,name=branch-misses/"
"cpu/event=0x00,umask=0x0e,name=bus-cycles/"
"cpu/event=0x00,umask=0x08,name=cache-misses/"
"cpu/event=0x00,umask=0x00,name=instructions/"
"msr/aperf/"
"msr/mperf/"
"msr/pperf/"
"msr/smi/"
"msr/tsc/"
"power/energy-cores/"
"power/energy-pkg/"
"power/energy-ram/"
)
# Get the start time
start_time=$(date +%s)
# Create a directory to store the output files
output_dir="perf_results"
mkdir -p "$output_dir"
# Iterate through each counter and monitor them
for counter in "${counters[@]}"; do
# Extract the counter name for file storage
counter_name=$(echo "$counter" | awk -F"/" '{print $NF}')
# Create an output file for the counter
output_file="${output_dir}/${counter_name}.txt"
# Run perf command to monitor the counter at each interval and append to the file
run=1
while true; do
measurement=$(perf stat -a -e "$counter" -I 500 sleep 0.5 2>&1)
{
echo "Run: $run"
echo "Time: $(date)"
echo "Counter: $counter_name"
echo "Measurement: $measurement"
echo
} >> "$output_file"
# Check if the duration has elapsed
current_time=$(date +%s)
elapsed_time=$((current_time - start_time))
if [ "$elapsed_time" -ge "$duration" ]; then
break
fi
((run++))
done &
done
# Wait for the monitoring to complete
wait
# Display a message indicating the monitoring is finished
echo "Monitoring completed."
The output directory is created properly, but the files for each counter are not generated at all. Can someone assist with where I am going wrong?
I attempted to modify the script for just one counter and one file, but I ended up with the same result. A directory is generated, but with no result file inside.
Viewing the hidden .txt file that is generated, I see that all of the results are there. What I want is all of the results for each counter to be placed into their own .txt file. For example, branch-instructions would have a file in the output directory called branch-instructions.txt and so on.