static SafeMessage GetMessage() { try { string messagestext = File.ReadAllText("messages.json"); //Get all messages from the json file List <SafeMessage> messages = JsonConvert.DeserializeObject <List <SafeMessage> >(messagestext); SafeMessage message = messages[0]; //Get first element messages.RemoveAt(0); //Remove that element string removedtext = JsonConvert.SerializeObject(messages); //Convert list - first object to string //May error because file is open, try until successful while (true) { try { if (messages.Count > 0) //Write remaining messages { File.WriteAllText("messages.json", removedtext); break; } else //Write empty list { File.WriteAllText("messages.json", "[]"); break; } } catch { } } return(message); } catch //No message available { return(new SafeMessage()); } }
//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); } } }
public static unsafe void HandleMessageCPU(SafeMessage msg, int index) { Console.WriteLine(String.Format("HandleMessageCPU {0}", index)); //Check if configmsg is initialized. If not, it's a normal message. bool config = false; if (config) //It's a config message { //Cut pending batch so we can make and send the block then a config block SafeBatch newbatch = Cutter.Cut(); Console.WriteLine("Config Message, newbatch message count={0}", newbatch.Messages.Count()); if (newbatch.Messages.Count() > 0) { Task.Factory.StartNew(() => MakeBlockCPU(newbatch, false)); } //Now make a config block SafeBatch newconfigbatch = new SafeBatch(); newconfigbatch.Messages = new SafeEnvelope[1]; newconfigbatch.Messages[0] = msg.ConfigMsg; Task.Factory.StartNew(() => MakeBlockCPU(newconfigbatch, true)); } else //It's a normal message { //Stopwatch sw1 = new Stopwatch(); //sw1.Start(); SafeOrderedReturn OR = Cutter.Ordered(msg.NormalMsg, index); //sw1.Stop(); //string output = "Time taken: " + sw1.ElapsedTicks + " ticks" + Environment.NewLine; //Console.WriteLine(output); //Ordered returns 0, 1, or 2 batches, process each one Console.WriteLine("Normal Message, batches: {0}, {1}", OR.Batches[0], OR.Batches[1]); Parallel.For(0, 2, i => { if (OR.Batches[i] != null) { Task.Factory.StartNew(() => MakeBlockCPU(OR.Batches[i], false)); } }); //Handle setting of timer bool pending = OR.IsPendingBatch; //There is a pending batch bool timernull = Timer == new DateTime(); //Timer is default value if (!timernull && !pending) //There is a timer but no pending batches, unnecessary { Timer = new DateTime(); //Set timer to default value } else if (timernull && pending) { Timer = DateTime.Now; //Start timer } } }
static SafeMessage GetMessageDontRemove(int index) { try { string messagestext = File.ReadAllText("messages.json"); //Get all messages from the json file List <SafeMessage> messages = JsonConvert.DeserializeObject <List <SafeMessage> >(messagestext); SafeMessage message = messages[index]; //Get first element //May error because file is open, try until successful return(message); } catch //No message available { return(new SafeMessage()); } }
static unsafe Message ConvertToUnsafe(SafeMessage msg) { Message usmsg = new Message(); usmsg.ConfigSeq = msg.ConfigSeq; usmsg.ConfigMsg = new Envelope(); if (msg.ConfigMsg.Payload.Length > 0) { IntPtr cp = Marshal.AllocHGlobal(msg.ConfigMsg.Payload.Length); Marshal.Copy(msg.ConfigMsg.Payload, 0, cp, msg.ConfigMsg.Payload.Length); usmsg.ConfigMsg.Payload = (byte *)cp.ToPointer(); } if (msg.ConfigMsg.Signature.Length > 0) { IntPtr cs = Marshal.AllocHGlobal(msg.ConfigMsg.Signature.Length); Marshal.Copy(msg.ConfigMsg.Signature, 0, cs, msg.ConfigMsg.Signature.Length); usmsg.ConfigMsg.Signature = (byte *)cs.ToPointer(); } usmsg.NormalMsg = new Envelope(); if (msg.NormalMsg.Payload.Length > 0) { IntPtr np = Marshal.AllocHGlobal(msg.NormalMsg.Payload.Length); Marshal.Copy(msg.NormalMsg.Payload, 0, np, msg.NormalMsg.Payload.Length); usmsg.NormalMsg.Payload = (byte *)np.ToPointer(); } if (msg.NormalMsg.Signature.Length > 0) { IntPtr ns = Marshal.AllocHGlobal(msg.NormalMsg.Signature.Length); Marshal.Copy(msg.NormalMsg.Signature, 0, ns, msg.NormalMsg.Signature.Length); usmsg.NormalMsg.Signature = (byte *)ns.ToPointer(); } return(usmsg); }