cpu_freq 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/sh -e
  2. #
  3. # cpu_freq: calculate current cpu frequency
  4. #
  5. # Copyright (C) 2008 Canonical Ltd.
  6. # Copyright (C) 2011-2014 Dustin Kirkland
  7. #
  8. # Authors: Dustin Kirkland <kirkland@byobu.org>
  9. #
  10. # This program is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, version 3 of the License.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. __cpu_freq_detail() {
  22. cat /proc/cpuinfo
  23. }
  24. __cpu_freq() {
  25. local hz freq count
  26. if [ -r "/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq" ]; then
  27. read hz < /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
  28. fpdiv $hz "1000000" 1 # 1Ghz
  29. freq="$_RET"
  30. elif [ -r "/proc/cpuinfo" ]; then
  31. if egrep -q -s -i -m 1 "^cpu MHz|^clock" /proc/cpuinfo; then
  32. freq=$(egrep -i -m 1 "^cpu MHz|^clock" /proc/cpuinfo | awk -F"[:.]" '{ printf "%01.1f", $2 / 1000 }')
  33. else
  34. # Must scale frequency by number of processors, if counting bogomips
  35. count=$(getconf _NPROCESSORS_ONLN 2>/dev/null || grep -ci "^processor" /proc/cpuinfo)
  36. freq=$(egrep -i -m 1 "^bogomips" /proc/cpuinfo | awk -F"[:.]" '{ print $2 }')
  37. freq=$(printf "%s %s" "$freq" "$count" | awk '{printf "%01.1f\n", $1/$2/1000}')
  38. fi
  39. elif hz=$(sysctl -n hw.cpufrequency 2>/dev/null); then
  40. fpdiv $hz "1000000000" 1 # 1Ghz
  41. freq="$_RET"
  42. fi
  43. [ -n "$freq" ] || return
  44. color b c W; printf "%s" "$freq"; color -; color c W; printf "%s" "$ICON_GHz"; color --
  45. }
  46. # vi: syntax=sh ts=4 noexpandtab