Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use lscpu | grep 'MHz' to get my clock speed. I am getting different results based on the method I am calling this command.

Method 1: typing it in the terminal
CPU MHz: 1200.000

Method 2: Encapsulating it in a script called test.sh
Script code:

#! /bin/bash
cpu=$(lscpu | grep 'MHz')
echo $cpu

Then, in the terminaL

$ chmod +x test.sh
$ ./test.sh

Output:
CPU MHz: 1200.000

So far so good. But now, method 3: hiting the 'run or view current file' in Geany on test.sh. Output:
CPU MHz: 2200.000

Method 4: writing a python script called test.py that calls test.sh.
Python script code:

#! /usr/bin/python
import subprocess

def test():
    subprocess.call("./test.sh")

if __name__ == '__main__':
    test()

Then, in the terminal:
$ python test.py

Output:
CPU MHz: 2200.000

I've tried both python and python3. Both give the same (erroneous) result.

Why does lscpu give a different output when it is run indirectly? And how can I fix this/circumvent this? Is something wrong in my code? For what it's worth, I am running Ubuntu 13.10.

Thanks :-).

share|improve this question
    
Gx1sptDTDa, good question, thanks. But you should really get a simple name, lol I wonder I spell it correctly here in my comment. –  beginer Jul 27 at 9:50

2 Answers 2

up vote 2 down vote accepted

That's a coincidence, it's your system changing the current cpu frequency.

Check with this command in a terminal:

watch -n 0 "lscpu | grep 'MHz'"

You will see (when waiting a bit) that the system's cpu frequency is switching.

You can also get your current cpu frequency with this command:

cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_cur_freq

And your maximum/minimum cpu freq with those:

cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq

Change the behavior? The cpu governor decides what frequency should be used. See this documentation of the linux kernel to decide with cpu governor fits best for you.

  • Get the governor: cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
  • Change the governor: echo "powersave" >/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor
share|improve this answer
    
Aha! I guess calling an extra interpreter (in the case of python) requires more work, thus the governor ups the frequency? Makes some sense. –  Gx1sptDTDa Apr 8 at 14:03

Most modern CPUs can adjust their clock frequency. The default governor on linux is "on demand" which keeps the clock speed low to save power when idle, and speeds it up to do work when needed.

See https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.