[6983] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7259] | 3 | * Copyright (C) 2002-2012 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;
|
---|
| 36 | public static ConfigManager Instance {
|
---|
| 37 | get { return instance; }
|
---|
| 38 | set { instance = value; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | /// <summary>
|
---|
| 42 | /// if Asleep is true, the Slave won't accept any new jobs
|
---|
| 43 | /// </summary>
|
---|
| 44 | public bool Asleep { get; set; }
|
---|
| 45 | private TaskManager jobManager;
|
---|
| 46 | private Slave slave;
|
---|
| 47 | private PerformanceCounter cpuCounter;
|
---|
| 48 | private PerformanceCounter memCounter;
|
---|
| 49 |
|
---|
| 50 | /// <summary>
|
---|
| 51 | /// Constructor for the singleton, must recover Guid, Calendar, ...
|
---|
| 52 | /// </summary>
|
---|
| 53 | public ConfigManager(TaskManager jobManager) {
|
---|
| 54 | this.jobManager = jobManager;
|
---|
| 55 | cpuCounter = new PerformanceCounter();
|
---|
| 56 | cpuCounter.CategoryName = "Processor";
|
---|
| 57 | cpuCounter.CounterName = "% Processor Time";
|
---|
| 58 | cpuCounter.InstanceName = "_Total";
|
---|
| 59 | memCounter = new PerformanceCounter("Memory", "Available Bytes", true);
|
---|
| 60 |
|
---|
| 61 | Asleep = false;
|
---|
| 62 | slave = new Slave();
|
---|
| 63 | slave.Id = GetUniqueMachineId();
|
---|
| 64 | slave.Name = Environment.MachineName;
|
---|
| 65 | slave.Cores = Environment.ProcessorCount;
|
---|
| 66 | slave.Memory = GetPhysicalMemory();
|
---|
| 67 | slave.CpuArchitecture = Environment.Is64BitOperatingSystem ? CpuArchitecture.x64 : CpuArchitecture.x86;
|
---|
| 68 | slave.OperatingSystem = Environment.OSVersion.VersionString;
|
---|
| 69 | slave.CpuSpeed = GetCpuSpeed();
|
---|
| 70 |
|
---|
| 71 | UpdateSlaveInfo();
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | private void UpdateSlaveInfo() {
|
---|
| 75 | if (slave != null) {
|
---|
| 76 | slave.FreeMemory = GetFreeMemory();
|
---|
| 77 | slave.HbInterval = (int)Settings.Default.HeartbeatInterval.TotalSeconds;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /// <summary>
|
---|
| 82 | /// Get all the Information about the client
|
---|
| 83 | /// </summary>
|
---|
| 84 | /// <returns>the ClientInfo object</returns>
|
---|
| 85 | public Slave GetClientInfo() {
|
---|
| 86 | UpdateSlaveInfo();
|
---|
| 87 | return slave;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public int GetFreeCores() {
|
---|
| 91 | return slave.Cores.HasValue ? slave.Cores.Value - SlaveStatusInfo.UsedCores : 0;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /// <summary>
|
---|
| 95 | /// collects and returns information that get displayed by the Client Console
|
---|
| 96 | /// </summary>
|
---|
| 97 | /// <returns></returns>
|
---|
| 98 | public StatusCommons GetStatusForClientConsole() {
|
---|
| 99 | StatusCommons st = new StatusCommons();
|
---|
| 100 | st.ClientGuid = slave.Id;
|
---|
| 101 |
|
---|
| 102 | st.Status = WcfService.Instance.ConnState;
|
---|
| 103 | st.ConnectedSince = WcfService.Instance.ConnectedSince;
|
---|
| 104 |
|
---|
| 105 | st.TotalCores = slave.Cores.HasValue ? slave.Cores.Value : 0;
|
---|
| 106 | st.FreeCores = GetFreeCores();
|
---|
| 107 | st.Asleep = this.Asleep;
|
---|
| 108 |
|
---|
| 109 | st.JobsStarted = SlaveStatusInfo.TasksStarted;
|
---|
| 110 | st.JobsAborted = SlaveStatusInfo.TasksAborted;
|
---|
| 111 | st.JobsFinished = SlaveStatusInfo.TasksFinished;
|
---|
| 112 | st.JobsFetched = SlaveStatusInfo.TasksFetched;
|
---|
| 113 | st.JobsFailed = SlaveStatusInfo.TasksFailed;
|
---|
| 114 | st.ExceptionsOccured = SlaveStatusInfo.ExceptionsOccured;
|
---|
| 115 |
|
---|
| 116 | st.Jobs = jobManager.GetExecutionTimes().Select(x => new TaskStatus { TaskId = x.Key, ExecutionTime = x.Value }).ToList();
|
---|
| 117 |
|
---|
| 118 | return st;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | public Dictionary<Guid, TimeSpan> GetExecutionTimeOfAllJobs() {
|
---|
| 122 | Dictionary<Guid, TimeSpan> prog = new Dictionary<Guid, TimeSpan>();
|
---|
| 123 | try {
|
---|
| 124 | prog = jobManager.GetExecutionTimes();
|
---|
| 125 | }
|
---|
| 126 | catch (Exception ex) {
|
---|
[7171] | 127 | SlaveClientCom.Instance.LogMessage(string.Format("Exception was thrown while trying to get execution times: {0}", ex.Message));
|
---|
[6983] | 128 | }
|
---|
| 129 | return prog;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | public static Guid GetUniqueMachineId() {
|
---|
| 133 | Guid id;
|
---|
| 134 | try {
|
---|
| 135 | id = GetUniqueMachineIdFromMac();
|
---|
| 136 | }
|
---|
| 137 | catch {
|
---|
| 138 | // fallback if something goes wrong...
|
---|
| 139 | id = new Guid(Environment.MachineName.GetHashCode(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
---|
| 140 | }
|
---|
| 141 | return id;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /// <summary>
|
---|
| 145 | /// returns total physical memory of the machine in MB
|
---|
| 146 | /// </summary>
|
---|
| 147 | private static int? GetPhysicalMemory() {
|
---|
| 148 | long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
|
---|
| 149 | if (res != null)
|
---|
| 150 | return (int)(res / 1024 / 1024);
|
---|
| 151 | else
|
---|
| 152 | return null;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /// <summary>
|
---|
| 156 | /// returns CPU frequence of the machine in Mhz
|
---|
| 157 | /// </summary>
|
---|
| 158 | private static int? GetCpuSpeed() {
|
---|
| 159 | return (int)GetWMIValue("Win32_Processor", "MaxClockSpeed");
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /// <summary>
|
---|
| 163 | /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...)
|
---|
| 164 | /// and the machine name.
|
---|
| 165 | /// Format:
|
---|
| 166 | ///
|
---|
| 167 | /// D1 D2 D3 Res. D4
|
---|
| 168 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
---|
| 169 | /// |n a m e|0 0|0 0|0 0 mac address|
|
---|
| 170 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
---|
| 171 | ///
|
---|
| 172 | /// The mac address is saved in the last 48 bits of the Data 4 segment
|
---|
| 173 | /// of the guid (first 2 bytes of Data 4 are reserved).
|
---|
| 174 | /// D1 contains the hash of the machinename.
|
---|
| 175 | /// </summary>
|
---|
| 176 | private static Guid GetUniqueMachineIdFromMac() {
|
---|
| 177 | ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
---|
| 178 | ManagementObjectCollection mgtCol = mgtClass.GetInstances();
|
---|
| 179 |
|
---|
| 180 | foreach (ManagementObject mgtObj in mgtCol) {
|
---|
| 181 | foreach (var prop in mgtObj.Properties) {
|
---|
| 182 | if (prop.Value != null && prop.Name == "MACAddress") {
|
---|
| 183 | try {
|
---|
| 184 | //simply take the first nic
|
---|
| 185 | string mac = prop.Value.ToString();
|
---|
| 186 | byte[] b = new byte[8];
|
---|
| 187 | string[] macParts = mac.Split(':');
|
---|
| 188 | if (macParts.Length == 6) {
|
---|
| 189 | for (int i = 0; i < macParts.Length; i++) {
|
---|
| 190 | b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | // also get machine name and save it to the first 4 bytes
|
---|
| 194 | Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
|
---|
| 195 | return guid;
|
---|
| 196 | } else
|
---|
| 197 | throw new Exception("Error getting mac addresse");
|
---|
| 198 | }
|
---|
| 199 | catch {
|
---|
| 200 | throw new Exception("Error getting mac addresse");
|
---|
| 201 | }
|
---|
| 202 | }
|
---|
| 203 | }
|
---|
| 204 | }
|
---|
| 205 | throw new Exception("Error getting mac addresse");
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | /// <summary>
|
---|
| 209 | /// return numeric value of a single hex-char
|
---|
| 210 | /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array)
|
---|
| 211 | /// </summary>
|
---|
| 212 | static int ParseNybble(char c) {
|
---|
| 213 | if (c >= '0' && c <= '9') {
|
---|
| 214 | return c - '0';
|
---|
| 215 | }
|
---|
| 216 | if (c >= 'A' && c <= 'F') {
|
---|
| 217 | return c - 'A' + 10;
|
---|
| 218 | }
|
---|
| 219 | if (c >= 'a' && c <= 'f') {
|
---|
| 220 | return c - 'a' + 10;
|
---|
| 221 | }
|
---|
| 222 | throw new ArgumentException("Invalid hex digit: " + c);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | private static long? GetWMIValue(string clazz, string property) {
|
---|
| 226 | ManagementClass mgtClass = new ManagementClass(clazz);
|
---|
| 227 | ManagementObjectCollection mgtCol = mgtClass.GetInstances();
|
---|
| 228 |
|
---|
| 229 | foreach (ManagementObject mgtObj in mgtCol) {
|
---|
| 230 | foreach (var prop in mgtObj.Properties) {
|
---|
| 231 | if (prop.Value != null && prop.Name == property) {
|
---|
| 232 | try {
|
---|
| 233 | return long.Parse(prop.Value.ToString());
|
---|
| 234 | }
|
---|
| 235 | catch {
|
---|
| 236 | return null;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | }
|
---|
| 241 | return null;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | /// <summary>
|
---|
| 245 | /// returns free memory of machine in MB
|
---|
| 246 | /// </summary>
|
---|
| 247 | public int GetFreeMemory() {
|
---|
| 248 | int mb = 0;
|
---|
| 249 |
|
---|
| 250 | try {
|
---|
| 251 | mb = (int)(memCounter.NextValue() / 1024 / 1024);
|
---|
| 252 | }
|
---|
| 253 | catch { }
|
---|
| 254 | return mb;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | public float GetCpuUtilization() {
|
---|
| 258 | float cpuVal = 0.0F;
|
---|
| 259 |
|
---|
| 260 | try {
|
---|
| 261 | return cpuCounter.NextValue();
|
---|
| 262 | }
|
---|
| 263 | catch { }
|
---|
| 264 | return cpuVal;
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
| 267 | }
|
---|