//Waits for a message and makes new thread to handle when received static unsafe void Main(string[] args) { Writer = new BlockWriter(GetPrevHeader()); GPUWrapper.main(); //Initialize GPU //Initialize 100 transactions int index = 0; int testnum = 10; int finalblockcount = testnum / 10; //int finalblockcount = testnum; List <SafeMessage> msgsjson = JsonConvert.DeserializeObject <List <SafeMessage> >(File.ReadAllText("messages.json")); bool printed = false; Stopwatch sw = new Stopwatch(); sw.Start(); for (int i = 0; i < testnum; i++) { SafeMessage message = msgsjson.ElementAt(index); if (message.ConfigSeq != null) //If message exists { if (useGPU) { Console.WriteLine(String.Format("Test {0}, HandleMessage posted", i)); Message unsafemessage = ConvertToUnsafe(message); int temp = i; //Concurrency issue happens if I don't do this Task t = Task.Factory.StartNew(() => HandleMessage(unsafemessage, temp)); //Run message handler t.Wait(); } else //Use CPU { Console.WriteLine(String.Format("Test {0}, HandleMessageCPU posted", i)); int temp = i; Task.Factory.StartNew(() => HandleMessageCPU(message, temp)); //Run message handler } } else if (Timer != new DateTime() && (DateTime.Now - Timer).TotalSeconds > Timeout) { Console.WriteLine(String.Format("Test {0}, HandleTimer posted", i)); Timer = new DateTime(); //Reset timer Task.Factory.StartNew(() => HandleTimer()); } index = (index + 1) % 500; } while (true) { if (BlockCount >= (finalblockcount) && !printed) { sw.Stop(); Console.WriteLine("Time to process " + testnum + " transactions: " + sw.ElapsedMilliseconds + " ms"); Console.WriteLine("Transactions per second: " + (1000 * (double)testnum / sw.ElapsedMilliseconds) + " tps"); //Console.WriteLine("Avg time to order (no batch returned): " + (OrderTime / (double)(testnum - finalblockcount)) + " ms"); //Console.WriteLine("Avg time to fully handle msg: " + (TotalHandleTime / (double)testnum) + " ms"); //total handle time is consistently .01ms more than avg order time //Console.WriteLine("Avg time to order (batch returned): " + (OrderTimeCut / (double)finalblockcount) + " ms"); printed = true; //Environment.Exit(0); } } }
//If timer runs out, cut any pending batches and make them into a block //Ensures that pending messages will always be sent through after some timeout public static void HandleTimer() { Batch newbatch = GPUWrapper.Cut(); if (newbatch.MsgCount > 0) { Task.Factory.StartNew(() => MakeBlock(newbatch, false)); } }
//public static long TotalHandleTime = 0; public static unsafe void HandleMessage(Message msg, int index) { //Stopwatch testsw = new Stopwatch(); //testsw.Start(); //Check if configmsg's payload is initialized. If not, it's a normal message. if ((IntPtr)msg.ConfigMsg.Payload != IntPtr.Zero) //It's a config message { Console.WriteLine("[HandleMessage] config message"); //Cut pending batch so we can make and send the block then a config block Batch newbatch = GPUWrapper.Cut(); if (newbatch.MsgCount > 0) { Task.Factory.StartNew(() => MakeBlock(newbatch, false)); } //Now make a config block Batch newconfigbatch = new Batch(); newconfigbatch.Messages[0] = msg.ConfigMsg; Task.Factory.StartNew(() => MakeBlock(newconfigbatch, true)); } else //It's a normal message { Console.WriteLine("[HandleMessage] normal message"); Batch Batch1 = new Batch(); Batch Batch2 = new Batch(); //Stopwatch sw1 = new Stopwatch(); //sw1.Start(); bool IsPendingBatch = GPUWrapper.Ordered(msg.NormalMsg, ref Batch1, ref Batch2, index); //sw1.Stop(); //if(Batch2.MsgCount > 0) //{ // OrderTimeCut += sw1.ElapsedMilliseconds; //} //else //{ // OrderTime += sw1.ElapsedMilliseconds; //} //string output = "Time taken: " + sw1.ElapsedMilliseconds + " ms" + Environment.NewLine; //Console.WriteLine(output); //Ordered returns 0, 1, or 2 batches, process each one returned into a block if (Batch1.MsgCount > 0) { Task.Factory.StartNew(() => MakeBlock(Batch1, false)); } if (Batch2.MsgCount > 0) { Task.Factory.StartNew(() => MakeBlock(Batch2, false)); } //Handle setting of timer bool timernull = Timer == new DateTime(); //Timer is default value if (!timernull && !IsPendingBatch) //There is a timer but no pending batches, unnecessary { Timer = new DateTime(); //Set timer to default value } else if (timernull && IsPendingBatch) { Timer = DateTime.Now; //Start timer } } //testsw.Stop(); //TotalHandleTime += testsw.ElapsedMilliseconds; //Console.WriteLine("Time taken: " + testsw.ElapsedMilliseconds); }