Comparison of domain socket communication domain shared memory communication performance

Source: Internet
Author: User
Tags socket error

Recently, I encountered a problem where two processes need to exchange some data in real time. If the data volume is not large, 72 bytes are required. At that time, it was estimated that domain sockets were used for simplicity.

During subsequent performance tests, we suddenly found that when the concurrency of network packets was large, the performance suddenly fell sharply. We found that there were a lot of read and write operations with strace tracking, the code is traced here, and it is found that the domain socket requires constant read and write operations. Although the data security is achieved in order, this operation performance is too low. I just thought, what is the difference between the two? Run two programs to make it clear at a glance. Some codes are readily available on the Internet, with limited levels.


The results should be written before you can't see: the domain socket and the shared memory are no more than an order of magnitude. If the domain socket is walking, the shared memory is similar to the sports car.

The detailed time value is more than 30 times different (tested using perf Stat ).


1. Domain Socket Client:

# Include <stdio. h> # include <stdlib. h> # include <sys/IPC. h> # include <sys/types. h> # include <sys/socket. h> # include <sys/time. h> # include <sys/UN. h> # include <errno. h> # include <string. h> # define dis_msg_file "/tmp /. dis_msg "typedef struct _ key_args {unsigned int key; Union {limit 4_addr; u_int32_t limit 6_addr [4];} sip; Union {limit 4_addr; limit 6_addr [4];} dip; unsigned short sport; unsigned sh ORT dport; unsigned char protocol; unsigned short match_type; unsigned short iptype; // unsigned short mask; int core_id;} key_args_t; typedef struct _ struct {long mtype; unsigned int key; int core_num; int msg_type; key_args_t karg;} dis_msg_buff_t; int main () {int I = 0; int FD = 0; int Len = 0; int ret = 0; char reerrno = 0; struct sockaddr_un ADDR; dis_msg_buff_t MSG; time_t t; t = time (null); FD = socket (af_uni X, sock_stream, 0); If (FD <0) {printf ("client: Socket error \ n"); Return-1;} memset (& ADDR, 0x00, sizeof (struct sockaddr_un); ADDR. sun_family = af_unix; strncpy (ADDR. sun_path, dis_msg_file, strlen (dis_msg_file); Len = sizeof (ADDR. sun_family); # ifdef have_sun_lenlen = ADDR. sun_len = sun_len (& ADDR); # elselen = sizeof (ADDR. sun_family) + strlen (ADDR. sun_path); # endif/* have_sun_len */ret = connect (FD, (Str UCT sockaddr *) & ADDR, Len); If (Ret <0) {printf ("client: connect error \ n"); Return-1 ;} /* ------------------------ 1000000 communications --------------------------- */while (I ++ <) {memset (& MSG, 0x00, sizeof (MSG); If (write (FD, & MSG, sizeof (MSG) <0) {printf ("client: write error \ n"); goto err;} while (1) {int nbytes; reerrno = 0; nbytes = read (FD, & reerrno, sizeof (reerrno); If (nbytes <= 0 & errno! = Eintr) goto err; If (nbytes> 0) {If (reerrno = 1) break; elsegoto err ;}}} /* ------------------------ communication times-end -------------------------- */close (FD); printf ("client: Success % d \ n", time (null)-T); Return 0; err: Close (FD); printf ("client: Failed \ n"); Return-1 ;}


2. Domain socket server:

# Include <stdio. h> # include <stdlib. h> # include <string. h> # include <malloc. h> # include <sys/types. h> # include <errno. h> # include <sys/STAT. h> # include <fcntl. h> # include <sys/select. h> # include <unistd. h> # include <termios. h> # include <sys/STAT. h>/*********** timer header file ****************/# include <sys/time. h> # include <signal. h>/*********** inter-process socket communication header file **********/# include <sys/socket. h> # include <sys/UN. h> # define unix_domain "/tmp /. dis_msg "typedef struct _ key_args {unsigned int key; Union {limit 4_addr; u_int32_t limit 6_addr [4];} sip; Union {limit 4_addr; limit 6_addr [4];} dip; unsigned short sport; unsigned short dport; unsigned char protocol; unsigned short match_type; unsigned short iptype; // unsigned short mask; int core_id;} struct; typedef struct _ struct {long mtype; unsigned int key; int core_num; int msg_type; key_args_t karg;} Comment; void main () {socklen_t clt_addr_len; int listen_fd; int com_fd; int ret = 0; char reerrno = 0; int I; dis_msg_buff_t MSG; int Len; struct sockaddr_un clt_addr; struct sockaddr_un srv_addr; // create a socket for communication. The communication domain is the Unix communication domain listen_fd = socket (af_unix, sock_stream, 0); If (listen_fd <0) {printf ("server: cannot create listening socket \ n"); Return ;}// set the server address parameter srv_addr.sun_family = af_unix; strncpy (srv_addr.sun_path, unix_domain, sizeof (srv_addr.sun_path)-1); unlink (unix_domain); // binds the socket and server address information ret = BIND (listen_fd, (struct sockaddr *) & srv_addr, sizeof (srv_addr); If (ret =-1) {printf ("cannot bind server socket \ n"); close (listen_fd); unlink (unix_domain ); return;} // listen on the socket and determine whether there is a connection request ret = listen (listen_fd, 1); If (ret =-1) {printf ("cannot listen the client CONNECT Request \ n"); close (listen_fd); unlink (unix_domain); return;} chmod (unix_domain, 00777 ); // set the communication File Permission // when a connection request is sent, call the accept function to establish the connection between the server and the client Len = sizeof (clt_addr); com_fd = accept (listen_fd, (struct sockaddr *) & clt_addr, & Len); If (com_fd <0) {printf ("cannot accept client CONNECT request"); close (listen_fd); unlink (unix_domain ); return;}/* ------------------------ times of communication ------------------------- */while (1) {// read and output the connection information sent by the client memset (& MSG, 0x00, sizeof (MSG); int recv_php_num = read (com_fd, & MSG, sizeof (MSG); If (recv_php_num <= 0) {printf ("read error \ n "); return;} reerrno = 1; write (com_fd, & reerrno, sizeof (reerrno); reerrno = 0 ;}}


3. Shared Memory client:

# Include <stdio. h> # include <stdlib. h> # include <sys/SHM. h> # include <sys/IPC. h> # include <errno. h> # include <string. h> # include <time. h> # define my_shm_id 815 struct shared_use_at {int written_by_you; char some_text [72] ;}; int main () {int shmid, RET; time_t t; t = time (null ); shmid = shmget (my_shm_id, sizeof (struct shared_use_at), 0666); If (shmid> 0) printf ("create a shared memory segment % d \ n", shmid ); else if (Shmid = eexist) {printf ("shared memory is exist! \ N "); exit (0);} printf (" shmid = % d, eexist = % d \ n ", shmid, eexist); struct shmid_ds shmds; ret = shmctl (shmid, ipc_stat, & shmds); If (ret = 0) {printf ("size of memory segment is % d \ n", (INT) shmds. shm_segsz); printf ("Number of attches % d \ n", (INT) shmds. shm_nattch);} else {printf ("shmctl () call failed \ n");} int running = 1; char buffer [100] = {0 }; void * shared_memory = (void *) 0; struct shared_use_at * shared_stuff; shared_memory = shmat (shmid, (void *) 0, 0); If (shared_memory = (void *) -1) {printf ("shmat failed \ n"); exit (0);} shared_stuff = (struct shared_use_at *) shared_memory; /* ------------------------ 1000000 calls */while (running ++ <) {While (shared_stuff-> written_by_you = 1) {} memset (shared_stuff-> some_text, 0x00, 72); shared_stuff-> some_text [0] = 'O'; shared_stuff-> written_by_you = 1 ;} /* ------------------------ communications -- end --------------------- */printf ("time = % d \ n", (INT) (Time (null)-t )); shared_stuff-> some_text [0] = 'K'; shared_stuff-> written_by_you = 1; ret = shmdt (shared_memory); If (ret = 0) printf ("shmdt memory \ n"); else printf ("shmdt memory failed \ n"); ret = shmctl (shmid, ipc_rmid, 0); If (ret = 0) printf ("shared memory removed \ n"); elseprintf ("shared memory removed failed \ n"); Return 0 ;}


4. Shared Memory Server:

# Include <stdio. h> # include <stdlib. h> # include <sys/SHM. h> # include <sys/IPC. h> # include <errno. h> # define my_shm_id 815 struct shared_use_at {int written_by_you; char some_text [72] ;}; int main () {printf ("page size = % d \ n ", getpagesize (); int shmid, RET; shmid = shmget (my_shm_id, sizeof (struct shared_use_at), 0666 | ipc_creat); If (shmid> 0) printf ("create a shared memory segment % d \ n", shmid); struct shmid_ds shmds; ret = shmctl (shmid, ipc_stat, & shmds); If (ret = 0) {printf ("size of memory segment is % d \ n", (INT) shmds. shm_segsz); printf ("Number of attches % d \ n", (INT) shmds. shm_nattch);} else {printf ("shmctl () call failed \ n");} int running = 1; int arunning = 1; void * shared_memory = (void *) 0; struct shared_use_at * shared_stuff; shared_memory = shmat (shmid, (void *) 0, 0); If (shared_memory = (void *)-1) {printf ("shmat failed \ n"); exit (0);} shared_stuff = (struct shared_use_at *) shared_memory; shared_stuff-> written_by_you = 0; /* ------------------------ bytes of communication */while (running ++) {If (shared_stuff-> written_by_you) {If (shared_stuff-> some_text [0] = 'O ') arunning ++; If (shared_stuff-> some_text [0] = 'k') break; shared_stuff-> written_by_you = 0 ;}} /* ---------------------- communication times -- end worker */printf ("arunning = % d, running = % d \ n", arunning, running); ret = shmdt (shared_memory ); if (ret = 0) printf ("shmdt memory \ n"); else printf ("shmdt memory failed \ n"); ret = shmctl (shmid, ipc_rmid, 0 ); if (ret = 0) printf ("shared memory removed \ n"); elseprintf ("shared memory removed failed \ n"); Return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.