VirtualBox

Changeset 32912 in vbox for trunk/src/VBox/VMM


Ignore:
Timestamp:
Oct 5, 2010 1:04:39 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
66395
Message:

First sketch for guest execution capping

Location:
trunk/src/VBox/VMM
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/VMM/EM.cpp

    r32173 r32912  
    6464#include <iprt/string.h>
    6565#include <iprt/stream.h>
     66#include <iprt/thread.h>
    6667
    6768
     
    144145        pVCpu->em.s.pPatmGCState = PATMR3QueryGCStateHC(pVM);
    145146        AssertMsg(pVCpu->em.s.pPatmGCState, ("PATMR3QueryGCStateHC failed!\n"));
     147
     148        /* Force reset of the time slice. */
     149        pVCpu->em.s.u64TimeSliceStart = 0;
    146150
    147151# define EM_REG_COUNTER(a, b, c) \
     
    388392        EM_REG_COUNTER(&pVCpu->em.s.StatForcedActions,     "/PROF/CPU%d/EM/ForcedActions",     "Profiling forced action execution.");
    389393        EM_REG_COUNTER(&pVCpu->em.s.StatHalted,            "/PROF/CPU%d/EM/Halted",            "Profiling halted state (VMR3WaitHalted).");
     394        EM_REG_COUNTER(&pVCpu->em.s.StatCapped,            "/PROF/CPU%d/EM/Capped",            "Profiling capped state (sleep).");
    390395        EM_REG_COUNTER(&pVCpu->em.s.StatREMTotal,          "/PROF/CPU%d/EM/REMTotal",          "Profiling emR3RemExecute (excluding FFs).");
    391396        EM_REG_COUNTER(&pVCpu->em.s.StatRAWTotal,          "/PROF/CPU%d/EM/RAWTotal",          "Profiling emR3RawExecute (excluding FFs).");
     
    16591664    Assert(rcIrq == VINF_SUCCESS || rcIrq == rc);
    16601665    return rc;
     1666}
     1667
     1668
     1669/**
     1670 * Check if the preset execution time cap restricts guest execution scheduling.
     1671 *
     1672 * @returns true if allowed, false otherwise
     1673 * @param   pVM         The VM to operate on.
     1674 * @param   pVCpu       The VMCPU to operate on.
     1675 *
     1676 */
     1677bool emR3IsExecutionAllowed(PVM pVM, PVMCPU pVCpu)
     1678{
     1679    uint64_t u64UserTime, u64KernelTime;
     1680
     1681    if (    pVM->uCpuExecutionCap != 100
     1682        &&  RT_SUCCESS(RTThreadGetExecutionTimeMilli(RTThreadSelf(), &u64KernelTime, &u64UserTime)))
     1683    {
     1684        uint64_t u64TimeNow = RTTimeMilliTS();
     1685        if (pVCpu->em.s.u64TimeSliceStart + EM_TIME_SLICE < u64TimeNow)
     1686        {
     1687            /* New time slice. */
     1688            pVCpu->em.s.u64TimeSliceStart     = u64TimeNow;
     1689            pVCpu->em.s.u64TimeSliceStartExec = u64KernelTime + u64UserTime;
     1690            pVCpu->em.s.u64TimeSliceExec      = 0;
     1691        }
     1692        pVCpu->em.s.u64TimeSliceExec = u64KernelTime + u64UserTime - pVCpu->em.s.u64TimeSliceStartExec;
     1693
     1694        if (pVCpu->em.s.u64TimeSliceExec >= (EM_TIME_SLICE * 100) / pVM->uCpuExecutionCap)
     1695            return false;
     1696    }
     1697    return true;
    16611698}
    16621699
     
    19632000                 */
    19642001                case EMSTATE_RAW:
    1965                     rc = emR3RawExecute(pVM, pVCpu, &fFFDone);
     2002                    if (emR3IsExecutionAllowed(pVM, pVCpu))
     2003                    {
     2004                        rc = emR3RawExecute(pVM, pVCpu, &fFFDone);
     2005                    }
     2006                    else
     2007                    {
     2008                        /* Give up this time slice; virtual time continues */
     2009                        STAM_REL_PROFILE_START(&pVCpu->em.s.StatCapped, u);
     2010                        RTThreadSleep(2);
     2011                        STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatCapped, u);
     2012                        rc = VINF_SUCCESS;
     2013                    }
    19662014                    break;
    19672015
     
    19702018                 */
    19712019                case EMSTATE_HWACC:
    1972                     rc = emR3HwAccExecute(pVM, pVCpu, &fFFDone);
     2020                    if (emR3IsExecutionAllowed(pVM, pVCpu))
     2021                    {
     2022                        rc = emR3HwAccExecute(pVM, pVCpu, &fFFDone);
     2023                    }
     2024                    else
     2025                    {
     2026                        /* Give up this time slice; virtual time continues */
     2027                        STAM_REL_PROFILE_START(&pVCpu->em.s.StatCapped, u);
     2028                        RTThreadSleep(2);
     2029                        STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatCapped, u);
     2030                        rc = VINF_SUCCESS;
     2031                    }
    19732032                    break;
    19742033
     
    19772036                 */
    19782037                case EMSTATE_REM:
    1979                     rc = emR3RemExecute(pVM, pVCpu, &fFFDone);
    1980                     Log2(("EMR3ExecuteVM: emR3RemExecute -> %Rrc\n", rc));
     2038                    if (emR3IsExecutionAllowed(pVM, pVCpu))
     2039                    {
     2040                        rc = emR3RemExecute(pVM, pVCpu, &fFFDone);
     2041                        Log2(("EMR3ExecuteVM: emR3RemExecute -> %Rrc\n", rc));
     2042                    }
     2043                    else
     2044                    {
     2045                        /* Give up this time slice; virtual time continues */
     2046                        STAM_REL_PROFILE_START(&pVCpu->em.s.StatCapped, u);
     2047                        RTThreadSleep(2);
     2048                        STAM_REL_PROFILE_STOP(&pVCpu->em.s.StatCapped, u);
     2049                        rc = VINF_SUCCESS;
     2050                    }
    19812051                    break;
    19822052
  • trunk/src/VBox/VMM/EMInternal.h

    r28800 r32912  
    5454#define EMMWAIT_FLAG_MONITOR_ACTIVE     RT_BIT(2)
    5555
     56/** EM time slice in ms; used for capping execution time. */
     57#define EM_TIME_SLICE                   1000
    5658
    5759/**
     
    352354#endif
    353355
     356    /** Start of the current time slice in ms. */
     357    uint64_t                u64TimeSliceStart;
     358    /** Start of the current time slice in thread execution time (ms). */
     359    uint64_t                u64TimeSliceStartExec;
     360    /** Current time slice value. */
     361    uint64_t                u64TimeSliceExec;
     362    uint64_t                u64Alignment;
     363
    354364    /* MWait halt state. */
    355365    struct
     
    391401    STAMPROFILE             StatForcedActions;
    392402    STAMPROFILE             StatHalted;
     403    STAMPROFILE             StatCapped;
    393404    STAMPROFILEADV          StatHwAccEntry;
    394405    STAMPROFILE             StatHwAccExec;
  • trunk/src/VBox/VMM/include/internal/em.h

    r32171 r32912  
    2121#include <VBox/em.h>
    2222
    23 VMMR3DECL(int) EMR3NotifyResume(PVM pVM);
    24 VMMR3DECL(int) EMR3NotifySuspend(PVM pVM);
     23VMMR3DECL(int)  EMR3NotifyResume(PVM pVM);
     24VMMR3DECL(int)  EMR3NotifySuspend(PVM pVM);
    2525
    2626#endif
Note: See TracChangeset for help on using the changeset viewer.

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