• <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)

            隨筆檔案

            收藏夾

            同行

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产色综合久久无码有码| 奇米综合四色77777久久| 久久精品国产亚洲av瑜伽| 国内精品久久久久久久影视麻豆| 国内精品久久久久国产盗摄| 亚洲精品无码久久久| 国产精品国色综合久久| 久久精品国产99久久香蕉| 久久亚洲精品成人av无码网站| 久久国产影院| 精品久久久无码人妻中文字幕豆芽| 国产精品青草久久久久福利99 | 无码人妻久久一区二区三区蜜桃| 久久中文字幕人妻熟av女| 99热热久久这里只有精品68| 777午夜精品久久av蜜臀| 久久久人妻精品无码一区| 色综合久久综合网观看| 久久精品久久久久观看99水蜜桃| 久久99国产精品成人欧美| 久久久久久亚洲精品成人 | 久久人人爽人人爽人人片AV东京热| 欧美一区二区三区久久综| 久久伊人五月丁香狠狠色| 热久久最新网站获取| 品成人欧美大片久久国产欧美...| 伊人色综合久久天天人手人婷| 久久久这里只有精品加勒比| 国产福利电影一区二区三区久久久久成人精品综合 | 久久精品无码免费不卡| 久久精品国产一区二区三区日韩| 亚洲AV无码久久寂寞少妇| 午夜精品久久久久久久久| 亚洲国产另类久久久精品小说| 久久婷婷五月综合国产尤物app| 中文字幕精品久久| 久久久久青草线蕉综合超碰| 伊人色综合久久天天人手人婷| 久久亚洲精品无码AV红樱桃| 久久精品无码专区免费青青| 久久精品黄AA片一区二区三区|