• 【windows + 大小核调度】ADL平台大小核调度尝试


    调用微软接口可以对qos level进行设置,demo如下:

    #include <iostream>
    #include <windows.h>
    #include <processthreadsapi.h>
    #include <TlHelp32.h>       // 头文件
    
    using namespace std;
    
    bool GetPIDByName(TCHAR* processName, int& pid)
    {
        // 创建系统快照
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hSnapshot == INVALID_HANDLE_VALUE)
        {
            return false;
        }
    
        PROCESSENTRY32 ps;
        ZeroMemory(&ps, sizeof(PROCESSENTRY32));
        ps.dwSize = sizeof(PROCESSENTRY32);
        if (!Process32First(hSnapshot, &ps))
        {
            return false;
        }
        do
        {
            if (lstrcmpi(ps.szExeFile, processName) == 0)
            {
                // 保存进程ID
                pid = ps.th32ProcessID;
               
                CloseHandle(hSnapshot);
    
                return true;
            }
        } while (Process32Next(hSnapshot, &ps));
    
        return false;
    }
    
    wchar_t* chr2wch(const char* buffer)
    {
        size_t len = strlen(buffer);
        size_t wlen = MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), NULL, 0);
        wchar_t* wBuf = new wchar_t[wlen + 1];
        RtlZeroMemory(wBuf, wlen+1);
        MultiByteToWideChar(CP_ACP, 0, (const char*)buffer, int(len), wBuf, int(wlen));
        return wBuf;
    }
    
    int main()
    {
    
        int option = 0;
        cout << "input a option ,0: low qos; 1: high qos;";
        cin >> option;
    
        int pid = 0;
        wstring procName(L"xxx.exe");  // 这里简单起见,写死了设置的进程名
        bool ret = GetPIDByName((TCHAR*)procName.c_str(), pid);
        //string procname("HiviewService.exe");
       // bool ret = GetPIDByName(chr2wch(procname.c_str()), pid);
        if (!ret) {
            cout << "Get pid failed." << endl;
            return -1;
        }
    
        HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
        if (INVALID_HANDLE_VALUE == handle) {
            cout << "Get handle failed" << endl;
            return -1;
        }
        
        PROCESS_POWER_THROTTLING_STATE PowerThrottling;
        RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
        PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
    
        if (option == 0) {
            //
        // EcoQoS
        // Turn EXECUTION_SPEED throttling on. 
        // ControlMask selects the mechanism and StateMask declares which mechanism should be on or off.
        //
            PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
            PowerThrottling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
    
            ret = SetProcessInformation(handle,
                ProcessPowerThrottling,
                &PowerThrottling,
                sizeof(PowerThrottling));
            cout << "Set low qos complete, ret(" << ret<<")"<< endl;
        }
        else {
            //
        // HighQoS
        // Turn EXECUTION_SPEED throttling off. 
        // ControlMask selects the mechanism and StateMask is set to zero as mechanisms should be turned off.
    
            PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
            PowerThrottling.StateMask = 0;
    
            SetProcessInformation(handle,
                ProcessPowerThrottling,
                &PowerThrottling,
                sizeof(PowerThrottling));
            cout << "Set high qos complete, ret(" << ret << ")" << endl;
        }
    }
    

    设置所有进程都为Eco:

    bool SetEcoQos(uint32_t pid, int option)
    {
        bool ret = false;
        HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
        if (INVALID_HANDLE_VALUE == handle) {
            cout << "Get handle failed" << endl;
            return false;
        }
    
        PROCESS_POWER_THROTTLING_STATE PowerThrottling;
        RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
        PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
    
        if (option == 0) {
            //
        // EcoQoS
        // Turn EXECUTION_SPEED throttling on. 
        // ControlMask selects the mechanism and StateMask declares which mechanism should be on or off.
        //
            PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
            PowerThrottling.StateMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
    
            ret = SetProcessInformation(handle,
                ProcessPowerThrottling,
                &PowerThrottling,
                sizeof(PowerThrottling));
            cout << "Set low qos complete, ret(" << ret << ")" << endl;
        }
        else {
            //
        // HighQoS
        // Turn EXECUTION_SPEED throttling off. 
        // ControlMask selects the mechanism and StateMask is set to zero as mechanisms should be turned off.
        //
    
            PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
            PowerThrottling.StateMask = 0;
    
            SetProcessInformation(handle,
                ProcessPowerThrottling,
                &PowerThrottling,
                sizeof(PowerThrottling));
            cout << "Set high qos complete, ret(" << ret << ")" << endl;
        }
        return true;
    }
    
    bool SetAllProcEco()
    {
        // 创建系统快照
        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
        if (hSnapshot == INVALID_HANDLE_VALUE)
        {
            return false;
        }
    
        PROCESSENTRY32 ps;
        ZeroMemory(&ps, sizeof(PROCESSENTRY32));
        ps.dwSize = sizeof(PROCESSENTRY32);
        if (!Process32First(hSnapshot, &ps))
        {
            return false;
        }
        do
        {
            DWORD pid = ps.th32ProcessID;
            CloseHandle(hSnapshot);
            SetEcoQos(pid, 0);
        } while (Process32Next(hSnapshot, &ps));
    }
    
    int main()
    {
        SetAllProcEco();
        return 0;
    }
    
  • 相关阅读:
    数据库事务隔离级别和传播
    InputStreamReader和BufferedReader的区别
    SQL Server
    java.util.List.subList ,开区间和闭区间
    java中/与%的区别
    mysql 日期比较
    intellij idea 分屏设置 与快捷键
    Java 根据年月日精确计算年龄
    IntelliJ IDEA控制台Console里没有查找快捷键
    eclipse svn同步资源库时忽略某些不需要提交文件类型和文件夹
  • 原文地址:https://www.cnblogs.com/mooooonlight/p/16281629.html
Copyright © 2020-2023  润新知