Finding out the CPU clock frequency (per core, per processor)

前端 未结 6 1929
无人及你
无人及你 2020-11-30 01:26

Programs like CPUz are very good at giving in depth information about the system (bus speed, memory timings, etc.)

However, is there a programmatic way of calculatin

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 01:43

    One of the most simple ways to do it is using RDTSC, but seeing as this is for anti-cheating mechanisms, I'd put this in as a kernel driver or a hyper-visor resident piece of code.

    You'd probably also need to roll your own timing code**, which again can be done with RDTSC (QPC as used in the example below uses RDTSC, and its in fact very simple to reverse engineer and use a local copy of, which means to tamper with it, you'd need to tamper with your driver).

    void GetProcessorSpeed()
    {
        CPUInfo* pInfo = this;
        LARGE_INTEGER qwWait, qwStart, qwCurrent;
        QueryPerformanceCounter(&qwStart);
        QueryPerformanceFrequency(&qwWait);
        qwWait.QuadPart >>= 5;
        unsigned __int64 Start = __rdtsc();
        do
        {
            QueryPerformanceCounter(&qwCurrent);
        }while(qwCurrent.QuadPart - qwStart.QuadPart < qwWait.QuadPart);
        pInfo->dCPUSpeedMHz = ((__rdtsc() - Start) << 5) / 1000000.0;
    }
    

    ** I this would be for security as @Mystical mentioned, but as I've never felt the urge to subvert low level system timing mechanisms, there might be more involved, would be nice if Mystical could add something on that :)

提交回复
热议问题