Use C to obtain the cpu usage, memory usage, and IO of processes

Source: Internet
Author: User
Tags filetime

/** @ File
* @ Brief statement of the Process statistics function
* @ Author Zhang yaotong
* @ Date 2009/05/03
* @ Version 0.1
*
*/
# Ifndef PROCESS_STAT_H
# Define PROCESS_STAT_H


# Ifdef _ cplusplus
Extern "C "{
# Endif

Typedef long int64_t;
Typedef unsigned long uint64_t;


/// Obtain the cpu usage of the current process.-1 error is returned.
Int get_cpu_usage ();


/// Get the memory and virtual memory usage of the current process.-1 failed to be returned, 0 succeeded.
Int get_memory_usage (uint64_t * mem, uint64_t * vmem );


/// Obtain the total number of I/O bytes read and written by the current process.-1 failed to be returned and 0 succeeded.
Int get_io_bytes (uint64_t * read_bytes, uint64_t * write_bytes );


# Ifdef _ cplusplus
}
# Endif

# Endif/* PROCESS_STAT_H */

/** @ File
* @ Brief Implementation of the Process statistics function
* @ Author Zhang yaotong
* @ Date 2009/05/03
* @ Version 0.1
*
* Some codes are from the MSDN example.
* Some code is from the google chromium project.
*
* You need to connect to psapi. lib.
*/


# Include <windows. h>
# Include <psapi. h>
# Include <assert. h>
# Include "process_stat.h"

/// Time Conversion
Static uint64_t file_time_2_utc (const FILETIME * ftime)
{
LARGE_INTEGER li;

Assert (ftime );
Li. LowPart = ftime-> dwLowDateTime;
Li. HighPart = ftime-> dwHighDateTime;
Return li. QuadPart;
}


/// Obtain the number of CPU Cores
Static int get_processor_number ()
{
SYSTEM_INFO info;
GetSystemInfo (& info );
Return (int) info. dwNumberOfProcessors;
}


Int get_cpu_usage ()
{
// Number of CPUs
Static int processor_count _ =-1;
// Last time
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 );

If (! GetProcessTimes (GetCurrentProcess (), & creation_time, & exit_time,
& Kernel_time, & user_time ))
{
// We dont assert here because in some cases (such as in the Task

Manager)
// We may call this function on a process that has just exited

We have
// Not yet has ed the notification.
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 ))
{
// First call, just set the last values.
Last_system_time _ = system_time;
Last_time _ = time;
Return-1;
}

System_time_delta = system_time-last_system_time _;
Time_delta = time-last_time _;

Assert (time_delta! = 0 );

If (time_delta = 0)
Return-1;

// We add time_delta/2 so the result is rounded.
Cpu = (int) (system_time_delta * 100 + time_delta/2)/time_delta );
Last_system_time _ = system_time;
Last_time _ = time;
Return cpu;
}

Int get_memory_usage (uint64_t * mem, uint64_t * vmem)
{
PROCESS_MEMORY_COUNTERS pmc;
If (GetProcessMemoryInfo (GetCurrentProcess (), & pmc, sizeof (pmc )))
{
If (mem) * mem = pmc. WorkingSetSize;
If (vmem) * vmem = pmc. PagefileUsage;
Return 0;
}
Return-1;
}

Int get_io_bytes (uint64_t * read_bytes, uint64_t * write_bytes)
{
IO_COUNTERS io_counter;
If (GetProcessIoCounters (GetCurrentProcess (), & io_counter ))
{
If (read_bytes) * read_bytes = io_counter.ReadTransferCount;
If (write_bytes) * write_bytes = io_counter.WriteTransferCount;
Return 0;
}
Return-1;
}

/** @ File
* @ Brief process statistics function test
* @ Author Zhang yaotong
* @ Date 2009/05/03
* @ Version 0.1
*
*/

# Include "process_stat.h"
# Include <stdio. h>
# Include <Windows. h>

Int main ()
{
While (1)
{
Int cpu;
Uint64_t mem, vmem, r, w;


Cpu = get_cpu_usage ();
Get_memory_usage (& mem, & vmem );
Get_io_bytes (& r, & w );

Printf ("CPU usage: % u", cpu );
Printf ("memory usage: % u bytes", mem );
Printf ("virtual memory usage: % u bytes", vmem );
Printf ("Total read: % u bytes", r );
Printf ("Total write: % u bytes", w );

Sleep (1000 );
}
Return 0;
}

 

Related Article

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.