一段有趣的代码, 让send消耗一慢一快,谁能解释?非常简单的代码

testtcpsend 发布于 2016/08/31 09:52
阅读 1K+
收藏 0
#include<sys/types.h>
 #include<sys/socket.h>
 #include<netinet/in.h>
 #include<arpa/inet.h>
 #include<unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include<sys/wait.h>
 #include <string.h>
#include <time.h>
#include <unistd.h>
 #include <fcntl.h>
#include <sys/time.h>
#include <stdio.h>
#include  <netinet/in.h>
 #include <netinet/tcp.h> // for TCP_NODELAY
#include <sys/time.h>
#include <sys/epoll.h>
#include <time.h>

typedef unsigned int            uint32_t;
 void xsleep(uint32_t ms)
 {
   struct timeval tval;
   tval.tv_sec = ms / 1000;
   tval.tv_usec = ( ms * 1000) % 1000000;
   select(0, NULL, NULL, NULL, &tval);
 } 


 int main(int argc, char ** argv)
 {
     int sockfd,new_fd;
     struct sockaddr_in my_addr;
     struct sockaddr_in their_addr;
     unsigned int sin_size, myport, lisnum;
 
     if(argv[1])  myport = atoi(argv[1]);
     else myport = 50000;
 
     if(argv[2])  lisnum = atoi(argv[2]);
     else lisnum = 2;
 
     if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
         perror("socket");
         exit(1);
     }
  printf("socket %d ok \n",myport);
//fcntl(sockfd, F_SETFL, O_NONBLOCK);
    my_addr.sin_family=PF_INET;
     my_addr.sin_port=htons(myport);
     my_addr.sin_addr.s_addr = INADDR_ANY;
     bzero(&(my_addr.sin_zero), 0);
     if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) {
         perror("bind");
         exit(1);
     }
  printf("bind ok \n");
 
     if (listen(sockfd, lisnum) == -1) {
         perror("listen");
         exit(1);
     }
  printf("listen ok \n");
 


    sin_size = sizeof(struct sockaddr_in);

xxx:
     if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) {
         perror("accept");
   goto xxx;
     }



     printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
fcntl(new_fd, F_SETFL, O_NONBLOCK);
int arg = 1;
setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, (char*)&arg, sizeof(int));
int nSendBuf=1024*1024*16;//¨¦??32K
setsockopt(new_fd,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int));


int epfd;
struct epoll_event ev,events[20];
epfd=epoll_create(256);

ev.data.fd=sockfd;
ev.events=EPOLLIN|EPOLLOUT;
epoll_ctl(epfd,EPOLL_CTL_ADD,sockfd,&ev);

ev.data.fd=new_fd;
ev.events=EPOLLIN|EPOLLOUT;
epoll_ctl(epfd,EPOLL_CTL_ADD,new_fd,&ev);



 int   step = 0;
   char  szSnd[1024] = {0};
   memset(szSnd,97,1024);
szSnd[100] = 0;

//time_t lasttime = time(NULL);
struct timeval lasttime;
gettimeofday(&lasttime,0);
double alltimeuse = 0.0;


int iii = 0;
while(iii++<100)
{
struct timeval lasttime;
gettimeofday(&lasttime,0);

 if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
     perror("send");
 }


struct timeval now_time;
gettimeofday(&now_time,0);
double timeuse = 1000000.0*(now_time.tv_sec - lasttime.tv_sec) + now_time.tv_usec - lasttime.tv_usec;
timeuse /=1000.0;
 printf("xxsend msg: %f\n",(timeuse));



{
struct timeval lasttime;
gettimeofday(&lasttime,0);

 if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
     perror("send");
 }


struct timeval now_time;
gettimeofday(&now_time,0);
double timeuse = 1000000.0*(now_time.tv_sec - lasttime.tv_sec) + now_time.tv_usec - lasttime.tv_usec;
timeuse /=1000.0;
 printf("xxsend1 msg: %f\n",(timeuse));
}

/*
for(int ccc=0; ccc<1000000; ccc++)
   {
char aaax[10240];
memset(aaax,0,10240);
}*/
xsleep(100);
//usleep(1000);
};

 exit(0);
 }



输出结果为:

xxsend msg: 0.028000
xxsend1 msg: 0.002000
xxsend msg: 0.143000
xxsend1 msg: 0.006000
xxsend msg: 0.142000
xxsend1 msg: 0.123000
xxsend msg: 0.504000
xxsend1 msg: 0.008000
xxsend msg: 0.135000
xxsend1 msg: 0.005000
xxsend msg: 0.144000
xxsend1 msg: 0.006000
xxsend msg: 0.146000
xxsend1 msg: 0.007000
xxsend msg: 0.122000
xxsend1 msg: 0.007000
xxsend msg: 0.167000
xxsend1 msg: 0.287000
xxsend msg: 0.145000
xxsend1 msg: 0.007000
xxsend msg: 0.046000
xxsend1 msg: 0.002000
xxsend msg: 0.116000
xxsend1 msg: 0.002000
xxsend msg: 0.152000
xxsend1 msg: 0.006000
xxsend msg: 0.145000

加载中
0
乌龟壳
乌龟壳

这是strace的结果,从内核的角度来说也有这个现象,应该和操作系统对进程的调度有关系

1472615714.178089 execve("./test", ["./test"], [/* 23 vars */]) = 0
1472615714.178294 brk(0)                = 0xdf7000
1472615714.178337 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
1472615714.178391 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa8b85d4000
1472615714.178434 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
1472615714.178473 open("/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
1472615714.178513 fstat(3, {st_mode=S_IFREG|0644, st_size=21256, ...}) = 0
1472615714.178547 mmap(NULL, 21256, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fa8b85ce000
1472615714.178577 close(3)              = 0
1472615714.178603 access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
1472615714.178638 open("/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
1472615714.178669 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320\37\2\0\0\0\0\0"..., 832) = 832
1472615714.178703 fstat(3, {st_mode=S_IFREG|0755, st_size=1840928, ...}) = 0
1472615714.178735 mmap(NULL, 3949248, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7fa8b7fef000
1472615714.178764 mprotect(0x7fa8b81aa000, 2093056, PROT_NONE) = 0
1472615714.178794 mmap(0x7fa8b83a9000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1ba000) = 0x7fa8b83a9000
1472615714.178833 mmap(0x7fa8b83af000, 17088, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7fa8b83af000
1472615714.178868 close(3)              = 0
1472615714.178901 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa8b85cd000
1472615714.178933 mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa8b85cb000
1472615714.178966 arch_prctl(ARCH_SET_FS, 0x7fa8b85cb740) = 0
1472615714.179052 mprotect(0x7fa8b83a9000, 16384, PROT_READ) = 0
1472615714.179088 mprotect(0x601000, 4096, PROT_READ) = 0
1472615714.179118 mprotect(0x7fa8b85d6000, 4096, PROT_READ) = 0
1472615714.179146 munmap(0x7fa8b85ce000, 21256) = 0
1472615714.179204 socket(PF_INET, SOCK_STREAM, IPPROTO_IP) = 3
1472615714.179249 setsockopt(3, SOL_SOCKET, SO_REUSEADDR, [1], 4) = 0
1472615714.179286 bind(3, {sa_family=AF_INET, sin_port=htons(8090), sin_addr=inet_addr("0.0.0.0")}, 16) = 0
1472615714.179336 fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
1472615714.179367 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fa8b85d3000
1472615714.179398 write(1, "bind ok \n", 9) = 9
1472615714.179558 listen(3, 100)        = 0
1472615714.179591 write(1, "listen ok \n", 11) = 11
1472615714.179731 accept(3, {sa_family=AF_INET, sin_port=htons(55142), sin_addr=inet_addr("127.0.0.1")}, [16]) = 4
1472615715.027067 write(1, "server: got connection from 127."..., 38) = 38
1472615715.027113 setsockopt(4, SOL_TCP, TCP_NODELAY, [1], 4) = 0
1472615715.027148 gettimeofday({1472615715, 27159}, NULL) = 0
1472615715.027181 gettimeofday({1472615715, 27258}, NULL) = 0
1472615715.027373 write(1, "id:0 use:0.099000\n", 18) = 18
1472615715.027512 gettimeofday({1472615715, 27520}, NULL) = 0
1472615715.027543 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.027588 gettimeofday({1472615715, 27595}, NULL) = 0
1472615715.027619 write(1, "id:1 use:0.075000\n", 18) = 18
1472615715.027747 gettimeofday({1472615715, 27760}, NULL) = 0
1472615715.027780 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.027821 gettimeofday({1472615715, 27829}, NULL) = 0
1472615715.027895 write(1, "id:2 use:0.069000\n", 18) = 18
1472615715.028028 gettimeofday({1472615715, 28036}, NULL) = 0
1472615715.028056 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.028096 gettimeofday({1472615715, 28104}, NULL) = 0
1472615715.028127 write(1, "id:3 use:0.068000\n", 18) = 18
1472615715.028257 gettimeofday({1472615715, 28265}, NULL) = 0
1472615715.028289 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.128549 gettimeofday({1472615715, 128559}, NULL) = 0
1472615715.128609 write(1, "id:6 use:100.294000\n", 20) = 20
1472615715.128896 gettimeofday({1472615715, 128904}, NULL) = 0
1472615715.128924 gettimeofday({1472615715, 128932}, NULL) = 0
1472615715.128954 write(1, "id:0 use:0.028000\n", 18) = 18
1472615715.129084 gettimeofday({1472615715, 129092}, NULL) = 0
1472615715.129113 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.129172 gettimeofday({1472615715, 129179}, NULL) = 0
1472615715.129203 write(1, "id:1 use:0.087000\n", 18) = 18
1472615715.129331 gettimeofday({1472615715, 129339}, NULL) = 0
1472615715.129359 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.129399 gettimeofday({1472615715, 129407}, NULL) = 0
1472615715.129430 write(1, "id:2 use:0.068000\n", 18) = 18
1472615715.129558 gettimeofday({1472615715, 129566}, NULL) = 0
1472615715.129586 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.129625 gettimeofday({1472615715, 129632}, NULL) = 0
1472615715.129655 write(1, "id:3 use:0.066000\n", 18) = 18
1472615715.129784 gettimeofday({1472615715, 129791}, NULL) = 0
1472615715.129811 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.230024 gettimeofday({1472615715, 230033}, NULL) = 0
1472615715.230077 write(1, "id:6 use:100.242000\n", 20) = 20
1472615715.230324 gettimeofday({1472615715, 230332}, NULL) = 0
1472615715.230352 gettimeofday({1472615715, 230359}, NULL) = 0
1472615715.230382 write(1, "id:0 use:0.027000\n", 18) = 18
1472615715.230512 gettimeofday({1472615715, 230520}, NULL) = 0
1472615715.230540 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.230600 gettimeofday({1472615715, 230607}, NULL) = 0
1472615715.230630 write(1, "id:1 use:0.087000\n", 18) = 18
1472615715.230761 gettimeofday({1472615715, 230768}, NULL) = 0
1472615715.230789 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.230829 gettimeofday({1472615715, 230836}, NULL) = 0
1472615715.230860 write(1, "id:2 use:0.068000\n", 18) = 18
1472615715.230984 gettimeofday({1472615715, 230992}, NULL) = 0
1472615715.231013 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.231063 gettimeofday({1472615715, 231070}, NULL) = 0
1472615715.231093 write(1, "id:3 use:0.078000\n", 18) = 18
1472615715.231220 gettimeofday({1472615715, 231227}, NULL) = 0
1472615715.231248 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.331467 gettimeofday({1472615715, 331477}, NULL) = 0
1472615715.331519 write(1, "id:6 use:100.250000\n", 20) = 20
1472615715.331760 gettimeofday({1472615715, 331768}, NULL) = 0
1472615715.331789 gettimeofday({1472615715, 331796}, NULL) = 0
1472615715.331818 write(1, "id:0 use:0.028000\n", 18) = 18
1472615715.331964 gettimeofday({1472615715, 331972}, NULL) = 0
1472615715.331992 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.332051 gettimeofday({1472615715, 332058}, NULL) = 0
1472615715.332082 write(1, "id:1 use:0.086000\n", 18) = 18
1472615715.332293 gettimeofday({1472615715, 332301}, NULL) = 0
1472615715.332322 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.332362 gettimeofday({1472615715, 332369}, NULL) = 0
1472615715.332392 write(1, "id:2 use:0.068000\n", 18) = 18
1472615715.332560 gettimeofday({1472615715, 332568}, NULL) = 0
1472615715.332588 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.332628 gettimeofday({1472615715, 332635}, NULL) = 0
1472615715.332658 write(1, "id:3 use:0.067000\n", 18) = 18
1472615715.332817 gettimeofday({1472615715, 332825}, NULL) = 0
1472615715.332845 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.433064 gettimeofday({1472615715, 433073}, NULL) = 0
1472615715.433116 write(1, "id:6 use:100.248000\n", 20) = 20
1472615715.433360 gettimeofday({1472615715, 433368}, NULL) = 0
1472615715.433388 gettimeofday({1472615715, 433395}, NULL) = 0
1472615715.433418 write(1, "id:0 use:0.027000\n", 18) = 18
1472615715.433548 gettimeofday({1472615715, 433556}, NULL) = 0
1472615715.433598 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.433657 gettimeofday({1472615715, 433664}, NULL) = 0
1472615715.433687 write(1, "id:1 use:0.108000\n", 18) = 18
1472615715.433883 gettimeofday({1472615715, 433895}, NULL) = 0
1472615715.433916 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.433955 gettimeofday({1472615715, 433996}, NULL) = 0
1472615715.434023 write(1, "id:2 use:0.101000\n", 18) = 18
1472615715.434153 gettimeofday({1472615715, 434161}, NULL) = 0
1472615715.434181 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.434258 gettimeofday({1472615715, 434266}, NULL) = 0
1472615715.434290 write(1, "id:3 use:0.105000\n", 18) = 18
1472615715.434407 gettimeofday({1472615715, 434414}, NULL) = 0
1472615715.434438 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.534668 gettimeofday({1472615715, 534680}, NULL) = 0
1472615715.534725 write(1, "id:6 use:100.266000\n", 20) = 20
1472615715.534977 gettimeofday({1472615715, 534986}, NULL) = 0
1472615715.535006 gettimeofday({1472615715, 535014}, NULL) = 0
1472615715.535037 write(1, "id:0 use:0.028000\n", 18) = 18
1472615715.535172 gettimeofday({1472615715, 535180}, NULL) = 0
1472615715.535201 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.535342 gettimeofday({1472615715, 535350}, NULL) = 0
1472615715.535373 write(1, "id:1 use:0.170000\n", 18) = 18
1472615715.535507 gettimeofday({1472615715, 535515}, NULL) = 0
1472615715.535536 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.535609 gettimeofday({1472615715, 535617}, NULL) = 0
1472615715.535641 write(1, "id:2 use:0.102000\n", 18) = 18
1472615715.535775 gettimeofday({1472615715, 535783}, NULL) = 0
1472615715.535803 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.535898 gettimeofday({1472615715, 535905}, NULL) = 0
1472615715.535929 write(1, "id:3 use:0.122000\n", 18) = 18
1472615715.536064 gettimeofday({1472615715, 536072}, NULL) = 0
1472615715.536092 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.636301 gettimeofday({1472615715, 636310}, NULL) = 0
1472615715.636352 write(1, "id:6 use:100.238000\n", 20) = 20
1472615715.636589 gettimeofday({1472615715, 636597}, NULL) = 0
1472615715.636618 gettimeofday({1472615715, 636625}, NULL) = 0
1472615715.636648 write(1, "id:0 use:0.028000\n", 18) = 18
1472615715.636777 gettimeofday({1472615715, 636784}, NULL) = 0
1472615715.636805 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.636942 gettimeofday({1472615715, 636949}, NULL) = 0
1472615715.636973 write(1, "id:1 use:0.165000\n", 18) = 18
1472615715.637104 gettimeofday({1472615715, 637111}, NULL) = 0
1472615715.637132 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.637208 gettimeofday({1472615715, 637216}, NULL) = 0
1472615715.637239 write(1, "id:2 use:0.105000\n", 18) = 18
1472615715.637368 gettimeofday({1472615715, 637376}, NULL) = 0
1472615715.637396 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.637466 gettimeofday({1472615715, 637473}, NULL) = 0
1472615715.637497 write(1, "id:3 use:0.097000\n", 18) = 18
1472615715.637669 gettimeofday({1472615715, 637677}, NULL) = 0
1472615715.637697 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.737897 gettimeofday({1472615715, 737906}, NULL) = 0
1472615715.737948 write(1, "id:6 use:100.229000\n", 20) = 20
1472615715.738165 gettimeofday({1472615715, 738173}, NULL) = 0
1472615715.738193 gettimeofday({1472615715, 738200}, NULL) = 0
1472615715.738223 write(1, "id:0 use:0.027000\n", 18) = 18
1472615715.738334 gettimeofday({1472615715, 738342}, NULL) = 0
1472615715.738362 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.738495 gettimeofday({1472615715, 738503}, NULL) = 0
1472615715.738527 write(1, "id:1 use:0.161000\n", 18) = 18
1472615715.738641 gettimeofday({1472615715, 738648}, NULL) = 0
1472615715.738668 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.738760 gettimeofday({1472615715, 738768}, NULL) = 0
1472615715.738792 write(1, "id:2 use:0.120000\n", 18) = 18
1472615715.738904 gettimeofday({1472615715, 738911}, NULL) = 0
1472615715.738932 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.739007 gettimeofday({1472615715, 739014}, NULL) = 0
1472615715.739038 write(1, "id:3 use:0.103000\n", 18) = 18
1472615715.739149 gettimeofday({1472615715, 739157}, NULL) = 0
1472615715.739177 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.839384 gettimeofday({1472615715, 839393}, NULL) = 0
1472615715.839437 write(1, "id:6 use:100.236000\n", 20) = 20
1472615715.839671 gettimeofday({1472615715, 839679}, NULL) = 0
1472615715.839699 gettimeofday({1472615715, 839706}, NULL) = 0
1472615715.839729 write(1, "id:0 use:0.027000\n", 18) = 18
1472615715.839872 gettimeofday({1472615715, 839879}, NULL) = 0
1472615715.839900 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.840036 gettimeofday({1472615715, 840044}, NULL) = 0
1472615715.840068 write(1, "id:1 use:0.165000\n", 18) = 18
1472615715.840199 gettimeofday({1472615715, 840207}, NULL) = 0
1472615715.840227 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.840298 gettimeofday({1472615715, 840305}, NULL) = 0
1472615715.840328 write(1, "id:2 use:0.098000\n", 18) = 18
1472615715.840458 gettimeofday({1472615715, 840465}, NULL) = 0
1472615715.840486 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.840561 gettimeofday({1472615715, 840569}, NULL) = 0
1472615715.840593 write(1, "id:3 use:0.104000\n", 18) = 18
1472615715.840721 gettimeofday({1472615715, 840729}, NULL) = 0
1472615715.840749 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615715.940958 gettimeofday({1472615715, 940968}, NULL) = 0
1472615715.941011 write(1, "id:6 use:100.239000\n", 20) = 20
1472615715.941246 gettimeofday({1472615715, 941254}, NULL) = 0
1472615715.941274 gettimeofday({1472615715, 941282}, NULL) = 0
1472615715.941304 write(1, "id:0 use:0.028000\n", 18) = 18
1472615715.941420 gettimeofday({1472615715, 941428}, NULL) = 0
1472615715.941448 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.941586 gettimeofday({1472615715, 941594}, NULL) = 0
1472615715.941617 write(1, "id:1 use:0.166000\n", 18) = 18
1472615715.941747 gettimeofday({1472615715, 941755}, NULL) = 0
1472615715.941776 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.941852 gettimeofday({1472615715, 941860}, NULL) = 0
1472615715.941883 write(1, "id:2 use:0.105000\n", 18) = 18
1472615715.942018 gettimeofday({1472615715, 942026}, NULL) = 0
1472615715.942047 sendto(4, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"..., 100, 0, NULL, 0) = 100
1472615715.942123 gettimeofday({1472615715, 942131}, NULL) = 0
1472615715.942155 write(1, "id:3 use:0.105000\n", 18) = 18
1472615715.942285 gettimeofday({1472615715, 942292}, NULL) = 0
1472615715.942312 select(0, NULL, NULL, NULL, {0, 100000}) = 0 (Timeout)
1472615716.042522 gettimeofday({1472615716, 42531}, NULL) = 0
1472615716.042594 write(1, "id:6 use:100.239000\n", 20) = 20
1472615716.042879 exit_group(0)         = ?
1472615716.044299 +++ exited with 0 +++



乌龟壳
乌龟壳
回复 @testtcpsend : 哥们,这事我懒得继续深入下去了,哈哈,你继续加油。
t
testtcpsend
知道了, strace -tt xxx
t
testtcpsend
乌龟壳, 能给一下你调用strace命令的参数吗?
t
testtcpsend
感谢乌龟壳!
0
乌龟壳
乌龟壳

这是我整理过的,你那段代码有点乱别人没耐心看

#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include<sys/wait.h>
#include <string.h>
#include <time.h> 
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <stdio.h>
#include  <netinet/in.h>
#include <netinet/tcp.h> // for TCP_NODELAY
#include <sys/time.h>
#include <sys/epoll.h>
#include <time.h>

#define timeit(code, id) \
    {   \
        struct timeval lasttime;    \
        struct timeval now_time;    \
        double timeuse;            \
        gettimeofday(&lasttime,0);  \
        code;    \
        gettimeofday(&now_time,0); \
        timeuse = 1000000.0*(now_time.tv_sec - lasttime.tv_sec) + now_time.tv_usec - lasttime.tv_usec; \
        timeuse /=1000.0; \
        printf("id:%d use:%f\n", id, timeuse); \
    }

typedef unsigned int            uint32_t; 
void xsleep(uint32_t ms)
{ 
    struct timeval tval;
    tval.tv_sec   = ms / 1000;
    tval.tv_usec  = ( ms * 1000) % 1000000;
    select(0, NULL, NULL, NULL, &tval);
}

int accept_client()
{
    int sockfd,new_fd;
    struct sockaddr_in my_addr;
    struct sockaddr_in their_addr;
    unsigned int sin_size, myport; 
    int yes = 1;

    myport = 8090; 

    if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { 
        perror("socket"); 
        exit(1); 
    } 

    setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));

    my_addr.sin_family=PF_INET; 
    my_addr.sin_port=htons(myport); 
    my_addr.sin_addr.s_addr = INADDR_ANY; 
    bzero(&(my_addr.sin_zero), 0); 
    if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)) == -1) { 
        perror("bind"); 
        exit(1); 
    } 
    printf("bind ok \n");

    if (listen(sockfd, 100) == -1) { 
        perror("listen"); 
        exit(1); 
    }
    printf("listen ok \n");

    sin_size = sizeof(struct sockaddr_in); 

    if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) { 
        perror("accept"); 
        exit(1);
    } 

    printf("server: got connection from %s\n",inet_ntoa(their_addr.sin_addr));
    //fcntl(new_fd, F_SETFL, O_NONBLOCK);
    setsockopt(new_fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
    //int nSendBuf=1024*1024*16;
    //setsockopt(new_fd,SOL_SOCKET,SO_SNDBUF,(const char*)&nSendBuf,sizeof(int)); 

    return new_fd;
}

int main(int argc, char ** argv) 
{ 
    int new_fd = accept_client();

    char  szSnd[1024] = {0};
    memset(szSnd,97,1024);
    szSnd[100] = 0;

    for(int iii=0; iii<10; iii++)
    {
        timeit(, 0);

        timeit (
            if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
                perror("send");
            }, 1);


        timeit (
            if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
                perror("send");
            }, 2);

        timeit (
            if (send(new_fd, szSnd, strlen(szSnd), 0) == -1) {
                perror("send");
            }, 3);
            
        timeit(xsleep(1), 6);


        /*timeit(
            for(int ccc=0; ccc<1000000; ccc++) {
                char aaax[10240];
                memset(aaax,0,10240);
            }, 6);*/
    }
}



0
乌龟壳
乌龟壳

这是我在双核主机上测试的结果,看起来和操作系统的线程调度有关

bind ok
listen ok
server: got connection from 127.0.0.1
id:0 use:0.001000
id:1 use:0.034000
id:2 use:0.017000
id:3 use:0.015000
id:6 use:100.123000
id:0 use:0.001000
id:1 use:0.048000
id:2 use:0.034000
id:3 use:0.033000
id:6 use:100.124000
id:0 use:0.000000
id:1 use:0.059000
id:2 use:0.034000
id:3 use:0.032000
id:6 use:100.129000
id:0 use:0.000000
id:1 use:0.078000
id:2 use:0.036000
id:3 use:0.033000
id:6 use:100.133000
id:0 use:0.000000
id:1 use:0.073000
id:2 use:0.033000
id:3 use:0.032000
id:6 use:100.127000
id:0 use:0.000000
id:1 use:0.069000
id:2 use:0.180000
id:3 use:0.029000
id:6 use:100.127000


0
注销_
注销_
你在两次send中间加一个xsleep(100);,你看看是不是两次send时间差不多了?嘻嘻~
返回顶部
顶部