#include #include #include #include long unsigned countInner; char *sfmtTime(char *buf, struct timeval tv) { char *tstr; tstr = ctime(&tv.tv_sec); if (tstr != NULL) { int tslen; tslen = strlen(tstr); if (tstr[tslen-1] == '\n') tstr[tslen-1] = 0; sprintf(buf, "%s + %06ldus", tstr, (long) tv.tv_usec); } else sprintf(buf, "%ld + %06ldus (ctime failed)", (long) tv.tv_sec, (long) tv.tv_usec); return buf; } long dtv(struct timeval tva, struct timeval tvb) { long ds = tva.tv_sec - tvb.tv_sec ; long dus = tva.tv_usec - tvb.tv_usec; return (dus + (1000000*ds)); } void inner() { int i; for (i = 0; i < 10000000; i++) countInner ++; return; } int main(int argc, char **argv) { long unsigned countOuter, lastCount = 0; struct timeval tva, tvb; int res, ReportPeriod_S, ReportPeriod_US; long since; char buft[100]; if (argc != 2) { fprintf(stderr, "Usage: %s report-period\n", argv[0]); return 1; } ReportPeriod_S = atoi(argv[1]); ReportPeriod_US = ReportPeriod_S * 1000 * 1000; fprintf(stdout, "Report Period = %g seconds\n", ReportPeriod_US/(double)1.0e6); res = gettimeofday(&tva, NULL); if (res != 0) exit(22); for (countOuter = 0; ; countOuter++) { inner(); res = gettimeofday(&tvb, NULL); if (res != 0) exit(22); since = dtv(tvb, tva); if (since > ReportPeriod_US) { long unsigned dCount = countOuter - lastCount; float rate = dCount * 1.0e6 / since; printf("At %s, count=%lu,\n" "d-count=%lu, d-t=%ld us, rate=%4.2f/s\n", sfmtTime(buft, tvb), countOuter, dCount, since, rate); lastCount = countOuter; tva = tvb; } } }