public void TimeTest() { MarshalFixedMemoryBlock memory = new MarshalFixedMemoryBlock((long)(1024 * 1024 * 1024) * 4); DateTime dt = DateTime.Now; long ltmp = (long)(1024 * 1024 * 1024) * 1; memory.WriteByte(ltmp, (byte)20); memory.WriteDatetime(memory.Position, dt); memory.WriteDouble(memory.Position, 20.5); memory.WriteFloat(memory.Position, 20.5f); memory.WriteString(memory.Position, dt.ToString(), Encoding.Unicode); memory.WriteInt(memory.Position, 20); memory.WriteLong(memory.Position, 20); memory.WriteShort(memory.Position, (short)20); memory.WriteUShort(memory.Position, (ushort)20); memory.WriteUInt(memory.Position, 20); memory.WriteULong(memory.Position, 20); MarshalFixedMemoryBlock mm = new MarshalFixedMemoryBlock(1024); Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < 1000000; i++) { mm.WriteBytesDirect(500, mm.StartMemory, (int)ltmp, 50); memory.WriteUShortDirect(550, 50); memory.WriteByte(550, 1); } sw.Stop(); Debug.Print("10000000:" + sw.ElapsedMilliseconds); }
/// <summary> /// /// </summary> /// <param name="memory"></param> /// <param name="stream"></param> public static MarshalFixedMemoryBlock RecordDataToLog(this HisDataMemoryBlockCollection3 memory, Stream stream) { MarshalFixedMemoryBlock mfb = new MarshalFixedMemoryBlock(memory.TagAddress.Count * 12 + 4); mfb.WriteInt(0, (int)mfb.Length); foreach (var vv in memory.TagAddress) { mfb.Write(vv.Key); mfb.Write(stream.Position); RecordToLog2(new IntPtr(memory.ReadDataBaseAddressByIndex(vv.Value)), memory.ReadDataSizeByIndex(vv.Value), stream); } return(mfb); }
/// <summary> /// 拷贝DataRegion数据 /// </summary> /// <param name="msource">源流</param> /// <param name="sourceheadpoint">源数据块指针</param> /// <param name="tagcount">变量个数</param> /// <param name="blockcount">数据块个数</param> /// <param name="mtarget">目标流</param> /// <param name="targetheadpoint">目标数据块</param> private unsafe void CopyRegionData(System.IO.Stream msource, MarshalFixedMemoryBlock sourceheadpoint, int tagcount, int blockcount, System.IO.Stream mtarget, out MarshalFixedMemoryBlock targetheadpoint) { //Stopwatch sw = new Stopwatch(); //sw.Start(); MarshalFixedMemoryBlock re = new MarshalFixedMemoryBlock(sourceheadpoint.Length); MarshalFixedMemoryBlock data = new MarshalFixedMemoryBlock(blockcount * 302 * 264 * 2); //int bufferLenght = 2*1024 * 1024; //IntPtr[] databuffers = new IntPtr[blockcount]; //long[] databufferLocations = new long[blockcount]; //int[] databufferLens = new int[blockcount]; //for(int i=0;i<blockcount;i++) //{ // databuffers[i] = Marshal.AllocHGlobal(bufferLenght); //} //sw.Stop(); //LoggerService.Service.Debug("HisDataArrange", "初始化分配内存 耗时:"+ ltmp1 +"," +(sw.ElapsedMilliseconds-ltmp1)); mtarget.Position += tagcount * 8 * blockcount; long mtargetposition = mtarget.Position; int offset = 0; for (int tagid = 0; tagid < tagcount; tagid++) { data.Clear(); offset = 0; mtargetposition = mtarget.Position; for (int blockid = 0; blockid < blockcount; blockid++) { CheckPaused(); var baseaddress = sourceheadpoint.ReadInt(blockid * tagcount * 8 + tagid * 8); //var baseaddress = sourceheadpoint.ReadLong(j * tagcount * 8 + i * 8 + 4); if (baseaddress > 0) { //重新组织数据块指针的分布形式,使得一个变量的数据块指针在一起 //re.WriteInt(j * 12 + i * blockcount * 12, (int)(offset | 0x80000000)); //re.WriteLong(j * 12 + i * blockcount * 12 + 4, mtargetposition); re.WriteLong(blockid * 8 + tagid * blockcount * 8, mtargetposition + offset); int datasize = 0; int dataloc = 0; if (baseaddress >= databufferLocations[blockid] && (baseaddress - databufferLocations[blockid] + 4) <= databufferLens[blockid] && (baseaddress - databufferLocations[blockid] + 4 + MemoryHelper.ReadInt32((void *)databuffers[blockid], baseaddress - databufferLocations[blockid])) <= databufferLens[blockid]) { datasize = MemoryHelper.ReadInt32((void *)databuffers[blockid], baseaddress - databufferLocations[blockid]); dataloc = (int)(baseaddress - databufferLocations[blockid] + 4); } else { int len = (int)Math.Min(bufferLenght, msource.Length - msource.Position); msource.Position = baseaddress; msource.Read(new Span <byte>((void *)databuffers[blockid], len)); databufferLocations[blockid] = baseaddress; databufferLens[blockid] = len; datasize = MemoryHelper.ReadInt32((void *)databuffers[blockid], 0); dataloc = (int)(baseaddress - databufferLocations[blockid] + 4); } data.CheckAndResize(data.Position + datasize + 4, 0.5); data.WriteInt(offset, datasize); offset += 4; if ((dataloc + datasize) > bufferLenght || datasize <= 0) { targetheadpoint = null; continue; } Buffer.MemoryCopy((void *)(databuffers[blockid] + dataloc), (void *)(data.Buffers + offset), datasize, datasize); data.Position += datasize; offset += datasize; } else { //重新组织数据块指针的分布形式,使得一个变量的数据块指针在一起 //re.WriteInt(j * 12 + i * blockcount * 12, 0); re.WriteLong(blockid * 8 + tagid * blockcount * 8, 0); } } data.WriteToStream(mtarget, 0, offset); } data.Dispose(); //for (int i = 0; i < blockcount; i++) //{ // Marshal.FreeHGlobal(databuffers[i]); //} targetheadpoint = re; }