kaitoy / pcap4j

A Java library for capturing, crafting, and sending packets.

Home Page:https://www.pcap4j.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

High CPU utilization

naveengauba opened this issue · comments

I am using this library to capture a particular http packet on Linux (CentOS). When I enable packet sniffing the CPU utilization goes all the way to 100%. Disabling packet sniffing brings the CPU utilization to less than 1%. Do you have any suggestions here? Here is a snippet of my code.

PcapHandle.Builder pcapHandleBuilder = new PcapHandle.Builder(pcapNetworkInterface.getName()) .snaplen(snapLength).promiscuousMode(PromiscuousMode.NONPROMISCUOUS).timeoutMillis(readTimeoutMillis).bufferSize(bufferSize); pcapHandle = pcapHandleBuilder.build(); pcapHandle.setBlockingMode(BlockingMode.NONBLOCKING); StringBuilder sbBpfFilter = new StringBuilder(" tcp port ").append(80).append(" or ").append(443).append(" and dst ").append(defaultInterfaceAddress.getHostAddress()); String bpfFilter = sbBpfFilter.toString(); pcapHandle.setFilter(bpfFilter, BpfCompileMode.OPTIMIZE); pcapHandle.loop(-1, new MyPacketListener(pcapHandle), packetProcessor);

Appreciate any help here?

I also took a thread-dump and the analysis shows the High CPU usage could be caused by org.pcap4j.core.NativeMappings.pcap_loop method

priority:5 - threadId:0x00007fd48cd08000 - nativeId:0x559c - nativeId (decimal):21916 - state:RUNNABLE stackTrace: Nov 16 14:47:25 Dev-PFE-05 sh[21902]: java.lang.Thread.State: RUNNABLE Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at org.pcap4j.core.NativeMappings.pcap_loop(Native Method) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at org.pcap4j.core.PcapHandle.doLoop(PcapHandle.java:834) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at org.pcap4j.core.PcapHandle.loop(PcapHandle.java:770) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at com.qumu.kodiak.http.perf.PerformancePacketSniffer$PacketSnifferRunnable.run(PerformancePacketSniffer.java:147) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.FutureTask.run(FutureTask.java:266) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) Nov 16 14:47:25 Dev-PFE-05 sh[21902]: at java.lang.Thread.run(Thread.java:748)

Just curious : Why are you using builders ?

@LucBerge I had to set so many parameters onto PcapHandle, so decided to use Builder. Do you think thats the cause of the CPU usage issue? I could change that.

Hello there, same for me, while tcpdump (with the same filter) is not killing my cpu... I think this is due to pcap4j impl...

Hello, just found hasardously how to fix high cpu load !! Just use PcapHandle#loop(packetCount, listener, threadPool) instead of PcapHandle#loop(packetCount, listener)

I don't know why but cpu load dropped after this method use. Maybe a single threaded pool is enough, depends on your needs !