• <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 閱讀(3772) 評論(0)  編輯 收藏 引用

            導航

            <2011年12月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統計

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久婷婷激情综合色综合俺也去| 要久久爱在线免费观看| 久久精品国产亚洲AV大全| 久久精品国产亚洲av影院| 99久久亚洲综合精品成人| 久久综合色之久久综合| 亚洲∧v久久久无码精品| 国产69精品久久久久9999| 婷婷国产天堂久久综合五月| 久久婷婷五月综合色奶水99啪| 国产激情久久久久影院老熟女| 亚洲国产成人久久一区WWW| 成人妇女免费播放久久久| 亚洲v国产v天堂a无码久久| 潮喷大喷水系列无码久久精品| 亚洲精品无码久久久| 一本伊大人香蕉久久网手机| 国产69精品久久久久9999APGF | 色综合久久88色综合天天 | 亚洲va久久久噜噜噜久久男同 | 久久无码专区国产精品发布| 久久噜噜电影你懂的| 亚洲va久久久噜噜噜久久狠狠| 久久无码AV中文出轨人妻| 国产精品青草久久久久福利99| 久久66热人妻偷产精品9| 成人久久免费网站| 久久亚洲日韩看片无码| 久久影视综合亚洲| 青青久久精品国产免费看| 精品久久综合1区2区3区激情| 9久久9久久精品| 狠狠色噜噜狠狠狠狠狠色综合久久| 亚洲AV无码久久精品色欲| 丁香色欲久久久久久综合网| 伊人久久久AV老熟妇色| 一本一本久久a久久精品综合麻豆| 午夜精品久久久久成人| 99久久国产亚洲综合精品| 精品久久久无码21p发布 | 久久性生大片免费观看性|