第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從進(jìn)程內(nèi)部確定CPU和內(nèi)存消耗?

如何從進(jìn)程內(nèi)部確定CPU和內(nèi)存消耗?

如何從進(jìn)程內(nèi)部確定CPU和內(nèi)存消耗?我曾經(jīng)負(fù)責(zé)從運(yùn)行中的應(yīng)用程序中確定以下性能參數(shù):可用虛擬內(nèi)存總量當(dāng)前使用的虛擬內(nèi)存我的進(jìn)程當(dāng)前使用的虛擬內(nèi)存可用內(nèi)存總數(shù)Ram目前使用我的進(jìn)程目前使用的RAM目前使用的CPU百分比我的進(jìn)程當(dāng)前使用的CPU%代碼必須在Windows和Linux上運(yùn)行。盡管這似乎是一項(xiàng)標(biāo)準(zhǔn)任務(wù),但在手冊(Win 32 API,GNU docs)中以及在Internet上找到必要的信息花了我?guī)滋斓臅r(shí)間,因?yàn)殛P(guān)于這個(gè)主題有很多不完整/不正確/過時(shí)的信息要找出來。為了避免其他人經(jīng)歷同樣的麻煩,我認(rèn)為收集所有零散的信息,再加上我在一個(gè)地方經(jīng)過反復(fù)試驗(yàn)發(fā)現(xiàn)的信息,是個(gè)好主意。
查看完整描述

3 回答

?
侃侃無極

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊

上面的一些值很容易從適當(dāng)?shù)腤in 32 API中獲得,我只是在這里列出它們的完整性。然而,另一些則需要從性能數(shù)據(jù)幫助庫(PDH)中獲得,這有點(diǎn)“不直觀”,需要大量痛苦的嘗試和錯(cuò)誤才能開始工作。(至少花了我一段時(shí)間,也許我只是有點(diǎn)傻.)

注意:為了清楚起見,以下代碼省略了所有錯(cuò)誤檢查。檢查返回碼.!


  • 總虛擬內(nèi)存:

    #include "windows.h"MEMORYSTATUSEX memInfo;memInfo.dwLength = sizeof(MEMORYSTATUSEX);GlobalMemoryStatusEx(&memInfo);
    DWORDLONG totalVirtualMem = memInfo.ullTotalPageFile;

    注意:這里的名字“TotalPageFile”有點(diǎn)誤導(dǎo)。實(shí)際上,這個(gè)參數(shù)給出了“虛擬內(nèi)存大小”,即交換文件加上已安裝RAM的大小。

  • 當(dāng)前使用的虛擬內(nèi)存:

    與“總虛擬內(nèi)存”中的代碼相同,然后

    DWORDLONG virtualMemUsed = memInfo.ullTotalPageFile - memInfo.ullAvailPageFile;
  • 當(dāng)前進(jìn)程當(dāng)前使用的虛擬內(nèi)存:

    #include "windows.h"#include "psapi.h"PROCESS_MEMORY_COUNTERS_EX pmc;GetProcessMemoryInfo(GetCurrentProcess(), 
    &pmc, sizeof(pmc));SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;



  • 總物理內(nèi)存(RAM):

    與“總虛擬內(nèi)存”中的代碼相同,然后

    DWORDLONG totalPhysMem = memInfo.ullTotalPhys;
  • 目前使用的物理內(nèi)存:

    Same code as in "Total Virtual Memory" and thenDWORDLONG physMemUsed = memInfo.ullTotalPhys - memInfo.ullAvailPhys;
  • 當(dāng)前進(jìn)程當(dāng)前使用的物理內(nèi)存:

    與“當(dāng)前進(jìn)程當(dāng)前使用的虛擬內(nèi)存”中的代碼相同,然后

    SIZE_T physMemUsedByMe = pmc.WorkingSetSize;



  • 目前使用的CPU:

    #include "TCHAR.h"#include "pdh.h"static PDH_HQUERY cpuQuery;static PDH_HCOUNTER cpuTotal;void init(){
        PdhOpenQuery(NULL, NULL, &cpuQuery);
        // You can also use L"\\Processor(*)\\% Processor Time" and get individual CPU values with
         PdhGetFormattedCounterArray()
        PdhAddEnglishCounter(cpuQuery, L"\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal);
        PdhCollectQueryData(cpuQuery);}double getCurrentValue(){
        PDH_FMT_COUNTERVALUE counterVal;
    
        PdhCollectQueryData(cpuQuery);
        PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal);
        return counterVal.doubleValue;}
  • 當(dāng)前進(jìn)程當(dāng)前使用的CPU:

    #include "windows.h"static ULARGE_INTEGER lastCPU, lastSysCPU, lastUserCPU;static int numProcessors;static
     HANDLE self;void init(){
        SYSTEM_INFO sysInfo;
        FILETIME ftime, fsys, fuser;
    
        GetSystemInfo(&sysInfo);
        numProcessors = sysInfo.dwNumberOfProcessors;
    
        GetSystemTimeAsFileTime(&ftime);
        memcpy(&lastCPU, &ftime, sizeof(FILETIME));
    
        self = GetCurrentProcess();
        GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
        memcpy(&lastSysCPU, &fsys, sizeof(FILETIME));
        memcpy(&lastUserCPU, &fuser, sizeof(FILETIME));}double getCurrentValue(){
        FILETIME ftime, fsys, fuser;
        ULARGE_INTEGER now, sys, user;
        double percent;
    
        GetSystemTimeAsFileTime(&ftime);
        memcpy(&now, &ftime, sizeof(FILETIME));
    
        GetProcessTimes(self, &ftime, &ftime, &fsys, &fuser);
        memcpy(&sys, &fsys, sizeof(FILETIME));
        memcpy(&user, &fuser, sizeof(FILETIME));
        percent = (sys.QuadPart - lastSysCPU.QuadPart) +
            (user.QuadPart - lastUserCPU.QuadPart);
        percent /= (now.QuadPart - lastCPU.QuadPart);
        percent /= numProcessors;
        lastCPU = now;
        lastUserCPU = user;
        lastSysCPU = sys;
    
        return percent * 100;}

linux

在linux上,一開始似乎很明顯的選擇是使用POSIX API,如getrusage()等。我花了一些時(shí)間試著讓它發(fā)揮作用,但卻沒有得到有意義的價(jià)值。當(dāng)我最終檢查內(nèi)核源代碼時(shí),我發(fā)現(xiàn)這些API顯然還沒有在Linux2.6內(nèi)核中完全實(shí)現(xiàn)!

最后,我通過合并讀取偽文件系統(tǒng)獲得了所有的值。/proc和內(nèi)核調(diào)用。

  • 總虛擬內(nèi)存:

    #include "sys/types.h"#include "sys/sysinfo.h"struct sysinfo memInfo;sysinfo (&memInfo);long long totalVirtualMem 
    = memInfo.totalram;//Add other values in next statement to avoid int overflow on right hand 
    side...totalVirtualMem += memInfo.totalswap;totalVirtualMem *= memInfo.mem_unit;
  • 當(dāng)前使用的虛擬內(nèi)存:

    與“總虛擬內(nèi)存”中的代碼相同,然后

    long long virtualMemUsed = memInfo.totalram - memInfo.freeram;//Add other values in next statement to avoid 
    int overflow on right hand side...virtualMemUsed += memInfo.totalswap - memInfo.freeswap;virtualMemUsed *= memInfo.mem_unit;
  • 當(dāng)前進(jìn)程當(dāng)前使用的虛擬內(nèi)存:

    #include "stdlib.h"#include "stdio.h"#include "string.h"int parseLine(char* line){
        // This assumes that a digit will be found and the line ends in " Kb".
        int i = strlen(line);
        const char* p = line;
        while (*p <'0' || *p > '9') p++;
        line[i-3] = '\0';
        i = atoi(p);
        return i;}int getValue(){ //Note: this value is in KB!
        FILE* file = fopen("/proc/self/status", "r");
        int result = -1;
        char line[128];
    
        while (fgets(line, 128, file) != NULL){
            if (strncmp(line, "VmSize:", 7) == 0){
                result = parseLine(line);
                break;
            }
        }
        fclose(file);
        return result;}



  • 總物理內(nèi)存(RAM):

    與“總虛擬內(nèi)存”中的代碼相同,然后

    long long totalPhysMem = memInfo.totalram;//Multiply in next statement to avoid int overflow on right hand
     side...totalPhysMem *= memInfo.mem_unit;
  • 目前使用的物理內(nèi)存:

    與“總虛擬內(nèi)存”中的代碼相同,然后

    long long physMemUsed = memInfo.totalram - memInfo.freeram;//Multiply in next statement to avoid int overflow 
    on right hand side...physMemUsed *= memInfo.mem_unit;
  • 當(dāng)前進(jìn)程當(dāng)前使用的物理內(nèi)存:

    在“當(dāng)前進(jìn)程當(dāng)前使用的虛擬內(nèi)存”中更改getValue()如下:

    int getValue(){ //Note: this value is in KB!
        FILE* file = fopen("/proc/self/status", "r");
        int result = -1;
        char line[128];
    
        while (fgets(line, 128, file) != NULL){
            if (strncmp(line, "VmRSS:", 6) == 0){
                result = parseLine(line);
                break;
            }
        }
        fclose(file);
        return result;}




  • 目前使用的CPU:

    #include "stdlib.h"#include "stdio.h"#include "string.h"static unsigned long long lastTotalUser, lastTotalUserLow, 
    lastTotalSys, lastTotalIdle;void init(){
        FILE* file = fopen("/proc/stat", "r");
        fscanf(file, "cpu %llu %llu %llu %llu", &lastTotalUser, &lastTotalUserLow,
            &lastTotalSys, &lastTotalIdle);
        fclose(file);}double getCurrentValue(){
        double percent;
        FILE* file;
        unsigned long long totalUser, totalUserLow, totalSys, totalIdle, total;
    
        file = fopen("/proc/stat", "r");
        fscanf(file, "cpu %llu %llu %llu %llu", &totalUser, &totalUserLow,
            &totalSys, &totalIdle);
        fclose(file);
    
        if (totalUser < lastTotalUser || totalUserLow < lastTotalUserLow ||
            totalSys < lastTotalSys || totalIdle < lastTotalIdle){
            //Overflow detection. Just skip this value.
            percent = -1.0;
        }
        else{
            total = (totalUser - lastTotalUser) + (totalUserLow - lastTotalUserLow) +
                (totalSys - lastTotalSys);
            percent = total;
            total += (totalIdle - lastTotalIdle);
            percent /= total;
            percent *= 100;
        }
    
        lastTotalUser = totalUser;
        lastTotalUserLow = totalUserLow;
        lastTotalSys = totalSys;
        lastTotalIdle = totalIdle;
    
        return percent;}
  • 當(dāng)前進(jìn)程當(dāng)前使用的CPU:

    #include "stdlib.h"#include "stdio.h"#include "string.h"#include "sys/times.h"#include "sys/vtimes.h"static clock_t lastCPU,
     lastSysCPU, lastUserCPU;static int numProcessors;void init(){
        FILE* file;
        struct tms timeSample;
        char line[128];
    
        lastCPU = times(&timeSample);
        lastSysCPU = timeSample.tms_stime;
        lastUserCPU = timeSample.tms_utime;
    
        file = fopen("/proc/cpuinfo", "r");
        numProcessors = 0;
        while(fgets(line, 128, file) != NULL){
            if (strncmp(line, "processor", 9) == 0) numProcessors++;
        }
        fclose(file);}double getCurrentValue(){
        struct tms timeSample;
        clock_t now;
        double percent;
    
        now = times(&timeSample);
        if (now <= lastCPU || timeSample.tms_stime < lastSysCPU ||
            timeSample.tms_utime < lastUserCPU){
            //Overflow detection. Just skip this value.
            percent = -1.0;
        }
        else{
            percent = (timeSample.tms_stime - lastSysCPU) +
                (timeSample.tms_utime - lastUserCPU);
            percent /= (now - lastCPU);
            percent /= numProcessors;
            percent *= 100;
        }
        lastCPU = now;
        lastSysCPU = timeSample.tms_stime;
        lastUserCPU = timeSample.tms_utime;
    
        return percent;}

Todo:其他平臺(tái)

我假設(shè),除了讀取/proc偽文件系統(tǒng)的部分之外,一些Linux代碼也適用于unix。也許在unix上,這些部件可以由getrusage()還有類似的功能?如果有Unix技術(shù)的人可以編輯這個(gè)答案并填寫詳細(xì)信息?!


查看完整回答
反對 回復(fù) 2019-06-06
  • 3 回答
  • 0 關(guān)注
  • 960 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)