public void KeyProcStart(string VMName, string keyProcName)
            {
                DynamicAdjustment dynamicAdjustment = new DynamicAdjustment();

                if (VMName == "VM1")
                {
                    if (keyProcName == "chrome")
                    {
                        dynamicAdjustment.AdjustCPULimit(VMState.VM1, 70000);
                    }
                    dynamicAdjustment.AdjustCPUWeight(VMState.VM1, 10000);
                }
                if (VMName == "VM2")
                {
                    if (keyProcName == "chrome")
                    {
                        dynamicAdjustment.AdjustCPULimit(VMState.VM2, 70000);
                    }
                    dynamicAdjustment.AdjustCPUWeight(VMState.VM2, 10000);
                }
                if (VMName == "VM3")
                {
                    if (keyProcName == "chrome")
                    {
                        dynamicAdjustment.AdjustCPULimit(VMState.VM3, 70000);
                    }
                    dynamicAdjustment.AdjustCPUWeight(VMState.VM3, 10000);
                }
                return;
            }
        public static bool ResumePowerOnVM(VirtualMachine vm, UInt64 PowerOnMemorySize, UInt64 CPU_Reverve = 50000, UInt64 CPU_Limit = 50000)
        {
            try
            {
                if (vm == null)
                {
                    Console.WriteLine("监测到虚拟机尚未安装");
                    return(false);
                }
                if (CPU_Reverve > CPU_Limit)
                {
                    Console.WriteLine("监测到虚拟机保留:" + CPU_Reverve.ToString() + "需要小于虚拟机限制资源:" + CPU_Limit.ToString());
                    return(false);
                }

                DynamicAdjustment adjustment = new DynamicAdjustment();

                bool ret = adjustment.AdjustCPUReservation(vm, CPU_Reverve);
                ret &= adjustment.AdjustCPULimit(vm, CPU_Limit);
                ret &= adjustment.AdjustMemorySize(vm, PowerOnMemorySize);
                ret &= adjustment.AdjustCPUWeight(vm, 100);

                return(true);
            }
            catch (Exception exp)
            {
                Console.WriteLine("监测到虚拟机准备恢复设置异常:" + exp.Message);
                return(false);
            }
        }
Example #3
0
        public static void DynamicAdjustTest()
        {
            ManagementScope  scope;
            ManagementObject managementService;

            scope             = new ManagementScope(@"\\.\root\virtualization\v2", null);
            managementService = WmiUtilities.GetVirtualMachineManagementService(scope);
            VirtualMachine vm = new VirtualMachine("TestVM", scope, managementService);

            UInt64            memSize           = vm.performanceSetting.MemoryUsage;
            DynamicAdjustment dynamicAdjustment = new DynamicAdjustment();
            bool ret = LoadBalancer.PreparePowerOnVM(vm, 4096, out bool isRequestMemory);

            ret &= LoadBalancer.ResumePowerOnVM(vm, 1144);

            memSize = memSize - memSize / 8;
            dynamicAdjustment.AdjustMemorySize(vm, memSize);
        }
        // 静态方法,供VMState类直接调用,请求开机时为虚拟机超量分配资源
        public static bool PreparePowerOnVM(VirtualMachine vm, UInt64 PowerOnMemorySize, out bool RequestMem, UInt64 CPU_Reverve = 50000, UInt64 CPU_Limit = 100000)
        {
            try
            {
                RequestMem = false;
                if (vm == null)
                {
                    Console.WriteLine("监测到虚拟机尚未安装");
                    return(false);
                }
                if (CPU_Reverve > CPU_Limit)
                {
                    Console.WriteLine("监测到虚拟机保留:" + CPU_Reverve.ToString() + "需要小于虚拟机限制资源:" + CPU_Limit.ToString());
                    return(false);
                }

                SystemInfo sysInfo             = new SystemInfo();
                long       hostAvailableMemory = sysInfo.MemoryAvailable;
                if (PowerOnMemorySize * 1024 * 1.2 > hostAvailableMemory)
                {
                    RequestMem = true;
                }

                DynamicAdjustment adjustment = new DynamicAdjustment();

                bool ret = adjustment.AdjustCPUCount(vm, 4);
                ret &= adjustment.AdjustMemorySize(vm, PowerOnMemorySize);
                ret &= adjustment.AdjustCPUReservation(vm, CPU_Reverve);
                ret &= adjustment.AdjustCPULimit(vm, CPU_Limit);
                ret &= adjustment.AdjustCPUWeight(vm, 10000);

                return(true);
            }
            catch (Exception exp)
            {
                Console.WriteLine("监测到虚拟机准备开启设置异常:" + exp.Message);
                RequestMem = false;
                return(false);
            }
        }