Skip to content

Instantly share code, notes, and snippets.

@holyjak
Last active January 30, 2024 22:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save holyjak/1b58dedae3207b4a56c9abcde5f3fdb5 to your computer and use it in GitHub Desktop.
Save holyjak/1b58dedae3207b4a56c9abcde5f3fdb5 to your computer and use it in GitHub Desktop.
Gnuplot script to plot memory, CPU usage of a process from `top`
#!/usr/bin/env -S gnuplot --persist -c
# Plot memory and CPU usage over time. Usage:
# usage-plot.gp <input file> [<output .png file>]
# where the input file has the columns `<unix time> <memory, with m/g suffix> <% cpu>`
# To create the input file, see https://gist.github.com/jakubholynet/931a3441982c833f5f8fcdcf54d05c91
# Arguments:
infile=ARG1
outfile=ARG2
set term x11
set title 'Memory, CPU usage from' . infile
set xdata time
set timefmt "%s"
set xlabel "Time [[hh:]mm:ss]"
set ylabel "Memory usage"
set format y '%.1s%cB'
set y2label 'CPU usage'
set format y2 '%.0s%%'
set y2tics nomirror
set tics out
set autoscale y
set autoscale y2
# Credit: Christoph @ https://stackoverflow.com/a/52822256/204205
resolveUnit(s)=(pos=strstrt("kmgtp",s[strlen(s):*]), real(s)*(1024**pos))
if (exists("outfile") && strlen(outfile) > 0) {
print "Outputting to the file ", outfile
set term png # 640,480
set output outfile
}
# Styling
set style line 1 linewidth 2 linecolor 'blue'
set style line 2 linecolor 'light-green'
#set xtics font ", 10"
set tics font ", 10"
set xtics rotate 60 # put label every 60s, make vertical so they don't clash in .png if too many
plot infile u 1:3 with lp axes x1y2 title "cpu" linestyle 2, \
infile using 1:(resolveUnit(stringcolumn(2))) with linespoints title "memory" linestyle 1
@holyjak
Copy link
Author

holyjak commented Jul 21, 2020 via email

@Dhaour9x
Copy link

Dhaour9x commented Jul 21, 2020

yes,

fsd@ubuntu:~$ gnuplot

	G N U P L O T
	Version 5.0 patchlevel 3    last modified 2016-02-21 

	Copyright (C) 1986-1993, 1998, 2004, 2007-2016
	Thomas Williams, Colin Kelley and many others

	gnuplot home:     http://www.gnuplot.info
	faq, bugs, etc:   type "help FAQ"
	immediate help:   type "help"  (plot window: hit 'h')

Terminal type set to 'qt'
gnuplot> 

I'm a bit confused, can you please give me more detailed instructions, which setps do I have to do to make this work. In you blog I don't really understand the steps.
Thank you

@holyjak
Copy link
Author

holyjak commented Jul 22, 2020 via email

@ricksorensen
Copy link

On linux the use -S option to split the env command args ('gnuplot --persist -c') into separate parts .. the default is to use all data after env concatenated as command. EG:
#!/usr/bin/env -S gnuplot --persist -c

@holyjak
Copy link
Author

holyjak commented Jul 27, 2020 via email

@sikmir
Copy link

sikmir commented Feb 14, 2023

--- plot-usage.gp.orig	2023-02-14 16:59:13.000000000 +0300
+++ plot-usage.gp	2023-02-14 16:59:40.000000000 +0300
@@ -2,7 +2,7 @@
 # Plot memory and CPU usage over time. Usage:
 #  usage-plot.gp <input file> [<output .png file>]
 # where the input file has the columns `<unix time> <memory, with m/g suffix> <% cpu>`
-# To create the input file, see https://gist.github.com/jakubholynet/931a3441982c833f5f8fcdcf54d05c91
+# To create the input file, see https://gist.github.com/holyjak/931a3441982c833f5f8fcdcf54d05c91

 # Arguments:
 infile=ARG1

@sikmir
Copy link

sikmir commented Feb 14, 2023

".../plot-usage.gp" line 42: Trailing characters after numeric expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment