VirtualBox

Changeset 12548 in vbox


Ignore:
Timestamp:
Sep 17, 2008 5:59:49 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
36735
Message:

iprt/tstTSC: Extended it with an option (-f) for sampling the frequency instead of calcing the TSC drift.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstTSC.cpp

    r11326 r12548  
    3232*   Header Files                                                               *
    3333*******************************************************************************/
    34 #include <iprt/runtime.h>
     34#include <iprt/asm.h>
     35#include <iprt/getopt.h>
     36#include <iprt/initterm.h>
     37#include <iprt/mp.h>
     38#include <iprt/stream.h>
     39#include <iprt/string.h>
     40#include <iprt/thread.h>
     41#include <iprt/time.h>
    3542
    3643
     
    152159}
    153160
    154 
    155 int main()
     161static int tstTSCCalcDrift(void)
    156162{
    157     RTR3Init();
    158 
    159163    /*
    160164     * This is only relevant to on SMP systems.
     
    369373    return g_cFailed != 0 || g_cRead != cCpus;
    370374}
     375
     376
     377static int tstTSCCalcFrequency(uint32_t cMsDuration)
     378{
     379    /*
     380     * Sample the TSC and time, sleep the requested time and calc the deltas.
     381     */
     382    uint64_t uNanoTS = RTTimeSystemNanoTS();
     383    uint64_t uTSC    = ASMReadTSC();
     384    RTThreadSleep(cMsDuration);
     385    uNanoTS = RTTimeSystemNanoTS() - uNanoTS;
     386    uTSC    = ASMReadTSC() - uTSC;
     387
     388    /*
     389     * Calc the frequency.
     390     */
     391    RTPrintf("tstTSC: %RU64 ticks in %RU64 ns\n", uTSC, uNanoTS);
     392    uint64_t cHz = uTSC / ((long double)uNanoTS / (long double)1000000000);
     393    RTPrintf("tstTSC: Frequency %RU64 Hz", cHz);
     394    if (cHz > _1G)
     395    {
     396        cHz += _1G / 20;
     397        RTPrintf("  %RU64.%RU64 GHz", cHz / _1G, (cHz % _1G) / (_1G / 10));
     398    }
     399    else if (cHz > _1M)
     400    {
     401        cHz += _1M / 20;
     402        RTPrintf("  %RU64.%RU64 MHz", cHz / _1M, (cHz % _1M) / (_1M / 10));
     403    }
     404    RTPrintf("\n");
     405    return 0;
     406}
     407
     408
     409int main(int argc, char **argv)
     410{
     411    RTR3Init();
     412
     413    /*
     414     * Parse arguments.
     415     */
     416    bool fCalcFrequency = false;
     417    uint32_t cMsDuration = 1000; /* 1 sec */
     418    static const RTOPTIONDEF s_aOptions[] =
     419    {
     420        { "--duration",         'd', RTGETOPT_REQ_UINT32 },
     421        { "--calc-frequency",   'f', RTGETOPT_REQ_NOTHING },
     422        { "--help",             'h', RTGETOPT_REQ_NOTHING }
     423    };
     424    int iArg = 1;
     425    int ch;
     426    RTOPTIONUNION Value;
     427    while ((ch = RTGetOpt(argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), &iArg, &Value)))
     428        switch (ch)
     429        {
     430            case 'd':   cMsDuration = Value.u32; break;
     431            case 'f':   fCalcFrequency = true; break;
     432            case 'h':
     433                RTPrintf("usage: tstTSC\n"
     434                         "   or: tstTSC <-f|--calc-frequency> [--duration|-d ms]\n");
     435                return 1;
     436            default:
     437                RTStrmPrintf(g_pStdErr, "tstTSC: Unknown arg or error (ch=%d)\n", ch);
     438                return 1;
     439        }
     440    if (iArg != argc)
     441    {
     442        RTStrmPrintf(g_pStdErr, "tstTSC: too many arguments\n");
     443        return 1;
     444    }
     445
     446    if (fCalcFrequency)
     447        return tstTSCCalcFrequency(cMsDuration);
     448    return tstTSCCalcDrift();
     449}
     450
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette