/****************************************************** /* /* consume ratio freq /* to cosnume 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 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){ unsigned long long begin, now; begin = stamp(); for (;;) { /* forever */ now = stamp(); if ((long long)(now - begin) > spin) { usleep(total - spin); begin += total; } /* endif */ } /* endfor */ } /* endprocedure */ int main(int argc, char **argv){ double frac, freq, period; printf("consume: %s %s\n", argv[1], argv[2]); switch(argc){ case 3: frac = atof(argv[1]); freq = atof(argv[2]); break; default: fprintf(stderr, "%s \n frac -- [1.0-100.0] %% of time to burn\n freq -- [hz] (min 1.0) frequency of burn/sleep cycle\n", argv[0]); return -1; } /* endcase */ printf("consume: %lf %lf\n", frac, freq); if (frac > 100.0){ frac = 100.0; } /* endif */ if (frac < 1.0){ frac = 1.0; } /* endif */ if (freq < 1.0){ freq = 1.0; } /* endif */ period = 1000000.0 / freq; printf("consume: %lf %lf %lf\n", frac, freq, period); consume((int)(period * frac / 100.0 + 0.5), (int)(period + 0.5)); return 0; } /* endprocedure */