Beispiel #1
0
        public unsafe PacketGenerator(string pciAddr)
        {
            InitMempool();

            var dev = new IxgbeDevice(pciAddr, 1, 1);

            // TODO: switch to C# 7.3 and replace with Span<PacketBuffer> buffers = stackalloc PacketBuffer[BatchSize];
            var buffersArray = stackalloc PacketBuffer[BatchSize];
            var buffers      = new Span <PacketBuffer>(buffersArray, BatchSize);

            var   statsOld  = new DeviceStats(dev);
            var   statsNew  = new DeviceStats(dev);
            ulong counter   = 0;
            var   stopWatch = new Stopwatch();

            stopWatch.Start();

            int seqNum = 0;

            while (true)
            {
                var batchCount = _mempool.GetPacketBuffers(buffers);
                var batch      = buffers.Slice(0, batchCount);
                foreach (var buf in batch)
                {
                    buf.WriteData(PacketSize - 4, seqNum++);
                }
                dev.TxBatchBusyWait(0, batch);

                if ((counter++ & 0xFFF) == 0 && stopWatch.ElapsedMilliseconds > 100)
                {
                    stopWatch.Stop();
                    var nanos = stopWatch.ElapsedTicks;
                    dev.ReadStats(ref statsNew);
                    statsNew.PrintStatsDiff(ref statsOld, (ulong)nanos);
                    statsOld = statsNew;
                    counter  = 0;
                    stopWatch.Restart();
                }
            }
        }