@@ -6,7 +6,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
-#include <sys/timeb.h>
+#include <time.h>
#include <sched.h>
#include <errno.h>
#include <string.h>
@@ -22,10 +22,12 @@ int main(int argc, char **argv) {
long long tsc, old_tsc, new_tsc;
long long aperf, old_aperf, new_aperf;
long long mperf, old_mperf, new_mperf;
- struct timeb before, after;
+ struct timespec before, after;
+ clockid_t clkid;
long long int start, finish, total;
cpu_set_t cpuset;
+
if (argc != 2) {
usage(argv[0]);
return 1;
@@ -41,6 +43,10 @@ int main(int argc, char **argv) {
sprintf(msr_file_name, "/dev/cpu/%d/msr", cpu);
fd = open(msr_file_name, O_RDONLY);
+ #define CLOCKFD 3
+ #define FD_TO_CLOCKID(fd) ((~(clockid_t) (fd) << 3) | CLOCKFD)
+ clkid = FD_TO_CLOCKID(fd);
+
if (fd == -1) {
printf("/dev/cpu/%d/msr: %s\n", cpu, strerror(errno));
@@ -55,7 +61,7 @@ int main(int argc, char **argv) {
return 1;
}
- ftime(&before);
+ clock_gettime(clkid,&before);
pread(fd, &old_tsc, sizeof(old_tsc), 0x10);
pread(fd, &old_aperf, sizeof(old_mperf), 0xe7);
pread(fd, &old_mperf, sizeof(old_aperf), 0xe8);
@@ -64,7 +70,7 @@ int main(int argc, char **argv) {
sqrt(i);
}
- ftime(&after);
+ clock_gettime(clkid,&after);
pread(fd, &new_tsc, sizeof(new_tsc), 0x10);
pread(fd, &new_aperf, sizeof(new_mperf), 0xe7);
pread(fd, &new_mperf, sizeof(new_aperf), 0xe8);
@@ -73,11 +79,10 @@ int main(int argc, char **argv) {
aperf = new_aperf-old_aperf;
mperf = new_mperf-old_mperf;
- start = before.time*1000 + before.millitm;
- finish = after.time*1000 + after.millitm;
- total = finish - start;
-
- printf("runTime: %4.2f\n", 1.0*total/1000);
- printf("freq: %7.0f\n", tsc / (1.0*aperf / (1.0 * mperf)) / total);
+ start = before.tv_sec*1000000 + before.tv_nsec;
+ finish = after.tv_sec*1000000 + after.tv_nsec;
+ total = finish - start;
+ printf("runTime: %4.2f\n", 1.0*total/1000000);
+ printf("freq: %7.0f\n", tsc / (1.0 * aperf /1.0 * (mperf)) / total);
return 0;
}