Removed unnecessary control VMMemoryControlsBase and moved the logic of the VMs

property to the derived classes (they don't share much of it anyway) and the method
CalcMemoryUsed to the VMShinyBar class where it is needed. Also made the latter's
property Increment non-browsable.

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
This commit is contained in:
Konstantina Chremmou 2018-08-13 01:39:09 +01:00 committed by Mihaela Stoica
parent c67f85ebaa
commit 6885c21b5e
8 changed files with 90 additions and 131 deletions

View File

@ -1,80 +0,0 @@
/* Copyright (c) Citrix Systems, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms,
* with or without modification, are permitted provided
* that the following conditions are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using XenAPI;
namespace XenAdmin.Controls.Ballooning
{
public class VMMemoryControlsBase : UserControl
{
protected List<VM> vms;
protected VM vm0;
protected List<VM_metrics> vm_metrics = new List<VM_metrics>(); // metrics for all the VMs
public virtual List<VM> VMs
{
set
{
vms = value;
vm0 = vms[0]; // just an abbreviation for when we don't care which VM we look at
vm_metrics = new List<VM_metrics>(vms.Count);
foreach (VM vm in vms)
vm_metrics.Add(vm.Connection.Resolve(vm.metrics));
}
}
protected long CalcMemoryUsed()
{
long memoryUsed = 0;
if (vm0.power_state == vm_power_state.Running || vm0.power_state == vm_power_state.Paused)
{
// Calculate the average memory used by these VMs
int count = 0;
foreach (VM_metrics vm_metric in vm_metrics)
{
if (vm_metric != null)
{
memoryUsed += vm_metric.memory_actual;
++count;
}
}
if (count > 0)
memoryUsed /= count;
}
return memoryUsed;
}
}
}

View File

@ -71,7 +71,6 @@ namespace XenAdmin.Controls.Ballooning
//
// vmShinyBar
//
this.vmShinyBar.Increment = 0D;
resources.ApplyResources(this.vmShinyBar, "vmShinyBar");
this.vmShinyBar.Name = "vmShinyBar";
this.vmShinyBar.SliderDragged += new System.EventHandler(this.vmShinyBar_SliderDragged);

View File

@ -30,11 +30,6 @@
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using XenAPI;
using XenAdmin.Commands;
@ -63,8 +58,7 @@ namespace XenAdmin.Controls.Ballooning
// Calculate the maximum legal value of dynamic minimum
CalcMaxDynMin();
// Shiny bar
vmShinyBar.Initialize(vm0, vms.Count > 1, CalcMemoryUsed(), true);
vmShinyBar.Populate(vms, true);
// Radio buttons and "DMC Unavailable" warning
if (ballooning)

View File

@ -33,13 +33,13 @@ using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using XenAdmin.Core;
using XenAPI;
namespace XenAdmin.Controls.Ballooning
{
public class VMMemoryControlsEdit : VMMemoryControlsBase
public class VMMemoryControlsEdit : UserControl
{
// Was ballooning on at the time when we entered the dialog?
// (Like other settings on the dialog, it deliberately doesn't reflect later changes: see CA-34476).
@ -49,12 +49,22 @@ namespace XenAdmin.Controls.Ballooning
private long maxMemAllowed = VM.DEFAULT_MEM_ALLOWED;
protected bool firstPaint = true;
public override List<VM> VMs
protected List<VM> vms;
protected VM vm0;
public List<VM> VMs
{
set
{
base.VMs = value;
ballooning = vm0.has_ballooning();
vms = value;
if (vms == null)
return;
if (vms.Count > 0)
{
vm0 = vms[0];
ballooning = vm0.has_ballooning();
}
firstPaint = true;
maxMemAllowed = CalcMaxMemAllowed();
}

View File

@ -105,7 +105,6 @@ namespace XenAdmin.Controls.Ballooning
// vmShinyBar
//
resources.ApplyResources(this.vmShinyBar, "vmShinyBar");
this.vmShinyBar.Increment = 0D;
this.vmShinyBar.Name = "vmShinyBar";
//
// VMMemoryControlsNoEdit

View File

@ -41,24 +41,45 @@ using XenAPI;
namespace XenAdmin.Controls.Ballooning
{
public partial class VMMemoryControlsNoEdit : VMMemoryControlsBase
public partial class VMMemoryControlsNoEdit : UserControl
{
private List<VM> vms;
private VM vm0;
private List<VM_metrics> vm_metrics;
public VMMemoryControlsNoEdit()
{
InitializeComponent();
}
public override List<VM> VMs
public List<VM> VMs
{
set
{
base.VMs = value;
foreach (VM vm in vms)
vm.PropertyChanged += vm_PropertyChanged;
foreach (VM_metrics metrics in vm_metrics)
UnregisterHandlers();
vms = value;
if (vms == null)
{
vm_metrics = null;
return;
}
if (vms.Count > 0)
vm0 = vms[0];
vm_metrics = new List<VM_metrics>();
foreach (VM vm in vms)
{
vm.PropertyChanged += vm_PropertyChanged;
var metrics = vm.Connection.Resolve(vm.metrics);
if (metrics != null)
{
vm_metrics.Add(metrics);
metrics.PropertyChanged += vm_metrics_PropertyChanged;
}
}
}
}
@ -75,8 +96,7 @@ namespace XenAdmin.Controls.Ballooning
(null == vms.Find(vm => !(vm.power_state == vm_power_state.Halted ||
vm.power_state == vm_power_state.Running && !vm.GetVirtualisationStatus().HasFlag(XenAPI.VM.VirtualisationStatus.UNKNOWN))));
// Shiny bar
vmShinyBar.Initialize(vm0, vms.Count > 1, CalcMemoryUsed(), false);
vmShinyBar.Populate(vms, false);
// Spinners
bool ballooning = vm0.has_ballooning();
@ -100,13 +120,13 @@ namespace XenAdmin.Controls.Ballooning
}
}
void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
private void vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "power_state" || e.PropertyName == "virtualisation_status" || e.PropertyName == "name_label")
Program.Invoke(this,Refresh);
}
void vm_metrics_PropertyChanged(object sender, PropertyChangedEventArgs e)
private void vm_metrics_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "memory_actual")
Refresh();
@ -119,17 +139,18 @@ namespace XenAdmin.Controls.Ballooning
else
Program.MainWindow.ShowPerConnectionWizard(vm0.Connection, new BallooningWizard(vms));
}
internal void UnregisterHandlers()
{
if (vms == null)
return;
if (vms != null)
foreach (var vm in vms)
if (vm != null)
vm.PropertyChanged -= vm_PropertyChanged;
foreach (var vm in vms)
vm.PropertyChanged -= vm_PropertyChanged;
foreach (var metrics in vm_metrics.Where(m => m != null))
metrics.PropertyChanged -= vm_metrics_PropertyChanged;
if (vm_metrics != null)
foreach (var metrics in vm_metrics)
if (metrics != null)
metrics.PropertyChanged -= vm_metrics_PropertyChanged;
}
}
}

View File

@ -34,6 +34,7 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XenAdmin.Core;
@ -60,13 +61,8 @@ namespace XenAdmin.Controls.Ballooning
private bool multiple;
// The increment in which the user can move the draggers, in bytes
double increment;
public double Increment
{
get { return increment; }
set { increment = value; }
}
[Browsable(false)]
public double Increment { get; set; }
public double Dynamic_min
{
@ -136,16 +132,39 @@ namespace XenAdmin.Controls.Ballooning
private bool mouseIsDown = false;
private double BytesPerPixel;
public void Initialize(VM vm, bool multiple, long memoryUsed, bool allowEdit)
public void Populate(List<VM> vms, bool allowMemEdit)
{
this.multiple = multiple;
this.memoryUsed = memoryUsed;
this.static_min = vm.memory_static_min;
this.static_max = vm.memory_static_max;
this.dynamic_min = dynamic_min_orig = Util.CorrectRoundingErrors(vm.memory_dynamic_min);
this.dynamic_max = dynamic_max_orig = Util.CorrectRoundingErrors(vm.memory_dynamic_max);
this.has_ballooning = vm.has_ballooning();
this.allowEdit = allowEdit;
var vm = vms[0];
multiple = vms.Count > 1;
memoryUsed = CalcMemoryUsed(vms);
static_min = vm.memory_static_min;
static_max = vm.memory_static_max;
dynamic_min = dynamic_min_orig = Util.CorrectRoundingErrors(vm.memory_dynamic_min);
dynamic_max = dynamic_max_orig = Util.CorrectRoundingErrors(vm.memory_dynamic_max);
has_ballooning = vm.has_ballooning();
allowEdit = allowMemEdit;
}
private static long CalcMemoryUsed(List<VM> vms)
{
var memories = (from VM vm in vms
where vm != null && vm.metrics != null &&
(vm.power_state == vm_power_state.Running || vm.power_state == vm_power_state.Paused)
let metrics = vm.Connection.Resolve(vm.metrics)
where metrics != null
select metrics.memory_actual).ToList();
if (memories.Count > 0)
try
{
return Convert.ToInt64(memories.Average());
}
catch
{
// ignore; we'll return 0
}
return 0;
}
public void ChangeSettings(double static_min, double dynamic_min, double dynamic_max, double static_max)

View File

@ -3445,9 +3445,6 @@
<Compile Include="Controls\Ballooning\VMMemoryControlsAdvanced.Designer.cs">
<DependentUpon>VMMemoryControlsAdvanced.cs</DependentUpon>
</Compile>
<Compile Include="Controls\Ballooning\VMMemoryControlsBase.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Controls\Ballooning\VMMemoryControlsBasic.cs">
<SubType>UserControl</SubType>
</Compile>