如何动态扩展内存映射文件

时间:2011-05-23 11:21:45

标签: c# .net-4.0 memory-mapped-files

我用C#来解决以下问题...... - 创建一个可以快速接收大量数据的应用程序 - 您必须能够分析收到的数据,同时传入更多数据。 - 使用尽可能少的CPU和磁盘

我对算法的想法是......

SIZE = 10MB
Create a mmf with the size of SIZE
On data recived:
  if data can't fit mmf: increase mmf.size by SIZE
  write the data to mmf

- >当使用之前的“房间/空间”时,光盘上的尺寸以10MB的块数增加。

如何在C#中完成“通过SIZE增加mmf.size”?我已经找到了很多关于创建mmfs和视图的简单示例,但是我看到的唯一地方(link)我看到的代码实际上增加了mmfs区域使用了无法编译的代码。任何帮助都会受到极大关注。

EDIT 这会导致异常:

private void IncreaseFileSize()
{
    int theNewMax = this.currentMax + INCREMENT_SIZE;
    this.currentMax = theNewMax;

    this.mmf.Dispose();

    this.mmf = MemoryMappedFile.CreateFromFile(this.FileName, FileMode.Create, "MyMMF", theNewMax);
    this.view = mmf.CreateViewAccessor(0, theNewMax);            
}

抛出此异常:进程无法访问文件“C:\ Users \ moberg \ Documents \ data.bin”,因为它正由另一个进程使用。

5 个答案:

答案 0 :(得分:26)

Once you map a file in memory, you cannot increase its size.这是内存映射文件的已知限制。

  

...您必须计算或估计已完成文件的大小,因为文件映射对象的大小是静态的;一旦创建,它们的大小就不会增加或减少。

一种策略是使用存储在non-persisted memory mapped files of a given size中的块,比如1GB或2GB。您可以通过自己设计的顶级ViewAccessor来管理这些(可能需要从MemoryMappedViewAccessor执行所需方法的基本方法)。

编辑或者你可以创建一个你想要使用的最大尺寸的非持久性内存映射文件(比如8GB启动,并带有一个参数来启动你的启动时调整它)应用程序)并检索每个逻辑块的MemoryMappedViewAccessor个。在请求每个视图之前,非持久文件不会使用物理资源。

答案 1 :(得分:5)

嗯,你可以!!

这是我对可增长内存映射文件的实现:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.MemoryMappedFiles;

namespace MmbpTree
{
    public unsafe sealed class GrowableMemoryMappedFile : IDisposable
    {

        private const int AllocationGranularity = 64 * 1024;

        private class MemoryMappedArea
        {
            public MemoryMappedFile Mmf;
            public byte* Address;
            public long Size;
        }


        private FileStream fs;

        private List<MemoryMappedArea> areas = new List<MemoryMappedArea>();
        private long[] offsets;
        private byte*[] addresses;

        public long Length
        {
            get {
                CheckDisposed();
                return fs.Length;
            }
        }

        public GrowableMemoryMappedFile(string filePath, long initialFileSize)
        {
            if (initialFileSize <= 0 || initialFileSize % AllocationGranularity != 0)
            {
                throw new ArgumentException("The initial file size must be a multiple of 64Kb and grater than zero");
            }
            bool existingFile = File.Exists(filePath);
            fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
            if (existingFile)
            {
                if (fs.Length <=  0 || fs.Length % AllocationGranularity != 0)
                {
                    throw new ArgumentException("Invalid file. Its lenght must be a multiple of 64Kb and greater than zero");
                }
            }
            else
            { 
                fs.SetLength(initialFileSize);
            }
            CreateFirstArea();
        }

        private void CreateFirstArea()
        {
            var mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite,  null, HandleInheritability.None, true);
            var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), 
                Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                0, 0, new UIntPtr((ulong) fs.Length), null);
            if (address == null) throw new Win32Exception();

            var area = new MemoryMappedArea
            {
                Address = address,
                Mmf = mmf,
                Size = fs.Length
            };
            areas.Add(area);

            addresses = new byte*[] { address };
            offsets = new long[] { 0 };

        }


        public void Grow(long bytesToGrow)
        {
            CheckDisposed();
            if (bytesToGrow <= 0 || bytesToGrow % AllocationGranularity != 0)  {
                throw new ArgumentException("The growth must be a multiple of 64Kb and greater than zero");
            }
            long offset = fs.Length;
            fs.SetLength(fs.Length + bytesToGrow);
            var mmf = MemoryMappedFile.CreateFromFile(fs, null, fs.Length, MemoryMappedFileAccess.ReadWrite, null, HandleInheritability.None, true);
            uint* offsetPointer = (uint*)&offset;
            var lastArea = areas[areas.Count - 1];
            byte* desiredAddress = lastArea.Address + lastArea.Size;
            var address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(), 
                Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), desiredAddress);
            if (address == null) {
                address = Win32FileMapping.MapViewOfFileEx(mmf.SafeMemoryMappedFileHandle.DangerousGetHandle(),
                   Win32FileMapping.FileMapAccess.Read | Win32FileMapping.FileMapAccess.Write,
                   offsetPointer[1], offsetPointer[0], new UIntPtr((ulong)bytesToGrow), null);
            }
            if (address == null) throw new Win32Exception();
            var area = new MemoryMappedArea {
                Address = address,
                Mmf = mmf,
                Size = bytesToGrow
            };
            areas.Add(area);
            if (desiredAddress != address) {
                offsets = offsets.Add(offset);
                addresses = addresses.Add(address);
            }
        }

        public byte* GetPointer(long offset)
        {
            CheckDisposed();
            int i = offsets.Length;
            if (i <= 128) // linear search is more efficient for small arrays. Experiments show 140 as the cutpoint on x64 and 100 on x86.
            {
                while (--i > 0 && offsets[i] > offset);
            }
            else // binary search is more efficient for large arrays
            {
                i = Array.BinarySearch<long>(offsets, offset);
                if (i < 0) i = ~i - 1;
            }
            return addresses[i] + offset - offsets[i];
        }

        private bool isDisposed;

        public void Dispose()
        {
            if (isDisposed) return;
            isDisposed = true;
            foreach (var a in this.areas)
            {
                Win32FileMapping.UnmapViewOfFile(a.Address);
                a.Mmf.Dispose();
            }
            fs.Dispose();
            areas.Clear();
        }

        private void CheckDisposed()
        {
            if (isDisposed) throw new ObjectDisposedException(this.GetType().Name);
        }

        public void Flush()
        {
            CheckDisposed();
            foreach (var area in areas)
            {
                if (!Win32FileMapping.FlushViewOfFile(area.Address, new IntPtr(area.Size))) {
                    throw new Win32Exception();
                }
            }
            fs.Flush(true);
        }
    }
}

以下是Win32FileMapping类:

using System;
using System.Runtime.InteropServices;

namespace MmbpTree
{
    public static unsafe class Win32FileMapping
    {
        [Flags]
        public enum FileMapAccess : uint
        {
            Copy = 0x01,
            Write = 0x02,
            Read = 0x04,
            AllAccess = 0x08,
            Execute = 0x20,
        }

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern byte* MapViewOfFileEx(IntPtr mappingHandle,
                                            FileMapAccess access,
                                            uint offsetHigh,
                                            uint offsetLow,
                                            UIntPtr bytesToMap,
                                            byte* desiredAddress);

        [DllImport("kernel32.dll", SetLastError = true)]
        public static extern bool UnmapViewOfFile(byte* address);


        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool FlushViewOfFile(byte* address, IntPtr bytesToFlush);
    }
}

这里有Extensions课程:

using System;

namespace MmbpTree
{
    public static class Extensions
    {
        public static T[] Add<T>(this T[] array, T element)
        {
            var result = new T[array.Length + 1];
            Array.Copy(array, result, array.Length);
            result[array.Length] = element;
            return result;
        }

        public static unsafe byte*[] Add(this byte*[] array, byte* element)
        {
            var result = new byte*[array.Length + 1];
            Array.Copy(array, result, array.Length);
            result[array.Length] = element;
            return result;
        }
    }
}

正如你所看到的,我采取了不安全的方法。它是获得内存映射文件性能优势的唯一方法。

要使用此功能,您需要考虑以下概念:

  • 阻止页面。这是您使用的连续内存地址和存储空间的最小区域。块或页面的大小必须是基础系统页面大小的倍数( 4Kb )。
  • 初始文件大小。它必须是块或页面大小的倍数,并且必须是系统分配粒度的倍数( 64Kb )。
  • 文件增长。它必须是块或页面大小的倍数,并且必须是系统分配粒度的倍数( 64Kb )。

例如,您可能希望使用页面大小为1Mb,文件增长为64Mb,初始大小为1Gb。您可以通过调用GetPointer获取指向页面的指针,使用Grow扩展文件并使用Flush刷新文件:

const int InitialSize = 1024 * 1024 * 1024;
const int FileGrowth = 64 * 1024 * 1024;
const int PageSize = 1024 * 1024;
using (var gmmf = new GrowableMemoryMappedFile("mmf.bin", InitialSize))
{
    var pageNumber = 32;
    var pointer = gmmf.GetPointer(pageNumber * PageSize);

    // you can read the page content:
    byte firstPageByte = pointer[0];
    byte lastPageByte = pointer[PageSize - 1];

    // or write it
    pointer[0] = 3;
    pointer[PageSize -1] = 43;


    /* allocate more pages when needed */
    gmmf.Grow(FileGrowth);

    /* use new allocated pages */

    /* flushing the file writes to the underlying file */ 
    gmmf.Flush();

}

答案 2 :(得分:1)

代码无法编译的原因是它使用了不存在的重载。 要么自己创建一个文件流并将其传递给正确的重载(假设2000将是你的新大小):

FileStream fs = new FileStream("C:\MyFile.dat", FileMode.Open);
MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile(fs, "someName", 2000,
 MemoryMappedFileAccess.ReadWriteExecute, null, HandleInheritablity.None, false);

或者使用此重载来跳过创建过滤器:

MemoryMappedFile mmf = MemoryMappedFile.CreateFromFile("C:\MyFile.dat", 
          FileMode.Open, "someName", 2000);

答案 3 :(得分:0)

使用带有capacity参数的MemoryMappedFile.CreateFromFile重载。

答案 4 :(得分:0)

我发现关闭并重新创建具有相同名称但新大小的mmf可以适用于所有意图和目的

                using (var mmf = MemoryMappedFile.CreateOrOpen(SenderMapName, 1))
                {
                    mmf.SafeMemoryMappedFileHandle.Close();
                }
                using (var sender = MemoryMappedFile.CreateNew(SenderMapName, bytes.Length))

它真的很快。