#!/usr/bin/env python # # Simple script to monitor CPU utilization and frequency on FreeBSD. # Requires the sysctl wrapper module (sysctl.py). # interval = 2 # pause interval in seconds from time import sleep from sysctl import * dev_cpu_0_freq = sysctlnametomib("dev.cpu.0.freq") dev_cpu_0_freqlevels = sysctlnametomib("dev.cpu.0.freq_levels") max_freq = max([ int(pair.split('/')[0]) for pair in sysctl_str(dev_cpu_0_freqlevels).split() ]) kern_cptime = sysctlnametomib("kern.cp_time") cptime_1 = sysctl_ulongarray(kern_cptime, 5) try: while True: cptime_0 = cptime_1 sleep (interval) cptime_1 = sysctl_ulongarray(kern_cptime, 5) cp_diff = [cptime_1[i] - cptime_0[i] for i in range(5)] cp_total = sum(cp_diff) cp_cpu = cp_total - cp_diff[4] # Everything except idle time. freq = sysctl_int(dev_cpu_0_freq) print "cpu: %6.2f%% freq: %d MHz" % ((100.0 * cp_cpu) / cp_total, freq) except KeyboardInterrupt: pass #--