• <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 下檢測進(jìn)程cpu使用率

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

            其實(shí)Windows的進(jìn)程使用率是計(jì)算出來的,在一段很短的時間內(nèi),計(jì)算某進(jìn)程使用CPU的時間,除以所有進(jìn)程使用CPU的時間,即為該進(jìn)程的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ù)為進(jìn)程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 閱讀(3807) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

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

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产L精品国产亚洲区久久| 熟妇人妻久久中文字幕| 久久久久亚洲AV无码麻豆| 久久久噜噜噜久久熟女AA片 | 国产成人久久精品麻豆一区 | 久久香蕉综合色一综合色88| 久久性精品| 久久精品国产亚洲av水果派| 精品一区二区久久久久久久网站| 久久国产欧美日韩精品免费| 久久国产精品-国产精品| 久久青青草原精品国产软件| 99久久精品免费看国产一区二区三区| 99久久精品国产一区二区三区| 亚洲av伊人久久综合密臀性色| 久久99国产精品久久99果冻传媒| 亚洲午夜无码久久久久小说| 国产精品美女久久久久AV福利| 无码任你躁久久久久久老妇App| 久久99精品国产一区二区三区| 久久婷婷五月综合国产尤物app| 久久久久久毛片免费看| 91超碰碰碰碰久久久久久综合| 久久人人爽人人爽人人片av麻烦 | 久久久久久人妻无码| 人妻精品久久久久中文字幕 | 九九精品久久久久久噜噜| 国内精品伊人久久久久网站| 国内精品久久久久久野外| 久久久这里只有精品加勒比| 久久久久亚洲AV成人网人人软件| 久久99亚洲网美利坚合众国| 国产婷婷成人久久Av免费高清| 亚洲午夜无码久久久久小说| 日韩亚洲国产综合久久久| 精品国产91久久久久久久a| www.久久热.com| 久久久久久久久久久免费精品| 久久精品国产一区二区三区日韩| 99久久无色码中文字幕| 狠狠色婷婷综合天天久久丁香|