Limiting CPU usage under Linux: SOLUTION

Questions and Answers : Unix/Linux : Limiting CPU usage under Linux: SOLUTION
Message board moderation

To post messages, you must log in.

AuthorMessage
jombo

Send message
Joined: 19 Jun 05
Posts: 9
Credit: 507
RAC: 0
Italy
Message 125753 - Posted: 20 Jun 2005, 21:45:16 UTC
Last modified: 20 Jun 2005, 22:36:31 UTC

i know that this isn't the best place for news and announcements... but i can't post in forum yet because i have too few credits.
because a lot of people would use boinc only if they can be able to adjust max cpu usage (for example cause they don't want to burn their laptop :) i wrote a simple program that attempts to do this job under Linux (under Windows there is ThreadMaster service, that works fine).
the code is very small, uses only SIGSTOP and SIGCONT signals, and can be improved, then i post it here.
to compile: gcc -o cpulimit cpulimit.c
to run: cpulimit {seti pid} {cpu limit}

//cpulimit.c
/**
* Simple program to limit the cpu usage of a process
*
* Author: Angelo Marletta (marlonx80@hotmail.com)
*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <sys/resource.h>

//pid of the controlled process
int pid;

//SIGINT signal handler
void quit(int sig) {
&nbsp;&nbsp;&nbsp;&nbsp;//let the process continue if it's stopped
&nbsp;&nbsp;&nbsp;&nbsp;kill(pid,SIGCONT);
&nbsp;&nbsp;&nbsp;&nbsp;exit(0);
}

int main(int argc, char **argv) {

&nbsp;&nbsp;&nbsp;&nbsp;if (argc!=3) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,"Usage: %s {pid} {max cpu percentage}\\n",argv[0]);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;pid=atoi(argv[1]);
&nbsp;&nbsp;&nbsp;&nbsp;int limit=atoi(argv[2]);
&nbsp;&nbsp;&nbsp;&nbsp;if (limit100) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,"limit must be in the range 0-100\\n");
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;//if possible renice this process, so it has more responsiveness
&nbsp;&nbsp;&nbsp;&nbsp;if (setpriority(PRIO_PROCESS,getpid(),-20)!=0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;printf("Warning: cannot renice.\\nTo work better you should run this program as root.\\n");
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;signal(SIGINT,quit);

&nbsp;&nbsp;&nbsp;&nbsp;//time quantum in microseconds
&nbsp;&nbsp;&nbsp;&nbsp;int period=100000;
&nbsp;&nbsp;&nbsp;&nbsp;struct timespec twork,tsleep; //time to work, and time to sleep
&nbsp;&nbsp;&nbsp;&nbsp;twork.tv_sec=0;
&nbsp;&nbsp;&nbsp;&nbsp;twork.tv_nsec=period*limit*10;
&nbsp;&nbsp;&nbsp;&nbsp;tsleep.tv_sec=0;
&nbsp;&nbsp;&nbsp;&nbsp;tsleep.tv_nsec=period*(100-limit)*10;

&nbsp;&nbsp;&nbsp;&nbsp;while(1) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (kill(pid,SIGSTOP)!=0) break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nanosleep(&tsleep,NULL);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (kill(pid,SIGCONT)!=0) break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nanosleep(&twork,NULL);
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;perror("kill()");
&nbsp;&nbsp;&nbsp;&nbsp;exit(1);
}


oh....i forgot...ThreadMaster *sometimes* works!
ID: 125753 · Report as offensive

Questions and Answers : Unix/Linux : Limiting CPU usage under Linux: SOLUTION


 
©2024 University of California
 
SETI@home and Astropulse are funded by grants from the National Science Foundation, NASA, and donations from SETI@home volunteers. AstroPulse is funded in part by the NSF through grant AST-0307956.