On Wed, May 09, 2007 at 09:24:17PM +0200, Dmitry Adamushko wrote:
> One quick observation.
> 
> Isn't it important for both processes to have the same "loops_per_ms" value?

Good catch.

I have modified the testcase based on this observation (using
setitimer).

-- 
Regards,
vatsa
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/resource.h>

volatile int time_elapsed;
int run_length  = 52;   // in milliseconds
int sleep_length = 24;  // in milliseconds
int epoch_time = 5;     // in seconds

void alrm_handler(int signo)
{
        time_elapsed = 1;
}

main(int argc, char *argv[])
{
        double prevusage = 0;
        struct itimerval timer;
        time_t prevtime;

        if (argc > 1) {
                run_length = atoi(argv[1]);
                if (argc > 2)
                        sleep_length = atoi(argv[2]);
                if (argc > 3)
                        epoch_time = atoi(argv[3]);
        }

        signal(SIGVTALRM, alrm_handler);
        memset(&timer, 0, sizeof(timer));

        timer.it_value.tv_sec = run_length / 1000;
        timer.it_value.tv_usec = (run_length % 1000) * 1000;

        printf ("run time = %d ms (%d sec + %d usec), sleep time = %d ms,"
                " epoch time = %d s\n", run_length, timer.it_value.tv_sec,
                timer.it_value.tv_usec, sleep_length, epoch_time);

        prevtime = time(NULL);
        while (1) {
                time_t curtime, deltatime;
                struct rusage stats;
                int rc;

                rc = setitimer(ITIMER_VIRTUAL, &timer, NULL);
                if (rc < 0) {
                        perror("setitimer");
                        exit(1);
                }

                time_elapsed = 0;
                while (!time_elapsed)
                ;

                usleep(sleep_length * 1000);

                curtime = time(NULL);
                deltatime = curtime - prevtime;
                if (deltatime >=  epoch_time) {
                        double curusage, deltausage;

                        getrusage(0, &stats);
                        curusage = stats.ru_utime.tv_sec +
                                   stats.ru_utime.tv_usec * 1e-6 +
                                   stats.ru_stime.tv_sec + 
                                   stats.ru_stime.tv_usec * 1e-6;

                        deltausage = curusage - prevusage;
                        printf ("Obtained %3.2f seconds of execution time in"
                              " %d elapsed seconds \n", deltausage, deltatime);
                        prevtime = curtime;
                        prevusage = curusage;
                }
        }
}

Reply via email to