VirtualBox

Changeset 9904 in vbox for trunk/src/VBox/Runtime/r3


Ignore:
Timestamp:
Jun 25, 2008 11:03:03 AM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
32323
Message:

Global and per-VM CPU usage API added

Location:
trunk/src/VBox/Runtime/r3
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/os2/system-os2.cpp

    r8245 r9904  
    6666}
    6767
     68RTDECL(int) RTSystemProcessorGetUsageStats(PRTCPUUSAGESTATS pStats)
     69{
     70    /* @todo Implement! */
     71    return VERR_NOT_IMPLEMENTED;
     72}
     73
     74RTDECL(int) RTProcessGetProcessorUsageStats(RTPROCESS pid, PRTPROCCPUUSAGESTATS pStats)
     75{
     76    /* @todo Implement! */
     77    return VERR_NOT_IMPLEMENTED;
     78}
     79
  • trunk/src/VBox/Runtime/r3/posix/system-posix.cpp

    r8245 r9904  
    3535#include <iprt/system.h>
    3636#include <iprt/assert.h>
     37#include <iprt/err.h>
     38#include <iprt/string.h>
     39#include <iprt/mem.h>
    3740
    3841#include <unistd.h>
     42#include <stdio.h>
    3943#if !defined(RT_OS_SOLARIS)
    4044# include <sys/sysctl.h>
     
    8892}
    8993
     94/**
     95 * Gets the current figures of overall system processor usage.
     96 * 
     97 * @remarks To get meaningful stats this function has to be
     98 *          called twice with a bit of delay between calls. This
     99 *          is due to the fact that at least two samples of
     100 *          system usage stats are needed to calculate the load.
     101 * 
     102 * @returns None.
     103 */
     104RTDECL(int) RTSystemProcessorGetUsageStats(PRTCPUUSAGESTATS pStats)
     105{
     106    int rc = VINF_SUCCESS;
     107    uint32_t u32UserNow, u32NiceNow, u32SystemNow, u32IdleNow;
     108    uint32_t u32UserDelta, u32SystemDelta, u32IdleDelta, u32BusyDelta, u32TotalDelta;
     109    FILE *f = fopen("/proc/stat", "r");
     110
     111    if (f)
     112    {
     113        if (fscanf(f, "cpu %u %u %u %u", &u32UserNow, &u32NiceNow, &u32SystemNow, &u32IdleNow) == 4)
     114        {
     115            u32UserDelta   = (u32UserNow - pStats->u32RawUser) + (u32NiceNow - pStats->u32RawNice);
     116            u32SystemDelta = u32SystemNow - pStats->u32RawSystem;
     117            u32IdleDelta   = u32IdleNow - pStats->u32RawIdle;
     118            u32BusyDelta   = u32UserDelta + u32SystemDelta;
     119            u32TotalDelta  = u32BusyDelta + u32IdleDelta;
     120            pStats->u32User   = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32UserDelta / u32TotalDelta);
     121            pStats->u32System = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32SystemDelta / u32TotalDelta);
     122            pStats->u32Idle   = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32IdleDelta / u32TotalDelta);
     123            /* Update the base. */
     124            pStats->u32RawUser   = u32UserNow;
     125            pStats->u32RawNice   = u32NiceNow;
     126            pStats->u32RawSystem = u32SystemNow;
     127            pStats->u32RawIdle   = u32IdleNow;
     128        }
     129        else
     130            rc = VERR_FILE_IO_ERROR;
     131        fclose(f);
     132    }
     133    else
     134        rc = VERR_ACCESS_DENIED;
     135
     136    return rc;
     137}
     138
     139/**
     140 * Gets the current processor usage for a partucilar process.
     141 * 
     142 * @remarks To get meaningful stats this function has to be
     143 *          called twice with a bit of delay between calls. This
     144 *          is due to the fact that at least two samples of
     145 *          system usage stats are needed to calculate the load.
     146 * 
     147 * @returns None.
     148 */
     149RTDECL(int) RTProcessGetProcessorUsageStats(RTPROCESS pid, PRTPROCCPUUSAGESTATS pStats)
     150{
     151    int rc = VINF_SUCCESS;
     152    uint32_t u32UserNow, u32NiceNow, u32SystemNow, u32IdleNow;
     153    uint32_t u32UserDelta, u32SystemDelta;
     154    uint64_t u64TotalNow, u64TotalDelta;
     155    FILE *f = fopen("/proc/stat", "r");
     156
     157    if (f)
     158    {
     159        if (fscanf(f, "cpu %u %u %u %u", &u32UserNow, &u32NiceNow, &u32SystemNow, &u32IdleNow) == 4)
     160        {
     161            char *pszName;
     162            pid_t pid2;
     163            char c;
     164            int iTmp;
     165            unsigned uTmp;
     166            unsigned long ulTmp, ulUserNow, ulSystemNow;
     167            char buf[80]; /* @todo: this should be tied to max allowed proc name. */
     168
     169            u64TotalNow = (uint64_t)u32UserNow + u32NiceNow + u32SystemNow + u32IdleNow;
     170            fclose(f);
     171            RTStrAPrintf(&pszName, "/proc/%d/stat", pid);
     172            //printf("Opening %s...\n", pszName);
     173            f = fopen(pszName, "r");
     174            RTMemFree(pszName);
     175
     176            if (f)
     177            {
     178                if (fscanf(f, "%d %s %c %d %d %d %d %d %u %lu %lu %lu %lu %lu %lu",
     179                           &pid2, buf, &c, &iTmp, &iTmp, &iTmp, &iTmp, &iTmp, &uTmp,
     180                           &ulTmp, &ulTmp, &ulTmp, &ulTmp, &ulUserNow, &ulSystemNow) == 15)
     181                {
     182                    Assert((pid_t)pid == pid2);
     183                    u32UserDelta      = ulUserNow - pStats->u32RawProcUser;
     184                    u32SystemDelta    = ulSystemNow - pStats->u32RawProcSystem;
     185                    u64TotalDelta     = u64TotalNow - pStats->u64RawTotal;
     186                    pStats->u32User   = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32UserDelta / u64TotalDelta);
     187                    pStats->u32System = (uint32_t)(IPRT_USAGE_MULTIPLIER * u32SystemDelta / u64TotalDelta);
     188//                  printf("%d: user=%u%% system=%u%% / raw user=%u raw system=%u total delta=%u\n", pid,
     189//                         pStats->u32User / 10000000, pStats->u32System / 10000000,
     190//                         pStats->u32RawProcUser, pStats->u32RawProcSystem, u64TotalDelta);
     191                    /* Update the base. */
     192                    pStats->u32RawProcUser   = ulUserNow;
     193                    pStats->u32RawProcSystem = ulSystemNow;
     194                    pStats->u64RawTotal      = u64TotalNow;
     195//                  printf("%d: updated raw user=%u raw system=%u raw total=%u\n", pid,
     196//                         pStats->u32RawProcUser, pStats->u32RawProcSystem, pStats->u64RawTotal);
     197                }
     198                else
     199                    rc = VERR_FILE_IO_ERROR;
     200            }
     201            else
     202                rc = VERR_ACCESS_DENIED;
     203        }
     204        else
     205            rc = VERR_FILE_IO_ERROR;
     206        fclose(f);
     207    }
     208    else
     209        rc = VERR_ACCESS_DENIED;
     210
     211    return rc;
     212}
  • trunk/src/VBox/Runtime/r3/win/system-win.cpp

    r8245 r9904  
    6161}
    6262
     63RTDECL(int) RTSystemProcessorGetUsageStats(PRTCPUUSAGESTATS pStats)
     64{
     65    /* @todo Implement! */
     66    return VERR_NOT_IMPLEMENTED;
     67}
     68
     69RTDECL(int) RTProcessGetProcessorUsageStats(RTPROCESS pid, PRTPROCCPUUSAGESTATS pStats)
     70{
     71    /* @todo Implement! */
     72    return VERR_NOT_IMPLEMENTED;
     73}
     74
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette