Skip to content

Commit

Permalink
Refactored stats.sh to display approx. cpu usage and mem usage in .cs…
Browse files Browse the repository at this point in the history
…v format
  • Loading branch information
matlorr committed Oct 1, 2024
1 parent 6a8abf4 commit 4ac38ef
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions relay/stats.sh
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#!/bin/bash
#!/usr/bin/env bash
cores=$(nproc --all)
cpu_measure=$(top -bn2 | grep '%Cpu' | tail -1)
mem_measure=$(top -bn2 | grep 'Mem' | head -1)

cpu=$(echo $cpu_measure | awk -v cores=$cores '{print 1-($8/(cores*100))}')
mem=$(echo $mem_measure | awk '{print $8/$4}')

printf "CPU, MEM\n%.2f, %.2f\n" $cpu $mem

# first argument is the process ID
pid="$1"

# number of CPUs available
nproc=$(nproc --all)

# hacky way to print the content of a CSV file containing CPU/Mem usage of the process
top -bn2 -p $pid | awk -v nproc=$nproc 'NR > 16 {$12=substr($0,72); printf "CPU, MEM\n%.2f, %.2f\n", $9/nproc, $10}'

0 comments on commit 4ac38ef

Please sign in to comment.