Пример #1
0
        public static string PosixIsSwappingOnHddInsteadOfSsd()
        {
            {
                var path = KernelVirtualFileSystemUtils.ReadSwapInformationFromSwapsFile();
                if (path == null) // on error return as if no swap problem
                {
                    return(null);
                }

                string foundRotationalDiskDrive = null;
                foreach (string partition in path)
                {
                    // remove numbers at end of string (i.e.: /dev/sda5 ==> sda)
                    var reg      = new System.Text.RegularExpressions.Regex(@"\d+$"); // we do not check swap file, only partitions
                    var disk     = reg.Replace(partition, "").Replace("/dev/", "");
                    var filename = $"/sys/block/{disk}/queue/rotational";
                    var isHdd    = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);


                    if (isHdd == -1)
                    {
                        return(null);
                    }
                    if (isHdd == 1)
                    {
                        foundRotationalDiskDrive = filename;
                    }
                    else if (isHdd != 0)
                    {
                        if (Log.IsOperationsEnabled)
                        {
                            Log.Operations($"Got invalid value (not 0 or 1) from {filename} = {isHdd}, assumes this is not a rotational disk");
                        }
                        foundRotationalDiskDrive = null;
                    }
                }

                string hddSwapsInsteadOfSsd = null;
                if (foundRotationalDiskDrive != null)
                {
                    // search if ssd drive is available
                    HashSet <string> disks = KernelVirtualFileSystemUtils.GetAllDisksFromPartitionsFile();
                    foreach (var disk in disks)
                    {
                        var filename = $"/sys/block/{disk}/queue/rotational";
                        var isHdd    = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);
                        if (isHdd == 0)
                        {
                            hddSwapsInsteadOfSsd = disk;
                            break;
                        }
                    }
                }

                return(hddSwapsInsteadOfSsd);
            }
        }
Пример #2
0
        public void WriteAndReadPageUsingCryptoPager_WhenTwoSwapDefined_AllResultShouldBeNotNull()
        {
            var fileName = GetTempFileName();

            string[] lines =
            {
                "Filename				Type		Size	Used	Priority",
                "/dev/sda6                               partition	999420	56320	-2",
                "/dev/sdb7                               partition	999420	56320	-2"
            };
            System.IO.File.WriteAllLines(fileName, lines);

            var swap = KernelVirtualFileSystemUtils.ReadSwapInformationFromSwapsFile(fileName);

            Assert.All(swap, s => Assert.NotNull(s.DeviceName));
        }
Пример #3
0
        public static string PosixIsSwappingOnHddInsteadOfSsd()
        {
            try
            {
                var swaps = KernelVirtualFileSystemUtils.ReadSwapInformationFromSwapsFile();
                if (swaps.Length == 0) // on error return as if no swap problem
                {
                    return(null);
                }

                string foundRotationalDiskDrive = null;
                for (int i = 0; i < swaps.Length; i++)
                {
                    if (swaps[i].IsDeviceSwapFile)
                    {
                        continue; // we do not check swap file, only partitions
                    }
                    // remove numbers at end of string (i.e.: /dev/sda5 ==> sda)
                    var disk     = _regExRemoveNumbers.Replace(swaps[i].DeviceName, "").Replace("/dev/", "");
                    var filename = $"/sys/block/{disk}/queue/rotational";
                    var isHdd    = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);

                    if (isHdd == -1)
                    {
                        return(null);
                    }
                    if (isHdd == 1)
                    {
                        foundRotationalDiskDrive = filename;
                    }
                    else if (isHdd != 0)
                    {
                        if (Log.IsOperationsEnabled)
                        {
                            Log.Operations($"Got invalid value (not 0 or 1) from {filename} = {isHdd}, assumes this is not a rotational disk");
                        }
                        foundRotationalDiskDrive = null;
                    }
                }

                string hddSwapsInsteadOfSsd = null;
                if (foundRotationalDiskDrive != null)
                {
                    // search if ssd drive is available
                    HashSet <string> disks = KernelVirtualFileSystemUtils.GetAllDisksFromPartitionsFile();
                    foreach (var disk in disks)
                    {
                        var filename = $"/sys/block/{disk}/queue/rotational";
                        var isHdd    = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);
                        if (isHdd == 0)
                        {
                            hddSwapsInsteadOfSsd = disk;
                            break;
                        }
                    }
                }

                return(hddSwapsInsteadOfSsd);
            }
            catch (Exception ex)
            {
                Log.Info("Error while trying to determine if hdd swaps instead of ssd on linux, ignoring this check and assuming no hddswap", ex);
                // ignore
                return(null);
            }
        }
Пример #4
0
        public void WriteAndReadPageUsingCryptoPager_WhenCall_AllResultShouldBeNotNull()
        {
            var swap = KernelVirtualFileSystemUtils.ReadSwapInformationFromSwapsFile();

            Assert.All(swap, s => Assert.NotNull(s.DeviceName));
        }
Пример #5
0
        public static string PosixIsSwappingOnHddInsteadOfSsd()
        {
            try
            {
                var swaps = KernelVirtualFileSystemUtils.ReadSwapInformationFromSwapsFile();
                if (swaps.Length == 0) // on error return as if no swap problem
                {
                    return(null);
                }

                const string sysBlockDirectoryPath = "/sys/block/";

                var blocks = Directory
                             .GetDirectories(sysBlockDirectoryPath)
                             .Select(x => x.Substring(sysBlockDirectoryPath.Length))
                             .Where(x => x.StartsWith("loop") == false)
                             .ToList();

                if (blocks.Count == 0)
                {
                    return(null);
                }

                string foundRotationalDiskDrive = null;
                for (int i = 0; i < swaps.Length; i++)
                {
                    if (swaps[i].IsDeviceSwapFile)
                    {
                        continue; // we do not check swap file, only partitions
                    }
                    if (TryFindDisk(swaps[i].DeviceName, out var disk) == false)
                    {
                        continue;
                    }

                    var filename = $"/sys/block/{disk}/queue/rotational";
                    if (File.Exists(filename) == false)
                    {
                        continue;
                    }

                    var isHdd = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);
                    if (isHdd == -1)
                    {
                        return(null);
                    }
                    if (isHdd == 1)
                    {
                        foundRotationalDiskDrive = filename;
                    }
                    else if (isHdd != 0)
                    {
                        if (Log.IsOperationsEnabled)
                        {
                            Log.Operations($"Got invalid value (not 0 or 1) from {filename} = {isHdd}, assumes this is not a rotational disk");
                        }
                        foundRotationalDiskDrive = null;
                    }
                }

                string hddSwapsInsteadOfSsd = null;
                if (foundRotationalDiskDrive != null)
                {
                    // search if ssd drive is available
                    foreach (var partitionDisk in KernelVirtualFileSystemUtils.GetAllDisksFromPartitionsFile())
                    {
                        //ignore ramdisks (ram0..ram15 etc)
                        if (partitionDisk.StartsWith("ram", StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        if (TryFindDisk(partitionDisk, out var disk) == false)
                        {
                            continue;
                        }

                        var filename = $"/sys/block/{disk}/queue/rotational";
                        if (File.Exists(filename) == false)
                        {
                            continue;
                        }

                        var isHdd = KernelVirtualFileSystemUtils.ReadNumberFromFile(filename);
                        if (isHdd == 0)
                        {
                            hddSwapsInsteadOfSsd = disk;
                            break;
                        }
                    }
                }

                return(hddSwapsInsteadOfSsd);

                bool TryFindDisk(string deviceName, out string disk)
                {
                    disk = null;

                    if (string.IsNullOrWhiteSpace(deviceName))
                    {
                        return(false);
                    }

                    deviceName = deviceName.Replace("/dev/", string.Empty);

                    foreach (var block in blocks)
                    {
                        if (deviceName.Contains(block))
                        {
                            disk = block;
                            return(true);
                        }
                    }

                    return(false);
                }
            }
            catch (Exception ex)
            {
                Log.Info("Error while trying to determine if hdd swaps instead of ssd on linux, ignoring this check and assuming no hddswap", ex);
                // ignore
                return(null);
            }
        }