CPU utilization is sinusoidal, and CPU utilization of real-time output processes and threads

Source: Internet
Author: User
Tags filetime

CPU utilization is sinusoidal, and CPU utilization of real-time output processes and threads

#include "stdafx.h" #include <windows.h> #include <math.h>//time conversion static __int64 FILE_TIME_2_UTC (const    filetime* ftime) {Large_integer li; Li.    LowPart = ftime->dwlowdatetime; Li.    Highpart = ftime->dwhighdatetime; Return LI. QuadPart;}    Gets the number of cores of the CPU static int Get_processor_number () {system_info INFO;    GetSystemInfo (&info); return (int) info.dwnumberofprocessors;}    Get process CPU occupied int get_cpu_usage (HANDLE hand) {//cpu number static int processor_count_ =-1;    Last time static __int64 last_time_ = 0;    static __int64 last_system_time_ = 0;    FILETIME now;    FILETIME Creation_time;    FILETIME Exit_time;    FILETIME Kernel_time;    FILETIME User_time;    __int64 System_time;    __int64 time;    __int64 System_time_delta;    __int64 Time_delta;    int cpu = -1;IF (hand==null) return CPU;    if (Processor_count_ = =-1) {Processor_count_ = Get_processor_number ();    } getsystemtimeasfiletime (&now); HANDLE hprocess = OpenProcess (process_aLl_access, FALSE, PID); if (!    Getprocesstimes (hand, &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-1;    } System_time_delta = System_time-last_system_time_;    Time_delta = time-last_time_;    if (Time_delta = = 0) return-1;    CPU = (int) ((System_time_delta * + TIME_DELTA/2)/Time_delta);    Last_system_time_ = System_time;    Last_time_ = time; return CPU;}    Get thread CPU occupied int get_thread_cpu_usage (HANDLE hand) {//cpu number static int processor_count_ =-1;    Last time static __int64 last_time_ = 0;    static __int64 last_system_time_ = 0;    FILETIME now;    FILETIME Creation_time;    FILETIME Exit_time; FILETIME Kernel_time;    FILETIME User_time;    __int64 System_time;    __int64 time;    __int64 System_time_delta;    __int64 Time_delta;    int cpu = -1;IF (hand==null) return CPU;    if (Processor_count_ = =-1) {Processor_count_ = Get_processor_number ();    } getsystemtimeasfiletime (&now);    HANDLE hprocess = OpenProcess (Process_all_access, FALSE, PID); if (!    Getthreadtimes (hand, &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-1;    } System_time_delta = System_time-last_system_time_;    Time_delta = time-last_time_;    if (Time_delta = = 0) return-1;    CPU = (int) ((System_time_delta * + TIME_DELTA/2)/Time_delta); Last_system_time_ = SyStem_time;    Last_time_ = time; return CPU;} CPU utilization is sine-realized const double SPLIT = 0.01;const int COUNT = 200;const double PI = 3.14159265;const int INTERVAL = 300;  UINT Threadfunt () {DWORD busyspan[count];  Array of busy times DWORD Idlespan[count];    Array of idle times int half = INTERVAL/2;    Double radian = 0.0;        for (int i = 0; i < COUNT; i++) {Busyspan[i] = (DWORD) (Half + (sin (PI * radian) * half));        Idlespan[i] = Interval-busyspan[i];    Radian + = SPLIT;    } DWORD startTime = 0;    int j = 0;        while (true) {j = j% COUNT;        StartTime = GetTickCount ();        while ((GetTickCount ()-StartTime) <= busyspan[j]);        Sleep (Idlespan[j]);    j + +; } return 0;} int main (int argc, char* argv[]) {HANDLE M,c;dword threadid;int i=0;//Let the process run on the specified processor Setprocessaffinitymask ( GetCurrentProcess (), 0x00000001//cpu mask); m=getcurrentprocess ();//c=createthread (null,//0,//(lpthread_s Tart_routine) threadfunt,//null,//0,//&threadid); Sleep (C=createthread) (null,0, (lpthread_start_routine) Threadfunt,null,0,&threadid), while (i<200) { Sleep;p rintf ("cpu:%d%% thread:%d%%\n", Get_cpu_usage (M), Get_thread_cpu_usage (c)); i++;} printf ("Hello world!\n"); return 0;}

CPU utilization is sinusoidal, and CPU utilization of real-time output processes and threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.