1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 | if (Settings.Default.NrOfCoresToScavenge < 1 || Settings.Default.NrOfCoresToScavenge > Environment.ProcessorCount) {
|
---|
66 | slave.Cores = Environment.ProcessorCount;
|
---|
67 | } else {
|
---|
68 | slave.Cores = Settings.Default.NrOfCoresToScavenge;
|
---|
69 | }
|
---|
70 | slave.Memory = GetPhysicalMemory();
|
---|
71 | slave.CpuArchitecture = Environment.Is64BitOperatingSystem ? CpuArchitecture.x64 : CpuArchitecture.x86;
|
---|
72 | slave.OperatingSystem = Environment.OSVersion.VersionString;
|
---|
73 | slave.CpuSpeed = GetCpuSpeed();
|
---|
74 | slave.IsDisposable = true;
|
---|
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;
|
---|
119 |
|
---|
120 | st.Jobs = jobManager.GetExecutionTimes().Select(x => new TaskStatus { TaskId = x.Key, ExecutionTime = x.Value }).ToList();
|
---|
121 |
|
---|
122 | return st;
|
---|
123 | }
|
---|
124 |
|
---|
125 | public Dictionary<Guid, TimeSpan> GetExecutionTimeOfAllJobs() {
|
---|
126 | Dictionary<Guid, TimeSpan> prog = new Dictionary<Guid, TimeSpan>();
|
---|
127 | try {
|
---|
128 | prog = jobManager.GetExecutionTimes();
|
---|
129 | }
|
---|
130 | catch (Exception ex) {
|
---|
131 | SlaveClientCom.Instance.LogMessage(string.Format("Exception was thrown while trying to get execution times: {0}", ex.Message));
|
---|
132 | }
|
---|
133 | return prog;
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static Guid GetUniqueMachineId() {
|
---|
137 | // mock slave id
|
---|
138 | return Guid.NewGuid();
|
---|
139 | /*
|
---|
140 | Guid id;
|
---|
141 |
|
---|
142 | try {
|
---|
143 | id = GetUniqueMachineIdFromMac();
|
---|
144 | }
|
---|
145 | catch {
|
---|
146 | // fallback if something goes wrong...
|
---|
147 | id = new Guid(Environment.MachineName.GetHashCode(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
---|
148 | }
|
---|
149 | return id;
|
---|
150 | */
|
---|
151 | }
|
---|
152 |
|
---|
153 | /// <summary>
|
---|
154 | /// returns total physical memory of the machine in MB
|
---|
155 | /// </summary>
|
---|
156 | private static int? GetPhysicalMemory() {
|
---|
157 | long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
|
---|
158 | if (res != null)
|
---|
159 | return (int)(res / 1024 / 1024);
|
---|
160 | else
|
---|
161 | return null;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /// <summary>
|
---|
165 | /// returns CPU frequence of the machine in Mhz
|
---|
166 | /// </summary>
|
---|
167 | private static int? GetCpuSpeed() {
|
---|
168 | return (int)GetWMIValue("Win32_Processor", "MaxClockSpeed");
|
---|
169 | }
|
---|
170 |
|
---|
171 | /// <summary>
|
---|
172 | /// Generate a guid based on mac address of the first found nic (yes, mac addresses are not unique...)
|
---|
173 | /// and the machine name.
|
---|
174 | /// Format:
|
---|
175 | ///
|
---|
176 | /// D1 D2 D3 Res. D4
|
---|
177 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
---|
178 | /// |n a m e|0 0|0 0|0 0 mac address|
|
---|
179 | /// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
---|
180 | ///
|
---|
181 | /// The mac address is saved in the last 48 bits of the Data 4 segment
|
---|
182 | /// of the guid (first 2 bytes of Data 4 are reserved).
|
---|
183 | /// D1 contains the hash of the machinename.
|
---|
184 | /// </summary>
|
---|
185 | private static Guid GetUniqueMachineIdFromMac() {
|
---|
186 | ManagementClass mgtClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
|
---|
187 | ManagementObjectCollection mgtCol = mgtClass.GetInstances();
|
---|
188 |
|
---|
189 | foreach (ManagementObject mgtObj in mgtCol) {
|
---|
190 | foreach (var prop in mgtObj.Properties) {
|
---|
191 | if (prop.Value != null && prop.Name == "MACAddress") {
|
---|
192 | try {
|
---|
193 | //simply take the first nic
|
---|
194 | string mac = prop.Value.ToString();
|
---|
195 | byte[] b = new byte[8];
|
---|
196 | string[] macParts = mac.Split(':');
|
---|
197 | if (macParts.Length == 6) {
|
---|
198 | for (int i = 0; i < macParts.Length; i++) {
|
---|
199 | b[i + 2] = (byte)((ParseNybble(macParts[i][0]) << 4) | ParseNybble(macParts[i][1]));
|
---|
200 | }
|
---|
201 |
|
---|
202 | // also get machine name and save it to the first 4 bytes
|
---|
203 | Guid guid = new Guid(Environment.MachineName.GetHashCode(), 0, 0, b);
|
---|
204 | return guid;
|
---|
205 | } else
|
---|
206 | throw new Exception("Error getting mac addresse");
|
---|
207 | }
|
---|
208 | catch {
|
---|
209 | throw new Exception("Error getting mac addresse");
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 | }
|
---|
214 | throw new Exception("Error getting mac addresse");
|
---|
215 | }
|
---|
216 |
|
---|
217 | /// <summary>
|
---|
218 | /// return numeric value of a single hex-char
|
---|
219 | /// (see: http://stackoverflow.com/questions/854012/how-to-convert-hex-to-a-byte-array)
|
---|
220 | /// </summary>
|
---|
221 | static int ParseNybble(char c) {
|
---|
222 | if (c >= '0' && c <= '9') {
|
---|
223 | return c - '0';
|
---|
224 | }
|
---|
225 | if (c >= 'A' && c <= 'F') {
|
---|
226 | return c - 'A' + 10;
|
---|
227 | }
|
---|
228 | if (c >= 'a' && c <= 'f') {
|
---|
229 | return c - 'a' + 10;
|
---|
230 | }
|
---|
231 | throw new ArgumentException("Invalid hex digit: " + c);
|
---|
232 | }
|
---|
233 |
|
---|
234 | private static long? GetWMIValue(string clazz, string property) {
|
---|
235 | ManagementClass mgtClass = new ManagementClass(clazz);
|
---|
236 | ManagementObjectCollection mgtCol = mgtClass.GetInstances();
|
---|
237 |
|
---|
238 | foreach (ManagementObject mgtObj in mgtCol) {
|
---|
239 | foreach (var prop in mgtObj.Properties) {
|
---|
240 | if (prop.Value != null && prop.Name == property) {
|
---|
241 | try {
|
---|
242 | return long.Parse(prop.Value.ToString());
|
---|
243 | }
|
---|
244 | catch {
|
---|
245 | return null;
|
---|
246 | }
|
---|
247 | }
|
---|
248 | }
|
---|
249 | }
|
---|
250 | return null;
|
---|
251 | }
|
---|
252 |
|
---|
253 | /// <summary>
|
---|
254 | /// returns free memory of machine in MB
|
---|
255 | /// </summary>
|
---|
256 | public int GetFreeMemory() {
|
---|
257 | try {
|
---|
258 | return (int)(memCounter.NextValue() / 1024 / 1024);
|
---|
259 | }
|
---|
260 | catch {
|
---|
261 | return 0;
|
---|
262 | }
|
---|
263 | }
|
---|
264 |
|
---|
265 | public float GetCpuUtilization() {
|
---|
266 | try {
|
---|
267 | return cpuCounter.NextValue();
|
---|
268 | }
|
---|
269 | catch {
|
---|
270 | return 0.0f;
|
---|
271 | }
|
---|
272 | }
|
---|
273 | }
|
---|
274 | }
|
---|