Skip to content

Commit

Permalink
Update bashvm.sh
Browse files Browse the repository at this point in the history
more accurate CPU usage values with less leading zeros
  • Loading branch information
babywhale321 authored Dec 15, 2024
1 parent 53ab171 commit c01690b
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions bashvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,11 @@ while true; do
cpu_time_2=$(virsh domstats $vm_name | grep "vcpu.$i.time" | cut -d= -f2)
# Delta is to be equal of time between recordings
delta_t=1
# convert delta_t to nanoseconds
delta_t_ns=$(echo "$delta_t * 1000000000" | bc)
# Calculate the scaling factor
scaling_factor=$(echo "scale=10; 100 / $delta_t_ns" | bc)
# Calculate the individual core usage
cpu_usage=$(echo "scale=2; (($cpu_time_2 - $cpu_time_1) * $scaling_factor)" | bc)
echo "CPU $i Usage: $cpu_usage%"
# Calculate the difference in CPU time (nanoseconds)
delta_cpu_time=$((cpu_time_2 - cpu_time_1))
# Normalize CPU usage as a percentage of total time available
cpu_usage=$(echo "scale=2; ($delta_cpu_time / ($delta_t * 1000000000)) * 100" | bc)
printf "CPU %d Usage: %.2f%%\n" "$i" "$cpu_usage"
done
;;
2)
Expand Down

0 comments on commit c01690b

Please sign in to comment.