public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); var ms = new HighwaySettings(1024, 1, 1024); // Allocates 100 fragments and continuously resets one, while // new fragments are allocated. void allocAndManualReset(IMemoryHighway hw) { var F = new List <MemoryFragment>(); for (int i = 0; i < 100; i++) { F.Add(hw.AllocFragment(4)); } } if (opt.Contains("mh")) { using (var hw = new HeapHighway(ms, 1024)) allocAndManualReset(hw); } if (opt.Contains("nh")) { using (var hw = new MarshalHighway(ms, 1024)) allocAndManualReset(hw); } if (opt.Contains("mmf")) { using (var hw = new MappedHighway(ms, 1024)) allocAndManualReset(hw); } if (!Passed.HasValue) { Passed = true; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); var ms = new HighwaySettings(1024); // so that no refs are held ms.RegisterForProcessExitCleanup = false; // The unmanaged resource var MMF_FileID = string.Empty; // Must be in a separate non-async function so that no refs are holding it. void fin() { // Create without 'using' and forget to dispose var mmh = new MappedHighway(ms, 1024); MMF_FileID = mmh[0].FileID; mmh.Alloc(100); } fin(); GC.Collect(2); Thread.Sleep(5000); if (!File.Exists(MMF_FileID)) { Print.AsSuccess($"MappedHighway: the underlying file {MMF_FileID} was cleaned up by the finalizer."); Passed = true; } else { Print.AsTestFailure("MappedHighway: the finalizer was not triggered or the file deletion failed."); Passed = false; } IsComplete = true; }
public MappedHighway(HighwaySettings stg) : base(stg) => Create(DEF_MMF_LANES);
/// <summary> /// Creates new lanes with the specified lengths and settings. /// When needed, the MemoryCarriage will create the new lanes with settings.DefaultCapacity in length. /// Note that every lane is a separate memory mapped file. /// </summary> /// <param name="stg">Generic settings for all MemoryCarriage derivatives.</param> /// <param name="lanes">The initial setup.</param> public MappedHighway(HighwaySettings stg, params int[] lanes) : base(stg) => Create(lanes);
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); const int MAX_LANES = 4; const int LANE_SIZE = 100; const int ALLOC_SIZE = 25; const int TARGET_LANE_IDX = 1; var stg = new HighwaySettings(LANE_SIZE, MAX_LANES); var iH = new Dictionary <string, IMemoryHighway>(); iH.Add("nh", new MarshalHighway(stg, stg.DefaultCapacity)); iH.Add("mh", new HeapHighway(stg, stg.DefaultCapacity)); iH.Add("mmf", new MappedHighway(stg, stg.DefaultCapacity)); foreach (var kp in iH) { if (opt.Contains(kp.Key)) { using (var hw = kp.Value) { var hwName = hw.GetType().Name; for (int i = 0; i < MAX_LANES * 4; i++) { hw.AllocFragment(ALLOC_SIZE); } if (hw.ReopenLane(TARGET_LANE_IDX) != null) { Passed = false; FailureMessage = $"{hwName}: ReopenLane returned a non disposed lane."; return; } $"Disposing a lane".AsInfo(); hw.DisposeLane(TARGET_LANE_IDX); if (hw.ReopenLane(TARGET_LANE_IDX) == null) { Passed = false; FailureMessage = $"{hwName}: ReopenLane failed to create a lane."; return; } for (int i = 0; i < 4; i++) { hw.AllocFragment(ALLOC_SIZE); } var tfc = hw.GetTotalActiveFragments(); var allocs = hw[TARGET_LANE_IDX].Allocations; if (tfc != MAX_LANES * 4) { Passed = false; FailureMessage = $"{hwName}: The reopened lane was not used for allocations as expected."; return; } if (allocs != 4) { Passed = false; FailureMessage = $"{hwName}: The highway has {allocs} lanes; expected {LANE_SIZE / ALLOC_SIZE}"; return; } $"OK: {hwName} successfully reopens a lane.".AsSuccess(); } } } Passed = true; IsComplete = true; }
bool reset(List <string> opt) { var stg = new HighwaySettings(4000, 8, 10000); var iH = new Dictionary <string, IMemoryHighway>(); iH.Add("mh", new HeapHighway(stg, 4000)); iH.Add("nh", new MarshalHighway(stg, 4000)); iH.Add("mmf", new MappedHighway(stg, 4000)); foreach (var kp in iH) { var hwName = kp.Value.GetType().Name; var F = new List <MemoryFragment>(); if (opt.Contains(kp.Key)) { var hw = kp.Value; using (hw) { F.Add(hw.AllocFragment(500)); F.Add(hw.AllocFragment(500)); F.Add(hw.AllocFragment(500)); hw.AllocFragment(1000); // lost F.Add(hw.AllocFragment(500)); F.Add(hw.AllocFragment(1000)); foreach (var f in F) { f.Dispose(); } var af = hw.GetTotalActiveFragments(); if (af == 1) { Print.AsInnerInfo("{0} has {1} non disposed fragments", hwName, af); af = hw.GetTotalActiveFragments(); if (af != 1) { Passed = false; FailureMessage = string.Format("{0}: expected one ghost fragment, found {1}.", hwName, af); return(false); } Print.Trace("Forcing reset.. ", ConsoleColor.Magenta, hwName, af); var lane0 = hw[0]; lane0.Force(false, true); af = hw.GetTotalActiveFragments(); if (af != 0) { Passed = false; FailureMessage = string.Format("{0}: expected 0 ghost fragments after forcing a reset, found {1}.", hwName, af); return(false); } else { Print.Trace("{0} has {1} allocations and offset {2}", ConsoleColor.Green, hwName, lane0.Allocations, lane0.Offset); } } else { Passed = false; FailureMessage = string.Format("{0}: the active fragments count is wrong, should be 1.", hwName); return(false); } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black); } } } return(true); }
public HeapHighway(HighwaySettings stg) : base(stg) => Create(DEF_HEAP_LANES);
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh", "vh" }); } var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh", "vh"); var stg = new HighwaySettings(1024, 2); var iH = new Dictionary <string, IMemoryHighway>(); var LEN = stg.DefaultCapacity; iH.Add("mh", new HeapHighway(stg, LEN, LEN)); iH.Add("nh", new MarshalHighway(stg, LEN, LEN)); iH.Add("mmf", new MappedHighway(stg, LEN, LEN)); iH.Add("vh", new VirtualHighway()); foreach (var kp in iH) { var hwName = kp.Value.GetType().Name; if (opt.Contains(kp.Key)) { var hw = kp.Value; using (hw) { var F = new MemoryFragment[] { hw.AllocFragment(200), hw.AllocFragment(200) }; foreach (var f in F) { var p = 0; bool b = true; int i = 10; double d = 2.2; DateTime dt = DateTime.Now; char c = 'c'; Guid g = Guid.NewGuid(); byte[] ba = new byte[3] { 1, 2, 3 }; p = f.Write(b, p); p = f.Write(c, p); p = f.Write(dt, p); p = f.Write(d, p); p = f.Write(i, p); p = f.Write(g, p); f.Write(ba, p, ba.Length); p = 0; bool br = false; int ir = 0; double dr = 0; DateTime dtr = DateTime.MinValue; char cr = char.MinValue; Guid gr = Guid.Empty; byte[] bar = new byte[3]; p = f.Read(ref br, p); p = f.Read(ref cr, p); p = f.Read(ref dtr, p); p = f.Read(ref dr, p); p = f.Read(ref ir, p); p = f.Read(ref gr, p); f.Read(bar, p, 0); if (br != b || cr != c || dtr != dt || dr != d || ir != i || gr != g || !Assert.SameValues(ba, bar)) { Passed = false; FailureMessage = "The reads do not match the writes."; return; } } $"{hwName}: fragment reads and writes primitive types correctly.".AsSuccess(); } } } if (!Passed.HasValue) { Passed = true; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); var stg = new HighwaySettings(2000, 8, 10000); var iH = new Dictionary <string, IMemoryHighway>(); iH.Add("mh", new HeapHighway(stg, 2000, 2000, 2000)); iH.Add("nh", new MarshalHighway(stg, 2000, 2000, 2000)); iH.Add("mmf", new MappedHighway(stg, 2000, 2000, 2000)); foreach (var kp in iH) { var hwName = kp.Value.GetType().Name; var F = new List <MemoryFragment>(); if (opt.Contains(kp.Key)) { var hw = kp.Value; using (hw) { var lane1 = hw[1]; var lane2 = hw[2]; lane1.Force(true); F.Add(hw.AllocFragment(1500)); F.Add(hw.AllocFragment(1500)); try { var af = hw.GetTotalActiveFragments(); if (af != 2) { Passed = false; FailureMessage = string.Format("{0}: expected 2 active fragments, got {1}", hwName, af); return; } if (lane1.Allocations > 0) { Passed = false; FailureMessage = string.Format("{0}: the lane #1 was force closed, it should have 0 fragments, got {1}", hwName, lane1.Allocations); return; } if (lane2.Allocations < 1) { Passed = false; FailureMessage = string.Format("{0}: the lane #2 should have at least one allocation, found {1}", hwName, lane2.Allocations); return; } $"{hwName}: closing a lane works as expected".AsSuccess(); lane1.Force(false); // should go into lane1 because lane0 has 1500/2000 F.Add(hw.AllocFragment(1500)); if (lane1.Allocations != 1) { Passed = false; FailureMessage = string.Format("{0}: the lane #1 was force opened, it should have 1 fragments, got {1}", hwName, lane1.Allocations); return; } } finally { foreach (var f in F) { f.Dispose(); } } Print.Trace(hw.FullTrace(), 2, true, ConsoleColor.Cyan, ConsoleColor.Black, null); } } } if (!Passed.HasValue) { Passed = true; } IsComplete = true; }
public async Task Start(IDictionary <string, List <string> > args) { await Task.Delay(0); if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); const int ALLOC_SIZE = 100; const int FRAGS_COUNT = 100; var ccAllocs = new int[3] { 1, 2, 3 }; foreach (var cc in ccAllocs) { // The initial hw space will fit half of the fragments var stg = new HighwaySettings((FRAGS_COUNT / 2) * ALLOC_SIZE); var iH = new Dictionary <string, IMemoryHighway>(); // Update the no-luckGate capacity stg.ConcurrentNewLaneAllocations = cc; iH.Add("nh", new MarshalHighway(stg, stg.DefaultCapacity)); iH.Add("mh", new HeapHighway(stg, stg.DefaultCapacity)); iH.Add("mmf", new MappedHighway(stg, stg.DefaultCapacity)); foreach (var kp in iH) { if (opt.Contains(kp.Key)) { var CCAllocs = new Task[FRAGS_COUNT]; var frags = new MemoryFragment[FRAGS_COUNT]; using (var hw = kp.Value) { var hwName = hw.GetType().Name; for (int i = 0; i < CCAllocs.Length; i++) { CCAllocs[i] = new Task((idx) => frags[(int)idx] = hw.AllocFragment(ALLOC_SIZE), i); } $"Starting all {CCAllocs.Length} concurrent allocations".AsInfo(); for (int i = 0; i < CCAllocs.Length; i++) { CCAllocs[i].Start(); } Task.WaitAll(CCAllocs); "Allocs complete".AsInfo(); var lanesCount = hw.GetLanesCount(); var totalFrags = hw.GetTotalActiveFragments(); if (lanesCount - 1 > cc) { Passed = false; FailureMessage = $"{hwName}: There are {lanesCount} lanes, cc: {cc}. Expected the no-luckGate to hold them off."; return; } var nullFrags = frags.Where(x => x == null).Count(); if (nullFrags > 0) { Passed = false; FailureMessage = $"{hwName}: There are {nullFrags} null fragments. The ConcurrentNewLaneAllocations is {cc}."; return; } $"{hwName}: Correctly allocated exactly {cc} new lanes.".AsSuccess(); } } } } Passed = true; IsComplete = true; }
public Task Start(IDictionary <string, List <string> > args) { if (args.ContainsKey("+all")) { args.Add("-store", new List <string>() { "mh", "mmf", "nh" }); } args.AssertAll("-store"); var opt = args["-store"]; opt.AssertNothingOutsideThese("mh", "mmf", "nh"); const int LANE_SIZE = 100; const int ALLOC_SIZE = 25; var stg = new HighwaySettings(LANE_SIZE, 1); var iH = new Dictionary <string, IMemoryHighway>(); iH.Add("nh", new MarshalHighway(stg, stg.DefaultCapacity)); iH.Add("mh", new HeapHighway(stg, stg.DefaultCapacity)); iH.Add("mmf", new MappedHighway(stg, stg.DefaultCapacity)); var BYTES = new byte[LANE_SIZE]; foreach (var kp in iH) { if (opt.Contains(kp.Key)) { using (var hw = kp.Value) { var hwName = hw.GetType().Name; for (int i = 0; i < LANE_SIZE / ALLOC_SIZE; i++) { var f = hw.AllocFragment(ALLOC_SIZE); for (int j = 0; j < f.Length; j++) { var b = (byte)i; f.Write(b, j); BYTES[i * ALLOC_SIZE + j] = b; } } var bytes = hw[0].GetAllBytes(); if (bytes.Length != BYTES.Length) { FailureMessage = $"{hwName} GetAllBytes returned span with unexpected length"; Passed = false; break; } for (int i = 0; i < bytes.Length; i++) { if (BYTES[i] != bytes[i]) { FailureMessage = $"{hwName} GetAllBytes returned span with incorrect data"; Passed = false; return(Task.CompletedTask); } } } } } Passed = true; IsComplete = true; return(Task.CompletedTask); }
public MarshalHighway(HighwaySettings stg) : base(stg) => Create(DEF_NHEAP_LANES);