/****************************************************** /* /* consume ratio freq /* to consume cpu time at ratio and frequency. /* The original code was from Peter Zijlstra, /* the kernal.org maintainer of the sched /* area of the kernel code. /* /* consume.c Smythies 2014.05.05 /* Fix usage message. /* /* consume.c Smythies 2013.11.04 /* Add a time to execute parameter. /* allow the fraction to be as low as 0.5 percent /* /* consume.c Smythies 2012.06.27 /* More precision desired. Switch to doubles for /* run time parameters. /* /* consume.c Smythies 2012.06.23 /* I think in frequency for this stuff, so change /* the run time parameter from period to frequency. /* Make it not optional. /* /* consume.c Smythies 2012.06.23 /* First just try to compile the code as sent /* from Peter Zijlstra. /* But converted to my coding standards (which will /* annoy Peter if it ever gets sent back). /* /******************************************************/ #include #include #include #include unsigned long long stamp(void){ struct timeval tv; gettimeofday(&tv, NULL); return (unsigned long long)tv.tv_sec * 1000000 + tv.tv_usec; } /* endprocedure */ void consume(int spin, int total, int elapsed){ unsigned long long begin, now, start, lelapsed; begin = stamp(); start = begin; now = begin; lelapsed = (long long)(elapsed * 1000000); while((long long)(now - start) < lelapsed) { /* for the requested time */ now = stamp(); if ((long long)(now - begin) > spin) { usleep(total - spin); begin += total; } /* endif */ } /* endwhile */ } /* endprocedure */ int main(int argc, char **argv){ double frac, freq, duration, period; printf("consume: %s %s %s\n", argv[1], argv[2], argv[3]); switch(argc){ case 4: frac = atof(argv[1]); freq = atof(argv[2]); duration = atof(argv[3]); break; default: fprintf(stderr, "%s \n frac -- [0.5-100.0] %% of time to burn\n freq -- [hz] (min 1.0) frequency of burn/sleep cycle\n duration -- [seconds] (0.0 means 1 day) time for program to run\n", argv[0]); return -1; } /* endcase */ /* printf("consume: %lf %lf %lf\n", frac, freq, duration); /* */ if (frac > 100.0){ frac = 100.0; } /* endif */ if (frac < 0.50){ frac = 0.50; } /* endif */ if (freq < 1.0){ freq = 1.0; } /* endif */ if (duration <= 0.0){ duration = 86400.0; } /* endif */ period = 1000000.0 / freq; /* printf("consume: %lf %lf %lf %lf\n", frac, freq, period, duration); /* */ consume((int)(period * frac / 100.0 + 0.5), (int)(period + 0.5), (int)(duration + 0.5)); return 0; } /* endprocedure */