How to determine CPU and memory consumption from inside a process?

后端 未结 9 1576
被撕碎了的回忆
被撕碎了的回忆 2020-11-21 11:28

I once had the task of determining the following performance parameters from inside a running application:

  • Total virtual memory available
  • Virtual memo
相关标签:
9条回答
  • 2020-11-21 12:12

    I used this following code in my C++ project and it worked fine:

    static HANDLE self;
    static int numProcessors;
    SYSTEM_INFO sysInfo;
    
    double percent;
    
    numProcessors = sysInfo.dwNumberOfProcessors;
    
    //Getting system times information
    FILETIME SysidleTime;
    FILETIME SyskernelTime; 
    FILETIME SysuserTime; 
    ULARGE_INTEGER SyskernelTimeInt, SysuserTimeInt;
    GetSystemTimes(&SysidleTime, &SyskernelTime, &SysuserTime);
    memcpy(&SyskernelTimeInt, &SyskernelTime, sizeof(FILETIME));
    memcpy(&SysuserTimeInt, &SysuserTime, sizeof(FILETIME));
    __int64 denomenator = SysuserTimeInt.QuadPart + SyskernelTimeInt.QuadPart;  
    
    //Getting process times information
    FILETIME ProccreationTime, ProcexitTime, ProcKernelTime, ProcUserTime;
    ULARGE_INTEGER ProccreationTimeInt, ProcexitTimeInt, ProcKernelTimeInt, ProcUserTimeInt;
    GetProcessTimes(self, &ProccreationTime, &ProcexitTime, &ProcKernelTime, &ProcUserTime);
    memcpy(&ProcKernelTimeInt, &ProcKernelTime, sizeof(FILETIME));
    memcpy(&ProcUserTimeInt, &ProcUserTime, sizeof(FILETIME));
    __int64 numerator = ProcUserTimeInt.QuadPart + ProcKernelTimeInt.QuadPart;
    //QuadPart represents a 64-bit signed integer (ULARGE_INTEGER)
    
    percent = 100*(numerator/denomenator);
    
    0 讨论(0)
  • 2020-11-21 12:15

    in windows you can get cpu usage by code bellow:

    #include <windows.h>
    #include <stdio.h>
    
        //------------------------------------------------------------------------------------------------------------------
        // Prototype(s)...
        //------------------------------------------------------------------------------------------------------------------
        CHAR cpuusage(void);
    
        //-----------------------------------------------------
        typedef BOOL ( __stdcall * pfnGetSystemTimes)( LPFILETIME lpIdleTime, LPFILETIME lpKernelTime, LPFILETIME lpUserTime );
        static pfnGetSystemTimes s_pfnGetSystemTimes = NULL;
    
        static HMODULE s_hKernel = NULL;
        //-----------------------------------------------------
        void GetSystemTimesAddress()
        {
            if( s_hKernel == NULL )
            {   
                s_hKernel = LoadLibrary( L"Kernel32.dll" );
                if( s_hKernel != NULL )
                {
                    s_pfnGetSystemTimes = (pfnGetSystemTimes)GetProcAddress( s_hKernel, "GetSystemTimes" );
                    if( s_pfnGetSystemTimes == NULL )
                    {
                        FreeLibrary( s_hKernel ); s_hKernel = NULL;
                    }
                }
            }
        }
        //----------------------------------------------------------------------------------------------------------------
    
        //----------------------------------------------------------------------------------------------------------------
        // cpuusage(void)
        // ==============
        // Return a CHAR value in the range 0 - 100 representing actual CPU usage in percent.
        //----------------------------------------------------------------------------------------------------------------
        CHAR cpuusage()
        {
            FILETIME               ft_sys_idle;
            FILETIME               ft_sys_kernel;
            FILETIME               ft_sys_user;
    
            ULARGE_INTEGER         ul_sys_idle;
            ULARGE_INTEGER         ul_sys_kernel;
            ULARGE_INTEGER         ul_sys_user;
    
            static ULARGE_INTEGER    ul_sys_idle_old;
            static ULARGE_INTEGER  ul_sys_kernel_old;
            static ULARGE_INTEGER  ul_sys_user_old;
    
            CHAR  usage = 0;
    
            // we cannot directly use GetSystemTimes on C language
            /* add this line :: pfnGetSystemTimes */
            s_pfnGetSystemTimes(&ft_sys_idle,    /* System idle time */
                &ft_sys_kernel,  /* system kernel time */
                &ft_sys_user);   /* System user time */
    
            CopyMemory(&ul_sys_idle  , &ft_sys_idle  , sizeof(FILETIME)); // Could been optimized away...
            CopyMemory(&ul_sys_kernel, &ft_sys_kernel, sizeof(FILETIME)); // Could been optimized away...
            CopyMemory(&ul_sys_user  , &ft_sys_user  , sizeof(FILETIME)); // Could been optimized away...
    
            usage  =
                (
                (
                (
                (
                (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
                (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
                )
                -
                (ul_sys_idle.QuadPart-ul_sys_idle_old.QuadPart)
                )
                *
                (100)
                )
                /
                (
                (ul_sys_kernel.QuadPart - ul_sys_kernel_old.QuadPart)+
                (ul_sys_user.QuadPart   - ul_sys_user_old.QuadPart)
                )
                );
    
            ul_sys_idle_old.QuadPart   = ul_sys_idle.QuadPart;
            ul_sys_user_old.QuadPart   = ul_sys_user.QuadPart;
            ul_sys_kernel_old.QuadPart = ul_sys_kernel.QuadPart;
    
            return usage;
        }
        //------------------------------------------------------------------------------------------------------------------
        // Entry point
        //------------------------------------------------------------------------------------------------------------------
        int main(void)
        {
            int n;
            GetSystemTimesAddress();
            for(n=0;n<20;n++)
            {
                printf("CPU Usage: %3d%%\r",cpuusage());
                Sleep(2000);
            }
            printf("\n");
            return 0;
        }
    
    0 讨论(0)
  • 2020-11-21 12:16

    Mac OS X - CPU

    Overall CPU usage:

    From Retrieve system information on MacOS X? :

    #include <mach/mach_init.h>
    #include <mach/mach_error.h>
    #include <mach/mach_host.h>
    #include <mach/vm_map.h>
    
    static unsigned long long _previousTotalTicks = 0;
    static unsigned long long _previousIdleTicks = 0;
    
    // Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
    // You'll need to call this at regular intervals, since it measures the load between
    // the previous call and the current one.
    float GetCPULoad()
    {
       host_cpu_load_info_data_t cpuinfo;
       mach_msg_type_number_t count = HOST_CPU_LOAD_INFO_COUNT;
       if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&cpuinfo, &count) == KERN_SUCCESS)
       {
          unsigned long long totalTicks = 0;
          for(int i=0; i<CPU_STATE_MAX; i++) totalTicks += cpuinfo.cpu_ticks[i];
          return CalculateCPULoad(cpuinfo.cpu_ticks[CPU_STATE_IDLE], totalTicks);
       }
       else return -1.0f;
    }
    
    float CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
    {
      unsigned long long totalTicksSinceLastTime = totalTicks-_previousTotalTicks;
      unsigned long long idleTicksSinceLastTime  = idleTicks-_previousIdleTicks;
      float ret = 1.0f-((totalTicksSinceLastTime > 0) ? ((float)idleTicksSinceLastTime)/totalTicksSinceLastTime : 0);
      _previousTotalTicks = totalTicks;
      _previousIdleTicks  = idleTicks;
      return ret;
    }
    
    0 讨论(0)
  • 2020-11-21 12:18

    QNX

    Since this is like a "wikipage of code" I want to add some code from the QNX Knowledge base (note: this is not my work, but I checked it and it works fine on my system):

    How to get CPU usage in %: http://www.qnx.com/support/knowledgebase.html?id=50130000000P9b5

    #include <atomic.h>
    #include <libc.h>
    #include <pthread.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/iofunc.h>
    #include <sys/neutrino.h>
    #include <sys/resmgr.h>
    #include <sys/syspage.h>
    #include <unistd.h>
    #include <inttypes.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/debug.h>
    #include <sys/procfs.h>
    #include <sys/syspage.h>
    #include <sys/neutrino.h>
    #include <sys/time.h>
    #include <time.h>
    #include <fcntl.h>
    #include <devctl.h>
    #include <errno.h>
    
    #define MAX_CPUS 32
    
    static float Loads[MAX_CPUS];
    static _uint64 LastSutime[MAX_CPUS];
    static _uint64 LastNsec[MAX_CPUS];
    static int ProcFd = -1;
    static int NumCpus = 0;
    
    
    int find_ncpus(void) {
        return NumCpus;
    }
    
    int get_cpu(int cpu) {
        int ret;
        ret = (int)Loads[ cpu % MAX_CPUS ];
        ret = max(0,ret);
        ret = min(100,ret);
        return( ret );
    }
    
    static _uint64 nanoseconds( void ) {
        _uint64 sec, usec;
        struct timeval tval;
        gettimeofday( &tval, NULL );
        sec = tval.tv_sec;
        usec = tval.tv_usec;
        return( ( ( sec * 1000000 ) + usec ) * 1000 );
    }
    
    int sample_cpus( void ) {
        int i;
        debug_thread_t debug_data;
        _uint64 current_nsec, sutime_delta, time_delta;
        memset( &debug_data, 0, sizeof( debug_data ) );
        
        for( i=0; i<NumCpus; i++ ) {
            /* Get the sutime of the idle thread #i+1 */
            debug_data.tid = i + 1;
            devctl( ProcFd, DCMD_PROC_TIDSTATUS,
            &debug_data, sizeof( debug_data ), NULL );
            /* Get the current time */
            current_nsec = nanoseconds();
            /* Get the deltas between now and the last samples */
            sutime_delta = debug_data.sutime - LastSutime[i];
            time_delta = current_nsec - LastNsec[i];
            /* Figure out the load */
            Loads[i] = 100.0 - ( (float)( sutime_delta * 100 ) / (float)time_delta );
            /* Flat out strange rounding issues. */
            if( Loads[i] < 0 ) {
                Loads[i] = 0;
            }
            /* Keep these for reference in the next cycle */
            LastNsec[i] = current_nsec;
            LastSutime[i] = debug_data.sutime;
        }
        return EOK;
    }
    
    int init_cpu( void ) {
        int i;
        debug_thread_t debug_data;
        memset( &debug_data, 0, sizeof( debug_data ) );
    /* Open a connection to proc to talk over.*/
        ProcFd = open( "/proc/1/as", O_RDONLY );
        if( ProcFd == -1 ) {
            fprintf( stderr, "pload: Unable to access procnto: %s\n",strerror( errno ) );
            fflush( stderr );
            return -1;
        }
        i = fcntl(ProcFd,F_GETFD);
        if(i != -1){
            i |= FD_CLOEXEC;
            if(fcntl(ProcFd,F_SETFD,i) != -1){
                /* Grab this value */
                NumCpus = _syspage_ptr->num_cpu;
                /* Get a starting point for the comparisons */
                for( i=0; i<NumCpus; i++ ) {
                    /*
                    * the sutime of idle thread is how much
                    * time that thread has been using, we can compare this
                    * against how much time has passed to get an idea of the
                    * load on the system.
                    */
                    debug_data.tid = i + 1;
                    devctl( ProcFd, DCMD_PROC_TIDSTATUS, &debug_data, sizeof( debug_data ), NULL );
                    LastSutime[i] = debug_data.sutime;
                    LastNsec[i] = nanoseconds();
                }
                return(EOK);
            }
        }
        close(ProcFd);
        return(-1);
    }
    
    void close_cpu(void){
        if(ProcFd != -1){
            close(ProcFd);
            ProcFd = -1;
        }
    }
    
    int main(int argc, char* argv[]){
        int i,j;
        init_cpu();
        printf("System has: %d CPUs\n", NumCpus);
        for(i=0; i<20; i++) {
            sample_cpus();
            for(j=0; j<NumCpus;j++)
            printf("CPU #%d: %f\n", j, Loads[j]);
            sleep(1);
        }
        close_cpu();
    }
    

    How to get the free (!) memory: http://www.qnx.com/support/knowledgebase.html?id=50130000000mlbx

    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <err.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    
    int main( int argc, char *argv[] ){
        struct stat statbuf;
        paddr_t freemem;
        stat( "/proc", &statbuf );
        freemem = (paddr_t)statbuf.st_size;
        printf( "Free memory: %d bytes\n", freemem );
        printf( "Free memory: %d KB\n", freemem / 1024 );
        printf( "Free memory: %d MB\n", freemem / ( 1024 * 1024 ) );
        return 0;
    } 
    
    0 讨论(0)
  • 2020-11-21 12:21

    Linux

    In Linux, this information is available in the /proc file system. I'm not a big fan of the text file format used, as each Linux distribution seems to customize at least one important file. A quick look as the source to 'ps' reveals the mess.

    But here is where to find the information you seek:

    /proc/meminfo contains the majority of the system-wide information you seek. Here it looks like on my system; I think you are interested in MemTotal, MemFree, SwapTotal, and SwapFree:

    Anderson cxc # more /proc/meminfo
    MemTotal:      4083948 kB
    MemFree:       2198520 kB
    Buffers:         82080 kB
    Cached:        1141460 kB
    SwapCached:          0 kB
    Active:        1137960 kB
    Inactive:       608588 kB
    HighTotal:     3276672 kB
    HighFree:      1607744 kB
    LowTotal:       807276 kB
    LowFree:        590776 kB
    SwapTotal:     2096440 kB
    SwapFree:      2096440 kB
    Dirty:              32 kB
    Writeback:           0 kB
    AnonPages:      523252 kB
    Mapped:          93560 kB
    Slab:            52880 kB
    SReclaimable:    24652 kB
    SUnreclaim:      28228 kB
    PageTables:       2284 kB
    NFS_Unstable:        0 kB
    Bounce:              0 kB
    CommitLimit:   4138412 kB
    Committed_AS:  1845072 kB
    VmallocTotal:   118776 kB
    VmallocUsed:      3964 kB
    VmallocChunk:   112860 kB
    HugePages_Total:     0
    HugePages_Free:      0
    HugePages_Rsvd:      0
    Hugepagesize:     2048 kB
    

    For CPU utilization, you have to do a little work. Linux makes available overall CPU utilization since system start; this probably isn't what you are interested in. If you want to know what the CPU utilization was for the last second, or 10 seconds, then you need to query the information and calculate it yourself.

    The information is available in /proc/stat, which is documented pretty well at http://www.linuxhowtos.org/System/procstat.htm; here is what it looks like on my 4-core box:

    Anderson cxc #  more /proc/stat
    cpu  2329889 0 2364567 1063530460 9034 9463 96111 0
    cpu0 572526 0 636532 265864398 2928 1621 6899 0
    cpu1 590441 0 531079 265949732 4763 351 8522 0
    cpu2 562983 0 645163 265796890 682 7490 71650 0
    cpu3 603938 0 551790 265919440 660 0 9040 0
    intr 37124247
    ctxt 50795173133
    btime 1218807985
    processes 116889
    procs_running 1
    procs_blocked 0
    

    First, you need to determine how many CPUs (or processors, or processing cores) are available in the system. To do this, count the number of 'cpuN' entries, where N starts at 0 and increments. Don't count the 'cpu' line, which is a combination of the cpuN lines. In my example, you can see cpu0 through cpu3, for a total of 4 processors. From now on, you can ignore cpu0..cpu3, and focus only on the 'cpu' line.

    Next, you need to know that the fourth number in these lines is a measure of idle time, and thus the fourth number on the 'cpu' line is the total idle time for all processors since boot time. This time is measured in Linux "jiffies", which are 1/100 of a second each.

    But you don't care about the total idle time; you care about the idle time in a given period, e.g., the last second. Do calculate that, you need to read this file twice, 1 second apart.Then you can do a diff of the fourth value of the line. For example, if you take a sample and get:

    cpu  2330047 0 2365006 1063853632 9035 9463 96114 0
    

    Then one second later you get this sample:

    cpu  2330047 0 2365007 1063854028 9035 9463 96114 0
    

    Subtract the two numbers, and you get a diff of 396, which means that your CPU had been idle for 3.96 seconds out of the last 1.00 second. The trick, of course, is that you need to divide by the number of processors. 3.96 / 4 = 0.99, and there is your idle percentage; 99% idle, and 1% busy.

    In my code, I have a ring buffer of 360 entries, and I read this file every second. That lets me quickly calculate the CPU utilization for 1 second, 10 seconds, etc., all the way up to 1 hour.

    For the process-specific information, you have to look in /proc/pid; if you don't care abut your pid, you can look in /proc/self.

    CPU used by your process is available in /proc/self/stat. This is an odd-looking file consisting of a single line; for example:

    19340 (whatever) S 19115 19115 3084 34816 19115 4202752 118200 607 0 0 770 384 2
     7 20 0 77 0 266764385 692477952 105074 4294967295 134512640 146462952 321468364
    8 3214683328 4294960144 0 2147221247 268439552 1276 4294967295 0 0 17 0 0 0 0
    

    The important data here are the 13th and 14th tokens (0 and 770 here). The 13th token is the number of jiffies that the process has executed in user mode, and the 14th is the number of jiffies that the process has executed in kernel mode. Add the two together, and you have its total CPU utilization.

    Again, you will have to sample this file periodically, and calculate the diff, in order to determine the process's CPU usage over time.

    Edit: remember that when you calculate your process's CPU utilization, you have to take into account 1) the number of threads in your process, and 2) the number of processors in the system. For example, if your single-threaded process is using only 25% of the CPU, that could be good or bad. Good on a single-processor system, but bad on a 4-processor system; this means that your process is running constantly, and using 100% of the CPU cycles available to it.

    For the process-specific memory information, you ahve to look at /proc/self/status, which looks like this:

    Name:   whatever
    State:  S (sleeping)
    Tgid:   19340
    Pid:    19340
    PPid:   19115
    TracerPid:      0
    Uid:    0       0       0       0
    Gid:    0       0       0       0
    FDSize: 256
    Groups: 0 1 2 3 4 6 10 11 20 26 27
    VmPeak:   676252 kB
    VmSize:   651352 kB
    VmLck:         0 kB
    VmHWM:    420300 kB
    VmRSS:    420296 kB
    VmData:   581028 kB
    VmStk:       112 kB
    VmExe:     11672 kB
    VmLib:     76608 kB
    VmPTE:      1244 kB
    Threads:        77
    SigQ:   0/36864
    SigPnd: 0000000000000000
    ShdPnd: 0000000000000000
    SigBlk: fffffffe7ffbfeff
    SigIgn: 0000000010001000
    SigCgt: 20000001800004fc
    CapInh: 0000000000000000
    CapPrm: 00000000ffffffff
    CapEff: 00000000fffffeff
    Cpus_allowed:   0f
    Mems_allowed:   1
    voluntary_ctxt_switches:        6518
    nonvoluntary_ctxt_switches:     6598
    

    The entries that start with 'Vm' are the interesting ones:

    • VmPeak is the maximum virtual memory space used by the process, in kB (1024 bytes).
    • VmSize is the current virtual memory space used by the process, in kB. In my example, it's pretty large: 651,352 kB, or about 636 megabytes.
    • VmRss is the amount of memory that have been mapped into the process' address space, or its resident set size. This is substantially smaller (420,296 kB, or about 410 megabytes). The difference: my program has mapped 636 MB via mmap(), but has only accessed 410 MB of it, and thus only 410 MB of pages have been assigned to it.

    The only item I'm not sure about is Swapspace currently used by my process. I don't know if this is available.

    0 讨论(0)
  • 2020-11-21 12:23

    Linux

    A portable way of reading memory and load numbers is the sysinfo call

    Usage

       #include <sys/sysinfo.h>
    
       int sysinfo(struct sysinfo *info);
    

    DESCRIPTION

       Until Linux 2.3.16, sysinfo() used to return information in the
       following structure:
    
           struct sysinfo {
               long uptime;             /* Seconds since boot */
               unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
               unsigned long totalram;  /* Total usable main memory size */
               unsigned long freeram;   /* Available memory size */
               unsigned long sharedram; /* Amount of shared memory */
               unsigned long bufferram; /* Memory used by buffers */
               unsigned long totalswap; /* Total swap space size */
               unsigned long freeswap;  /* swap space still available */
               unsigned short procs;    /* Number of current processes */
               char _f[22];             /* Pads structure to 64 bytes */
           };
    
       and the sizes were given in bytes.
    
       Since Linux 2.3.23 (i386), 2.3.48 (all architectures) the structure
       is:
    
           struct sysinfo {
               long uptime;             /* Seconds since boot */
               unsigned long loads[3];  /* 1, 5, and 15 minute load averages */
               unsigned long totalram;  /* Total usable main memory size */
               unsigned long freeram;   /* Available memory size */
               unsigned long sharedram; /* Amount of shared memory */
               unsigned long bufferram; /* Memory used by buffers */
               unsigned long totalswap; /* Total swap space size */
               unsigned long freeswap;  /* swap space still available */
               unsigned short procs;    /* Number of current processes */
               unsigned long totalhigh; /* Total high memory size */
               unsigned long freehigh;  /* Available high memory size */
               unsigned int mem_unit;   /* Memory unit size in bytes */
               char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding to 64 bytes */
           };
    
       and the sizes are given as multiples of mem_unit bytes.
    
    0 讨论(0)
提交回复
热议问题