VirtualBox

Changeset 9937

Show
Ignore:
Timestamp:
06/25/08 20:18:13 (5 months ago)
Author:
vboxsync
Message:

Main: Corrected getProcessorUsage() impl (docs, spacing etc).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/VBox/Main/HostImpl.cpp

    r9904 r9937  
    169169 
    170170#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    171     /* 
    172      * Start resource usage sampler. 
    173      */ 
    174     //printf("Creating sampling timer with Host::staticSamplerCallback, this=%p\n", this); 
    175     int rc = RTTimerCreate(&m_pUsageSampler, VBOX_USAGE_SAMPLER_INTERVAL, Host::staticSamplerCallback, this); 
    176     if (RT_FAILURE(rc)) 
    177     { 
    178         AssertMsgFailed(("Failed to create resource usage sampling timer, rc=%d!\n", rc)); 
    179         return E_FAIL; 
     171    /* Start resource usage sampler */ 
     172    { 
     173        int vrc = RTTimerCreate (&mUsageSampler, VBOX_USAGE_SAMPLER_INTERVAL, 
     174                                 UsageSamplerCallback, this); 
     175        AssertMsgRC (vrc, ("Failed to create resource usage sampling " 
     176                           "timer (%Rra)\n", vrc)); 
     177        if (RT_FAILURE (vrc)) 
     178            return E_FAIL; 
    180179    } 
    181180#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     
    184183    return S_OK; 
    185184} 
    186  
    187 #ifdef VBOX_WITH_RESOURCE_USAGE_API 
    188 void Host::staticSamplerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick) 
    189 { 
    190     ((Host*)pvUser)->usageSamplerCallback(); 
    191 } 
    192  
    193 void Host::usageSamplerCallback() 
    194 { 
    195     int rc = RTSystemProcessorGetUsageStats(&m_CpuStats); 
    196     if (RT_FAILURE(rc)) 
    197     { 
    198         AssertMsgFailed(("Failed to get CPU stats, rc=%d!\n", rc)); 
    199     } 
    200     //printf("Host::usageSamplerCallback: user=%u%% system=%u%% &m_CpuStats=%p this=%p\n", 
    201     //     m_CpuStats.u32User / 10000000, m_CpuStats.u32System / 10000000, &m_CpuStats, this); 
    202     //printf("user=%.2f system=%.2f idle=%.2f\n", m_CpuStats.u32User/10000000., m_CpuStats.u32System/10000000., m_CpuStats.u32Idle/10000000.); 
    203 } 
    204 #endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    205185 
    206186/** 
     
    231211 
    232212#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    233     /* 
    234      * Destroy resource usage sampler. 
    235      */ 
    236     int rc = RTTimerDestroy(m_pUsageSampler); 
    237     if (RT_FAILURE(rc)) 
    238     { 
    239         AssertMsgFailed(("Failed to destroy resource usage sampling timer, rc=%d!\n", rc)); 
     213    /* Destroy resource usage sampler */ 
     214    { 
     215        int vrc = RTTimerDestroy (mUsageSampler); 
     216        AssertMsgRC (vrc, ("Failed to destroy resource usage " 
     217                           "sampling timer (%Rra)\n", vrc)); 
    240218    } 
    241219#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     
    11281106} 
    11291107 
    1130 /** 
    1131  * Obtains the results of the latest measurement of overall CPU  
    1132  * usage on this host.  
    1133  *  
    1134  * @returns error code. 
    1135  * 
    1136  * @param   user      out   % of CPU time spent in user mode. 
    1137  * @param   system    out   % of CPU time spent in kernel mode. 
    1138  * @param   idle      out   % of idle CPU time. 
    1139  *  
    1140  */ 
    1141 STDMETHODIMP Host::GetProcessorUsage(ULONG *user, ULONG *system, ULONG *idle) 
     1108STDMETHODIMP Host::GetProcessorUsage (ULONG *aUser, ULONG *aSystem, ULONG *aIdle) 
    11421109{ 
    11431110#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    1144     *user   = m_CpuStats.u32User; 
    1145     *system = m_CpuStats.u32System; 
    1146     *idle   = m_CpuStats.u32Idle; 
     1111    if (aUser == NULL || aSystem == NULL || aIdle == NULL) 
     1112        return E_POINTER; 
     1113 
     1114    *aUser   = mCpuStats.u32User; 
     1115    *aSystem = mCpuStats.u32System; 
     1116    *aIdle   = mCpuStats.u32Idle; 
    11471117 
    11481118    return S_OK; 
     
    27452715#endif /* RT_OS_WINDOWS */ 
    27462716 
     2717#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     2718 
     2719/* static */ 
     2720void Host::UsageSamplerCallback (PRTTIMER pTimer, void *pvUser, uint64_t iTick) 
     2721{ 
     2722    AssertReturnVoid (pvUser != NULL); 
     2723    static_cast <Host *> (pvUser)->usageSamplerCallback(); 
     2724} 
     2725 
     2726void Host::usageSamplerCallback() 
     2727{ 
     2728    int vrc = RTSystemProcessorGetUsageStats (&mCpuStats); 
     2729    AssertMsgRC (vrc, ("Failed to get CPU stats (%Rra)\n", vrc)); 
     2730 
     2731//  LogFlowThisFunc (("user=%u%% system=%u%% &mCpuStats=%p\n", 
     2732//                    mCpuStats.u32User / 10000000, 
     2733//                    mCpuStats.u32System / 10000000, &mCpuStats); 
     2734//  LogFlowThisFunc (("user=%.2f system=%.2f idle=%.2f\n", mCpuStats.u32User/10000000., 
     2735//                    mCpuStats.u32System/10000000., mCpuStats.u32Idle/10000000.); 
     2736} 
     2737 
     2738#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     2739 
  • trunk/src/VBox/Main/MachineImpl.cpp

    r9904 r9937  
    147147    mSession.mPid = NIL_RTPROCESS; 
    148148    mSession.mState = SessionState_Closed; 
     149 
     150#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     151    mUsageSampler = NULL; 
     152#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    149153} 
    150154 
     
    498502 
    499503#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    500     /* 
    501      * Start resource usage sampler. 
    502      */ 
    503     printf("Creating sampling timer with Machine::staticSamplerCallback, this=%p\n", this); 
    504     int result = RTTimerCreate(&m_pUsageSampler, VBOX_USAGE_SAMPLER_INTERVAL, Machine::staticSamplerCallback, this); 
    505     if (RT_FAILURE(result)) 
    506     { 
    507         AssertMsgFailed(("Failed to create resource usage sampling timer, rc=%d!\n", result)); 
    508         rc = E_FAIL; 
     504    /* Start resource usage sampler */ 
     505    { 
     506        vrc = RTTimerCreate (&mData->mUsageSampler, VBOX_USAGE_SAMPLER_INTERVAL, 
     507                             Machine::UsageSamplerCallback, this); 
     508        AssertMsgRC (vrc, ("Failed to create resource usage " 
     509                            "sampling timer(%Rra)\n", vrc)); 
     510        if (RT_FAILURE (vrc)) 
     511            rc = E_FAIL; 
    509512    } 
    510513#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     
    635638 
    636639#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    637     /* 
    638      * Destroy resource usage sampler. 
    639      */ 
    640     int rc = RTTimerDestroy(m_pUsageSampler); 
    641     if (RT_FAILURE(rc)) 
    642     { 
    643         AssertMsgFailed(("Failed to destroy resource usage sampling timer, rc=%d!\n", rc)); 
     640    /* Destroy resource usage sampler */ 
     641    { 
     642        int vrc = RTTimerDestroy (mData->mUsageSampler); 
     643        AssertMsgRC (vrc, ("Failed to destroy resource usage " 
     644                           "sampling timer (%Rra)\n", vrc)); 
    644645    } 
    645646#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     
    702703    LogFlowThisFuncLeave(); 
    703704} 
    704  
    705 #ifdef VBOX_WITH_RESOURCE_USAGE_API 
    706 void Machine::staticSamplerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick) 
    707 { 
    708     ((Machine*)pvUser)->usageSamplerCallback(); 
    709 } 
    710  
    711 void Machine::usageSamplerCallback() 
    712 { 
    713     //LogFlowThisFunc(("mData->mSession.mPid = %u &m_CpuStats = %p)\n", mData->mSession.mPid, &m_CpuStats)); 
    714     if (mData->mSession.mPid != NIL_RTPROCESS) { 
    715         int rc = RTProcessGetProcessorUsageStats(mData->mSession.mPid, &m_CpuStats); 
    716         if (RT_FAILURE(rc)) 
    717         { 
    718             AssertMsgFailed(("Failed to get CPU stats, rc=%d!\n", rc)); 
    719         } 
    720     } 
    721     else 
    722     { 
    723         m_CpuStats.u32User   = 0; 
    724         m_CpuStats.u32System = 0; 
    725     } 
    726 //  printf("Machine::usageSamplerCallback: user=%u%% system=%u%% &m_CpuStats=%p this=%p\n", 
    727 //         m_CpuStats.u32User / 10000000, m_CpuStats.u32System / 10000000, &m_CpuStats, this); 
    728     //printf("user=%.2f system=%.2f\n", m_CpuStats.u32User/10000000., m_CpuStats.u32System/10000000.); 
    729 } 
    730 #endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    731  
    732705 
    733706// IMachine properties 
     
    27112684 * session.  Otherwise read the value from machine extra data, where it is 
    27122685 * stored between sessions. 
    2713  *  
     2686 * 
    27142687 * @note since the way this method is implemented depends on whether or not 
    27152688 *       a session is currently open, we grab a write lock on the object, in 
    27162689 *       order to ensure that the session state does not change during the 
    27172690 *       call, and to force us to block if it is currently changing (either 
    2718  *       way) between open and closed.  
     2691 *       way) between open and closed. 
    27192692 */ 
    27202693STDMETHODIMP Machine::GetConfigRegistryValue (INPTR BSTR aKey, BSTR *aValue) 
     
    27822755 * session.  Otherwise read the value from machine extra data, where it is 
    27832756 * stored between sessions. 
    2784  *  
     2757 * 
    27852758 * @note since the way this method is implemented depends on whether or not 
    27862759 *       a session is currently open, we grab a write lock on the object, in 
    27872760 *       order to ensure that the session state does not change during the 
    27882761 *       call, and to force us to block if it is currently changing (either 
    2789  *       way) between open and closed.  
     2762 *       way) between open and closed. 
    27902763 */ 
    27912764STDMETHODIMP Machine::SetConfigRegistryValue (INPTR BSTR aKey, INPTR BSTR aValue) 
     
    28442817} 
    28452818 
    2846 /** 
    2847  * Obtains the results of the latest measurement of CPU usage by  
    2848  * this machine.  
    2849  *  
    2850  * @returns error code. 
    2851  * 
    2852  * @param   user      out   % of CPU time spent in user mode. 
    2853  * @param   system    out   % of CPU time spent in kernel mode. 
    2854  *  
    2855  */ 
    2856 STDMETHODIMP Machine::GetProcessorUsage(ULONG *user, ULONG *system) 
     2819STDMETHODIMP Machine::GetProcessorUsage (ULONG *aUser, ULONG *aSystem) 
    28572820{ 
    28582821#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    2859     *user   = m_CpuStats.u32User; 
    2860     *system = m_CpuStats.u32System; 
    2861 //  printf("Machine::GetProcessorUsage: user=%u%% system=%u%% &m_CpuStats=%p this=%p\n", 
    2862 //         m_CpuStats.u32User / 10000000, m_CpuStats.u32System / 10000000, &m_CpuStats, this); 
     2822    if (aUser == NULL || aSystem == NULL) 
     2823        return E_POINTER; 
     2824 
     2825//  LogFlowThisFunc (("user=%u%% system=%u%% &mData->mCpuStats=%p\n", 
     2826//                    mData->mCpuStats.u32User / 10000000, 
     2827//                    mData->mCpuStats.u32System / 10000000, &mData->mCpuStats)); 
     2828 
     2829    *aUser   = mData->mCpuStats.u32User; 
     2830    *aSystem = mData->mCpuStats.u32System; 
    28632831    return S_OK; 
    28642832#else /* !VBOX_WITH_RESOURCE_USAGE_API */ 
     
    73547322} 
    73557323 
     7324#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     7325 
     7326/* static */ 
     7327void Machine::UsageSamplerCallback (PRTTIMER pTimer, void *pvUser, uint64_t iTick) 
     7328{ 
     7329    AssertReturnVoid (pvUser != NULL); 
     7330    static_cast <Machine *> (pvUser)->usageSamplerCallback(); 
     7331} 
     7332 
     7333void Machine::usageSamplerCallback() 
     7334{ 
     7335//  LogFlowThisFunc (("mData->mSession.mPid = %u &mData->mCpuStats = %p)\n", 
     7336//                    mData->mSession.mPid, &m_CpuStats)); 
     7337    if (mData->mSession.mPid != NIL_RTPROCESS) 
     7338    { 
     7339        int vrc = RTProcessGetProcessorUsageStats (mData->mSession.mPid, 
     7340                                                   &mData->mCpuStats); 
     7341        AssertMsgRC (vrc, ("Failed to get CPU stats (%Rra)\n", vrc)); 
     7342    } 
     7343    else 
     7344    { 
     7345        mData->mCpuStats.u32User   = 0; 
     7346        mData->mCpuStats.u32System = 0; 
     7347    } 
     7348//  LogFlowThisFunc (("user=%u%% system=%u%% &mData->mCpuStats=%p\n", 
     7349//                    mData->mCpuStats.u32User / 10000000, 
     7350//                    mData->mCpuStats.u32System / 10000000, &mData->mCpuStats)); 
     7351//  LogFlowThisFunc (("user=%.2f system=%.2f\n", 
     7352//                    mData->mCpuStats.u32User/10000000., mData->mCpuStats.u32System/10000000.)); 
     7353} 
     7354 
     7355#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     7356 
    73567357///////////////////////////////////////////////////////////////////////////// 
    73577358// SessionMachine class 
     
    86068607} 
    86078608 
    8608 /** 
    8609  * Obtains the results of the latest measurement of CPU usage by  
    8610  * this machine.  
    8611  *  
    8612  * @returns error code. 
    8613  * 
    8614  * @param   user      out   % of CPU time spent in user mode. 
    8615  * @param   system    out   % of CPU time spent in kernel mode. 
    8616  *  
    8617  */ 
    8618 STDMETHODIMP SessionMachine::GetProcessorUsage(ULONG *user, ULONG *system) 
    8619 { 
    8620     return mPeer->GetProcessorUsage(user, system); 
    8621 } 
    8622  
    86238609// public methods only for internal purposes 
    86248610///////////////////////////////////////////////////////////////////////////// 
  • trunk/src/VBox/Main/idl/VirtualBox.xidl

    r9904 r9937  
    34673467      </param> 
    34683468    </method> 
    3469      
     3469 
    34703470    <method name="getConfigRegistryValue"> 
    34713471      <desc> 
     
    35063506    <method name="getProcessorUsage"> 
    35073507      <desc> 
    3508         Returns the current processor usage measured over all cores of all 
    3509         processors in the host system.  
    3510  
    3511         <note> 
    3512           The maximum value is 1000000000 which means that all cores of all CPUs  
    3513           are completely used. 
    3514         </note> 
     3508        Returns the current processor usage by this virtual machine measured 
     3509        over all cores of all processors in the host system. 
     3510 
     3511        The values returned for each parameter are in range from <tt>0</tt> (the 
     3512        machine is powered off or does not load the CPUs at all) to 
     3513        <tt>1 000 000 000</tt> (all cores of all CPUs are fully in use by this 
     3514        machine). 
    35153515      </desc> 
    35163516      <param name="user" type="unsigned long" dir="out"> 
    3517         <desc>The pecentage of processor time spent executing in user  
    3518           mode. 
     3517        <desc> 
     3518          Pecentage of processor time spent executing in user mode. 
    35193519        </desc> 
    35203520      </param> 
    35213521      <param name="system" type="unsigned long" dir="out"> 
    3522         <desc>The pecentage of processor time spent executing in kernel 
    3523           mode. 
     3522        <desc> 
     3523          Pecentage of processor time spent executing in kernel mode. 
    35243524        </desc> 
    35253525      </param> 
     
    50445044    <method name="getProcessorUsage"> 
    50455045      <desc> 
    5046         Returns the current processor usage measured over all cores of all 
    5047         processors in the host system.  
    5048  
    5049         <note> 
    5050           The maximum value is 1000000000 which means that all cores of all CPUs  
     5046        Returns the processor usage by the whole host system measured over all 
     5047        cores of all processors of the host machine. 
     5048 
     5049        The values returned for each parameter are in range from <tt>0</tt> (the 
     5050        machine is powered off or doesn't load the CPUs at all) to 
     5051        <tt>1 000 000 000</tt> (all cores of all CPUs are fully loaded by this 
     5052        machine). 
     5053 
     5054        <note> 
     5055          The maximum value is 1000000000 which means that all cores of all CPUs 
    50515056          are completely used. 
    50525057        </note> 
    50535058      </desc> 
    50545059      <param name="user" type="unsigned long" dir="out"> 
    5055         <desc>The pecentage of processor time spent executing in user  
    5056           mode. 
     5060        <desc> 
     5061          Pecentage of processor time spent executing in user mode. 
    50575062        </desc> 
    50585063      </param> 
    50595064      <param name="system" type="unsigned long" dir="out"> 
    5060         <desc>The pecentage of processor time spent executing in kernel 
    5061           mode. 
     5065        <desc> 
     5066          Pecentage of processor time spent executing in kernel mode. 
    50625067        </desc> 
    50635068      </param> 
    50645069      <param name="idle" type="unsigned long" dir="out"> 
    5065         <desc>The pecentage of processor time spent doing nothing. 
     5070        <desc> 
     5071          Pecentage of processor time spent doing nothing. 
    50665072        </desc> 
    50675073      </param> 
  • trunk/src/VBox/Main/include/HostImpl.h

    r9904 r9937  
    110110    STDMETHOD(RemoveUSBDeviceFilter) (ULONG aPosition, IHostUSBDeviceFilter **aFilter); 
    111111 
    112     STDMETHOD(GetProcessorUsage) (ULONG *user, ULONG *system, ULONG *idle); 
     112    STDMETHOD(GetProcessorUsage) (ULONG *aUser, ULONG *aSystem, ULONG *aIdle); 
    113113 
    114114    // public methods only for internal purposes 
     
    170170#endif 
    171171 
     172#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     173    /** Static timer callback. */ 
     174    static void UsageSamplerCallback (PRTTIMER pTimer, void *pvUser, uint64_t iTick); 
     175    /** Member timer callback. */ 
     176    void usageSamplerCallback(); 
     177#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     178 
    172179    ComObjPtr <VirtualBox, ComWeakRef> mParent; 
    173180 
     
    180187 
    181188#ifdef VBOX_WITH_RESOURCE_USAGE_API 
    182     /** Static timer callback. */ 
    183     static void staticSamplerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick); 
    184     /** Member timer callback. */ 
    185     void usageSamplerCallback(); 
    186  
    187189    /** Pointer to the usage sampling timer. */ 
    188     PRTTIMER m_pUsageSampler; 
    189     /** Time stamp of the last taken sample. */ 
    190     //uint64_t m_tsLastSampleTaken; 
     190    PRTTIMER mUsageSampler; 
    191191    /** Structure to hold processor usage stats. */ 
    192     RTCPUUSAGESTATS m_CpuStats; 
     192    RTCPUUSAGESTATS mCpuStats; 
    193193#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    194194}; 
  • trunk/src/VBox/Main/include/MachineImpl.h

    r9904 r9937  
    168168        ComObjPtr <Snapshot> mFirstSnapshot; 
    169169        ComObjPtr <Snapshot> mCurrentSnapshot; 
     170 
     171#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     172        /** Pointer to the usage sampling timer. */ 
     173        PRTTIMER mUsageSampler; 
     174        /** Structure to hold processor usage stats. */ 
     175        RTPROCCPUUSAGESTATS mCpuStats; 
     176#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    170177    }; 
    171178 
     
    713720    void copyFrom (Machine *aThat); 
    714721 
     722#ifdef VBOX_WITH_RESOURCE_USAGE_API 
     723    /** Static timer callback. */ 
     724    static void UsageSamplerCallback (PRTTIMER pTimer, void *pvUser, uint64_t iTick); 
     725    /** Member timer callback. */ 
     726    void usageSamplerCallback(); 
     727#endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
     728 
    715729    const InstanceType mType; 
    716730 
     
    745759    friend class SessionMachine; 
    746760    friend class SnapshotMachine; 
    747  
    748 #ifdef VBOX_WITH_RESOURCE_USAGE_API 
    749     /** Static timer callback. */ 
    750     static void staticSamplerCallback(PRTTIMER pTimer, void *pvUser, uint64_t iTick); 
    751     /** Member timer callback. */ 
    752     void usageSamplerCallback(); 
    753     /** Pointer to the usage sampling timer. */ 
    754     PRTTIMER m_pUsageSampler; 
    755     /** Structure to hold processor usage stats. */ 
    756     RTPROCCPUUSAGESTATS m_CpuStats; 
    757 #endif /* VBOX_WITH_RESOURCE_USAGE_API */ 
    758761}; 
    759762 
     
    826829        IConsole *aInitiator, MachineState_T *aMachineState, IProgress **aProgress); 
    827830 
    828     /* We need to override and call real Machine's method. */ 
    829     STDMETHOD(GetProcessorUsage) (ULONG *user, ULONG *system); 
    830  
    831831    // public methods only for internal purposes 
    832832 

© 2008 Sun Microsystems, Inc.
ContactPrivacy policy