📜 ⬆️ ⬇️

Here be dragons: Memory management in Windows as it is [3/3]


Catalog:
One
Two
Three

μTorrent


This is probably the funniest part. With obviously high programming skills, the authors either did not read or did not understand this document. Here is what happens with the default settings:


Thread 4600 opens for distribution a file with the system cache turned off. And everything is done without decreasing the priority:


I don’t use torrents and I don’t have files to distribute, and the return channels in local latitudes are quite narrow, so I had to write a "emulator" of the mutator. To begin with, the “old version” that did not turn off caching (wrote on the sysharp, because not everyone can have Visual Studio installed):
using System;
using System.IO;
using System.Threading;

namespace abuseio
{
    class Program
    {
        static void Main(string[] args)
        {
            for (var i = 0; i < 10; i++)
            {
                (new Thread(() => {
                    var file = File.OpenRead(args[0]);
                    var chunkSize = 16384;
                    var chunks = (int)(file.Length / chunkSize);
                    var rnd = new Random();
                    var buffer = new byte[chunkSize];
                    while (true) {
                        long index = rnd.Next(chunks);
                        file.Seek(index * chunkSize, SeekOrigin.Begin);
                        file.Read(buffer, 0, chunkSize);
                    }
                })).Start();
            }
        }
    }
}

( ). :

. :

( , ) Active , . Standby priority 7 :
, , shell32.dll:

« »:

, , . , - «» , . , .

«» :
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using Microsoft.Win32.SafeHandles;

namespace abuseio
{
    class Program
    {
        [Flags]
        enum FileAttributes : uint
        {
            Readonly = 0x00000001,
            Hidden = 0x00000002,
            System = 0x00000004,
            Directory = 0x00000010,
            Archive = 0x00000020,
            Device = 0x00000040,
            Normal = 0x00000080,
            Temporary = 0x00000100,
            SparseFile = 0x00000200,
            ReparsePoint = 0x00000400,
            Compressed = 0x00000800,
            Offline = 0x00001000,
            NotContentIndexed = 0x00002000,
            Encrypted = 0x00004000,
            Write_Through = 0x80000000,
            Overlapped = 0x40000000,
            NoBuffering = 0x20000000,
            RandomAccess = 0x10000000,
            SequentialScan = 0x08000000,
            DeleteOnClose = 0x04000000,
            BackupSemantics = 0x02000000,
            PosixSemantics = 0x01000000,
            OpenReparsePoint = 0x00200000,
            OpenNoRecall = 0x00100000,
            FirstPipeInstance = 0x00080000
        }

        [DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern SafeFileHandle CreateFile(
           string fileName,
           [MarshalAs(UnmanagedType.U4)] FileAccess fileAccess,
           [MarshalAs(UnmanagedType.U4)] FileShare fileShare,
           IntPtr securityAttributes,
           [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
           [MarshalAs(UnmanagedType.U4)] FileAttributes flags,
           IntPtr template);

        static void Main(string[] args)
        {
            for (var i = 0; i < 10; i++)
            {
                (new Thread(() => {
                    var fileHandle = CreateFile(args[0], FileAccess.Read, FileShare.Read, IntPtr.Zero, FileMode.Open, FileAttributes.NoBuffering, IntPtr.Zero);
                    var file = new FileStream(fileHandle, FileAccess.Read);
                    var chunkSize = 16384;
                    var chunks = (int)(file.Length / chunkSize);
                    var rnd = new Random();
                    var buffer = new byte[chunkSize];
                    while (true) {
                        long index = rnd.Next(chunks);
                        file.Seek(index * chunkSize, SeekOrigin.Begin);
                        file.Read(buffer, 0, chunkSize);
                    }
                })).Start();
            }
        }
    }
}

«» (, ):
1. -
2. . —
3.
4. Last but not least, . , «» .

, . :
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
using Microsoft.Win32.SafeHandles;

namespace abuseio
{
    class Program
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();

        enum ThreadPriority
        {
            THREAD_MODE_BACKGROUND_BEGIN = 0x00010000,
            THREAD_MODE_BACKGROUND_END = 0x00020000,
            THREAD_PRIORITY_ABOVE_NORMAL = 1,
            THREAD_PRIORITY_BELOW_NORMAL = -1,
            THREAD_PRIORITY_HIGHEST = 2,
            THREAD_PRIORITY_IDLE = -15,
            THREAD_PRIORITY_LOWEST = -2,
            THREAD_PRIORITY_NORMAL = 0,
            THREAD_PRIORITY_TIME_CRITICAL = 15
        }

        [DllImport("kernel32.dll")]
        static extern bool SetThreadPriority(IntPtr hThread, ThreadPriority nPriority);

        static void Main(string[] args)
        {
            for (var i = 0; i < 10; i++)
            {
                (new Thread(() => {
                    SetThreadPriority(GetCurrentThread(), ThreadPriority.THREAD_MODE_BACKGROUND_BEGIN);
                    var file = File.OpenRead(args[0]);
                    var chunkSize = 16384;
                    var chunks = (int)(file.Length / chunkSize);
                    var rnd = new Random();
                    var buffer = new byte[chunkSize];
                    while (true) {
                        long index = rnd.Next(chunks);
                        file.Seek(index * chunkSize, SeekOrigin.Begin);
                        file.Read(buffer, 0, chunkSize);
                    }
                })).Start();
            }
        }
    }
}


, , :

«» , :





, , . , 1-2-3.
1. :


2. :
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\utorrent.exe]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\utorrent.exe\PerfOptions]
"IoPriority"=dword:00000000
"PagePriority"=dword:00000001



3.

:



.

swappiness

, « ». (aging/trimming, ) — . swappiness , low latency , swappiness :
Decrease /proc/sys/vm/swappiness?

Swapout is good. It frees up unused memory. I run my desktop machines at
swappiness=100.

. , , . , , .

— .

.

')

Source: https://habr.com/ru/post/107637/


All Articles