May 14, 2024

Shell Scripting to Display Bar Graphs in Linux Terminal

 This week was quite hectic for me with course assignments and their merciless deadlines. One such assignment in Advanced Computer Architecture was simulation of various benchmarks in alpha architecture in simplescalar, a well-known computer architecture simulator. I was supposed to run a total of 4 benchmarks with different configurations of cache memory, instruction issue widths, commit widths, in-order execution modes, etc and I had to plot the required performance parameters for every benchmark. A conservative estimate would be around 40 plots!
 Since the simulation platform was Linux, I could breathe a sigh of relief since most things can be automated using a powerful tool called shell. Scripting made my life easier here since I can automate a bunch of simulations without having to keep an eye on each and every simulation which would take anywhere from 20 minutes to 20 hours.
 The problem arose when it came to plotting the performance results. Because, each simulation had its own output file which has its performance parameters. While most of my colleagues resorted to manually taking down values from the simulation file and plotting them using Microsoft excel, I thought why not this can be automated. 😉 
 But plotting a graph in terminal will not be as simple as plotting it in Excel, but if I manage to pull it off, that will be something I can be a little proud of. Besides that, I show (off to :p) my friends, how awesome piece of a tool shell is. 

APPROACH:

 A simple performance comparison plot would be a bar chart. The wget tool’s download progress indicator in a Linux command line is somewhat similar to 
============================================>| 89% 
This is a minimalistic indicator and does the job well. So, I thought of comparing the parameters using a horizontal bar graph which will be simple and visually appealing too. So my rough idea was to implement something like
         
param1:  |||||||||||||||||||||||||||||||||||||||||||||||||||| 67%
param2:  ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 75%
param3:  |||||||||||||||||||||||||||||| 39%

Pretty huh ? 🙂 This can be displayed in terminal interface and looks little geeky too. I started off writing a script which
  • Takes the simulation file name as command-line argument
  • Uses grep & awk to grab the performance parameters from the file
  • Find the maximum and taking it as a reference (100%) 
  • Scaling others and plot in command-line as vertical bar graph
This worked well, and did the job ! But I found that something was missing. Colors!! Displaying the output in colors and weird way in Turbo C (graphics.h) was my something I loved in school. A little googling of about colored text outputs in terminal led me to an impressive tool called tput. It does basic coloring of texts, few formatting options like bold, underline, etc.
 So I modified my code and added this feature. What a beauty!! The output had a colorful geeky touch which was quite impressive.

SAMPLE OUTPUTS:

Sample Script: