xenadmin/XenAdmin/Controls/GPU/GpuPlacementPolicyPanel.cs
Konstantina Chremmou a1fc0f5e7d CP-6085: Per-pool overview of vGPUs (PR-1675)
# HG changeset patch
# User Mihaela Stoica <Mihaela.Stoica@citrix.com>
# Date 1382997218 0
#      Mon Oct 28 21:53:38 2013 +0000
# Node ID c82052cb3e73d23ce4d1f14cb9f771b7e43f1cdd
# Parent  765758a41fcc8491cf3a269e071baf96cd619b41

Signed-off-by: Konstantina Chremmou <konstantina.chremmou@citrix.com>
2013-11-14 11:15:52 +00:00

147 lines
5.0 KiB
C#

using System.ComponentModel;
using System.Windows.Forms;
using XenAdmin.Dialogs;
using XenAPI;
namespace XenAdmin.Controls
{
public partial class GpuPlacementPolicyPanel : UserControl
{
public GpuPlacementPolicyPanel()
{
InitializeComponent();
GPU_group_CollectionChangedWithInvoke = Program.ProgramInvokeHandler(GPU_group_CollectionChanged);
}
IXenObject xenObject;
private readonly CollectionChangeEventHandler GPU_group_CollectionChangedWithInvoke;
public IXenObject XenObject
{
set
{
System.Diagnostics.Trace.Assert(value is Pool || value is Host);
xenObject = value;
RegisterHandlers();
PopulatePage();
Refresh();
}
}
private void PopulatePage()
{
var currentAllocationAlgorithm = allocation_algorithm.unknown;
foreach (GPU_group gpu_group in xenObject.Connection.Cache.GPU_groups)
{
if (gpu_group.allocation_algorithm != currentAllocationAlgorithm && currentAllocationAlgorithm != allocation_algorithm.unknown)
{
// a mixture of allocation algorithms
currentAllocationAlgorithm = allocation_algorithm.unknown;
break;
}
currentAllocationAlgorithm = gpu_group.allocation_algorithm;
}
placementPolicyLabel.Text = string.Format(Messages.GPU_PLACEMENT_POLICY_DESCRIPTION,
PlacementPolicyWrapper.ToString(currentAllocationAlgorithm));
}
private void GPU_group_CollectionChanged(object sender, CollectionChangeEventArgs e)
{
switch (e.Action)
{
case CollectionChangeAction.Add:
RegisterGpuGroupHandlers((GPU_group) e.Element);
break;
case CollectionChangeAction.Remove:
UnregisterGpuGroupHandlers((GPU_group) e.Element);
break;
}
PopulatePage();
}
private void gpu_group_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "allocation_algorithm")
{
PopulatePage();
}
}
private void RegisterGpuGroupHandlers(GPU_group gpu_group)
{
gpu_group.PropertyChanged -= gpu_group_PropertyChanged;
gpu_group.PropertyChanged += gpu_group_PropertyChanged;
}
private void UnregisterGpuGroupHandlers(GPU_group gpu_group)
{
gpu_group.PropertyChanged -= gpu_group_PropertyChanged;
}
private void RegisterHandlers()
{
if (xenObject == null)
return;
xenObject.Connection.Cache.DeregisterCollectionChanged<GPU_group>(GPU_group_CollectionChangedWithInvoke);
xenObject.Connection.Cache.RegisterCollectionChanged<GPU_group>(GPU_group_CollectionChangedWithInvoke);
foreach (GPU_group gpu_group in xenObject.Connection.Cache.GPU_groups)
{
UnregisterGpuGroupHandlers(gpu_group);
RegisterGpuGroupHandlers(gpu_group);
}
}
private void UnregisterHandlers()
{
if (xenObject == null)
return;
xenObject.Connection.Cache.DeregisterCollectionChanged<GPU_group>(GPU_group_CollectionChangedWithInvoke);
foreach (GPU_group gpu_group in xenObject.Connection.Cache.GPU_groups)
UnregisterGpuGroupHandlers(gpu_group);
}
private void editPlacementPolicyButton_Click(object sender, System.EventArgs e)
{
using (PropertiesDialog propertiesDialog = new PropertiesDialog(xenObject))
{
propertiesDialog.SelectPage(propertiesDialog.PoolGpuEditPage);
propertiesDialog.ShowDialog(this);
}
}
private void GpuPlacementPolicyPanel_VisibleChanged(object sender, System.EventArgs e)
{
if (Visible)
RegisterHandlers();
else
UnregisterHandlers();
}
}
public static class PlacementPolicyWrapper
{
public static string ToString(allocation_algorithm x)
{
switch (x)
{
case allocation_algorithm.breadth_first:
return Messages.GPU_PLACEMENT_POLICY_MAX_PERFORMANCE_DESCRIPTION;
case allocation_algorithm.depth_first:
return Messages.GPU_PLACEMENT_POLICY_MAX_DENSITY_DESCRIPTION;
default:
return Messages.GPU_PLACEMENT_POLICY_MIXED_DESCRIPTION;
}
}
}
}