我正在尝试使用以下代码使用 tcp 环回连接将一些字节发送到第三方应用程序(在同一台服务器上运行)。
struct sockaddr_in serv_addr;
struct hostent *server;
int sockfd = socket(PF_INET, SOCK_STREAM, 0);
server = gethostbyname(host_address);
bzero((char *) &serv_addr, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *) server->h_addr, (char *) &serv_addr.sin_addr.s_addr, server->h_length);
/**** Port No. Set ****/
serv_addr.sin_port = htons(portno);
int sockKeepAliveOption = 1;
int al = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void*) &sockKeepAliveOption, sizeof (sockKeepAliveOption));
if (al == -1) {
std::cout << "Setsocket option err: SO_KEEPALIVE --unable to set keep alive tcp connection." << std::endl;
}
else {
std::cout << "S0_KEEPALIVE set, with SOL_SOCKET.. . ..\n" << std::endl;
}
我一次发送 400 个字节,每秒发送 100 次。我正在使用以下代码发送
int n = send(sockfd,sendB,400, ONLOAD_MSG_WARM);
我的问题是,我的抖动很高。我得到最小延迟 3 us,平均 7 us 和最大 19 us。我该如何优化它?
谢谢
编辑于 2014 年 8 月 28 日。
让我添加更多信息。我也在不同线程中从同一端口接收数据,但在我发送之后。我还通过以下代码为每个线程分配一个核心,并且除了核心 0 之外的所有 cpu 都与调度程序隔离。
thread1= new std::thread(myfunction, input1, input2);
pthread_t thread_hnd = thread1->native_handle();
CPU_SET(5, &cpuset);
s = pthread_setaffinity_np(thread_hnd, sizeof (cpu_set_t), &cpuset);
当我每 1 毫秒连续发送一次时,我得到了很好的数字(3 或 4 我们),但如果频率较低(比如每秒 1-5 次),那么有时我会得到大约 20 我们,但平均大约是 7 我们。
可以从不同线程在同一端口上侦听和发送产生抖动吗?
第二次编辑于 2014 年 8 月 28 日。
这是我的 CPU 状态。它不会进入 C3。Core 2 [7] 是我通过环回发送数据的线程。
Cpu speed from cpuinfo 3499.00Mhz
True Frequency (without accounting Turbo) 3499 MHz
Socket [0] - [physical cores=6, logical cores=6, max online cores ever=6]
CPU Multiplier 35x || Bus clock frequency (BCLK) 99.97 MHz
TURBO ENABLED on 6 Cores, Hyper Threading OFF
Max Frequency without considering Turbo 3598.97 MHz (99.97 x [36])
Max TURBO Multiplier (if Enabled) with 1/2/3/4/5/6 cores is 38x/37x/36x/36x/36x/36x
Real Current Frequency 3600.17 MHz (Max of below)
Core [core-id] :Actual Freq (Mult.) C0% Halt(C1)% C3 % C6 % Temp
Core 1 [0]: 3600.17 (36.01x) 1.08 98.9 0 0 41
Core 2 [1]: 3595.44 (35.96x) 1.07 98.9 0 0 46
Core 3 [2]: 3595.28 (35.96x) 1 99.1 0 0 40
Core 4 [3]: 3599.01 (36.00x) 1 99.9 0 0 46
Core 5 [4]: 3599.51 (36.01x) 0 100 0 0 50
Core 6 [5]: 3598.97 (36.00x) 100 0 0 0 56
Socket [1] - [physical cores=6, logical cores=6, max online cores ever=6]
CPU Multiplier 35x || Bus clock frequency (BCLK) 99.97 MHz
TURBO ENABLED on 6 Cores, Hyper Threading OFF
Max Frequency without considering Turbo 3598.97 MHz (99.97 x [36])
Max TURBO Multiplier (if Enabled) with 1/2/3/4/5/6 cores is 38x/37x/36x/36x/36x/36x
Real Current Frequency 3600.12 MHz (Max of below)
Core [core-id] :Actual Freq (Mult.) C0% Halt(C1)% C3 % C6 % Temp
Core 1 [6]: 3598.97 (36.00x) 100 0 0 0 56
Core 2 [7]: 3598.51 (36.00x) 1.12 98.8 0 0 49
Core 3 [8]: 3599.98 (36.01x) 1.94 98 0 0 45
Core 4 [9]: 3598.97 (36.00x) 100 0 0 0 56
Core 5 [10]: 3599.48 (36.01x) 1 99.9 0 0 48
Core 6 [11]: 3600.12 (36.01x) 3.44 96.5 0 0 45
C0 = Processor running without halting
C1 = Processor running with halts (States >C0 are power saver)
C3 = Cores running with PLL turned off and core cache turned off
C6 = Everything in C3 + core state saved to last level cache
Above values in table are in percentage over the last 1 sec
[core-id] refers to core-id number in /proc/cpuinfo