public void ReportReentrancy02() { var thread = new Thread(() => { UnsafeWriter.AllowUnsafeWriterCaching = true; UnsafeWriterStatistics.ClearEvents(); UnsafeWriterStatistics.ReportReentrancy = true; try { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { using (var nestedCookie = UnsafeWriter.NewThreadLocalWriter()) cookie.Writer.Write(0); } } finally { UnsafeWriterStatistics.ReportReentrancy = false; } }); thread.Start(); thread.Join(); var reentrancyEvent = UnsafeWriterStatistics.GetEvents().First(@event => @event.Type == UnsafeWriterStatistics.EventType.REENTRANCY); Assert.IsTrue(reentrancyEvent.Stacktraces.Count == 2, "reentrancyEvent.Stacktraces.Count == 2"); }
public void Test1() { while (NativeMemoryPool.TryFreeMemory()) { } var th = new Thread(() => { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(false); Assert.AreEqual(1, NativeMemoryPool.SampleUsed()); } Assert.AreEqual(1, NativeMemoryPool.SampleCount()); }); th.Start(); th.Join(); Assert.AreEqual(0, NativeMemoryPool.SampleUsed()); SpinWait.SpinUntil(() => { if (NativeMemoryPool.SampleCount() > 0) { GC.Collect(); } else { return(true); } return(false); }, 1000); Assert.AreEqual(0, NativeMemoryPool.SampleCount()); }
private void SendAck(long seqN) { try { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(ACK_MSG_LEN); cookie.Writer.Write(seqN); cookie.CopyTo(myAckPkgHeader); } lock (mySocketSendLock) Socket.Send(myAckPkgHeader); } catch (ObjectDisposedException) { Log.Verbose($"{Id}: Socket was disposed during ACK, seqn = {seqN}"); } catch (SocketException e) { // looks like this does not deserve a warn, as the only thing that can happen is a fatal socket failure anyway, and that will likely be reported properly from other threads Log.Verbose(e, $"{Id}: ${e.GetType()} raised during ACK, seqn = {seqN}"); } catch (Exception e) { Log.Warn(e, $"{Id}: {e.GetType()} raised during ACK, seqn = {seqN}"); } }
private void Ping() { try { Log.Trace()?.Log($"{Id}: send PING " + $"currentTimeStamp: {myCurrentTimeStamp}, " + $"counterpartTimestamp: {myCounterpartTimestamp}, " + $"counterpartNotionTimestamp: {myCounterpartNotionTimestamp}"); if (!ConnectionEstablished(myCurrentTimeStamp, myCounterpartNotionTimestamp)) { HeartbeatAlive.Value = false; } using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(PING_LEN); cookie.Writer.Write(myCurrentTimeStamp); cookie.Writer.Write(myCounterpartTimestamp); cookie.CopyTo(myPingPkgHeader); } lock (mySocketSendLock) Socket.Send(myPingPkgHeader); ++myCurrentTimeStamp; } catch (ObjectDisposedException) { Log.Verbose($"{Id}: Socket was disposed during PING"); } catch (Exception e) { Log.Warn(e, $"{Id}: {e.GetType()} raised during PING"); } }
public void TestFreeMemoryStress() { bool run = true; var thread = new Thread(() => { while (run) { NativeMemoryPool.TryFreeMemory(); } }); thread.Start(); var sw = Stopwatch.StartNew(); while (sw.ElapsedMilliseconds < 500) { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(1); } using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(1); } } run = false; thread.Join(); }
public void TestWriterIsReused02() { UnsafeWriter firstWriter = null, secondWriter = null; var thread = new Thread(() => { UnsafeWriter.AllowUnsafeWriterCaching = true; try { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { firstWriter = cookie.Writer; } using (var cookie = UnsafeWriter.NewThreadLocalWriterNoCaching()) { secondWriter = cookie.Writer; } } finally { UnsafeWriter.AllowUnsafeWriterCaching = false; } }); thread.Start(); thread.Join(); Assert.IsNotNull(firstWriter, "firstWriter != null"); Assert.IsNotNull(secondWriter, "secondWriter != null"); Assert.IsTrue(ReferenceEquals(firstWriter, secondWriter), "object.ReferenceEquals(firstWriter, secondWriter)"); }
public void M() { using (var y = UnsafeWriter.NewThreadLocalWriter()) { y.Writer.Write("hhhhdd"); } }
private void PutLong(ByteBufferAsyncProcessor buffer, long l) { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(l); buffer.Put(cookie); } }
public int[] Build(IPsiSourceFile sourceFile, SeldomInterruptChecker interruptChecker) { using (UnsafeWriter.Cookie unsafeWriterCookie = UnsafeWriter.NewThreadLocalWriter()) { TrigramIndexEntryBuilder indexEntryBuilder = new TrigramIndexEntryBuilder(unsafeWriterCookie); foreach (TrigramToken trigramToken in new BufferTrigramSource(new StringBuffer(ASSET_REFERENCE_IDENTIFIER))) { indexEntryBuilder.Add(trigramToken); } UnsafeIntArray entryData = indexEntryBuilder.Build(); return(entryData.ToIntArray()); } }
public void Send<TContext>(RdId id, TContext context, Action<TContext, UnsafeWriter> writer) { Assertion.Require(!id.IsNil, "!id.IsNil"); Assertion.AssertNotNull(writer, "writer != null"); using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(0); //placeholder for length id.Write(cookie.Writer); writer(context, cookie.Writer); cookie.WriteIntLengthToCookieStart(); SendPkg(cookie); } }
public void TestReportAccessCounter() { var thread = new Thread(() => { UnsafeWriterStatistics.ClearEvents(); for (int i = 0; i < UnsafeWriterStatistics.ReportAccessCounterThreshold + UnsafeWriterStatistics.ReportOnOfN; ++i) { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { } } }); thread.Start(); thread.Join(); var accessCounterEvent = UnsafeWriterStatistics.GetEvents().First(@event => @event.Type == UnsafeWriterStatistics.EventType.ACCESS_COUNTER); }
public void Send <TContext>(RdId id, TContext context, Action <TContext, UnsafeWriter> writer) { lock (mySendQ) { if (mySendQ.Count > 0 || !Connected.Value) { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { writer(context, cookie.Writer); mySendQ.Enqueue(new KeyValuePair <RdId, byte[]>(id, cookie.CloneData())); } return; } } RealWire.Send(id, context, writer); }
public void ReportAllocationOnNonCachedThread() { var thread = new Thread(() => { UnsafeWriterStatistics.ClearEvents(); using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { for (int i = 0; i < UnsafeWriterStatistics.ReportAllocationOnNonCachedThreadThreshold + 1; ++i) { cookie.Writer.Write(0); } } }); thread.Start(); thread.Join(); var memoryAllocationEvent = UnsafeWriterStatistics.GetEvents().First(@event => @event.Type == UnsafeWriterStatistics.EventType.MEMORY_ALLOCATION); }
private void SendAck(long seqN) { try { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(ACK_MSG_LEN); cookie.Writer.Write(seqN); cookie.CopyTo(myAckPkgHeader); } lock (mySocketSendLock) Socket.Send(myAckPkgHeader); } catch (Exception e) { Log.Warn(e, $"{Id}: Exception raised during ACK, seqn = {seqN}"); } }
public void Perf() { var sw = Stopwatch.StartNew(); Parallel.For(0, Environment.ProcessorCount, new ParallelOptions() { MaxDegreeOfParallelism = -1 }, x => { for (int a = 0; a < 100_000_000; a++) { using (var y = UnsafeWriter.NewThreadLocalWriter()) { y.Writer.Write("hhhhdd"); } } }); Console.WriteLine(sw.ElapsedMilliseconds); }
private void Ping() { if (BackwardsCompatibleWireFormat) { return; } try { if (!ConnectionEstablished(myCurrentTimeStamp, myCounterpartNotionTimestamp)) { if (HeartbeatAlive.Value) // log only on change { Log.Trace()?.Log($"Disconnect detected while sending PING {Id}: " + $"currentTimeStamp: {myCurrentTimeStamp}, " + $"counterpartTimestamp: {myCounterpartTimestamp}, " + $"counterpartNotionTimestamp: {myCounterpartNotionTimestamp}"); } HeartbeatAlive.Value = false; } using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(PING_LEN); cookie.Writer.Write(myCurrentTimeStamp); cookie.Writer.Write(myCounterpartTimestamp); cookie.CopyTo(myPingPkgHeader); } lock (mySocketSendLock) Socket.Send(myPingPkgHeader); ++myCurrentTimeStamp; } catch (ObjectDisposedException) { Log.Verbose($"{Id}: Socket was disposed during PING"); } catch (Exception e) { Log.Warn(e, $"{Id}: {e.GetType()} raised during PING"); } }
public void ReportReentrancy01() { var thread = new Thread(() => { UnsafeWriter.AllowUnsafeWriterCaching = true; UnsafeWriterStatistics.ClearEvents(); using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { using (var nestedCookie = UnsafeWriter.NewThreadLocalWriter()) cookie.Writer.Write(0); } }); thread.Start(); thread.Join(); var reentrancyEvent = UnsafeWriterStatistics.GetEvents().FirstOrDefault(@event => @event.Type == UnsafeWriterStatistics.EventType.REENTRANCY); Assert.IsNull(reentrancyEvent); }
public void TestPolymorphicSimple() { var serializers = new Serializers(); serializers.Register(MyTestObject.Read, MyTestObject.Write); var serializationCtx = new SerializationCtx(serializers); var testObject = new MyTestObject("Monomorphic"); byte[] data; using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { serializers.Write(serializationCtx, cookie.Writer, testObject); data = cookie.CloneData(); } MyTestObject newTestObject = null; UnsafeReader.With(data, reader => newTestObject = serializers.Read <MyTestObject>(serializationCtx, reader, null)); Assert.AreEqual(testObject.Data, newTestObject.Data); }
private void SendAck(long seqN) { try { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(ACK_MSG_LEN); cookie.Writer.Write(seqN); cookie.CopyTo(myAckPkgHeader); } lock (mySocketSendLock) Socket.Send(myAckPkgHeader); } catch (ObjectDisposedException) { Log.Verbose($"{Id}: Socket was disposed during ACK, seqn = {seqN}"); } catch (Exception e) { Log.Warn(e, $"{Id}: ${e.GetType()} raised during ACK, seqn = {seqN}"); } }
private readonly byte[] myAckPkgHeader = new byte[PkgHeaderLen]; //different threads private void Send0(byte[] data, int offset, int len, ref long seqN) { try { if (seqN == 0) { seqN = ++mySentSeqn; } using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(len); cookie.Writer.Write(seqN); cookie.CopyTo(mySendPkgHeader); } lock (mySocketSendLock) { Socket.Send(mySendPkgHeader, 0, PkgHeaderLen, SocketFlags.None); Socket.Send(data, offset, len, SocketFlags.None); } WrittenBytesCount += len; } catch (Exception e) { if (e is SocketException || e is ObjectDisposedException) { SendBuffer.Pause(DisconnectedPauseReason); LogTraffic(); } else { Log.Error(e); } } }
public int[] Build(IPsiSourceFile sourceFile, SeldomInterruptChecker interruptChecker) { var file = sourceFile.GetDominantPsiFile <UnityYamlLanguage>() as IYamlFile; if (file == null) { return(null); } using (UnsafeWriter.Cookie unsafeWriterCookie = UnsafeWriter.NewThreadLocalWriter()) { TrigramIndexEntryBuilder indexEntryBuilder = new TrigramIndexEntryBuilder(unsafeWriterCookie); foreach (var yamlDocument in file.Documents) { foreach (TrigramToken trigramToken in new BufferTrigramSource(yamlDocument.GetTextAsBuffer())) { indexEntryBuilder.Add(trigramToken); } } UnsafeIntArray entryData = indexEntryBuilder.Build(); return(entryData.ToIntArray()); } }
public unsafe void StressTestWithAck() { // LogLog.SeverityFilter = LoggingLevel.VERBOSE; // LogLog.RecordsChanged += record => { Console.WriteLine(record.Format(true)); }; long prev = 0; ByteBufferAsyncProcessor buffer = null; buffer = new ByteBufferAsyncProcessor("TestAsyncProcessor", 8, delegate(byte[] data, int offset, int len, ref long seqN) { long l = 0; Log.Root.Catch(() => { fixed(byte *b = data) { l = UnsafeReader.CreateReader(b, 8).ReadLong(); Assert.True(l > prev); prev = l; if (l % 1 == 0) { Ack(l); } } }); seqN = l; }); buffer.ShrinkIntervalMs = 10; buffer.Start(); void Ack(long seqn) { buffer?.Acknowledge(seqn); } var start = Environment.TickCount; bool Until() => Environment.TickCount - start < 1000; long next = 0; var tasks = new List <Task>(); for (int i = 0; i < 4; i++) { tasks.Add(Task.Run(() => { var rnd = new Random(); while (Until()) { lock (tasks) { using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(++next); buffer.Put(cookie); } } if (rnd.Next(1000) < 1) { Thread.Sleep(1); } if (rnd.Next(1000) < 5) { buffer.Clear(); } } })); } Task.WaitAll(tasks.ToArray()); // Console.WriteLine(next); // Console.WriteLine(buffer.ChunkCount); }
public void Test1() { UnsafeReader reader; using (var cookie = UnsafeWriter.NewThreadLocalWriter()) { cookie.Writer.Write(false); cookie.Writer.Write(true); cookie.Writer.Write((byte)0); cookie.Writer.Write((byte)10); cookie.Writer.Write('y'); cookie.Writer.Write('й'); cookie.Writer.Write(1234.5678m); cookie.Writer.Write(1234.5678d); cookie.Writer.Write((short)1000); cookie.Writer.Write((int)1001); cookie.Writer.Write((long)-1002); cookie.Writer.Write((string)null); cookie.Writer.Write(""); cookie.Writer.Write("abcd = yй"); cookie.Writer.Write((int[])(null)); cookie.Writer.Write(new int[0]); cookie.Writer.Write(new[] { 1, 2, 3 }); cookie.Writer.Write(UnsafeWriter.StringDelegate, (string[])null); cookie.Writer.Write(UnsafeWriter.StringDelegate, new string[0]); cookie.Writer.Write(UnsafeWriter.StringDelegate, new[] { "a", "b", "c" }); cookie.Writer.Write(UnsafeWriter.StringDelegate, (List <string>)null); cookie.Writer.Write(UnsafeWriter.StringDelegate, new List <string>()); cookie.Writer.Write(UnsafeWriter.StringDelegate, new List <string> { "d", "e" }); reader = UnsafeReader.CreateReader(cookie.Data, cookie.Count); } Assert.False(reader.ReadBoolean()); Assert.True(reader.ReadBoolean()); Assert.AreEqual(0, reader.ReadByte()); Assert.AreEqual(10, reader.ReadByte()); Assert.AreEqual('y', reader.ReadChar()); Assert.AreEqual('й', reader.ReadChar()); Assert.AreEqual(1234.5678m, reader.ReadDecimal()); Assert.AreEqual(1234.5678d, reader.ReadDouble(), 1e-6); Assert.AreEqual(1000, reader.ReadInt16()); Assert.AreEqual(1001, reader.ReadInt32()); Assert.AreEqual(-1002, reader.ReadInt64()); Assert.Null(reader.ReadString()); Assert.AreEqual("", reader.ReadString()); Assert.AreEqual("abcd = yй", reader.ReadString()); Assert.Null(reader.ReadIntArray()); Assert.AreEqual(new int[0], reader.ReadIntArray()); Assert.AreEqual(new[] { 1, 2, 3 }, reader.ReadIntArray()); Assert.Null(reader.ReadArray(UnsafeReader.StringDelegate)); Assert.AreEqual(new string[0], reader.ReadArray(UnsafeReader.StringDelegate)); Assert.AreEqual(new[] { "a", "b", "c" }, reader.ReadArray(UnsafeReader.StringDelegate)); Assert.Null(reader.ReadCollection(UnsafeReader.StringDelegate, n => new List <string>(n))); CollectionAssert.AreEqual(new List <string>(), reader.ReadCollection(UnsafeReader.StringDelegate, n => new List <string>(n))); CollectionAssert.AreEqual(new List <string> { "d", "e" }, reader.ReadCollection(UnsafeReader.StringDelegate, n => new List <string>(n))); }