From 5d75d587849d586b34d76e1ee584e0db707888f2 Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Sat, 30 Mar 2019 00:27:32 +0000 Subject: [PATCH] Ref T570 Measure sortHint using true CPU time routines. --- src/blackmisc/cputime.cpp | 76 +++++++++++++++++++ src/blackmisc/cputime.h | 30 ++++++++ .../testaircraftsituation.cpp | 13 ++-- 3 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 src/blackmisc/cputime.cpp create mode 100644 src/blackmisc/cputime.h diff --git a/src/blackmisc/cputime.cpp b/src/blackmisc/cputime.cpp new file mode 100644 index 000000000..ce321b6fe --- /dev/null +++ b/src/blackmisc/cputime.cpp @@ -0,0 +1,76 @@ +/* Copyright (C) 2019 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +#include "blackmisc/cputime.h" +#include + +#if defined(Q_OS_WIN32) +#include +#elif defined(Q_OS_UNIX) +#include +#endif + +namespace BlackMisc +{ + +#if defined(Q_OS_WIN32) + + static int getCpuTimeMs(const FILETIME &kernelTime, const FILETIME &userTime) + { + const ULARGE_INTEGER kernel { { kernelTime.dwLowDateTime, kernelTime.dwHighDateTime } }; + const ULARGE_INTEGER user { { userTime.dwLowDateTime, userTime.dwHighDateTime } }; + const quint64 usecs = (kernel.QuadPart + user.QuadPart) / 10ull; + return static_cast(usecs / 1000ull); + } + int getProcessCpuTimeMs() + { + FILETIME creationTime, exitTime, kernelTime, userTime; + GetProcessTimes(GetCurrentProcess(), &creationTime, &exitTime, &kernelTime, &userTime); + return getCpuTimeMs(kernelTime, userTime); + } + int getThreadCpuTimeMs() + { + FILETIME creationTime, exitTime, kernelTime, userTime; + GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime, &userTime); + return getCpuTimeMs(kernelTime, userTime); + } + +#elif defined(Q_OS_UNIX) + + static int getCpuTimeMs(const timespec &ts) + { + const double secs = static_cast(ts.tv_sec) + static_cast(ts.tv_nsec) / 1e9; + return static_cast(secs * 1000); + } + int getProcessCpuTimeMs() + { + timespec ts {}; + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts); + return getCpuTimeMs(ts); + } + int getThreadCpuTimeMs() + { + timespec ts {}; + clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts); + return getCpuTimeMs(ts); + } + +#else // Q_OS_UNIX + + int getProcessCpuTimeMs() + { + return 0; // not implemented + } + int getThreadCpuTimeMs() + { + return 0; // not implemented + } + +#endif + +} diff --git a/src/blackmisc/cputime.h b/src/blackmisc/cputime.h new file mode 100644 index 000000000..a30215492 --- /dev/null +++ b/src/blackmisc/cputime.h @@ -0,0 +1,30 @@ +/* Copyright (C) 2019 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated, + * or distributed except according to the terms contained in the LICENSE file. + */ + +//! \file + +#ifndef BLACKMISC_CPUTIME_H +#define BLACKMISC_CPUTIME_H + +#include "blackmisc/blackmiscexport.h" + +namespace BlackMisc +{ + /*! + * Get the time in milliseconds that the CPU has spent executing the current process. + * This is the sum of getThreadCpuTimeMs() for all threads. + */ + BLACKMISC_EXPORT int getProcessCpuTimeMs(); + + /*! + * Get the time in milliseconds that the CPU has spent executing the current thread. + */ + BLACKMISC_EXPORT int getThreadCpuTimeMs(); +} + +#endif diff --git a/tests/blackmisc/aviation/testaircraftsituation/testaircraftsituation.cpp b/tests/blackmisc/aviation/testaircraftsituation/testaircraftsituation.cpp index a9eefb3c8..cb21e1366 100644 --- a/tests/blackmisc/aviation/testaircraftsituation/testaircraftsituation.cpp +++ b/tests/blackmisc/aviation/testaircraftsituation/testaircraftsituation.cpp @@ -14,6 +14,7 @@ #include "blackmisc/aviation/aircraftsituationchange.h" #include "blackmisc/aviation/aircraftsituationlist.h" #include "blackmisc/network/fsdsetup.h" +#include "blackmisc/cputime.h" // #include "blackmisc/math/mathutils.h" // #include "blackmisc/stringutils.h" #include "test.h" @@ -244,6 +245,7 @@ namespace BlackMiscTest } QTime time; + int cpuTime = getThreadCpuTimeMs(); time.start(); for (int i = 0; i < Loops; ++i) { @@ -254,8 +256,9 @@ namespace BlackMiscTest QVERIFY(s1.getAdjustedMSecsSinceEpoch() < s2.getAdjustedMSecsSinceEpoch()); } } - const int noHint = time.elapsed(); + const auto noHint = std::make_pair(time.elapsed(), getThreadCpuTimeMs() - cpuTime); + cpuTime = getThreadCpuTimeMs(); time.start(); for (int i = 0; i < Loops; ++i) { @@ -266,12 +269,12 @@ namespace BlackMiscTest QVERIFY(s1.getAdjustedMSecsSinceEpoch() < s2.getAdjustedMSecsSinceEpoch()); } } - const int hint = time.elapsed(); - const double ratio = static_cast(hint) / static_cast(noHint); // expected <1.0 + const auto hint = std::make_pair(time.elapsed(), getThreadCpuTimeMs() - cpuTime); + const double ratio = static_cast(hint.second) / static_cast(noHint.second); // expected <1.0 //qDebug() << "MacOS:" << boolToYesNo(CBuildConfig::isRunningOnMacOSPlatform()); - qDebug() << "Access without hint" << noHint << "ms"; - qDebug() << "Access with hint" << hint << "ms"; + qDebug() << "Access without hint" << noHint.first << "ms (CPU time" << noHint.second << "ms)"; + qDebug() << "Access with hint" << hint.first << "ms (CPU time" << hint.second << "ms)"; qDebug() << "Access ratio" << ratio; #if 0