| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * Copyright 2008-2009 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| 5 | * Modified By: Garrett Trant <gtrant@users.sourceforge.net> |
|---|
| 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Text; |
|---|
| 25 | using System.Reflection; |
|---|
| 26 | using System.Runtime.InteropServices; |
|---|
| 27 | using Microsoft.Win32.SafeHandles; |
|---|
| 28 | using System.ComponentModel; |
|---|
| 29 | |
|---|
| 30 | namespace Eraser.Util |
|---|
| 31 | { |
|---|
| 32 | public static class SystemInfo |
|---|
| 33 | { |
|---|
| 34 | /// <summary> |
|---|
| 35 | /// Retrieves the current value of the high-resolution performance counter. |
|---|
| 36 | /// </summary> |
|---|
| 37 | public static long PerformanceCounter |
|---|
| 38 | { |
|---|
| 39 | get |
|---|
| 40 | { |
|---|
| 41 | long result = 0; |
|---|
| 42 | if (NativeMethods.QueryPerformanceCounter(out result)) |
|---|
| 43 | return result; |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | /// <summary> |
|---|
| 49 | /// Gets the current CPU type of the system. |
|---|
| 50 | /// </summary> |
|---|
| 51 | /// <returns>One of the <see cref="ProcessorTypes"/> enumeration values.</returns> |
|---|
| 52 | public static ProcessorArchitecture ProcessorArchitecture |
|---|
| 53 | { |
|---|
| 54 | get |
|---|
| 55 | { |
|---|
| 56 | NativeMethods.SYSTEM_INFO info = new NativeMethods.SYSTEM_INFO(); |
|---|
| 57 | NativeMethods.GetSystemInfo(out info); |
|---|
| 58 | |
|---|
| 59 | switch (info.processorArchitecture) |
|---|
| 60 | { |
|---|
| 61 | case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_AMD64: |
|---|
| 62 | return ProcessorArchitecture.Amd64; |
|---|
| 63 | case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_IA64: |
|---|
| 64 | return ProcessorArchitecture.IA64; |
|---|
| 65 | case NativeMethods.SYSTEM_INFO.ProcessorArchitecture.PROCESSOR_ARCHITECTURE_INTEL: |
|---|
| 66 | return ProcessorArchitecture.X86; |
|---|
| 67 | default: |
|---|
| 68 | return ProcessorArchitecture.None; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public class DiskPerformanceInfo |
|---|
| 74 | { |
|---|
| 75 | unsafe internal DiskPerformanceInfo(NativeMethods.DiskPerformanceInfoInternal info) |
|---|
| 76 | { |
|---|
| 77 | BytesRead = info.BytesRead; |
|---|
| 78 | BytesWritten = info.BytesWritten; |
|---|
| 79 | ReadTime = info.ReadTime; |
|---|
| 80 | WriteTime = info.WriteTime; |
|---|
| 81 | IdleTime = info.IdleTime; |
|---|
| 82 | ReadCount = info.ReadCount; |
|---|
| 83 | WriteCount = info.WriteCount; |
|---|
| 84 | QueueDepth = info.QueueDepth; |
|---|
| 85 | SplitCount = info.SplitCount; |
|---|
| 86 | QueryTime = info.QueryTime; |
|---|
| 87 | StorageDeviceNumber = info.StorageDeviceNumber; |
|---|
| 88 | StorageManagerName = new string((char*)info.StorageManagerName); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | public long BytesRead { get; private set; } |
|---|
| 92 | public long BytesWritten { get; private set; } |
|---|
| 93 | public long ReadTime { get; private set; } |
|---|
| 94 | public long WriteTime { get; private set; } |
|---|
| 95 | public long IdleTime { get; private set; } |
|---|
| 96 | public uint ReadCount { get; private set; } |
|---|
| 97 | public uint WriteCount { get; private set; } |
|---|
| 98 | public uint QueueDepth { get; private set; } |
|---|
| 99 | public uint SplitCount { get; private set; } |
|---|
| 100 | public long QueryTime { get; private set; } |
|---|
| 101 | public uint StorageDeviceNumber { get; private set; } |
|---|
| 102 | public string StorageManagerName { get; private set; } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | /// <summary> |
|---|
| 106 | /// Queries the performance information for the given disk. |
|---|
| 107 | /// </summary> |
|---|
| 108 | /// <param name="diskHandle">A read-only handle to a device (disk).</param> |
|---|
| 109 | /// <returns>A DiskPerformanceInfo structure describing the performance |
|---|
| 110 | /// information for the given disk.</returns> |
|---|
| 111 | public static DiskPerformanceInfo QueryDiskPerformanceInfo(SafeFileHandle diskHandle) |
|---|
| 112 | { |
|---|
| 113 | if (diskHandle.IsInvalid) |
|---|
| 114 | throw new ArgumentException("The disk handle must not be invalid."); |
|---|
| 115 | |
|---|
| 116 | //This only works if the user has turned on the disk performance |
|---|
| 117 | //counters with 'diskperf -y'. These counters are off by default |
|---|
| 118 | NativeMethods.DiskPerformanceInfoInternal result = |
|---|
| 119 | new NativeMethods.DiskPerformanceInfoInternal(); |
|---|
| 120 | uint bytesReturned = 0; |
|---|
| 121 | if (NativeMethods.DeviceIoControl(diskHandle, NativeMethods.IOCTL_DISK_PERFORMANCE, |
|---|
| 122 | IntPtr.Zero, 0, out result, (uint)Marshal.SizeOf(result), out bytesReturned, IntPtr.Zero)) |
|---|
| 123 | { |
|---|
| 124 | return new DiskPerformanceInfo(result); |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | return null; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|
| 130 | } |
|---|