Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/ConfigManager.cs @ 5599

Last change on this file since 5599 was 5599, checked in by ascheibe, 13 years ago

#1233

  • rename 'Slave' namespace to 'SlaveCore' (and assemblies etc) to avoid problems with 'Slave' class
  • use svcutil (OKB-style)
File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
22using System;
23using System.Collections.Generic;
24using System.Diagnostics;
25using System.Management;
26using HeuristicLab.Clients.Hive.SlaveCore.Properties;
27
28
29namespace 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 {
37        if (instance == null) {
38          instance = new ConfigManager();
39        }
40        return instance;
41      }
42    }
43
44    public Core Core { get; set; }
45    private Slave slave;
46
47    /// <summary>
48    /// Constructor for the singleton, must recover Guid, Calendar, ...
49    /// </summary>
50    private ConfigManager() {
51      slave = new Slave();
52      slave.Id = GetUniqueMachineId();
53      slave.Name = Environment.MachineName;
54      slave.Cores = Environment.ProcessorCount;
55      slave.Memory = GetPhysicalMemory();
56      slave.CpuArchitecture = Environment.Is64BitOperatingSystem ? CpuArchitecture.x64 : CpuArchitecture.x86;
57      slave.OperatingSystem = Environment.OSVersion.VersionString;
58      slave.CpuSpeed = GetCpuSpeed();
59      slave.FreeMemory = GetFreeMemory();
60    }
61
62    /// <summary>
63    /// Get all the Information about the client
64    /// </summary>
65    /// <returns>the ClientInfo object</returns>
66    public Slave GetClientInfo() {
67      //TODO: how to display connectedsince in gui?
68      //hardwareInfo.Login = WcfService.Instance.ConnectedSince;
69      return slave;
70    }
71
72    /// <summary>
73    /// collects and returns information that get displayed by the Client Console
74    /// </summary>
75    /// <returns></returns>
76    public StatusCommons GetStatusForClientConsole() {
77      //Todo: Locking
78      StatusCommons st = new StatusCommons();
79      st.ClientGuid = slave.Id;
80
81      st.Status = WcfService.Instance.ConnState;
82      st.ConnectedSince = WcfService.Instance.ConnectedSince;
83
84      st.TotalCores = slave.Cores.HasValue ? slave.Cores.Value : 0;
85      st.FreeCores = slave.Cores.HasValue ? slave.Cores.Value - GetUsedCores() : 0;
86
87      st.JobsAborted = SlaveStatusInfo.JobsAborted;
88      st.JobsDone = SlaveStatusInfo.JobsProcessed;
89      st.JobsFetched = SlaveStatusInfo.JobsFetched;
90
91      Dictionary<Guid, Executor> engines = Core.ExecutionEngines;
92      st.Jobs = new List<JobStatus>();
93
94      lock (engines) {
95        foreach (KeyValuePair<Guid, Executor> kvp in engines) {
96          Executor e = kvp.Value;
97          st.Jobs.Add(new JobStatus { JobId = e.JobId, ExecutionTime = e.ExecutionTime, Since = e.CreationTime });
98        }
99      }
100      return st;
101    }
102
103    public Dictionary<Guid, TimeSpan> GetExecutionTimeOfAllJobs() {
104      Dictionary<Guid, TimeSpan> prog = new Dictionary<Guid, TimeSpan>();
105      Dictionary<Guid, Executor> engines = Core.ExecutionEngines;
106      lock (engines) {
107        foreach (KeyValuePair<Guid, Executor> kvp in engines) {
108          Executor e = kvp.Value;
109          prog[e.JobId] = e.ExecutionTime;
110        }
111      }
112      return prog;
113    }
114
115    public int GetUsedCores() {
116      Dictionary<Guid, Executor> engines = Core.ExecutionEngines;
117      Dictionary<Guid, Job> jobs = Core.Jobs;
118      int usedCores = 0;
119      lock (engines) {
120        foreach (KeyValuePair<Guid, Job> kvp in jobs)
121          usedCores += kvp.Value.CoresNeeded;
122      }
123      return usedCores;
124    }
125
126    public static Guid GetUniqueMachineId() {
127      // todo: instead of creating a new id, generate an ID from hardware IDs which is always the same for one machine
128      if (Settings.Default.Guid == Guid.Empty) {
129        Guid id = Guid.NewGuid();
130        Settings.Default.Guid = id;
131        Settings.Default.Save();
132        return id;
133      } else
134        return Settings.Default.Guid;
135    }
136
137    /// <summary>
138    /// MegaBytes
139    /// </summary>
140    private static int? GetPhysicalMemory() {
141      long? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
142      if (res != null)
143        return (int)(res / 1024 / 1024);
144      else
145        return null;
146    }
147
148    /// <summary>
149    /// Mhz
150    /// </summary>
151    private static int? GetCpuSpeed() {
152      return (int)GetWMIValue("Win32_Processor", "MaxClockSpeed");
153    }
154
155    private static long? GetWMIValue(string clazz, string property) {
156      ManagementClass mgtClass = new ManagementClass(clazz);
157      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
158
159      foreach (ManagementObject mgtObj in mgtCol) {
160        foreach (var prop in mgtObj.Properties) {
161          if (prop.Value != null && prop.Name == property) {
162            try {
163              return long.Parse(prop.Value.ToString());
164            }
165            catch {
166              return null;
167            }
168          }
169        }
170      }
171      return null;
172    }
173
174    public static int GetFreeMemory() {
175      PerformanceCounter counter = new PerformanceCounter("Memory", "Available Bytes", true);
176      int mb = (int)(counter.NextValue() / 1024 / 1024);
177      return mb;
178    }
179  }
180}
Note: See TracBrowser for help on using the repository browser.