memcalc.bash 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/bin/bash
  2. (set -o igncr) 2>/dev/null && set -o igncr; # this comment is required
  3. set -$-ue${DEBUG+xv}
  4. #######################################################################################################################
  5. # This script processes the memory consumption of an application and prints it out to the console.
  6. #
  7. # usage:
  8. # memcalc.bash <READELFFILE> <AVAILABLEFLASH> <AVAILABLESRAM> <STARTFLASH> <STARTSRAM>
  9. #
  10. #######################################################################################################################
  11. READELFFILE=$1 # file location of readelf output
  12. AVAILABLEFLASH=$2 # Max available internal flash
  13. AVAILABLESRAM=$3 # Max available internal SRAM
  14. STARTFLASH=$4 # Start of internal flash
  15. STARTSRAM=$5 # Start of internal SRAM
  16. ENDFLASH=$((STARTFLASH + AVAILABLEFLASH))
  17. ENDSRAM=$((STARTSRAM + AVAILABLESRAM))
  18. # Gather the numbers
  19. memcalc() {
  20. local internalFlash=0
  21. local internalSram=0
  22. printf " -------------------------------------------------- \n"
  23. printf " | %-20s | %-10s | %-8s | \n" 'Section Name' 'Address' 'Size'
  24. printf " -------------------------------------------------- \n"
  25. while IFS=$' \t\n\r' read -r line; do
  26. local lineArray=($line)
  27. local numElem=${#lineArray[@]}
  28. # Only look at potentially valid lines
  29. if [[ $numElem -ge 6 ]]; then
  30. # Section headers
  31. if [[ ${lineArray[0]} == "["* ]]; then
  32. local sectionElement=NULL
  33. local addrElement=00000000
  34. local sizeElement=000000
  35. for (( idx = 0 ; idx <= $numElem-4 ; idx = $idx+1 ));
  36. do
  37. if [[ ${lineArray[$idx]} == *"]" ]]; then
  38. sectionElement=${lineArray[$idx+1]}
  39. fi
  40. # Look for regions with SHF_ALLOC = A
  41. if [[ ${#lineArray[idx]} -eq 8 ]] && [[ ${#lineArray[idx+1]} -eq 6 ]] && [[ ${#lineArray[idx+2]} -eq 6 ]] \
  42. && [[ ${lineArray[$idx+4]} == *"A"* ]] ; then
  43. addrElement=${lineArray[$idx]}
  44. sizeElement=${lineArray[$idx+2]}
  45. fi
  46. done
  47. # Only consider non-zero size sections
  48. if [[ $addrElement != "00000000" ]]; then
  49. printf " | %-20s | 0x%-10s | %-8s | \n" $sectionElement $addrElement $((16#$sizeElement))
  50. # Use the section headers for SRAM tally
  51. if [[ "0x$addrElement" -ge "$STARTSRAM" ]] && [[ "0x$addrElement" -lt "$ENDSRAM" ]]; then
  52. internalSram=$((internalSram+$((16#$sizeElement))))
  53. fi
  54. fi
  55. # Program headers
  56. elif [[ ${lineArray[1]} == "0x"* ]] && [[ ${lineArray[2]} == "0x"* ]] && [[ ${lineArray[3]} == "0x"* ]] && [[ ${lineArray[4]} == "0x"* ]]\
  57. && [[ ${lineArray[3]} -ge "$STARTFLASH" ]] && [[ ${lineArray[3]} -lt "$ENDFLASH" ]]; then
  58. # Use the program headers for Flash tally
  59. internalFlash=$((internalFlash+${lineArray[4]}))
  60. fi
  61. fi
  62. done < "$READELFFILE"
  63. printf " -------------------------------------------------- \n\n"
  64. printf " %-41s %-8s \n" 'Total Internal Flash (Available)' $AVAILABLEFLASH
  65. printf " %-41s %-8s \n\n" 'Total Internal Flash (Utilized)' $internalFlash
  66. printf " %-41s %-8s \n" 'Total Internal SRAM (Available)' $AVAILABLESRAM
  67. printf " %-41s %-8s \n" 'Total Internal SRAM (Utilized)' $internalSram
  68. }
  69. memcalc