Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5531


Ignore:
Timestamp:
02/21/11 18:35:41 (14 years ago)
Author:
ascheibe
Message:

#1233 fixed reading memory and cpu speed

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave/3.4/ConfigManager.cs

    r5404 r5531  
    2222using System;
    2323using System.Collections.Generic;
    24 using HeuristicLab.Services.Hive.Common;
    25 using HeuristicLab.Clients.Hive.Slave;
     24using System.Diagnostics;
     25using System.Management;
     26using HeuristicLab.Clients.Hive.Slave.Properties;
    2627using HeuristicLab.Services.Hive.Common.DataTransfer;
    27 using HeuristicLab.Clients.Hive.Slave.Properties;
    28 using System.Management;
    29 using System.Diagnostics;
    3028
    3129
     
    4745    public Core Core { get; set; }
    4846    private HeuristicLab.Services.Hive.Common.DataTransfer.Slave slave;
    49    
     47
    5048    /// <summary>
    5149    /// Constructor for the singleton, must recover Guid, Calendar, ...
     
    5957      slave.CpuArchitecture = Environment.Is64BitOperatingSystem ? CpuArchitecture.x64 : CpuArchitecture.x86;
    6058      slave.OperatingSystem = Environment.OSVersion.VersionString;
     59      slave.CpuSpeed = GetCpuSpeed();
     60      slave.FreeMemory = GetFreeMemory();
    6161    }
    6262
     
    136136    }
    137137
    138     public static int GetPhysicalMemory() {
    139       return 1024; // todo
     138    private static int? GetPhysicalMemory() {
     139      int? res = GetWMIValue("Win32_ComputerSystem", "TotalPhysicalMemory");
     140      if (res != null)
     141        return res / 1024 / 1024;
     142      else
     143        return null;
     144    }
     145
     146    private static int? GetCpuSpeed() {
     147      return GetWMIValue("Win32_Processor", "MaxClockSpeed");
     148    }
     149
     150    private static int? GetWMIValue(string Class, string Property) {
     151      ManagementClass mgtClass = new ManagementClass(Class);
     152      ManagementObjectCollection mgtCol = mgtClass.GetInstances();
     153
     154      foreach (ManagementObject mgtObj in mgtCol) {
     155        foreach (var prop in mgtObj.Properties) {
     156          if (prop.Value != null && prop.Name == Property) {
     157            try {
     158              return int.Parse(prop.Value.ToString());
     159            }
     160            catch {
     161              return null;
     162            }
     163          }
     164        }
     165      }
     166      return null;
    140167    }
    141168
Note: See TracChangeset for help on using the changeset viewer.