Cpu Load

This is a discussion on Cpu Load within the C Programming forums, part of the General Programming Boards category; want to control the cpu load to test the power consume at a specified load. Exemple: -Fix the load at ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    6

    Cpu Load

    want to control the cpu load to test the power consume at a specified load.

    Exemple:
    -Fix the load at 65% and check the power consume of the computer using a Voltage and Current meter


    I was thinking in something like this:
    sleep(x);
    /*X time (X float and inside [0;1]) */
    while(1);
    /* during 1-X time */

    This doesn't work because the time unit is to large (sleep only accept seconds), i want something like miliseconds ou even microseconds.

    Any idea?

    Thanks

  2. #2
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89
    When using Dev-C++ with gcc on win2k, sleep() wil sleep for a number of milliseconds? But I guess that's a windows thing or something... Anyway, this is what I think you could use:

    usleep()
    Last edited by Snip; 03-28-2005 at 01:58 PM.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    usleep() works fine but i have another problem:

    Code:
    int x=6; /* It will load the cpu with an average of 60% */
    
    while(1){
    
     usleep(x);
     fullload(10-x); /* This doen't exist, i have to create it */
    
    }

    I need a function that do the inverse of sleep, something that full load's the cpu for a defined time.

    Any idea about a funtion that does this?

    I was thinking in something like this:

    Code:
    void fullload(int Delay){
     int timeStamp;
    
     timeStamp=currTimeInUSecond(); /* dont know if this exist, need something like this */
    
     /* Pooling until the Delay have pass */
     while(currTimeInUSecond()<timeStamp+Delay);
    
     return;
    }

    Where can i get a similar funcion currTimeInUSecond(); ?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Look into gettimeofday()
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Code:
    #include <stdio.h>
    #include <sys/time.h>
    
    #define LOAD 6
    
    void fullload(int Delay){
    
     int timeStamp;
     struct timeval tv;
     struct timezone tz;
    
     gettimeofday(&tv,&tz);
    
     timeStamp=tv.tv_usec;
    
     while(1){
      gettimeofday(&tv,&tz);
      if(timeStamp+Delay>tv.tv_usec)
       break;
     }
     return;
    }
    
    int main(){
     while(1){
      usleep(LOAD);
      fullload(10-LOAD); 
     }
    }

    Doesn't work... dont know why...
    The load's goes to 150% consulting /proc/loadavg

    Any idea?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Er...you're sleeping for 6 microseconds? There's 1 million microseconds in a second. I don't think the kernel's timer is even accurate enough to account for 6 microseconds. You'd be better off using something much larger like sleep for 1000 microseconds, fullload for 400 microseconds.

    EDIT: Also, if you don't care about the timezone parameter to gettimeofday() then just pass NULL.
    Last edited by itsme86; 03-28-2005 at 01:54 PM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Isn't it Milliseconds and not Microsseconds usleep is sleeping for?
    Anway I'm not sure this aproach will ever work. Its a multitaking OS so when you sleep other processes are using the processor.

  8. #8
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Code:
    #include <stdio.h>
    #include <sys/time.h>
    #include <unistd.h>
    
    /* Valor de 0 a 100 corresponde ao load de 0 a 100 */
    #define LOAD 7000
    
    void fullload(int Delay){
     
     long timeStampSeg;
     long timeStampUSeg;
     
     struct timeval tv;
    
     gettimeofday(&tv,NULL);
     
     timeStampUSeg=tv.tv_usec;
     timeStampSeg=tv.tv_sec;
     
     /* Espera activa ate o tempo ter passado */
     while(timeStampUSeg<(tv.tv_usec+tv.tv_sec*1000000+Delay)){
       gettimeofday(&tv,NULL);
     }
     return;
    }
    
    
    int main(){
    
     while(1){
      usleep(10000-LOAD);
      fullload(LOAD);
     }
    }
    Using this like u sugested it doen't load anything, "top" program indicate 0% cpu consume.

    If i comment usleep(); the load get's to 100%

  9. #9
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    Code:
    ]Isn't it Milliseconds and not Microsseconds usleep is sleeping for?
    Anway I'm not sure this aproach will ever work. Its a multitaking OS so when you sleep other processes are using the processor.
    struct timeval {
    time_t tv_sec; /* seconds */
    suseconds_t tv_usec; /* microseconds */
    };


    Its micro for sure.

    Thanks

  10. #10
    Registered User
    Join Date
    Mar 2005
    Posts
    6
    I founded a soulution:

    Code:
    #include <stdio.h>
    #include <sys/time.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    #include <signal.h>
    
    #define LOAD 20
    
    int main(){
    
     int pid;
     
     /* Create a Son and a Father process */
     pid=fork();
      
     /* If its the son consume 100% of resources */
     if(pid==0){   
      while(1);
     }
      
     /* Father */
     else{
      while(1){
       
       /* 100% Load */
       usleep(LOAD*10000);
       /* Stops child processes */
       kill(pid,SIGSTOP);
    
       /* 0% Load */   
       usleep(999999-LOAD*10000);
       /* Resume child process */
       kill(pid,SIGCONT);
      }
     }
     return 0;
    }

    Whis load's exact the cpu for the amount of % that i put int the top #define


    Thanks a lot to all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions on multiple thread programming
    By lehe in forum C Programming
    Replies: 11
    Last Post: 03-27-2009, 07:44 AM
  2. Sleep() causing unexplained reduction in cpu load
    By sajanphilip in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2008, 01:58 AM
  3. How can I load a cpu a certain percentage?
    By Smartiepants586 in forum C Programming
    Replies: 4
    Last Post: 06-07-2005, 12:15 PM
  4. retrieving current CPU load
    By kristy in forum C Programming
    Replies: 2
    Last Post: 11-30-2003, 08:30 AM
  5. Accessing CPU load using C in Windows
    By srpathak in forum C Programming
    Replies: 1
    Last Post: 10-31-2003, 03:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21