• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            windows 下檢測進程cpu使用率

            以下內(nèi)容主要來自http://www.sizeof.cn/html/2010/365.html ,對其中的一些細節(jié)進行了調(diào)整。
            最近在項目中需要監(jiān)測某個進程的CPU使用率,原本以為是一個很簡單的需求,想用Windows上的性能計數(shù)器來進行計算的,但是經(jīng)過嘗試之后,發(fā)現(xiàn)Windows性能計數(shù)器算出來的值根本不正確,不耐經(jīng)過互聯(lián)網(wǎng)的搜索,終于發(fā)現(xiàn)了以下計算方法,總的測試,發(fā)現(xiàn)結(jié)果還是比較精準的。

            其實Windows的進程使用率是計算出來的,在一段很短的時間內(nèi),計算某進程使用CPU的時間,除以所有進程使用CPU的時間,即為該進程的CPU使用率。具體代碼如下:#include <stdafx.h>

            #include <stdio.h>  
            #include <Windows.h>
            #include <iostream>

            using namespace std;

            typedef long long           int64_t;
            typedef unsigned long long  uint64_t;

            /// 時間轉(zhuǎn)換
            static uint64_t file_time_2_utc(const FILETIME* ftime)
            {
                LARGE_INTEGER li;

                li.LowPart = ftime->dwLowDateTime;
                li.HighPart = ftime->dwHighDateTime;
                return li.QuadPart;
            }

            /// 獲得CPU的核數(shù)
            static int get_processor_number()
            {
                SYSTEM_INFO info;
                GetSystemInfo(&info);
                return (int)info.dwNumberOfProcessors;
            }

            int get_cpu_usage(int pid)
            {  
                //cpu數(shù)量
                static int processor_count_ = -1;
                //上一次的時間
                static int64_t last_time_ = 0;
                static int64_t last_system_time_ = 0;

                FILETIME now;
                FILETIME creation_time;
                FILETIME exit_time;
                FILETIME kernel_time;
                FILETIME user_time;
                int64_t system_time;
                int64_t time;
                int64_t system_time_delta;
                int64_t time_delta;

                int cpu = -1;

                if(processor_count_ == -1)
                {
                    processor_count_ = get_processor_number();
                }

                GetSystemTimeAsFileTime(&now);

                HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, pid);
                if (!GetProcessTimes(hProcess, &creation_time, &exit_time, &kernel_time, &user_time))
                {
                    return -1;
                }
                system_time = (file_time_2_utc(&kernel_time) + file_time_2_utc(&user_time)) 
                    / processor_count_;  
                time = file_time_2_utc(&now);

                if ((last_system_time_ == 0) || (last_time_ == 0))
                {
                    last_system_time_ = system_time;
                    last_time_ = time;
                    return get_cpu_usage(pid);
                }

                system_time_delta = system_time - last_system_time_;
                time_delta = time - last_time_;

                if (time_delta == 0)
                    return get_cpu_usage(pid);

                cpu = (int)((system_time_delta * 100 + time_delta / 2) / time_delta);
                last_system_time_ = system_time;
                last_time_ = time;
                return cpu;
            }

            int main()   
            {  
                int cpu;
                int process_id;
                // 參數(shù)為進程id
                cin>>process_id;

                while(1)   
                {  
                    cpu = get_cpu_usage(process_id);
                    printf("CPU使用率: %d%%\n",cpu);

                    Sleep(1000);
                }   
                return 0;
            }

            posted on 2012-08-24 09:39 sheng 閱讀(3802) 評論(0)  編輯 收藏 引用

            導航

            <2018年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            蜜桃麻豆WWW久久囤产精品| 国产精品免费久久久久影院 | 久久久久久毛片免费播放| 婷婷综合久久中文字幕蜜桃三电影| 国产aⅴ激情无码久久| 国产午夜福利精品久久2021 | 狠狠色丁香久久综合婷婷| 国产午夜福利精品久久| 久久中文字幕人妻丝袜| 欧美一区二区精品久久| 久久久久久综合网天天| 一本久久久久久久| 婷婷久久久亚洲欧洲日产国码AV| 国产精品久久影院| 亚洲精品无码成人片久久| 国产农村妇女毛片精品久久| 久久精品国产亚洲AV电影| 亚洲欧美成人久久综合中文网| 99精品国产在热久久| 77777亚洲午夜久久多人| 久久精品国产一区二区三区| 久久精品视频网| 久久国产精品无码一区二区三区 | 中文精品久久久久人妻| 久久男人AV资源网站| 青青草国产成人久久91网| 久久综合给合久久国产免费 | 伊人丁香狠狠色综合久久| 人妻丰满AV无码久久不卡| 国产69精品久久久久9999APGF| 久久综合九色欧美综合狠狠| 久久99精品久久久久久野外| 伊人色综合久久| 99久久精品免费国产大片| 亚洲成色999久久网站| 色综合久久综合网观看| 99精品伊人久久久大香线蕉| 日韩欧美亚洲综合久久影院d3| 狠狠色噜噜狠狠狠狠狠色综合久久| 成人妇女免费播放久久久| 久久国产精品-久久精品|