Showing posts with label drawnow. Show all posts
Showing posts with label drawnow. Show all posts

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)



Sunday, March 29, 2015

Python display Raspberry Pi load average graphically

The file /proc/loadavg indicate the load average figures giving the number of jobs in the run queue (state R) or waiting for disk I/O (state D) averaged over 1, 5, and 15 minutes. This Python 2 example read the first figure (over 1 minutes) every second, and plot to figure using Matplotlib.


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

loadavg = []

plt.ion()
cnt=0

def plotLoadAvg():
    plt.ylim(0,4)
    plt.title('Raspberry Pi load average')
    plt.grid(True)
    plt.ylabel('usage')
    plt.plot(loadavg, 'bo-', label='usage')
    plt.legend(loc='upper right')

#pre-load dummy data
for i in range(0,100):
    loadavg.append(0)
    
while True:

    usage = os.popen("awk '{print $1}' /proc/loadavg").readline()
    print(usage)
    loadavg.append(usage)
    loadavg.pop(0)
    drawnow(plotLoadAvg)
    plt.pause(1)



For installation of the libraries, read "Install numpy, matplotlib and drawnow for Python 2".

Install numpy, matplotlib and drawnow for Python 2

This post show steps to install numpy, matplotlib and drawnow for Python 2 on Raspberry Pi, to prepare my exercise "Display Raspberry Pi CPU temperature graphically, using Python 2 with Matplotlib". The steps are same as in other Linux such as Ubuntu.

To check if your system have numpy, matplotlib and drawnow installed, enter the command in Python Shell:

>>> import numpy
>>> import matplotlib
>>> import drawnow

If you get error message like this, you have to install it.
ImportError: No module named numpy
ImportError: No module named matplotlob
ImportError: No module named drawnow

Install numpy, matplotlib and drawnow for Python 2:
$ sudo apt-get install python-numpy
$ sudo apt-get install python-matplotlib
$ sudo apt-get install python-pip
$ sudo pip install drawnow




For Python 3:

To install numpy and drawnow for Python 3 on Raspberry Pi, enter the command:
Install for Python 3:
$ sudo apt-get install python3-numpy
$ sudo apt-get install python3-pip
$ sudo pip-3.2 install drawnow

For matplotlib for Python 3, it seem not supported on Raspbian currently. I tried to build from source, refer to the post "Fail to build matplotlib for Python3, run on Raspberry Pi 2".