gpt4 book ai didi

linux - 高 CPU 使用率 - Linux 上的简单数据包接收器

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:25:09 24 4
gpt4 key购买 nike

我正在 Linux 下编写一个简单的应用程序来收集来自网络的所有数据包。我通过调用“recvfrom()”函数来使用阻塞接收。当我使用 hping3(每秒约 100k 原始帧,每个 130 字节)生成大网络负载时,“top”工具显示我的进程的 CPU 使用率很高——大约是 37-38%。这对我来说很有值(value)。当我减少数据包数量时,使用率会降低 - 例如每秒 4k 帧时顶部显示 3%。
我检查过 DC++ 的下载速度约为 10MB/s,它的进程使用的 CPU 不是 38%,而是 5%。 C 中是否有任何可编程的方式来减少 CPU 使用率并仍然接收大量帧?

我的处理器:英特尔 i5-2400 CPU @ 3.10Ghz

我的系统:带有 PREEMPT-RT 补丁的 Ubuntu 11.04 内核 3.6.6

这是我的代码:

#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <arpa/inet.h>

/* Socket descriptor. */
int mainSocket;

/* Buffer for frame. */
unsigned char* buffer;

int main(int argc, char* argv[])
{
/** Create socket. **/
mainSocket = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if (mainSocket == -1) {
printf("Error: cannot create socket!\n");
}

/** Create buffer for frame **/
buffer = malloc(ETH_FRAME_LEN);

printf("Listing...");

while(1) {

// Length of received packet
int length = recvfrom(mainSocket, buffer, ETH_FRAME_LEN, 0, NULL, NULL);
if(length > 0)
{
// ... do something ...
}
}

最佳答案

我不知道这是否有帮助,但在 Google 上我看到了:

DC++ 是否进行混杂接收?我不会这么猜的。因此,与其将您的性能与 DC++ 进行比较,不如将您的性能与 libpcap 等实用程序的性能进行比较。

关于linux - 高 CPU 使用率 - Linux 上的简单数据包接收器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14145540/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com