@@ -27,23 +27,23 @@ echo "Timestamp,Used_MB,Free_MB,Total_MB" > "$OUTPUT_FILE"
27
27
# Function to get system memory usage based on OS
28
28
get_memory_usage () {
29
29
TIMESTAMP=$( date +%s)
30
-
30
+
31
31
if [[ " $OS " == " macos" ]]; then
32
32
# macOS memory stats using vm_stat
33
33
VM_STAT=$( vm_stat)
34
34
PAGE_SIZE=$( sysctl -n hw.pagesize)
35
35
TOTAL_MEM=$( sysctl -n hw.memsize)
36
36
TOTAL_MB=$( echo " scale=2; $TOTAL_MEM / 1048576" | bc)
37
-
37
+
38
38
PAGE_FREE=$( echo " $VM_STAT " | grep " Pages free:" | awk ' {print $3}' | sed ' s/\.//' )
39
39
PAGE_ACTIVE=$( echo " $VM_STAT " | grep " Pages active:" | awk ' {print $3}' | sed ' s/\.//' )
40
40
PAGE_INACTIVE=$( echo " $VM_STAT " | grep " Pages inactive:" | awk ' {print $3}' | sed ' s/\.//' )
41
41
PAGE_SPECULATIVE=$( echo " $VM_STAT " | grep " Pages speculative:" | awk ' {print $3}' | sed ' s/\.//' )
42
42
PAGE_WIRED=$( echo " $VM_STAT " | grep " Pages wired down:" | awk ' {print $4}' | sed ' s/\.//' )
43
-
43
+
44
44
USED_PAGES=$(( $PAGE_ACTIVE + $PAGE_WIRED ))
45
45
USED_MB=$( echo " scale=2; ($USED_PAGES * $PAGE_SIZE ) / 1048576" | bc)
46
-
46
+
47
47
FREE_PAGES=$(( $PAGE_FREE + $PAGE_INACTIVE + $PAGE_SPECULATIVE ))
48
48
FREE_MB=$( echo " scale=2; ($FREE_PAGES * $PAGE_SIZE ) / 1048576" | bc)
49
49
else
@@ -53,7 +53,7 @@ get_memory_usage() {
53
53
USED_MB=$( echo " $MEM_INFO " | awk ' {print $3}' )
54
54
FREE_MB=$( echo " $MEM_INFO " | awk ' {print $4}' )
55
55
fi
56
-
56
+
57
57
echo " $TIMESTAMP ,$USED_MB ,$FREE_MB ,$TOTAL_MB " >> " $OUTPUT_FILE "
58
58
}
59
59
@@ -98,7 +98,7 @@ stats "${OUTPUT_FILE}" skip 1 using 1 prefix "ts_" nooutput
98
98
if (ts_records > 0) {
99
99
# Calculate the starting timestamp to use as time zero
100
100
first_ts = ts_min
101
-
101
+
102
102
# Plot the data with relative time
103
103
plot "${OUTPUT_FILE} " using (\$ 1-first_ts):2 with lines lw 2 title "Used Memory", \\
104
104
"${OUTPUT_FILE} " using (\$ 1-first_ts):3 with lines lw 2 title "Free Memory"
@@ -115,4 +115,6 @@ rm plot_script.gnu
115
115
116
116
echo " Memory usage graph saved as memory_usage.png"
117
117
echo " Raw data saved as $OUTPUT_FILE "
118
- echo " Script exited with code $SCRIPT_EXIT_CODE "
118
+ echo " Script exited with code $SCRIPT_EXIT_CODE "
119
+
120
+ exit $SCRIPT_EXIT_CODE
0 commit comments