提交 82348776 编写于 作者: cdy816's avatar cdy816

完成运行时,不停止的情况下,重新加载数据库功能。同时修改由于增加改功能,导致数据库多变量时采集不可用。

上级 ad87b462
...@@ -172,7 +172,9 @@ namespace Cdy.Tag ...@@ -172,7 +172,9 @@ namespace Cdy.Tag
public void IncRef() public void IncRef()
{ {
lock (mUserSizeLock) lock (mUserSizeLock)
Interlocked.Increment(ref mRefCount); {
mRefCount++;
}
} }
/// <summary> /// <summary>
...@@ -221,7 +223,8 @@ namespace Cdy.Tag ...@@ -221,7 +223,8 @@ namespace Cdy.Tag
private void Init(long size) private void Init(long size)
{ {
mBuffers = new byte[size]; mBuffers = new byte[size];
mHandles = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers, 0); mHandles = (IntPtr)mBuffers.AsMemory().Pin().Pointer;
//mHandles = System.Runtime.InteropServices.Marshal.UnsafeAddrOfPinnedArrayElement(mBuffers, 0);
mAllocSize = size; mAllocSize = size;
} }
...@@ -244,7 +247,6 @@ namespace Cdy.Tag ...@@ -244,7 +247,6 @@ namespace Cdy.Tag
public void ReAlloc(long size) public void ReAlloc(long size)
{ {
Init(size); Init(size);
GC.Collect();
} }
/// <summary> /// <summary>
...@@ -252,9 +254,7 @@ namespace Cdy.Tag ...@@ -252,9 +254,7 @@ namespace Cdy.Tag
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
mBuffers.AsSpan().Fill(0); Array.Clear(this.Buffers, 0, Buffers.Length);
LoggerService.Service.Info("FixedMemoryBlock", Name + " is clear !");
} }
...@@ -1002,7 +1002,7 @@ namespace Cdy.Tag ...@@ -1002,7 +1002,7 @@ namespace Cdy.Tag
{ {
//mDataBuffer = null; //mDataBuffer = null;
mBuffers = null; mBuffers = null;
LoggerService.Service.Erro("FixedMemoryBlock", Name + " Disposed "); //LoggerService.Service.Erro("FixedMemoryBlock", Name + " Disposed ");
//GC.Collect(); //GC.Collect();
} }
...@@ -1385,6 +1385,63 @@ namespace Cdy.Tag ...@@ -1385,6 +1385,63 @@ namespace Cdy.Tag
// stream.Flush(); // stream.Flush();
} }
/// <summary>
///
/// </summary>
/// <param name="memory"></param>
/// <param name="stream"></param>
public static void RecordToLog2(this FixedMemoryBlock memory, Stream stream)
{
stream.Write(memory.Buffers, 0, memory.Buffers.Length);
}
/// <summary>
///
/// </summary>
/// <param name="memory"></param>
/// <param name="fileName"></param>
public static void Dump(this FixedMemoryBlock memory, string fileName)
{
using (var stream = System.IO.File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
stream.Write(memory.Buffers, 0, memory.Buffers.Length);
stream.Flush();
}
}
/// <summary>
///
/// </summary>
/// <param name="memory"></param>
/// <param name="stream"></param>
public static void Dump(this FixedMemoryBlock memory, Stream stream)
{
stream.Write(memory.Buffers,0, memory.Buffers.Length);
}
/// <summary>
///
/// </summary>
/// <param name="memory"></param>
/// <param name="time"></param>
public static void Dump(this FixedMemoryBlock memory, DateTime time)
{
string fileName = memory.Name + "_" + time.ToString("yyyy_MM_dd_HH_mm_ss") + ".dmp";
fileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(typeof(MarshalFixedMemoryBlock).Assembly.Location), fileName);
Dump(memory, fileName);
}
/// <summary>
///
/// </summary>
/// <param name="memory"></param>
public static void Dump(this FixedMemoryBlock memory)
{
string fileName = memory.Name + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".dmp";
fileName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(typeof(MarshalFixedMemoryBlock).Assembly.Location), fileName);
Dump(memory, fileName);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
......
...@@ -1610,6 +1610,21 @@ namespace Cdy.Tag ...@@ -1610,6 +1610,21 @@ namespace Cdy.Tag
// stream.Flush(); // stream.Flush();
} }
public static void RecordToLog2(this MarshalFixedMemoryBlock memory, Stream stream)
{
int ltmp = (int)memory.AllocSize;
var source = memory.Handles;
var bvals = ArrayPool<byte>.Shared.Rent(ltmp);
while (ltmp > 0)
{
int ctmp = Math.Min(bvals.Length, ltmp);
Marshal.Copy(source, bvals, 0, ctmp);
stream.Write(bvals, 0, ctmp);
}
ArrayPool<byte>.Shared.Return(bvals);
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
......
...@@ -350,7 +350,7 @@ namespace Cdy.Tag ...@@ -350,7 +350,7 @@ namespace Cdy.Tag
mAllocSize = size; mAllocSize = size;
//GC.Collect(); //GC.Collect();
LoggerService.Service.Info("CheckAndResize", "CheckAndResize " + this.Name + " " + size, ConsoleColor.Red); LoggerService.Service.Info("MemoryBlock", "CheckAndResize " + this.Name + " " + size, ConsoleColor.Red);
} }
else else
{ {
......
...@@ -68,9 +68,9 @@ namespace Cdy.Tag ...@@ -68,9 +68,9 @@ namespace Cdy.Tag
NamedTags.Clear(); NamedTags.Clear();
foreach(var vv in Tags) foreach(var vv in Tags)
{ {
if(!NamedTags.ContainsKey(vv.Value.Name)) if(!NamedTags.ContainsKey(vv.Value.FullName))
{ {
NamedTags.Add(vv.Value.Name, vv.Value); NamedTags.Add(vv.Value.FullName, vv.Value);
} }
} }
} }
......
...@@ -81,8 +81,6 @@ namespace Cdy.Tag ...@@ -81,8 +81,6 @@ namespace Cdy.Tag
db.Name = xe.Attribute("Name").Value; db.Name = xe.Attribute("Name").Value;
db.Version = xe.Attribute("Version").Value; db.Version = xe.Attribute("Version").Value;
Dictionary<string, TagGroup> groups = new Dictionary<string, TagGroup>(); Dictionary<string, TagGroup> groups = new Dictionary<string, TagGroup>();
Dictionary<TagGroup, string> parents = new Dictionary<TagGroup, string>(); Dictionary<TagGroup, string> parents = new Dictionary<TagGroup, string>();
if(xe.Element("Groups")!=null) if(xe.Element("Groups")!=null)
......
...@@ -77,7 +77,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Consumer", "Consumer", "{72 ...@@ -77,7 +77,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Consumer", "Consumer", "{72
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HighSpeedApiDemo", "Example\Consumer\HighSpeedApiDemo\HighSpeedApiDemo.csproj", "{319E920E-9017-4B50-BBC3-D71D546B7DB2}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HighSpeedApiDemo", "Example\Consumer\HighSpeedApiDemo\HighSpeedApiDemo.csproj", "{319E920E-9017-4B50-BBC3-D71D546B7DB2}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SpiderDriverDemo", "Example\Producter\SpiderDriverDemo\SpiderDriverDemo.csproj", "{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpiderDriverDemo", "Example\Producter\SpiderDriverDemo\SpiderDriverDemo.csproj", "{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DBRuntimeTests", "RunTime\DBRuntimeTests\DBRuntimeTests.csproj", "{8E64B132-719D-4DBF-925C-F33B42A0B29E}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -197,6 +199,10 @@ Global ...@@ -197,6 +199,10 @@ Global
{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Debug|Any CPU.Build.0 = Debug|Any CPU {D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Release|Any CPU.ActiveCfg = Release|Any CPU {D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Release|Any CPU.Build.0 = Release|Any CPU {D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4}.Release|Any CPU.Build.0 = Release|Any CPU
{8E64B132-719D-4DBF-925C-F33B42A0B29E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8E64B132-719D-4DBF-925C-F33B42A0B29E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E64B132-719D-4DBF-925C-F33B42A0B29E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E64B132-719D-4DBF-925C-F33B42A0B29E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
...@@ -232,6 +238,7 @@ Global ...@@ -232,6 +238,7 @@ Global
{723CADA9-2D52-4F05-9661-1655F8182084} = {B48ACA8E-45C0-4F17-9638-5CEAC9E5A185} {723CADA9-2D52-4F05-9661-1655F8182084} = {B48ACA8E-45C0-4F17-9638-5CEAC9E5A185}
{319E920E-9017-4B50-BBC3-D71D546B7DB2} = {723CADA9-2D52-4F05-9661-1655F8182084} {319E920E-9017-4B50-BBC3-D71D546B7DB2} = {723CADA9-2D52-4F05-9661-1655F8182084}
{D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4} = {4DA41AEA-AE61-458B-B516-EE6BDDE180E2} {D30BF45C-2F6F-4A36-BDEB-761DE11AF3A4} = {4DA41AEA-AE61-458B-B516-EE6BDDE180E2}
{8E64B132-719D-4DBF-925C-F33B42A0B29E} = {1A69E6A7-33B3-4DAF-9D9D-FD7CD474FFD3}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {577CCEC3-4CDB-458A-B93D-F8579C2C3D8F} SolutionGuid = {577CCEC3-4CDB-458A-B93D-F8579C2C3D8F}
......
...@@ -88,6 +88,11 @@ namespace DBInRun ...@@ -88,6 +88,11 @@ namespace DBInRun
case "stop": case "stop":
Cdy.Tag.Runner.RunInstance.Stop(); Cdy.Tag.Runner.RunInstance.Stop();
break; break;
case "restart":
Task.Run(() => {
Cdy.Tag.Runner.RunInstance.ReStartDatabase();
});
break;
case "h": case "h":
Console.WriteLine(GetHelpString()); Console.WriteLine(GetHelpString());
break; break;
......
...@@ -10,6 +10,7 @@ using DBRuntime.His; ...@@ -10,6 +10,7 @@ using DBRuntime.His;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
...@@ -47,6 +48,7 @@ namespace Cdy.Tag ...@@ -47,6 +48,7 @@ namespace Cdy.Tag
//private long mTotalSize = 0; //private long mTotalSize = 0;
private int mLastDataRegionId;
#endregion ...Variables... #endregion ...Variables...
...@@ -91,7 +93,7 @@ namespace Cdy.Tag ...@@ -91,7 +93,7 @@ namespace Cdy.Tag
var histag = mHisTagService.ListAllTags(); var histag = mHisTagService.ListAllTags();
//计算数据区域个数 //计算数据区域个数
var mLastDataRegionId = -1; mLastDataRegionId = -1;
foreach (var vv in histag) foreach (var vv in histag)
{ {
var id = vv.Id; var id = vv.Id;
...@@ -110,6 +112,34 @@ namespace Cdy.Tag ...@@ -110,6 +112,34 @@ namespace Cdy.Tag
} }
} }
/// <summary>
///
/// </summary>
/// <param name="tags"></param>
public void ReSizeTagCompress(List<HisTag> tags)
{
List<CompressMemory2> ctmp = new List<CompressMemory2>();
ctmp.Add(mTargetMemorys.Last().Value);
foreach (var vv in tags)
{
var id = vv.Id;
var did = id / TagCountOneFile;
if (mLastDataRegionId != did)
{
var vvv = new CompressMemory2() { Id = did, Name = "CompressTarget" + did };
mTargetMemorys.Add(did, vvv);
mLastDataRegionId = did;
ctmp.Add(vvv);
}
}
foreach (var vv in ctmp)
{
vv.ReInit(ServiceLocator.Locator.Resolve<IHisEngine2>().CurrentMemory);
}
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
...@@ -165,6 +195,8 @@ namespace Cdy.Tag ...@@ -165,6 +195,8 @@ namespace Cdy.Tag
resetEvent.Set(); resetEvent.Set();
} }
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
...@@ -181,6 +213,19 @@ namespace Cdy.Tag ...@@ -181,6 +213,19 @@ namespace Cdy.Tag
return false; return false;
} }
/// <summary>
/// 等待空闲
/// </summary>
public void WaitForReady()
{
while (CheckIsBusy())
{
Thread.Sleep(10);
}
}
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
...@@ -216,7 +261,8 @@ namespace Cdy.Tag ...@@ -216,7 +261,8 @@ namespace Cdy.Tag
mm.Value.Compress(sm); mm.Value.Compress(sm);
}); });
ServiceLocator.Locator.Resolve<IHisEngine2>().ClearMemoryHisData(sm); sm.Clear();
// ServiceLocator.Locator.Resolve<IHisEngine2>().ClearMemoryHisData(sm);
sm.MakeMemoryNoBusy(); sm.MakeMemoryNoBusy();
ServiceLocator.Locator.Resolve<IDataSerialize2>().RequestToSave(); ServiceLocator.Locator.Resolve<IDataSerialize2>().RequestToSave();
......
...@@ -226,7 +226,7 @@ namespace Cdy.Tag ...@@ -226,7 +226,7 @@ namespace Cdy.Tag
//更新指针区域 //更新指针区域
this.WriteInt(0, (int)datasize); this.WriteInt(0, (int)datasize);
this.Write((int)source.TagAddress.Count); this.Write((int)mTagIds.Count);
long ltmp2 = sw.ElapsedMilliseconds; long ltmp2 = sw.ElapsedMilliseconds;
......
...@@ -17,7 +17,7 @@ namespace DBRuntime.His ...@@ -17,7 +17,7 @@ namespace DBRuntime.His
/// <summary> /// <summary>
/// ///
/// </summary> /// </summary>
public class HisDataMemoryBlock: MarshalFixedMemoryBlock public class HisDataMemoryBlock: FixedMemoryBlock
{ {
#region ... Variables ... #region ... Variables ...
......
...@@ -26,7 +26,7 @@ namespace DBRuntime.His ...@@ -26,7 +26,7 @@ namespace DBRuntime.His
#region ... Variables ... #region ... Variables ...
private SortedDictionary<int, HisDataMemoryBlock> mTagAddress = new SortedDictionary<int, HisDataMemoryBlock>(); private Dictionary<int, HisDataMemoryBlock> mTagAddress = new Dictionary<int, HisDataMemoryBlock>();
private int mRefCount = 0; private int mRefCount = 0;
...@@ -58,7 +58,7 @@ namespace DBRuntime.His ...@@ -58,7 +58,7 @@ namespace DBRuntime.His
/// 变量内存地址缓存 /// 变量内存地址缓存
/// Tuple 每项的含义:起始地址,值地址偏移,质量地址偏移,数据大小 /// Tuple 每项的含义:起始地址,值地址偏移,质量地址偏移,数据大小
/// </summary> /// </summary>
public SortedDictionary<int, HisDataMemoryBlock> TagAddress public Dictionary<int, HisDataMemoryBlock> TagAddress
{ {
get get
{ {
...@@ -88,7 +88,8 @@ namespace DBRuntime.His ...@@ -88,7 +88,8 @@ namespace DBRuntime.His
/// </summary> /// </summary>
public void IncRef() public void IncRef()
{ {
Interlocked.Increment(ref mRefCount); lock (mUserSizeLock)
mRefCount++;
} }
/// <summary> /// <summary>
...@@ -116,9 +117,9 @@ namespace DBRuntime.His ...@@ -116,9 +117,9 @@ namespace DBRuntime.His
/// <param name=""></param> /// <param name=""></param>
public void AddTagAddress(int id,HisDataMemoryBlock block) public void AddTagAddress(int id,HisDataMemoryBlock block)
{ {
if(!TagAddress.ContainsKey(id)) if(!mTagAddress.ContainsKey(id))
{ {
TagAddress.Add(id, block); mTagAddress.Add(id, block);
} }
} }
...@@ -128,9 +129,9 @@ namespace DBRuntime.His ...@@ -128,9 +129,9 @@ namespace DBRuntime.His
/// <param name="id"></param> /// <param name="id"></param>
public void RemoveTagAdress(int id) public void RemoveTagAdress(int id)
{ {
if(TagAddress.ContainsKey(id)) if(mTagAddress.ContainsKey(id))
{ {
TagAddress.Remove(id); mTagAddress.Remove(id);
} }
} }
...@@ -139,6 +140,7 @@ namespace DBRuntime.His ...@@ -139,6 +140,7 @@ namespace DBRuntime.His
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
if (TagAddress == null) return;
foreach(var vv in TagAddress) foreach(var vv in TagAddress)
{ {
vv.Value.Clear(); vv.Value.Clear();
...@@ -150,6 +152,7 @@ namespace DBRuntime.His ...@@ -150,6 +152,7 @@ namespace DBRuntime.His
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
while (this.IsBusy()) Thread.Sleep(1);
foreach (var vv in mTagAddress) foreach (var vv in mTagAddress)
vv.Value.Dispose(); vv.Value.Dispose();
mTagAddress.Clear(); mTagAddress.Clear();
...@@ -176,7 +179,7 @@ namespace DBRuntime.His ...@@ -176,7 +179,7 @@ namespace DBRuntime.His
{ {
foreach(var vv in memory.TagAddress) foreach(var vv in memory.TagAddress)
{ {
vv.Value.RecordToLog(stream); vv.Value.RecordToLog2(stream);
} }
} }
......
...@@ -221,8 +221,8 @@ namespace Cdy.Tag ...@@ -221,8 +221,8 @@ namespace Cdy.Tag
if (mRealEnginer != null) if (mRealEnginer != null)
{ {
if (mManager == null) //if (mManager == null)
mManager = new Cdy.Tag.HisDatabaseSerise().Load(); // mManager = new Cdy.Tag.HisDatabaseSerise().Load();
mLastProcesser = new TimerMemoryCacheProcesser2() { Id = 0 }; mLastProcesser = new TimerMemoryCacheProcesser2() { Id = 0 };
mRecordTimerProcesser.Clear(); mRecordTimerProcesser.Clear();
...@@ -315,6 +315,154 @@ namespace Cdy.Tag ...@@ -315,6 +315,154 @@ namespace Cdy.Tag
AllocMemory(); AllocMemory();
} }
/// <summary>
///
/// </summary>
public void Pause()
{
mRecordTimer.Stop();
}
/// <summary>
///
/// </summary>
public void Resume()
{
mRecordTimer.Start();
}
/// <summary>
/// 加载使能新的变量
/// </summary>
/// <param name="tags"></param>
/// <param name="mHisDatabase"></param>
public void ReLoadTags(IEnumerable<Tag.HisTag> tags,HisDatabase mHisDatabase)
{
mRecordTimer.Stop();
var realbaseaddr = this.mRealEnginer.Memory;
IntPtr realHandle = mRealEnginer.MemoryHandle;
HisRunTag mHisTag = null;
Tagbase mRealTag;
var histags = new List<HisRunTag>();
foreach (var vv in tags)
{
var realaddr = (int)mRealEnginer.GetDataAddr((int)vv.Id);
mRealTag = mRealEnginer.GetTagById(vv.Id);
switch (vv.TagType)
{
case Cdy.Tag.TagType.Bool:
case Cdy.Tag.TagType.Byte:
mHisTag = new ByteHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.Short:
case Cdy.Tag.TagType.UShort:
mHisTag = new ShortHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.Int:
case Cdy.Tag.TagType.UInt:
mHisTag = new IntHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.Long:
case Cdy.Tag.TagType.ULong:
mHisTag = new LongHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.Float:
mHisTag = new FloatHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters, Precision = (mRealTag as FloatTag).Precision };
break;
case Cdy.Tag.TagType.Double:
mHisTag = new DoubleHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters, Precision = (mRealTag as DoubleTag).Precision };
break;
case Cdy.Tag.TagType.DateTime:
mHisTag = new DateTimeHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.String:
mHisTag = new StirngHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.UIntPoint:
case Cdy.Tag.TagType.IntPoint:
mHisTag = new IntPointHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.UIntPoint3:
case Cdy.Tag.TagType.IntPoint3:
mHisTag = new IntPoint3HisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.ULongPoint:
case Cdy.Tag.TagType.LongPoint:
mHisTag = new LongPointHisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
case Cdy.Tag.TagType.ULongPoint3:
case Cdy.Tag.TagType.LongPoint3:
mHisTag = new LongPoint3HisRunTag() { Id = vv.Id, Circle = vv.Circle, Type = vv.Type, TagType = vv.TagType, RealMemoryAddr = realbaseaddr, RealMemoryPtr = realHandle, RealValueAddr = realaddr, CompressType = vv.CompressType, Parameters = vv.Parameters };
break;
}
mHisTags.Add(vv.Id, mHisTag);
histags.Add(mHisTag);
if (mHisTag.Type == Cdy.Tag.RecordType.Timer)
{
if (!mLastProcesser.AddTag(mHisTag))
{
mLastProcesser = new TimerMemoryCacheProcesser2() { Id = mLastProcesser.Id + 1 };
mLastProcesser.AddTag(mHisTag);
mRecordTimerProcesser.Add(mLastProcesser);
}
}
else
{
if (!mLastValueChangedProcesser.AddTag(mHisTag))
{
mLastValueChangedProcesser = new ValueChangedMemoryCacheProcesser2() { Name = "ValueChanged" + mTagCount };
mValueChangedProcesser.Add(mLastValueChangedProcesser);
}
}
mTagCount++;
}
int qulityOffset = 0;
int valueOffset = 0;
int blockheadsize = 0;
foreach (var vv in mHisTags)
{
var ss = CalMergeBlockSize(vv.Value.TagType, vv.Value.Type, blockheadsize, out valueOffset, out qulityOffset);
var abuffer = new HisDataMemoryBlock(ss) { TimerAddress = 0, ValueAddress = valueOffset, QualityAddress = qulityOffset, Id = 1 };
var bbuffer = new HisDataMemoryBlock(ss) { TimerAddress = 0, ValueAddress = valueOffset, QualityAddress = qulityOffset, Id = 2 };
mMergeMemory1.AddTagAddress(vv.Value.Id, abuffer);
mMergeMemory2.AddTagAddress(vv.Value.Id, bbuffer);
var css = CalCachDatablockSize(vv.Value.TagType, vv.Value.Type, blockheadsize, out valueOffset, out qulityOffset);
var cbuffer = new HisDataMemoryBlock(css) { TimerAddress = 0, ValueAddress = valueOffset, QualityAddress = qulityOffset, Id = 1 };
var dbuffer = new HisDataMemoryBlock(css) { TimerAddress = 0, ValueAddress = valueOffset, QualityAddress = qulityOffset, Id = 2 };
vv.Value.HisValueMemory1 = cbuffer;
vv.Value.HisValueMemory2 = dbuffer;
mCachMemory1.AddTagAddress(vv.Value.Id, cbuffer);
mCachMemory2.AddTagAddress(vv.Value.Id, dbuffer);
abuffer.Clear();
bbuffer.Clear();
cbuffer.Clear();
dbuffer.Clear();
vv.Value.DataSize = css;
}
this.mManager = mHisDatabase;
foreach (var vv in mRecordTimerProcesser) { if (!vv.IsStarted) vv.Start(); }
foreach (var vv in mValueChangedProcesser) { if (!vv.IsStarted) vv.Start(); }
mRecordTimer.Start();
}
/// <summary> /// <summary>
/// 计算每个变量数据块的大小 /// 计算每个变量数据块的大小
/// </summary> /// </summary>
...@@ -452,16 +600,6 @@ namespace Cdy.Tag ...@@ -452,16 +600,6 @@ namespace Cdy.Tag
int qulityOffset = 0; int qulityOffset = 0;
int valueOffset = 0; int valueOffset = 0;
//Dictionary<int, HisDataMemoryBlock> addressoffset = new Dictionary<int, HisDataMemoryBlock>();
//Dictionary<int, HisDataMemoryBlock> addressoffset2 = new Dictionary<int, HisDataMemoryBlock>();
//Dictionary<int, HisDataMemoryBlock> addressoffset3 = new Dictionary<int, HisDataMemoryBlock>();
//Dictionary<int, HisDataMemoryBlock> addressoffset4 = new Dictionary<int, HisDataMemoryBlock>();
mMergeMemory1 = new HisDataMemoryBlockCollection() { Name = "StoreMemory1", Id = 1 }; mMergeMemory1 = new HisDataMemoryBlockCollection() { Name = "StoreMemory1", Id = 1 };
mMergeMemory2 = new HisDataMemoryBlockCollection() { Name = "StoreMemory2", Id = 2 }; mMergeMemory2 = new HisDataMemoryBlockCollection() { Name = "StoreMemory2", Id = 2 };
...@@ -836,7 +974,6 @@ namespace Cdy.Tag ...@@ -836,7 +974,6 @@ namespace Cdy.Tag
/// <param name="dt"></param> /// <param name="dt"></param>
private void RecordAllFirstValue() private void RecordAllFirstValue()
{ {
foreach(var vv in mCurrentMergeMemory.TagAddress) foreach(var vv in mCurrentMergeMemory.TagAddress)
{ {
//数据内容: 时间戳(time1+time2+...) +数值区(value1+value2+...)+质量戳区(q1+q2+....) //数据内容: 时间戳(time1+time2+...) +数值区(value1+value2+...)+质量戳区(q1+q2+....)
......
...@@ -264,6 +264,7 @@ namespace Cdy.Tag ...@@ -264,6 +264,7 @@ namespace Cdy.Tag
Stopwatch sw = new Stopwatch(); Stopwatch sw = new Stopwatch();
sw.Start(); sw.Start();
string fileName = GetLogFilePath(mStartTime,mEndTime); string fileName = GetLogFilePath(mStartTime,mEndTime);
LoggerService.Service.Info("LogManager", "开始记日志录文件:" + fileName);
using (var stream = System.IO.File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) using (var stream = System.IO.File.Open(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{ {
stream.Write(BitConverter.GetBytes(TimeLen)); stream.Write(BitConverter.GetBytes(TimeLen));
......
...@@ -649,7 +649,7 @@ namespace Cdy.Tag ...@@ -649,7 +649,7 @@ namespace Cdy.Tag
if (mBlockPointMemory != null) mBlockPointMemory.Dispose(); if (mBlockPointMemory != null) mBlockPointMemory.Dispose();
mBlockPointMemory = new MemoryBlock(tags.Count() * 8,1024*1024); mBlockPointMemory = new MemoryBlock(tags.Count() * 8, 4 * 1024 * 1024);
mBlockPointMemory.Clear(); mBlockPointMemory.Clear();
LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan); LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan);
......
...@@ -52,6 +52,8 @@ namespace Cdy.Tag ...@@ -52,6 +52,8 @@ namespace Cdy.Tag
private int mBusyCount = 0; private int mBusyCount = 0;
private bool mIsStarted = false;
#endregion ...Variables... #endregion ...Variables...
#region ... Events ... #region ... Events ...
...@@ -87,6 +89,8 @@ namespace Cdy.Tag ...@@ -87,6 +89,8 @@ namespace Cdy.Tag
/// </summary> /// </summary>
public int Id { get; set; } public int Id { get; set; }
public bool IsStarted { get { return mIsStarted; } }
#endregion ...Properties... #endregion ...Properties...
#region ... Methods ... #region ... Methods ...
...@@ -120,6 +124,7 @@ namespace Cdy.Tag ...@@ -120,6 +124,7 @@ namespace Cdy.Tag
mRecordThread = new Thread(ThreadProcess); mRecordThread = new Thread(ThreadProcess);
mRecordThread.IsBackground=true; mRecordThread.IsBackground=true;
mRecordThread.Start(); mRecordThread.Start();
mIsStarted = true;
} }
/// <summary> /// <summary>
...@@ -130,6 +135,7 @@ namespace Cdy.Tag ...@@ -130,6 +135,7 @@ namespace Cdy.Tag
mIsClosed = true; mIsClosed = true;
resetEvent.Set(); resetEvent.Set();
closedEvent.WaitOne(); closedEvent.WaitOne();
mIsStarted = false;
} }
/// <summary> /// <summary>
......
...@@ -48,6 +48,8 @@ namespace Cdy.Tag ...@@ -48,6 +48,8 @@ namespace Cdy.Tag
private Thread mRecordThread; private Thread mRecordThread;
private bool mIsStarted = false;
#endregion ...Variables... #endregion ...Variables...
#region ... Events ... #region ... Events ...
...@@ -90,6 +92,11 @@ namespace Cdy.Tag ...@@ -90,6 +92,11 @@ namespace Cdy.Tag
private int mLastUpdateSecond = -1; private int mLastUpdateSecond = -1;
/// <summary>
///
/// </summary>
public bool IsStarted { get { return mIsStarted; } }
#endregion ...Properties... #endregion ...Properties...
#region ... Methods ... #region ... Methods ...
...@@ -130,6 +137,8 @@ namespace Cdy.Tag ...@@ -130,6 +137,8 @@ namespace Cdy.Tag
mRecordThread = new Thread(ThreadProcess); mRecordThread = new Thread(ThreadProcess);
mRecordThread.IsBackground=true; mRecordThread.IsBackground=true;
mRecordThread.Start(); mRecordThread.Start();
mIsStarted = true;
} }
/// <summary> /// <summary>
...@@ -141,7 +150,7 @@ namespace Cdy.Tag ...@@ -141,7 +150,7 @@ namespace Cdy.Tag
resetEvent.Set(); resetEvent.Set();
closedEvent.WaitOne(); closedEvent.WaitOne();
Clear(); Clear();
mIsStarted = false;
ServiceLocator.Locator.Resolve<IRealDataNotify>().UnSubscribeValueChangedForConsumer(this.Name); ServiceLocator.Locator.Resolve<IRealDataNotify>().UnSubscribeValueChangedForConsumer(this.Name);
} }
......
...@@ -649,7 +649,7 @@ namespace Cdy.Tag ...@@ -649,7 +649,7 @@ namespace Cdy.Tag
if (mBlockPointMemory != null) mBlockPointMemory.Dispose(); if (mBlockPointMemory != null) mBlockPointMemory.Dispose();
mBlockPointMemory = new MemoryBlock(tags.Count() * 8,1024*1024); mBlockPointMemory = new MemoryBlock(tags.Count() * 8, 1024 * 1024) { Name = "block"+ this.Id };
mBlockPointMemory.Clear(); mBlockPointMemory.Clear();
LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan); LoggerService.Service.Info("SeriseEnginer", "Cal BlockPointMemory memory size:" + (mBlockPointMemory.AllocSize) / 1024.0 / 1024 + "M", ConsoleColor.Cyan);
......
...@@ -9,6 +9,8 @@ ...@@ -9,6 +9,8 @@
using Cdy.Tag.Driver; using Cdy.Tag.Driver;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -132,12 +134,73 @@ namespace Cdy.Tag ...@@ -132,12 +134,73 @@ namespace Cdy.Tag
/// </summary> /// </summary>
private void LoadDatabase() private void LoadDatabase()
{ {
Stopwatch sw = new Stopwatch();
sw.Start();
this.mDatabase = new DatabaseSerise().Load(mDatabaseName); this.mDatabase = new DatabaseSerise().Load(mDatabaseName);
this.mRealDatabase = this.mDatabase.RealDatabase; this.mRealDatabase = this.mDatabase.RealDatabase;
this.mHisDatabase = this.mDatabase.HisDatabase; this.mHisDatabase = this.mDatabase.HisDatabase;
CurrentDatabaseVersion = this.mRealDatabase.Version; CurrentDatabaseVersion = this.mRealDatabase.Version;
CurrentDatabase = mRealDatabase.Name; CurrentDatabase = mRealDatabase.Name;
CurrentDatabaseLastUpdateTime = mRealDatabase.UpdateTime; CurrentDatabaseLastUpdateTime = mRealDatabase.UpdateTime;
sw.Stop();
LoggerService.Service.Info("LoadDatabase", "load " +mDatabaseName +" take " + sw.ElapsedMilliseconds.ToString() +" ms");
}
/// <summary>
/// 重新加载数据库
/// </summary>
public void ReStartDatabase()
{
LoggerService.Service.Info("ReStartDatabase", "start to restart database.",ConsoleColor.DarkYellow);
Stopwatch sw = new Stopwatch();
sw.Start();
var db = new DatabaseSerise().Load(mDatabaseName);
List<Tagbase> ltmp = new List<Tagbase>();
List<HisTag> htmp = new List<HisTag>();
foreach(var vv in db.RealDatabase.Tags.Where(e=>this.mRealDatabase.Tags.ContainsKey(e.Key)))
{
ltmp.Add(vv.Value);
}
foreach(var vv in ltmp)
{
if(db.HisDatabase.HisTags.ContainsKey(vv.Id))
{
htmp.Add(db.HisDatabase.HisTags[vv.Id]);
}
}
LoggerService.Service.Info("ReStartDatabase", "reload " + mDatabaseName + " take " + sw.ElapsedMilliseconds.ToString() + " ms");
compressEnginer.WaitForReady();
sw.Reset();
sw.Start();
hisEnginer.Pause();
realEnginer.Lock();
realEnginer.ReLoadTags(ltmp,db.RealDatabase);
realEnginer.UnLock();
hisEnginer.ReLoadTags(htmp, db.HisDatabase);
compressEnginer.ReSizeTagCompress(htmp);
hisEnginer.Resume();
this.mDatabase = db;
this.mRealDatabase = db.RealDatabase;
this.mHisDatabase = db.HisDatabase;
CurrentDatabaseVersion = db.Version;
CurrentDatabase = db.Name;
CurrentDatabaseLastUpdateTime = mRealDatabase.UpdateTime;
sw.Stop();
LoggerService.Service.Info("ReStartDatabase", "ReInit" + mDatabaseName + " take " + sw.ElapsedMilliseconds.ToString() + " ms");
LoggerService.Service.Info("ReStartDatabase", "start to restart database finish.", ConsoleColor.DarkYellow);
} }
/// <summary> /// <summary>
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="MSTest.TestAdapter" Version="2.1.0" />
<PackageReference Include="MSTest.TestFramework" Version="2.1.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DBRuntime\DBRuntime.csproj" />
</ItemGroup>
</Project>
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DBRuntime.His;
//==============================================================
// Copyright (C) 2020 Chongdaoyang Inc. All rights reserved.
//
//==============================================================
// Create by 种道洋 at 2020/8/8 15:22:48 .
// Version 1.0
// CDYWORK
//==============================================================
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
namespace DBRuntime.His.Tests
{
[TestClass()]
public class HisDataMemoryBlockCollectionTests
{
[TestMethod()]
public void ClearTest()
{
HisDataMemoryBlockCollection hdb = new HisDataMemoryBlockCollection();
for (int i = 0; i < 1000000; i++)
{
hdb.AddTagAddress(i, new HisDataMemoryBlock(3300));
}
hdb.Clear();
Stopwatch sw = new Stopwatch();
sw.Start();
foreach(var vv in hdb.TagAddress.Values)
{
vv.WriteShort(0, 10);
vv.WriteInt(10, 20);
vv.WriteDouble(20, 3.444);
Assert.IsTrue(vv.ReadShort(0) == 10);
Assert.IsTrue(vv.ReadInt(10) == 20);
Assert.IsTrue(vv.ReadDouble(20) == 3.444);
}
sw.Stop();
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册