You are not logged in.

#1 2013-01-04 13:47:30

mumo
Member
Registered: 2012-05-06
Posts: 22

A Way to S2RAM by certain CPU-load?!

hi there,

i'm searching for a method to suspend to ram whenever cpu-load falls under a certain value for a predefined period.

Currently i'm using xautolock to s2r on keyboard/mouse inactivity but this does not entirely fit my needs. - e.g. my PC is used as a media-server for an xbmc-client etc. suspending while downloading sth. or burning discs isn't nice either.
Using xautolocks -corner - paramater will prevent it from suspending but i have to admit i usually just forget to use it and it seems more like a workaround.

all i could find was sleepd-git in aur which isn't maintained anymore. does anyone know about a solution?

thank you very much.

p.s. i'm running compiz-standalone

Offline

#2 2013-01-04 14:34:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: A Way to S2RAM by certain CPU-load?!

You could try something like the following (just written, untested):

#include <stdio.h>

/* seconds between jiffy checks */
static const int timeout	= 5;
/* percent cpu use to stay awake */
static const int threshold	= 20;
/* sleep when ntimeouts checks are below threshold */
/*    This will sleep after 20 minutes below 20% cpu use (240 * 5 sec = 20 min) */
static const int ntimeouts	= 240;
static const char *cpufile	= "/proc/stat";
static const char *sleepcommand = "pm-suspend";

int main(int argc, const char **argv) {
	FILE *cpu;
	long l1,l2,l3,l4,j1,j2,j3,j4;
	int n, count;
	if ( !(cpu=fopen(cpufile,"r")) ) return -1;
	fscanf(cpu,"cpu %ld,%ld,%ld,%ld",&j1,&j2,&j3,&j4);
	fclose(cpu);
	for (;;sleep(timeout)) {
		if ( !(cpu=fopen(cpufile,"r")) ) continue;
		fscanf(cpu,"cpu %ld,%ld,%ld,%ld",&l1,&l2,&l3,&l4);
		fclose(cpu);
		if (l4 > j4) n = (int) 100 * (l1-j1+l2-j2+l3-j3) / (l1-j1+l2-j2+l3-j3+l4-j4);
		j1 = l1; j2 = l2; j3 = l3; j4 = l4;
		if (n < threshold) count ++;
		else count = 0;
		if (count > ntimeouts) {
			system(sleepcommand);
			count = 0;
		}
	}
}

This just loops checking the change in cpu "jiffies" every 'timeout' seconds.  For every loop that is below 'threshold' percent cpu use it increments a counter.  Any loop that goes above 'threshold' percent cpu use resets the counter.  If the counter reaches 'ntimeouts' the sleep command is executed.

Note that the settings (const declarations at the top) were arbitrarily chosen.  20 percent is pretty high, you'd likely want to lower that down quite a bit after seeing what your system is doing "at idle".  5% may be a better starting point to adjust from.

EDIT: added jN = lN line.

Last edited by Trilby (2013-01-04 15:59:32)


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#3 2013-01-04 15:09:51

mumo
Member
Registered: 2012-05-06
Posts: 22

Re: A Way to S2RAM by certain CPU-load?!

incredible! smile

thank you very much. i'll test it and respond.

Offline

#4 2013-01-04 16:01:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,554
Website

Re: A Way to S2RAM by certain CPU-load?!

Oops.  In my overly quick extracting of most of this code from one of my other tools, I missed a pretty important part.  I've added it back in an edit above.  It is the line that sets each of the jN values to the lN values.  jN should represent the cpu jiffies of the previous loop while lN are the jiffies just read.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

Board footer

Powered by FluxBB