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