Suggestions for shell script optimization articles and/or software
Posted on 2010-04-05
I have a 2000+ bash script that performs a maintenance activity and enumerates storage devices & reports state. It needs to run every 10 seconds, and after a few rounds of optimization, I've got it down to 4-8 seconds, depending on what changes between the last and current invocation. This script runs in an appliance, and compiling to C is not an option (because the company that contracted me wants it as a shell script for readability, and ease-of-maintenance). This runs on a multi-core system, and CPU overhead is not much of a bottleneck, but as we all know, shell scripts are basically pigs, so I want to be frugal with CPU resources but not anal about using them
So rather than post a section and ask for suggestions, I thought I would open this up for something more useful to the community as a whole.
Q1: Are there any freeware or open source shell script profiling tools that could assist in finding areas of the code that are greatest problems? Ideally is there a low-overhead way to create a precision timer and start/stop it as necessary to show elapsed time, and what the holdup was? I.e, for a given chunk of code, can I see if the process performed disk I/O, had any wait states, or context switching, which would then be a candidate for some tweaking?
Q2: Any good articles, tips, whatever in general on minimizing execution time of shell script? This particular environment is latest LINUX kernel and I am using bash, so while it will be useful in general, please keep in mind that if there is a product that can help, it has to work on bash/LINUX.
For example, a tip might be, "If you have to perform frequent lookups that involve reading data files, make use of /dev/shm to store those results, and just see if whatever you are looking up has changed since last polling time. If it has not changed, then just read results from a tmp file stored in the /dev/shm ramdisk. If it has changed, or no cross reference exists, then enumerate as before, and save results in /dev/shm.
Or another, use the native (( )) feature to perform numerical calculations rather than call the expr utility. I.e, (( x = x + 10 )) is more efficient then x=`expr $x + 10`
Thank you. (yes, expect a lot of split points over next day or two)