24

My desktop stays on all the time. It acts as a server and also performs other semi-essential household tasks. During the day it uses the OnDemand CPU setting. This essentially just scales the CPU frequency as it needs it and this works really well for me.

At night time, it flits around. Of course most of the time it's idling along at a low power but occasionally, it'll ramp up to do something. I would rather it stayed on PowerSave and tasks were just forced to take a little longer to complete.

Additionally, I have a Nvidia card (GTX 580) which can consume about the same amount of power as a small African village uses in a year when it wants to (glares at Minecraft). For unknown reasons (undoubtedly something happening on the desktop), this ramps up, the fans sound like they're charging up to fire lasers into space. At night time, I'd like to underclock the card as far as it'll go.

I've considered a simple root-run cron script to push these through but my working hours are variable. Last week I had a near-70 hour working week so I was up very early and still up very late, every day. If the system was honking along at super-underclock mode, I would have been furious. Is there something I could do to say, "Underclock if:

  • It's between 10pm and 9am
  • There hasn't been any mouse/keyboard activity for 10 minutes
  • There isn't a movie playing

If one of those conditions isn't true, the underclock needs to revert instantly. I don't want to wait for a cron-script to come along to wake the computer up from its slumber. Similarly, as soon as all of those do become true, it should go back to sleep.

What are my options?

Oli
  • 293,335
  • Which version are you on now? Until I can get home to my machine I'll mention you should be able to hook events using acpi events or pm-tools package. – hometoast Jun 06 '11 at 13:13

6 Answers6

20

Here's a program that does what you want:

#!/usr/bin/env python
# coding: utf8

import time
import sys
import commands


USER = commands.getoutput("cat /var/log/auth.log | grep " +
    sys.argv[0]).split("sudo:")[1].split()[0].strip()


def is_idle():
    return int(commands.getoutput("xprintidle")) >= 10 * 60 * 1000

def is_night():
    return time.localtime().tm_hour in (22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

def is_stumm():
    return "RUNNING" not in commands.getoutput(("sudo -u %s pacmd " +
        "list-sinks | grep RUNNING") % USER)


def main():
    powersave = False
    while 1:
        if is_idle() and is_night() and is_stumm():
            if not powersave:
                print "going into powersave mode"
                commands.getoutput("echo powersave > " +
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
                powersave = True
        else:
            if powersave:
                print "going into ondemand mode"
                commands.getoutput("echo ondemand > " +
                    "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor")
                powersave = False
        time.sleep(0.5)

if __name__ == '__main__':
    assert commands.getstatusoutput("xprintidle")[0] == 0, (
        "you need to `sudo apt-get install xprintidle`")
    assert commands.getoutput("whoami") == "root", (
        "you need to be root to run this program")
    main()

Note your preferences:

  • ... >= 10 * 60 * 1000
  • ... tm_hour in (22, 23, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) and
  • "RUNNING" not in ...

I'm using pulseaudio to find out if a movie is playing. This also affect music.

5

You can force your cpu scaling using:

cpufreq-selector -f [frequency]

There are a few more options (i.e. specific cpu, or selecting a governance rather than a speed) This does assume that your motherboard/cpu allows Ubuntu to manually control the scaling (apparently this is not always the case.)

The rest can be applied using a script, however you're being quite demanding of it ^_^. If you need an instant response that I think you have to have something periodically polling your system. From my, some what limited, knowledge there is nothing that announces the conditions you've specified.

A place to start investigating, however, is a screensaver. This turns on after a set period of inactivity (which would also be true at 10pm-9am) and can be inhibited by movie players. If you made a screensaver that scaled down the CPU when initializing, and restored it on wake, then you'd have what you want.

Unfortunately that's as far as I can take you, knowing nothing about screensavers and how they work.

Good luck.

4

You may not actually save more power by using the powersave governor.

Depending on your workload, finishing a work item quickly by expending a little more power and then returning to a deep sleep state rather than working for extended times with reduced power consumption may give better results.

Depending on your application, it may also be worthwhile to install a dedicated server with lower power requirements and a properly dimensioned power supply for that load, and simply turn off the desktop at night.

  • 1
    Sometimes you just want your computer to remain quiet, especially at night.. – Maxime R. Jul 06 '11 at 12:56
  • If you want quiet, not having the system run at all is the best way. I have a pretty loud machine for my daily work and a server in the broom closet for background tasks, the latter has a smaller power supply (=> less waste heat), CPU (=> less waste heat), graphics card (you get the idea), ... and uses the ondemand governor. I have zero noise in my bedroom at night, and power consumption for the server is about a third of what the desktop eats when idle, so I still save money. – Simon Richter Jul 07 '11 at 14:13
  • 1
    Sure, I agree, but in this particular case Oli uses the same machine as server and desktop. – Maxime R. Jul 08 '11 at 19:28
3

powernap is a handy tool for things like this, the nice thing is it looks at your process table to see if your system is busy, not your idle time at the keyboard.

Have you measured the powersaving for each governor? Like Simon I am skeptic that that would add up to a significant amount vs. getting the work done faster or trying to tune something like the monster video card.

powernap can be configured to just execute the governor switch or perhaps if your hardware supports it use the Wake On Lan (WOL) feature to take real naps in the middle of the night until it's needed.

Jorge Castro
  • 71,754
1

As far as the CPU goes, you can just switch the governor to powersave with, for instance, the cpu frequency scaling monitor applet. The video card is another story. I know my ATI card has a knob in /sys to adjust the performance between low, medium, and high, but fiddling with them does not seem to make any difference and often causes the video to become unstable.

psusi
  • 37,551
1

cpufreqd is designed for the specific problem you're having, you can create day and night profiles and set rules based on running programs.

Lie Ryan
  • 652