Monday, March 30, 2015

Python display CPUs frequency graphically, run on Raspberry Pi 2/Linux


In Linux, the file "/sys/devices/system/cpu/*/cpufreq/scaling_cur_freq" show the available frequency your CPU(s) are scaled to currently, in KHz.

In Raspberry Pi 2/Raspbian, where is:
/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq
/sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq

This Python 2 example run on Raspberry Pi 2, to display CPU frequency graphically. (It seem that all CPU run on the same frequency)


plotCpuFreq.py
import os
import matplotlib.pyplot as plt
from drawnow import *

# Run on Raspberry Pi 2
# 4 CPUs
cpu0Freq = []
cpu1Freq = []
cpu2Freq = []
cpu3Freq = []

plt.ion()
cnt=0

def plotCpuFreq():
    plt.ylim(0,1500000)
    plt.title('Raspberry Pi 2 CPUs Frequency')
    plt.grid(True)
    plt.ylabel('KHz')
    plt.plot(cpu0Freq, 'r^-', label='cpu0')
    plt.plot(cpu1Freq, 'c>-', label='cpu1')
    plt.plot(cpu2Freq, 'bv-', label='cpu2')
    plt.plot(cpu3Freq, 'm<-', label='cpu3')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,100):
    cpu0Freq.append(0)
    cpu1Freq.append(0)
    cpu2Freq.append(0)
    cpu3Freq.append(0)
    
while True:
    cpu0 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq").readline()
    cpu1 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq").readline()
    cpu2 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu2/cpufreq/scaling_cur_freq").readline()
    cpu3 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu3/cpufreq/scaling_cur_freq").readline()

    print(cpu0 + " " + cpu1 + " " + cpu2 + " " + cpu3)
    cpu0Freq.append(cpu0)
    cpu0Freq.pop(0)
    cpu1Freq.append(cpu1)
    cpu1Freq.pop(0)
    cpu2Freq.append(cpu2)
    cpu2Freq.pop(0)
    cpu3Freq.append(cpu3)
    cpu3Freq.pop(0)
    
    drawnow(plotCpuFreq)
    plt.pause(1)



To install needed libraries on Raspberry Pi, refer to "Install numpy, matplotlib and drawnow for Python 2".


The almost same code run on Linux with dual core.


plot2CpuFreq.py
import os
import matplotlib.pyplot as plt
from drawnow import *

# Run on Dual Core Atom
# with 2 CPU
cpu0Freq = []
cpu1Freq = []

plt.ion()
cnt=0

def plotCpuFreq():
    plt.ylim(0,2000000)
    plt.title('Dual Core CPUs Frequency')
    plt.grid(True)
    plt.ylabel('KHz')
    plt.plot(cpu0Freq, 'r^-', label='cpu0')
    plt.plot(cpu1Freq, 'c>-', label='cpu1')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,100):
    cpu0Freq.append(0)
    cpu1Freq.append(0)
    
while True:
    cpu0 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq").readline()
    cpu1 = os.popen("awk '{print $1}' /sys/devices/system/cpu/cpu1/cpufreq/scaling_cur_freq").readline()

    print(cpu0 + " " + cpu1)
    cpu0Freq.append(cpu0)
    cpu0Freq.pop(0)
    cpu1Freq.append(cpu1)
    cpu1Freq.pop(0)
    
    drawnow(plotCpuFreq)
    plt.pause(1)



No comments: