0

I have application that using PcapDotNet DLLs and send packets. In some Pcap files with high speed rate (~50 Mbit/Sec or ~9000 Packet per seconds) the play takes long time compare to the original Pcap length\duration and I can see that ~25% of my CPUs (i have 4 cores) are utilized. I have asked this question in the project page and the developer suggest I parallel my program so I can better utilize my resources because this is single thread program.

This is example of my function that sends the packets and it does the most pf the work, this function also include several events like report how many packets sent etc... (not include in mt example)

So my question is, assuming that the function is the one that does the most work How can I divide my CPU resources in a better way?

using (PacketSendBuffer sendBuffer = new PacketSendBuffer((uint)capLength))
{
    while (inputCommunicator.ReceivePacket(out packet) == PacketCommunicatorReceiveResult.Ok) // Read the packets from the file
    {
        if (packet != null)
        {
            try
            {
                _outputCommunicator.SendPacket(packet); // Send the Packets
                _sentPackets++;
            }
            catch (Exception ex)
            { 
                // Throw exception
            }
        }
    }
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176
user3328870
  • 341
  • 2
  • 7
  • 23
  • 1
    Pcap is just expensive, the kernel mode switch is untrivial. Parallelizing is pretty unlikely to improve anything, the threads probably will just take turns. Pcap has way too many moving parts, the only way to be sure is to try it. Don't assume you'll get anywhere. You'd be much ahead by not using Pcap, it isn't clear why you need it. Sockets implement streams, that can also be a file stream if necessary for testing. – Hans Passant Apr 18 '14 at 12:27

1 Answers1

0

Are you sure you're not spinning? What's the read timeout on your inputCommunicator? It might be a better idea to use callbacks rather than infinite loops to read the packets:

http://pcapdotnet.codeplex.com/wikipage?title=Pcap.Net%20Tutorial%20-%20Opening%20an%20adapter%20and%20capturing%20the%20packets

Or you could use asynchronous I/O instead, given that you're not actually doing any real CPU work.

In any case, try profiling the application. Find out where it spends time. Find any concurrency issues. You'll have a clearer picture then.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • Because i want to report how many packet sent i spinning and sent each packet separate but also i am using the example from PcapDotNet examples and send this high speed Pcap file using Send buffer it takes time – user3328870 Apr 18 '14 at 11:10
  • Can you show me an example how to use callbacks rather than infinite loops ? – user3328870 Apr 18 '14 at 11:28
  • @user3328870 A sample is in the link I posted. – Luaan Apr 18 '14 at 11:44