Go to the documentation of this file.00001 #include <fstream>
00002 #include <iostream>
00003 #include <stdint.h>
00004 #include "TimeUtil.h"
00005
00006 tick_t get_tick()
00007 {
00008 #ifdef _WIN32
00009 LARGE_INTEGER t;
00010 QueryPerformanceCounter(&t);
00011 return t.QuadPart;
00012 #elif defined(__x86_64__) || defined(__amd64__)
00013 unsigned int l=0,h=0;
00014 __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
00015 return (unsigned long long)h<<32|l;
00016 #elif defined(__i386__)
00017 unsigned int ret;
00018 __asm__ volatile ("rdtsc" : "=A" (ret) );
00019 return ret;
00020 #elif defined(__ARM_ARCH_7A__)
00021 uint32_t r = 0;
00022 asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
00023 return r;
00024 #elif defined(__AARCH64EL__)
00025 uint64_t b;
00026 asm volatile( "mrs %0, pmccntr_el0" : "=r"(b) :: "memory" );
00027 return b;
00028 #else
00029 return 0;
00030 #endif
00031 }
00032
00033 double get_cpu_frequency()
00034 {
00035 static double freq = -1;
00036 if (freq != -1) return freq;
00037 #if defined(_WIN32)
00038 LARGE_INTEGER li;
00039 QueryPerformanceFrequency(&li);
00040 freq = (double)li.QuadPart;
00041 #elif defined(__QNX__)
00042 freq = SYSPAGE_ENTRY( qtime )->cycles_per_sec;
00043 #elif defined(__APPLE__)
00044
00045 freq = 2.8e9;
00046 #else
00047 std::ifstream ifs("/proc/cpuinfo");
00048 if (!ifs.is_open()){
00049 std::cerr << "failed to open /proc/cpuinfo" << std::endl;
00050 return 0;
00051 }
00052 std::string token;
00053 while(!ifs.eof()){
00054 ifs >> token;
00055 if (token == "cpu"){
00056 ifs >> token;
00057 if (token == "MHz"){
00058 ifs >> token;
00059 ifs >> freq;
00060 freq *= 1e6;
00061 break;
00062 }
00063 }
00064 }
00065 ifs.close();
00066 #endif
00067 return freq;
00068 }
00069