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