简体   繁体   中英

Disable CPU package idle states in Windows from C++ code

I am successfully disabling CPU core C-states using this code (I'm working on Win10 and use Qt):

#include <QCoreApplication>
#include <QDebug>
#include "Windows.h"
extern "C" {
#include "powrprof.h"
}
#pragma comment(lib, "powrprof.lib")

int main()
{
    const DWORD DISABLED = 1;
    const DWORD ENABLED = 0;
    GUID *scheme;
    int error;

    error = PowerGetActiveScheme(NULL, &scheme);
    qDebug() << "PowerGetActiveScheme error code = " << error;
    error = PowerWriteACValueIndex(NULL, scheme, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_IDLE_DISABLE, DISABLED);
    qDebug() << "PowerWriteACValueIndex error code = " << error;
    error = PowerWriteDCValueIndex(NULL, scheme, &GUID_PROCESSOR_SETTINGS_SUBGROUP, &GUID_PROCESSOR_IDLE_DISABLE, DISABLED);
    qDebug() << "PowerWriteDCValueIndex error code = " << error;
    error = PowerSetActiveScheme(NULL, scheme);
    qDebug() << "PowerSetActiveScheme error code = " << error;
    return 0;
}

The reason behind this is that I am running an USB camera and figured out that I'm losing data packets when the processor enters idle modes. The code above works fine and overcomes this issue successfully. But it's actually a bit too much (disabling all C states appears to be unnecessary). I made some tests with the vendor software of the camera and found out that during acquisition not the core C-states stop, but the package C-states (if it is of any interest, I posted the analysis of this problem in the answer here https://superuser.com/questions/1648849/monitor-used-usb-bandwidth-in-win10 ).

So my question is: Can I adapt the above code to only disable package idle states? In case that's not possible, can I selectively disable core C-states?

Update: Based on the suggestion of @1201ProgramAlarm I tried to use SetThreadPriority() as in the minimal example below:

#include <QDebug>
#include <windows.h>
#include <conio.h>
#include "processthreadsapi.h"
    
int main()
{
    bool ok = false;
    ok = SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
    qDebug() << "SetPriorityClass ok = " << ok;
    ok = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
    qDebug() << "SetThreadPriority ok = " << ok;

    for (int i=1;i<100;i++) {
        qDebug() << "Here I am in some dummy loop...";
        if (_kbhit()) {
            break;
        }
       Sleep(1000);
    }
    return 0;
}

Unfortunately, this doesn't help and when monitoring the cpu package idle states (using HWiNFO64) I see no effect (package goes still idle as before).

The system is shutting down the USB device to save power.

This link provides the solution USB system power issue .

Open the Device manager as administrator. Find the camera. On the "Power Management" tab, deselect "Allow the computer to turn off this device to save power".

This can be done programmatically if the device ID is known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM