linux socketキャッシュ:core rmem_default rmem_max
13849 ワード
:
/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max
udp :
setsockopt , rmem_default.
setsockopt , rmem_max.
udp socket , .
udp socket rmem_default ( setsockopt ).
テストコード:
//udp.c
#include
#include
#include
#include
#include
#include
#include
#define SERVER_PORT 1201
#define MAX_DATA_LEN 1024 //
#define SERVER_SEND_SLEEP 1000*4 //
#define CLIENT_RECV_SLEEP 1000*100 //
//
static int serverfd = 0; // UDP
static int sendcount = 0; //
//
void* thread_server_send(void *arg)
{
static int threadidx = 0;
threadidx++;
char tname[256] = {0};
snprintf(tname, sizeof(tname), "thread-%d", threadidx);
//
char data[MAX_DATA_LEN] = {0};
int i = 0;
for ( ; i < MAX_DATA_LEN; i++)
{
data[i] = '0' + (i % 10);
}
//
struct sockaddr_in cliaddr = {0};
memcpy(&cliaddr, arg, sizeof(cliaddr));
int printer = 0;
while(1){
if (sendcount <= 0){
usleep(1000*10);
if (printer >= 0) printer++;
if (printer>250){
printf("%s is alive!
", tname);
printer = -1; //
}
continue;
}
int count = sendcount;
sleep(2);// , sendcount.
sendcount = 0;
printf("read new count value:%d
", count);
for ( i = 0; i < count; i++)
{
int result = sendto(serverfd, data, sizeof(data), 0, (struct sockaddr*)&cliaddr, sizeof(struct sockaddr_in));
printf("tname:%s fd:%d sendlen:%d client:%s:%u counter:%d
", tname, serverfd, result, inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port), i+1);
usleep(SERVER_SEND_SLEEP);
}
}
}
int exec_udp_server()
{
printf("######## exec udp server ########
");
if( (serverfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("Socket error.. %d
", errno);
exit( EXIT_FAILURE );
}
struct sockaddr_in servaddr;
bzero( &servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(SERVER_PORT);
//
int buflen = 0;
int paramlen = sizeof(buflen);
int ret = 0;
//setsockopt(listenfd, SOL_SOCKET, SO_SNDBUF, (char*)&nZero, sizeof(nZero));
getsockopt(serverfd, SOL_SOCKET, SO_SNDBUF, &buflen, ¶mlen);
printf("fd:%d send buffer length:%d result:%d
", serverfd, buflen, ret);
getsockopt(serverfd, SOL_SOCKET, SO_RCVBUF, &buflen, ¶mlen);
printf("fd:%d recv buffer length:%d result:%d
", serverfd, buflen, ret);
//
if( bind(serverfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1)
{
printf("Bind error... %d
", errno);
exit( EXIT_FAILURE );
}
printf("udp bind exec
");
//
while(1){
// :
// 1 , . , , .
// 2 , ( MAX_DATA_LEN), .
printf("menu info
");
printf("1:waiting for client
");
printf("2:ready to send data
");
char input[256] = {0};
gets(input);
if (strcmp(input, "1") == 0){
printf("waiting for client data now!
");
char recvbuf[1024] = {0};
struct sockaddr_in cliaddr = {0};
paramlen = sizeof(struct sockaddr_in);
ret = recvfrom(serverfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&cliaddr, ¶mlen);
printf("recv client data:%s len:%d
", recvbuf, ret);
int thread = 0;
pthread_create(&thread, NULL, thread_server_send, &cliaddr);
sleep(1);
}else if (strcmp(input, "2") == 0){
char input[256] = {0};
printf("the count you want to send:");//
gets(input);
sendcount = atoi(input);
printf("new count value:%d
", sendcount);
}
}
}
int exec_udp_client()
{
printf("######## exec udp client ########
");
int connfd;
if( (connfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1 )
{
printf("Socket error.. %d
", errno);
exit( EXIT_FAILURE );
}
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = inet_addr("192.168.6.37");
servaddr.sin_port = htons(SERVER_PORT);
//
int buflen = 0;
int paramlen = sizeof(buflen);
int ret = 0;
//setsockopt(listenfd, SOL_SOCKET, SO_SNDBUF, (char*)&nZero, sizeof(nZero));
getsockopt(connfd, SOL_SOCKET, SO_SNDBUF, &buflen, ¶mlen);
printf("fd:%d send buffer length:%d result:%d
", connfd, buflen, ret);
getsockopt(connfd, SOL_SOCKET, SO_RCVBUF, &buflen, ¶mlen);
printf("fd:%d recv buffer length:%d result:%d
", connfd, buflen, ret);
//
char sendbuf[1024] = "hello server, start now!";
int result = sendto(connfd, sendbuf, strlen(sendbuf), 0, (struct sockaddr*)&servaddr, sizeof(struct sockaddr_in));
printf("send server data:%s len:%d
", sendbuf, result);
//
char data[MAX_DATA_LEN+1] = {0};
int counter = 0;
while(1)
{
int len = sizeof(struct sockaddr_in);
int result = recvfrom(connfd, data, MAX_DATA_LEN, 0, (struct sockaddr*)&servaddr, &len);
if (result > 0) counter++;
printf("fd:%d recvlen:%d server:%s:%u counter:%d
", connfd, result, inet_ntoa(servaddr.sin_addr), ntohs(servaddr.sin_port), counter);
usleep(CLIENT_RECV_SLEEP);
}
close(connfd);
}
//main.c
// us .
// us
#include
#include
#include
#include
#include
#include
int main(int argc, char** argv[])
{
if (strcmp(argv[1], "us") == 0){
exec_udp_server();
}else if (strcmp(argv[1], "uc") == 0){
exec_udp_client();
}
return 0;
}
:
/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max
udp :
setsockopt , rmem_default.
setsockopt , rmem_max.
udp socket , .
udp socket rmem_default ( setsockopt ).
:
, getsockopt&SO_RCVBUF . -1.
-1:
1. , -1, .
2. 16K
sysctl -w net.core.rmem_max=16384
sysctl -w net.core.rmem_default=16384
3.
4. -2, 16.( 16K )
:
read new count value:16
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:1
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:2
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:3
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:4
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:5
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:6
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:7
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:8
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:9
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:10
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:11
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:12
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:13
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:14
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:15
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:58402 counter:16
:
fd:3 send buffer length:108544 result:0
fd:3 recv buffer length:16384 result:0
send server data:hello server, start now! len:24
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:1
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:2
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:3
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:4
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:5
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:6
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:7
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:8
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:9
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:10
16K, 16K . 10K.
, 48K, 48 , 30K.
: upd : SO_RCVBUF * 0.625 (0.625 , )
? .
, : sysctl -w net.core.rmem_max=xxxx ,
xxxx 2 , .
setsockopt SO_RCVBUF xxxx, 2 .
.
?
Sets or gets the maximum socket receive buffer in bytes.
The kernel doubles this value (to allow space for book‐keeping overhead)
when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2).
The default value is set by the /proc/sys/net/core/rmem_default file,
and the maximum allowed value is set by the/proc/sys/net/core/rmem_max file.
The minimum (doubled) value for this option is 256.
setsockopt getsockopt .
-2:
1. , -1, .
2. 16K
sysctl -w net.core.rmem_max=16384
sysctl -w net.core.rmem_default=16384
3. 1
4. -1
5. 2
6. -2, 16.( 16K )
:
read new count value:16
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:1
read new count value:16
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:1
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:2
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:2
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:3
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:3
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:4
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:4
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:5
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:5
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:6
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:6
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:7
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:7
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:8
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:8
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:9
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:9
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:10
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:10
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:11
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:11
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:12
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:12
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:13
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:13
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:14
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:14
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:15
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:15
tname:thread-2 fd:3 sendlen:1024 client:192.168.6.125:57408 counter:16
tname:thread-1 fd:3 sendlen:1024 client:192.168.6.125:55154 counter:16
1 :
fd:3 send buffer length:108544 result:0
fd:3 recv buffer length:16384 result:0
send server data:hello server, start now! len:24
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:1
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:2
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:3
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:4
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:5
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:6
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:7
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:8
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:9
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:10
2 :
fd:3 send buffer length:108544 result:0
fd:3 recv buffer length:16384 result:0
send server data:hello server, start now! len:24
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:1
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:2
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:3
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:4
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:5
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:6
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:7
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:8
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:9
fd:3 recvlen:1024 server:192.168.6.37:1201 counter:10
:
/proc/sys/net/core/rmem_default
/proc/sys/net/core/rmem_max
udp socket , .