[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[6983] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Diagnostics;
|
---|
| 25 | using System.Linq;
|
---|
| 26 | using System.Management;
|
---|
| 27 | using HeuristicLab.Clients.Hive.SlaveCore.Properties;
|
---|
| 28 |
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Clients.Hive.SlaveCore {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// accesses the server and sends his data (uuid, uptimes, hardware config)
|
---|
| 33 | /// </summary>
|
---|
| 34 | public class ConfigManager {
|
---|
| 35 | private static ConfigManager instance = null;
|
---|
[12236] | 36 | private const string vmwareNameString = "VMware";
|
---|
| 37 | private const string virtualboxNameString = "VirtualBox";
|
---|
[12273] | 38 | private const int macLength = 6;
|
---|
| 39 | private const int macLongLength = 8;
|
---|
[12236] | 40 |
|
---|
[6983] | 41 | public static ConfigManager Instance {
|
---|
| 42 | get { return instance; }
|
---|
| 43 | set { instance = value; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | /// <summary>
|
---|
| 47 | /// if Asleep is true, the Slave won't accept any new jobs
|
---|
| 48 | /// </summary>
|
---|
| 49 | public bool Asleep { get; set; }
|
---|
| 50 | private TaskManager jobManager;
|
---|
| 51 | private Slave slave;
|
---|
| 52 | private PerformanceCounter cpuCounter;
|
---|
| 53 | private PerformanceCounter memCounter;
|
---|
| 54 |
|
---|
| 55 | /// <summary>
|
---|
| 56 | /// Constructor for the singleton, must recover Guid, Calendar, ...
|
---|
| 57 | /// </summary>
|
---|
| 58 | public ConfigManager(TaskManager jobManager) {
|
---|
| 59 | this.jobManager = jobManager;
|
---|
| 60 | cpuCounter = new PerformanceCounter();
|
---|
| 61 | cpuCounter.CategoryName = "Processor";
|
---|
| 62 | cpuCounter.CounterName = "% Processor Time";
|
---|
| 63 | cpuCounter.InstanceName = "_Total";
|
---|
| 64 | memCounter = new PerformanceCounter("Memory", "Available Bytes", true);
|
---|
| 65 |
|
---|
| 66 | Asleep = false;
|
---|
| 67 | slave = new Slave();
|
---|
| 68 | slave.Id = GetUniqueMachineId();
|
---|
| 69 | slave.Name = Environment.MachineName;
|
---|
[7777] | 70 | if (Settings.Default.NrOfCoresToScavenge < 1 || Settings.Default.NrOfCoresToScavenge > Environment.ProcessorCount) {
|
---|
[14354] | 71 | slave.Cores = Environment.ProcessorCount;
|
---|
[7777] | 72 | } else {
|
---|
| 73 | slave.Cores = Settings.Default.NrOfCoresToScavenge;
|
---|
| 74 | }
|
---|
[6983] | 75 | slave.Memory = GetPhysicalMemory();
|
---|
| 76 | slave.CpuArchitecture = Environment.Is64BitOperatingSystem ? CpuArchitecture.x64 : CpuArchitecture.x86;
|
---|
| 77 | slave.OperatingSystem = Environment.OSVersion.VersionString;
|
---|
| 78 | slave.CpuSpeed = GetCpuSpeed();
|
---|
[7857] | 79 | slave.IsDisposable = true;
|
---|
[6983] | 80 |
|
---|
| 81 | UpdateSlaveInfo();
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | private void UpdateSlaveInfo() {
|
---|
| 85 | if (slave != null) {
|
---|
| 86 | slave.FreeMemory = GetFreeMemory();
|
---|
| 87 | slave.HbInterval = (int)Settings.Default.HeartbeatInterval.TotalSeconds;
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | /// <summary>
|
---|
| 92 | /// Get all the Information about the client
|
---|
| 93 | /// </summary>
|
---|
| 94 | /// <returns>the ClientInfo object</returns>
|
---|
| 95 | public Slave GetClientInfo() {
|
---|
| 96 | UpdateSlaveInfo();
|
---|
| 97 | return slave;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | public int GetFreeCores() {
|
---|
| 101 | return slave.Cores.HasValue ? slave.Cores.Value - SlaveStatusInfo.UsedCores : 0;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /// <summary>
|
---|
| 105 | /// collects and returns information that get displayed by the Client Console
|
---|
| 106 | /// </summary>
|
---|
| 107 | /// <returns></returns>
|
---|
| 108 | public StatusCommons GetStatusForClientConsole() {
|
---|
| 109 | StatusCommons st = new StatusCommons();
|
---|
| 110 | st.ClientGuid = slave.Id;
|
---|
| 111 |
|
---|
| 112 | st.Status = WcfService.Instance.ConnState;
|
---|
| 113 | st.ConnectedSince = WcfService.Instance.ConnectedSince;
|
---|
| 114 |
|
---|
| 115 | st.TotalCores = slave.Cores.HasValue ? slave.Cores.Value : 0;
|
---|
| 116 | st.FreeCores = GetFreeCores();
|
---|
| 117 | st.Asleep = this.Asleep;
|
---|
| 118 |
|
---|
| 119 | st.JobsStarted = SlaveStatusInfo.TasksStarted;
|
---|
| 120 | st.JobsAborted = SlaveStatusInfo.TasksAborted;
|
---|
| 121 | st.JobsFinished = SlaveStatusInfo.TasksFinished;
|
---|
| 122 | st.JobsFetched = SlaveStatusInfo.TasksFetched;
|
---|
| 123 | st.JobsFailed = SlaveStatusInfo.TasksFailed;
|
---|
| 124 |
|
---|
| 125 | st.Jobs = jobManager.GetExecutionTimes().Select(x => new TaskStatus { TaskId = x.Key, ExecutionTime = x.Value }).ToList();
|
---|
| 126 |
|
---|
| 127 | return st;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | public Dictionary<Guid, TimeSpan> GetExecutionTimeOfAllJobs() {
|
---|
| 131 | Dictionary<Guid, TimeSpan> prog = new Dictionary<Guid, TimeSpan>();
|
---|
| 132 | try {
|
---|
| 133 | prog = jobManager.GetExecutionTimes();
|
---|
[17316] | 134 | } catch (Exception ex) {
|
---|
[7171] | 135 | SlaveClientCom.Instance.LogMessage(string.Format("Exception was thrown while trying to get execution times: {0}", ex.Message));
|
---|
[6983] | 136 | }
|
---|
| 137 | return prog;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[17316] | 140 | /// <summary>
|
---|
| 141 | /// Returns the unique machine id of the slave
|
---|
| 142 | /// </summary>
|
---|
| 143 | /// <returns><see cref="Guid"/></returns>
|
---|
[6983] | 144 | public static Guid GetUniqueMachineId() {
|
---|
[17316] | 145 | // Due to the fact that repeated calculation of the machine ID can lead to a client registering at
|
---|
| 146 | // the Hive server multiple times with different IDs it's better to set the unique machine id only
|
---|
| 147 | // once, at first startup, and store it in core settings.
|
---|
| 148 | if (Settings.Default.MachineId == Guid.Empty) {
|
---|
| 149 | Settings.Default.MachineId = Guid.NewGuid();
|
---|
| 150 | Settings.Default.Save();
|
---|
[6983] | 151 | }
|
---|
[17316] | 152 |
|
---|
| 153 | return Settings.Default.MachineId;
|
---|
[6983] | 154 | }
|
---|
| 155 |
|
---|
| 156 | /// <summary>
|
---|
| 157 | /// returns total physical memory of the machine in MB
|
---|
| 158 | /// </summary>
|
---|
| 159 | private static int? GetPhysicalMemory() {
|
---|
| 160 | long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
|
---|
| 161 | if (res != null)
|
---|
| 162 | return (int)(res / 1024 / 1024);
|
---|
| 163 | else
|
---|
| 164 | return null;
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | /// <summary>
|
---|
| 168 | /// returns CPU frequence of the machine in Mhz
|
---|
| 169 | /// </summary>
|
---|
| 170 | private static int? GetCpuSpeed() {
|
---|
| 171 | return (int)GetWMIValue("Win32_Processor", "MaxClockSpeed");
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | private static long? GetWMIValue(string clazz, string property) {
|
---|
| 175 | ManagementClass mgtClass = new ManagementClass(clazz);
|
---|
| 176 | ManagementObjectCollection mgtCol = mgtClass.GetInstances();
|
---|
| 177 |
|
---|
| 178 | foreach (ManagementObject mgtObj in mgtCol) {
|
---|
| 179 | foreach (var prop in mgtObj.Properties) {
|
---|
| 180 | if (prop.Value != null && prop.Name == property) {
|
---|
| 181 | try {
|
---|
| 182 | return long.Parse(prop.Value.ToString());
|
---|
[17316] | 183 | } catch {
|
---|
[6983] | 184 | return null;
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 | return null;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /// <summary>
|
---|
| 193 | /// returns free memory of machine in MB
|
---|
| 194 | /// </summary>
|
---|
| 195 | public int GetFreeMemory() {
|
---|
| 196 | int mb = 0;
|
---|
| 197 |
|
---|
| 198 | try {
|
---|
| 199 | mb = (int)(memCounter.NextValue() / 1024 / 1024);
|
---|
[17316] | 200 | } catch { }
|
---|
[6983] | 201 | return mb;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | public float GetCpuUtilization() {
|
---|
| 205 | float cpuVal = 0.0F;
|
---|
| 206 |
|
---|
| 207 | try {
|
---|
| 208 | return cpuCounter.NextValue();
|
---|
[17316] | 209 | } catch { }
|
---|
[6983] | 210 | return cpuVal;
|
---|
| 211 | }
|
---|
| 212 | }
|
---|
| 213 | }
|
---|