• <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使用率

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

            其實Windows的進程使用率是計算出來的,在一段很短的時間內,計算某進程使用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;

            /// 時間轉換
            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的核數
            static int get_processor_number()
            {
                SYSTEM_INFO info;
                GetSystemInfo(&info);
                return (int)info.dwNumberOfProcessors;
            }

            int get_cpu_usage(int pid)
            {  
                //cpu數量
                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;
                // 參數為進程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)  編輯 收藏 引用

            導航

            <2012年8月>
            2930311234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            亚洲中文久久精品无码| 四虎影视久久久免费观看| 亚洲中文字幕久久精品无码喷水| 2021国内久久精品| 久久精品无码专区免费东京热| 国产精品久久永久免费| 久久人人爽人人爽AV片| 99久久精品免费看国产一区二区三区 | 国产美女亚洲精品久久久综合 | 久久久久久一区国产精品| 无码任你躁久久久久久老妇| 久久夜色精品国产网站| 国产精品久久久久乳精品爆| 久久人人爽人人爽人人片av麻烦 | 国产视频久久| 亚洲综合熟女久久久30p| 99久久99久久| 日韩精品久久无码中文字幕| 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲高清不卡 国产成人精品久久亚洲 | 亚洲成色WWW久久网站| 久久国产精品偷99| 国产成人精品久久一区二区三区| 欧美久久久久久精选9999| 99re这里只有精品热久久| 伊人久久综合无码成人网| 性做久久久久久久久久久| 热99re久久国超精品首页| 奇米综合四色77777久久| 99精品国产免费久久久久久下载| 国产精品一区二区久久精品无码| 91久久精一区二区三区大全| 精品国产乱码久久久久久人妻| 久久综合九色欧美综合狠狠| 狠狠色丁香婷婷综合久久来来去 | 国产精品久久永久免费| 亚洲国产精品无码久久98| 久久只有这里有精品4| 亚洲乱码日产精品a级毛片久久| 国产精品一区二区久久精品无码| 日本免费久久久久久久网站| 亚洲国产成人久久综合一|