public static ThreadPriority GetCurrentThreadPriority() { if (PlatformDetails.RunningOnPosix) { var threadId = Syscall.gettid(); var value = Syscall.getpriority((int)Prio.PROCESS, threadId); // we assume no failure here, since clearing errno isn't really possible here return(FixLinuxPriority(value)); } IntPtr handle = IntPtr.Zero; try { uint threadId = Win32ThreadsMethods.GetCurrentThreadId(); handle = Win32ThreadsMethods.OpenThread(ThreadAccess.QueryInformation, false, threadId); if (handle == IntPtr.Zero) { throw new Win32Exception("Failed to setup thread priority, couldn't open the current thread"); } return(Win32ThreadsMethods.GetThreadPriority(handle)); } finally { if (handle != IntPtr.Zero) { Win32ThreadsMethods.CloseHandle(handle); } } }
public ThreadStats() { ThreadInstance = Thread.CurrentThread; Name = ThreadInstance.Name; Id = ThreadInstance.ManagedThreadId; UnmanagedThreadId = PlatformDetails.RunningOnPosix ? Syscall.gettid() : (int)Win32ThreadsMethods.GetCurrentThreadId(); }
//method for no seek penalty private static RavenDriveType HasNoSeekPenalty(string sDrive) { var hDrive = CreateFileW(sDrive, 0, // No access to drive FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); DEVICE_SEEK_PENALTY_DESCRIPTOR querySeekPenaltyDesc; bool querySeekPenaltyResult; try { if (hDrive == INVALID_HANDLE_VALUE) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("CreateFile failed. " + message); } return(RavenDriveType.Unknown); } var IOCTL_STORAGE_QUERY_PROPERTY = CTL_CODE(IOCTL_STORAGE_BASE, 0x500, METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h var query_seek_penalty = new STORAGE_PROPERTY_QUERY { PropertyId = StorageDeviceSeekPenaltyProperty, QueryType = PropertyStandardQuery }; querySeekPenaltyDesc = new DEVICE_SEEK_PENALTY_DESCRIPTOR(); uint returnedQuerySeekPenaltySize; querySeekPenaltyResult = DeviceIoControl(hDrive, IOCTL_STORAGE_QUERY_PROPERTY, ref query_seek_penalty, (uint)Marshal.SizeOf(query_seek_penalty), ref querySeekPenaltyDesc, (uint)Marshal.SizeOf(querySeekPenaltyDesc), out returnedQuerySeekPenaltySize, IntPtr.Zero); } finally { Win32ThreadsMethods.CloseHandle(hDrive); } if (querySeekPenaltyResult == false) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("DeviceIoControl failed. " + message); } return(RavenDriveType.Unknown); } return(querySeekPenaltyDesc.IncursSeekPenalty == false ? RavenDriveType.SSD : RavenDriveType.HDD); }
public static unsafe void SetCurrentThreadPriority(ThreadPriority priority) { if (PlatformDetails.RunningOnPosix) { var nice = FixLinuxPriority(priority); var threadId = Syscall.gettid(); var success = Syscall.setpriority((int)Prio.PROCESS, threadId, nice); if (success != 0) { int lastError = Marshal.GetLastWin32Error(); if (_log.IsInfoEnabled) { _log.Info($"SetThreadPriority failed to set thread priority. threadId:{threadId}, error code {lastError} - {(Errno)lastError}"); } throw new InvalidOperationException("Failed to set priority to thread " + threadId + " with " + (Errno)lastError); } } else { IntPtr handle = IntPtr.Zero; try { uint threadId = Win32ThreadsMethods.GetCurrentThreadId(); handle = Win32ThreadsMethods.OpenThread(ThreadAccess.SetInformation, false, threadId); if (handle == IntPtr.Zero) { throw new Win32Exception("Failed to setup thread priority, couldn't open the current thread"); } var success = Win32ThreadsMethods.SetThreadPriority(handle, (int)priority); if (success == 0) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); if (_log.IsInfoEnabled) { _log.Info($"SetThreadPriority failed to set thread priority. threadId:{threadId}, error code {lastWin32ErrorCode}"); } throw new Win32Exception(lastWin32ErrorCode, "Failed to set priority to thread " + threadId); } } finally { Win32ThreadsMethods.CloseHandle(handle); } } }
public static ulong GetCurrentThreadId() { if (RunningOnPosix == false) { return(Win32ThreadsMethods.GetCurrentThreadId()); } if (RunningOnLinux) { return((ulong)Syscall.syscall0(PerPlatformValues.SyscallNumbers.SYS_gettid)); } // OSX return(macSyscall.pthread_self()); }
//method for disk extents public static uint?GetPhysicalDriveNumber(string driveLetter) { var sDrive = "\\\\.\\" + driveLetter + ":"; var hDrive = CreateFileW(sDrive, 0, // No access to drive FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); VOLUME_DISK_EXTENTS query_disk_extents; bool query_disk_extents_result; try { if (hDrive == INVALID_HANDLE_VALUE) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("CreateFile failed. " + message); } return(uint.MinValue); } uint IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = CTL_CODE(IOCTL_VOLUME_BASE, 0, METHOD_BUFFERED, FILE_ANY_ACCESS); // From winioctl.h query_disk_extents = new VOLUME_DISK_EXTENTS(); uint returned_query_disk_extents_size; query_disk_extents_result = DeviceIoControl(hDrive, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, IntPtr.Zero, 0, ref query_disk_extents, (uint)Marshal.SizeOf(query_disk_extents), out returned_query_disk_extents_size, IntPtr.Zero); } finally { Win32ThreadsMethods.CloseHandle(hDrive); } if (query_disk_extents_result == false || query_disk_extents.Extents.Length != 1) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("DeviceIoControl failed. " + message); } return(null); } return(query_disk_extents.Extents[0].DiskNumber); }
//method for nominal media rotation rate //(administrative privilege is required) private static RavenDriveType HasNominalMediaRotationRate(string sDrive) { var hDrive = CreateFileW(sDrive, GENERIC_READ | GENERIC_WRITE, //administrative privilege is required FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); ATAIdentifyDeviceQuery id_query; bool result; try { if (hDrive == INVALID_HANDLE_VALUE) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("CreateFile failed. " + message); } return(RavenDriveType.Unknown); } var ioctlAtaPassThrough = CTL_CODE(IOCTL_SCSI_BASE, 0x040b, METHOD_BUFFERED, FILE_READ_ACCESS | FILE_WRITE_ACCESS); // From ntddscsi.h id_query = new ATAIdentifyDeviceQuery(); id_query.data = new ushort[256]; id_query.header.Length = (ushort)Marshal.SizeOf(id_query.header); id_query.header.AtaFlags = (ushort)ATA_FLAGS_DATA_IN; id_query.header.DataTransferLength = (uint)(id_query.data.Length * 2); // Size of "data" in bytes id_query.header.TimeOutValue = 3; // Sec #pragma warning disable 618 id_query.header.DataBufferOffset = Marshal.OffsetOf(typeof(ATAIdentifyDeviceQuery), "data"); #pragma warning restore 618 id_query.header.PreviousTaskFile = new byte[8]; id_query.header.CurrentTaskFile = new byte[8]; id_query.header.CurrentTaskFile[6] = 0xec; // ATA IDENTIFY DEVICE uint retvalSize; result = DeviceIoControl(hDrive, ioctlAtaPassThrough, ref id_query, (uint)Marshal.SizeOf(id_query), ref id_query, (uint)Marshal.SizeOf(id_query), out retvalSize, IntPtr.Zero); } finally { Win32ThreadsMethods.CloseHandle(hDrive); } if (result == false) { var message = GetErrorMessage(Marshal.GetLastWin32Error()); if (Log.IsInfoEnabled) { Log.Info("DeviceIoControl failed. " + message); } return(RavenDriveType.Unknown); } //word index of nominal media rotation rate //(1 means non-rotate device) const int kNominalMediaRotRateWordIndex = 217; return(id_query.data[kNominalMediaRotRateWordIndex] == 1 ? RavenDriveType.SSD : RavenDriveType.HDD); }
public static int GetCurrentThreadId() { return(RunningOnPosix ? Syscall.gettid() : (int)Win32ThreadsMethods.GetCurrentThreadId()); }