TimeUtil.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <stdint.h>
4 #include "TimeUtil.h"
5 
7 {
8 #ifdef _WIN32
9  LARGE_INTEGER t;
10  QueryPerformanceCounter(&t);
11  return t.QuadPart;
12 #elif defined(__x86_64__) || defined(__amd64__)
13  unsigned int l=0,h=0;
14  __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
15  return (unsigned long long)h<<32|l;
16 #elif defined(__i386__)
17  unsigned int ret;
18  __asm__ volatile ("rdtsc" : "=A" (ret) );
19  return ret;
20 #elif defined(__ARM_ARCH_7A__)
21  uint32_t r = 0;
22  asm volatile("mrc p15, 0, %0, c9, c13, 0" : "=r"(r) );
23  return r;
24 #elif defined(__AARCH64EL__)
25  uint64_t b;
26  asm volatile( "mrs %0, pmccntr_el0" : "=r"(b) :: "memory" );
27  return b;
28 #else
29  return 0;
30 #endif
31 }
32 
34 {
35  static double freq = -1;
36  if (freq != -1) return freq;
37 #if defined(_WIN32)
38  LARGE_INTEGER li;
39  QueryPerformanceFrequency(&li);
40  freq = (double)li.QuadPart;
41 #elif defined(__QNX__)
42  freq = SYSPAGE_ENTRY( qtime )->cycles_per_sec;
43 #elif defined(__APPLE__)
44  // how to get cpu clock on Mac?
45  freq = 2.8e9;
46 #else
47  std::ifstream ifs("/proc/cpuinfo");
48  if (!ifs.is_open()){
49  std::cerr << "failed to open /proc/cpuinfo" << std::endl;
50  return 0;
51  }
52  std::string token;
53  while(!ifs.eof()){
54  ifs >> token;
55  if (token == "cpu"){
56  ifs >> token;
57  if (token == "MHz"){
58  ifs >> token;
59  ifs >> freq;
60  freq *= 1e6;
61  break;
62  }
63  }
64  }
65  ifs.close();
66 #endif
67  return freq;
68 }
69 
unsigned long long tick_t
Definition: TimeUtil.h:9
RTC::ReturnCode_t ret(RTC::Local::ReturnCode_t r)
ifs
long b
Definition: jpegint.h:371
JHUFF_TBL long freq[]
Definition: jchuff.h:47
t
tick_t get_tick()
Definition: TimeUtil.cpp:6
double get_cpu_frequency()
Definition: TimeUtil.cpp:33


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Sat May 8 2021 02:42:41