Changeset 36847 in vbox for trunk/src/VBox/Frontends/VBoxBalloonCtrl
- Timestamp:
- Apr 26, 2011 9:38:02 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxBalloonCtrl/VBoxBalloonCtrl.cpp
r36711 r36847 62 62 * of a per-machine based one. */ 63 63 #define VBOX_BALLOONCTRL_GLOBAL_PERFCOL 64 65 /** The semaphore we're blocking on. */66 static RTSEMEVENTMULTI g_BalloonControlEvent = NIL_RTSEMEVENTMULTI;67 64 68 65 /** The critical section for keep our stuff in sync. */ … … 120 117 }; 121 118 122 unsigned long g_ulTimeoutMS = 30 * 1000; /* Default is 30 seconds timeout. */123 unsigned long g_ulMemoryBalloonIncrementMB = 256;124 unsigned long g_ulMemoryBalloonDecrementMB = 128;119 static unsigned long g_ulTimeoutMS = 30 * 1000; /* Default is 30 seconds timeout. */ 120 static unsigned long g_ulMemoryBalloonIncrementMB = 256; 121 static unsigned long g_ulMemoryBalloonDecrementMB = 128; 125 122 /** Global balloon limit is 0, so disabled. Can be overridden by a per-VM 126 123 * "VBoxInternal/Guest/BalloonSizeMax" value. */ 127 unsigned long g_ulMemoryBalloonMaxMB = 0;128 unsigned long g_ulLowerMemoryLimitMB = 64;124 static unsigned long g_ulMemoryBalloonMaxMB = 0; 125 static unsigned long g_ulLowerMemoryLimitMB = 64; 129 126 130 127 /** Global objects. */ … … 152 149 typedef std::map<Bstr, VBOXBALLOONCTRL_MACHINE>::iterator mapVMIter; 153 150 typedef std::map<Bstr, VBOXBALLOONCTRL_MACHINE>::const_iterator mapVMIterConst; 154 mapVM g_mapVM;151 static mapVM g_mapVM; 155 152 156 153 /* Prototypes. */ 157 154 #define serviceLogVerbose(a) if (g_fVerbose) { serviceLog a; } 158 void serviceLog(const char *pszFormat, ...);159 160 bool machineIsRunning(MachineState_T enmState);161 mapVMIter machineGetByUUID(const Bstr &strUUID);162 int machineAdd(const ComPtr<IMachine> &rptrMachine);163 int machineUpdate(const ComPtr<IMachine> &rptrMachine, MachineState_T enmState);164 int machineUpdate(mapVMIter it, MachineState_T enmState);165 void machineRemove(mapVMIter it);166 167 unsigned long balloonGetMaxSize(const ComPtr<IMachine> &rptrMachine);168 bool balloonIsRequired(mapVMIter it);169 int balloonUpdate(mapVMIterConst it);170 171 HRESULT balloonCtrlSetup();172 void balloonCtrlShutdown();173 174 HRESULT createGlobalObjects();175 void deleteGlobalObjects();155 static void serviceLog(const char *pszFormat, ...); 156 157 static bool machineIsRunning(MachineState_T enmState); 158 static mapVMIter machineGetByUUID(const Bstr &strUUID); 159 static int machineAdd(const ComPtr<IMachine> &rptrMachine); 160 static int machineUpdate(const ComPtr<IMachine> &rptrMachine, MachineState_T enmState); 161 static int machineUpdate(mapVMIter it, MachineState_T enmState); 162 static void machineRemove(mapVMIter it); 163 164 static unsigned long balloonGetMaxSize(const ComPtr<IMachine> &rptrMachine); 165 static bool balloonIsRequired(mapVMIter it); 166 static int balloonUpdate(mapVMIterConst it); 167 168 static HRESULT balloonCtrlSetup(); 169 static void balloonCtrlShutdown(); 170 171 static HRESULT createGlobalObjects(); 172 static void deleteGlobalObjects(); 176 173 177 174 #ifdef RT_OS_WINDOWS … … 339 336 ASMAtomicWriteBool(&g_fCanceled, true); 340 337 341 if ( g_BalloonControlEvent != NIL_RTSEMEVENTMULTI)342 { 343 int rc = RTSemEventMultiSignal(g_BalloonControlEvent);338 if (!g_pEventQ) 339 { 340 int rc = g_pEventQ->interruptEventQueueProcessing(); 344 341 if (RT_FAILURE(rc)) 345 serviceLog("Error: RTSemEventMultiSignal failed with rc=%Rrc\n");342 serviceLog("Error: interruptEventQueueProcessing failed with rc=%Rrc\n", rc); 346 343 } 347 344 } … … 370 367 } 371 368 372 long getlBalloonDelta(unsigned long ulCurrentDesktopBalloonSize, unsigned long ulDesktopFreeMemory, unsigned long ulMaxBalloonSize)369 static long getlBalloonDelta(unsigned long ulCurrentDesktopBalloonSize, unsigned long ulDesktopFreeMemory, unsigned long ulMaxBalloonSize) 373 370 { 374 371 if (ulCurrentDesktopBalloonSize > ulMaxBalloonSize) … … 411 408 * @param enmState The VM's machine state to judge whether it's running or not. 412 409 */ 413 bool machineIsRunning(MachineState_T enmState)410 static bool machineIsRunning(MachineState_T enmState) 414 411 { 415 412 switch (enmState) … … 430 427 } 431 428 432 mapVMIter machineGetByUUID(const Bstr &strUUID)429 static mapVMIter machineGetByUUID(const Bstr &strUUID) 433 430 { 434 431 return g_mapVM.find(strUUID); 435 432 } 436 433 437 int machineAdd(const ComPtr<IMachine> &rptrMachine)434 static int machineAdd(const ComPtr<IMachine> &rptrMachine) 438 435 { 439 436 HRESULT rc; … … 451 448 com::SafeIfaceArray<IPerformanceMetric> metricAffected; 452 449 453 Bstr strMetricNames(L"Guest/RAM *");450 Bstr strMetricNames(L"Guest/RAM/Usage"); 454 451 strMetricNames.cloneTo(&metricNames[0]); 455 452 … … 481 478 g_mapVM.insert(std::make_pair(strUUID, m)); 482 479 483 serviceLogVerbose(("Added machine \"% s\"\n", Utf8Str(strUUID).c_str()));480 serviceLogVerbose(("Added machine \"%ls\"\n", strUUID.raw())); 484 481 485 482 } while (0); … … 488 485 } 489 486 490 void machineRemove(mapVMIter it)487 static void machineRemove(mapVMIter it) 491 488 { 492 489 if (it != g_mapVM.end()) 493 490 { 494 491 /* Must log before erasing the iterator because of the UUID ref! */ 495 serviceLogVerbose(("Removing machine \"% s\"\n", Utf8Str(it->first).c_str()));492 serviceLogVerbose(("Removing machine \"%ls\"\n", it->first.raw())); 496 493 497 494 /* … … 502 499 } 503 500 504 int machineUpdate(const ComPtr<IMachine> &rptrMachine, MachineState_T enmState)501 static int machineUpdate(const ComPtr<IMachine> &rptrMachine, MachineState_T enmState) 505 502 { 506 503 if ( !balloonGetMaxSize(rptrMachine) … … 512 509 } 513 510 514 int machineUpdate(mapVMIter it, MachineState_T enmState)511 static int machineUpdate(mapVMIter it, MachineState_T enmState) 515 512 { 516 513 Assert(it != g_mapVM.end()); … … 526 523 } 527 524 528 int getMetric(mapVMIterConst it, const Bstr& strName, LONG *pulData)525 static int getMetric(mapVMIterConst it, const Bstr& strName, LONG *pulData) 529 526 { 530 527 AssertPtrReturn(pulData, VERR_INVALID_PARAMETER); … … 595 592 * @param rptrMachine Pointer to specified machine. 596 593 */ 597 unsigned long balloonGetMaxSize(const ComPtr<IMachine> &rptrMachine)594 static unsigned long balloonGetMaxSize(const ComPtr<IMachine> &rptrMachine) 598 595 { 599 596 /* … … 637 634 * @param it Iterator pointing to the VM to be processed. 638 635 */ 639 bool balloonIsRequired(mapVMIter it)636 static bool balloonIsRequired(mapVMIter it) 640 637 { 641 638 /* Only do ballooning if we have a maximum balloon size set. */ … … 652 649 * @param it Iterator pointing to the VM to be processed. 653 650 */ 654 int balloonUpdate(mapVMIterConst it)651 static int balloonUpdate(mapVMIterConst it) 655 652 { 656 653 /* … … 669 666 { 670 667 #ifdef DEBUG 671 serviceLogVerbose(("% s: No metrics available yet!\n", Utf8Str(it->first).c_str()));668 serviceLogVerbose(("%ls: No metrics available yet!\n", it->first.raw())); 672 669 #endif 673 670 return VINF_SUCCESS; … … 677 674 lBalloonCur /= 1024; 678 675 679 serviceLogVerbose(("% s: Balloon: %ld, Free mem: %ld, Max ballon: %ld\n",680 Utf8Str(it->first).c_str(),676 serviceLogVerbose(("%ls: Balloon: %ld, Free mem: %ld, Max ballon: %ld\n", 677 it->first.raw(), 681 678 lBalloonCur, lMemFree, it->second.ulBalloonSizeMax)); 682 679 … … 688 685 Assert(lBalloonCur > 0); 689 686 690 serviceLog("% s: %s balloon by %ld to %ld ...\n",691 Utf8Str(it->first).c_str(),687 serviceLog("%ls: %s balloon by %ld to %ld ...\n", 688 it->first.raw(), 692 689 lDelta > 0 ? "Inflating" : "Deflating", lDelta, lBalloonCur); 693 690 … … 708 705 CHECK_ERROR_BREAK(guest, COMSETTER(MemoryBalloonSize)(lBalloonCur)); 709 706 else 710 serviceLog("Error: Unable to set new balloon size %ld for machine \"% s\", rc=%Rhrc",711 lBalloonCur, Utf8Str(it->first).c_str(), rc);707 serviceLog("Error: Unable to set new balloon size %ld for machine \"%ls\", rc=%Rhrc", 708 lBalloonCur, it->first.raw(), rc); 712 709 } while (0); 713 710 … … 717 714 } 718 715 else 719 serviceLog("Error: Unable to retrieve metrics for machine \"% s\", rc=%Rrc",720 Utf8Str(it->first).c_str(), vrc);716 serviceLog("Error: Unable to retrieve metrics for machine \"%ls\", rc=%Rrc", 717 it->first.raw(), vrc); 721 718 return vrc; 722 719 } 723 720 724 void vmListDestroy()721 static void vmListDestroy() 725 722 { 726 723 serviceLogVerbose(("Destroying VM list ...\n")); … … 746 743 } 747 744 748 int vmListBuild()745 static int vmListBuild() 749 746 { 750 747 serviceLogVerbose(("Building VM list ...\n")); … … 779 776 if (!fAccessible) 780 777 { 781 serviceLogVerbose(("Machine \"% s\" is inaccessible, skipping\n",782 Utf8Str(strUUID).c_str()));778 serviceLogVerbose(("Machine \"%ls\" is inaccessible, skipping\n", 779 strUUID.raw())); 783 780 continue; 784 781 } … … 788 785 789 786 if (g_fVerbose) 790 serviceLogVerbose(("Processing machine \"% s\" (state: %ld)\n",791 Utf8Str(strUUID).c_str(), machineState));787 serviceLogVerbose(("Processing machine \"%ls\" (state: %ld)\n", 788 strUUID.raw(), machineState)); 792 789 793 790 if (machineIsRunning(machineState)) … … 811 808 } 812 809 813 int balloonCtrlCheck() 814 { 810 static int balloonCtrlCheck() 811 { 812 static uint64_t uLast = UINT64_MAX; 813 uint64_t uNow = RTTimeProgramMilliTS() / g_ulTimeoutMS; 814 if (uLast == uNow) 815 return VINF_SUCCESS; 816 uLast = uNow; 817 815 818 int rc = RTCritSectEnter(&g_MapCritSect); 816 819 if (RT_SUCCESS(rc)) … … 838 841 } 839 842 840 HRESULT balloonCtrlSetup()843 static HRESULT balloonCtrlSetup() 841 844 { 842 845 HRESULT rc = S_OK; … … 868 871 } 869 872 870 void balloonCtrlShutdown()873 static void balloonCtrlShutdown() 871 874 { 872 875 serviceLog("Shutting down ballooning ...\n"); … … 879 882 } 880 883 881 RTEXITCODE balloonCtrlMain(HandlerArg *a)884 static RTEXITCODE balloonCtrlMain(HandlerArg *a) 882 885 { 883 886 HRESULT rc = S_OK; … … 885 888 do 886 889 { 890 int vrc = VINF_SUCCESS; 891 887 892 /* Initialize global weak references. */ 888 893 g_pEventQ = com::EventQueue::getMainEventQueue(); 889 894 890 895 RTCritSectInit(&g_MapCritSect); 891 892 int vrc = RTSemEventMultiCreate(&g_BalloonControlEvent);893 AssertRCReturn(vrc, RTEXITCODE_FAILURE);894 896 895 897 /* … … 945 947 * processes NULL events signalling event loop termination. 946 948 */ 947 g_pEventQ->processEventQueue( 500);949 g_pEventQ->processEventQueue(g_ulTimeoutMS / 10); 948 950 949 951 if (g_fCanceled) 950 952 { 951 953 serviceLog("Signal caught, exiting ...\n"); 952 break;953 }954 955 vrc = RTSemEventMultiWait(g_BalloonControlEvent, g_ulTimeoutMS);956 if (vrc != VERR_TIMEOUT && RT_FAILURE(vrc))957 {958 serviceLog("Error: RTSemEventMultiWait failed; rc=%Rrc\n", vrc);959 954 break; 960 955 } … … 980 975 981 976 RTCritSectDelete(&g_MapCritSect); 982 RTSemEventMultiDestroy(g_BalloonControlEvent);983 g_BalloonControlEvent = NIL_RTSEMEVENTMULTI;984 977 985 978 if (RT_FAILURE(vrc)) … … 991 984 } 992 985 993 void serviceLog(const char *pszFormat, ...)986 static void serviceLog(const char *pszFormat, ...) 994 987 { 995 988 va_list args; … … 1004 997 } 1005 998 1006 void logHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)999 static void logHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog) 1007 1000 { 1008 1001 /* Some introductory information. */ … … 1072 1065 } 1073 1066 1074 void displayHelp()1067 static void displayHelp() 1075 1068 { 1076 1069 RTStrmPrintf(g_pStdErr, "\nUsage: VBoxBalloonCtrl [options]\n\nSupported options (default values in brackets):\n"); … … 1148 1141 } 1149 1142 1150 void deleteGlobalObjects()1143 static void deleteGlobalObjects() 1151 1144 { 1152 1145 serviceLogVerbose(("Deleting local objects ...\n")); … … 1161 1154 * @return HRESULT 1162 1155 */ 1163 HRESULT createGlobalObjects()1156 static HRESULT createGlobalObjects() 1164 1157 { 1165 1158 serviceLogVerbose(("Creating local objects ...\n")); … … 1213 1206 case 'i': /* Interval. */ 1214 1207 g_ulTimeoutMS = ValueUnion.u32; 1208 if (g_ulTimeoutMS < 500) 1209 g_ulTimeoutMS = 500; 1215 1210 break; 1216 1211
Note:
See TracChangeset
for help on using the changeset viewer.