0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00

Add function for x86 and x64 arch

This commit is contained in:
Sergey Yagovtsev 2016-09-26 23:11:25 +03:00
parent 7b21801b65
commit f9b594918f
2 changed files with 48 additions and 2 deletions

View File

@ -556,7 +556,36 @@ uint32_t ProfileManager::dumpBlocksToStream(profiler::OStream& _outputStream)
#ifdef _WIN32
_outputStream.write(CPU_FREQUENCY);
#else
#if !defined(USE_STD_CHRONO)
double g_TicksPerNanoSec;
struct timespec begints, endts;
uint64_t begin = 0, end = 0;
clock_gettime(CLOCK_MONOTONIC, &begints);
begin = getCurrentTime();
volatile uint64_t i;
for (i = 0; i < 100000000; i++); /* must be CPU intensive */
end = getCurrentTime();
clock_gettime(CLOCK_MONOTONIC, &endts);
struct timespec tmpts;
const int NANO_SECONDS_IN_SEC = 1000000000;
tmpts.tv_sec = endts.tv_sec - begints.tv_sec;
tmpts.tv_nsec = endts.tv_nsec - begints.tv_nsec;
if (tmpts.tv_nsec < 0) {
tmpts.tv_sec--;
tmpts.tv_nsec += NANO_SECONDS_IN_SEC;
}
uint64_t nsecElapsed = tmpts.tv_sec * 1000000000LL + tmpts.tv_nsec;
g_TicksPerNanoSec = (double)(end - begin)/(double)nsecElapsed;
int64_t cpu_frequency = int(g_TicksPerNanoSec*1000000);
_outputStream.write(cpu_frequency*1000LL);
#else
_outputStream.write(0LL);
#endif
#endif
// Write begin and end time

View File

@ -40,6 +40,7 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include <unistd.h>
#include <sys/syscall.h>
#include <chrono>
#include <time.h>
#endif
inline uint32_t getCurrentThreadId()
@ -53,7 +54,7 @@ inline uint32_t getCurrentThreadId()
#endif
}
inline profiler::timestamp_t getCurrentTime()
static inline profiler::timestamp_t getCurrentTime()
{
#ifdef _WIN32
//see https://msdn.microsoft.com/library/windows/desktop/dn553408(v=vs.85).aspx
@ -62,8 +63,24 @@ inline profiler::timestamp_t getCurrentTime()
return 0;
return (profiler::timestamp_t)elapsedMicroseconds.QuadPart;
#else
//std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> time_point;
#if (defined(__GNUC__) || defined(__ICC))
#if defined(__i386__)
unsigned long long t;
__asm__ __volatile__("rdtsc" : "=A"(t));
return t;
#elif defined(__x86_64__)
unsigned int hi, lo;
__asm__ __volatile__("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
#endif
#else
return std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
#define USE_STD_CHRONO
#endif
#endif
}