Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5591 was 5536, checked in by cneumuel, 14 years ago

#1233

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