Java Read process CPU usage

Source: Internet
Author: User
Tags filetime

Original link

/* ------------------------------------------------------------------------- *//** An implementation of JNI methods in Com.vladium.utils.SystemInformation * class. The author compiled it using Microsoft Visual C + + But the code * should is easy-to-use with any compiler for Win32 Platfor M. * * For simplicity, this implementaion assumes JNI 1.2+ and omits error handling. * * (C) 2002, Vladimir Roubtsov [[email protected]*//* ------------------------------------------------------------------------- */#include<windows.h>#include<process.h>#include<winbase.h>#include"Com_vladium_utils_systeminformation.h"StaticJint s_pid;StaticHANDLE s_currentprocess;Static ints_numberofprocessors;/* ------------------------------------------------------------------------- *//** A helper function for converting FILETIME to A Longlong [safe from memory * Alignment point of view].*/StaticLONGLONGfileTimeToInt64 (ConstFILETIME *Time )    {Ularge_integer _time; _time. LowPart= time->Dwlowdatetime; _time. Highpart= time->Dwhighdatetime; return_time. QuadPart;}/* ......................................................................... *//** This method is added in JNI 1.2. It is executed once before all other * methods be called and is ostensibly for negotiating JNI spec versions, but * Can a LSO be conveniently used for initializing variables that would not * change throughout the lifetime of this process. */jniexport jint jnicalljni_onload (JAVAVM* VMS,void*reserved)            {System_info systemInfo; S_pid=_getpid (); S_currentprocess=getcurrentprocess (); GetSystemInfo (&systemInfo); S_numberofprocessors=systeminfo.dwnumberofprocessors; returnjni_version_1_2;}/* ......................................................................... *//** class:com_vladium_utils_systeminformation * method:getprocessid * Signature: () I*/jniexport jint jnicalljava_com_vladium_utils_systeminformation_getprocessid (jnienv*env, Jclass CLS) {    returns_pid;}/* ......................................................................... *//** class:com_vladium_utils_systeminformation * method:getprocesscputime * Signature: () J*/jniexport jlong jnicalljava_com_vladium_utils_systeminformation_getprocesscputime (JNIEnv*env, Jclass CLS)        {FILETIME creationtime, exittime, Kerneltime, Usertime; Getprocesstimes (s_currentprocess,& CreationTime, & Exittime, & Kerneltime, &usertime); return(Jlong) (FileTimeToInt64 (& kerneltime) + FileTimeToInt64 (& Usertime))/(S_numberofprocessors*10000));}/* ......................................................................... *//*Define min Elapsed time (in units of 10E-7 sec):*/#defineMin_elapsed_time (10000)/** class:com_vladium_utils_systeminformation * method:getprocesscpuusage * Signature: () D*/jniexport jdouble jnicalljava_com_vladium_utils_systeminformation_getprocesscpuusage (JNIEnv*env, Jclass CLS)       {FILETIME creationtime, exittime, Kerneltime, Usertime, Nowtime;        Longlong ElapsedTime; Getprocesstimes (s_currentprocess,& CreationTime, & Exittime, & Kerneltime, &usertime); Getsystemtimeasfiletime (&nowtime); /*Note:win32 system time isn't very precise [~10ms resolution], use sufficiently long sampling interval    s If you do use the of this method. */ElapsedTime= FileTimeToInt64 (& nowtime)-FileTimeToInt64 (&creationtime); if(ElapsedTime <min_elapsed_time)return 0.0; Else          return((jdouble) (FileTimeToInt64 (& kerneltime) + FileTimeToInt64 (& Usertime))/(S_numberofprocessors*elapsedtime);}#undefMin_elapsed_time

Java Read process CPU usage

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.