Latency measurements with Unix-domain sockets

I used the following two little programs:

============ unixdomain.c ==============
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

int
main (void)
{
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];

socket_fd = socket (PF_UNIX, SOCK_DGRAM, 0);
if (socket_fd < 0)
{
printf (“socket() failed\n”);
return 1;
}

/* start with a clean address structure */
memset (&address, 0, sizeof (struct sockaddr_un));

address.sun_family = AF_UNIX;
snprintf (address.sun_path, FILENAME_MAX, “./demo_socket”);

if (connect (socket_fd,
(struct sockaddr *) &address,
sizeof (struct sockaddr_un)) != 0)
{
printf (“connect() failed\n”);
return 1;
}

while (1)
{
struct timeval tv;

  gettimeofday (&tv, NULL);
  write (socket_fd, &tv, sizeof (tv));
  usleep (100000);
}

return 0;
}

===== unixdomain_server.c ========

#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>
#include <time.h>

int
main (int argc, char **argv)
{
struct sockaddr_un address;
int socket_fd;
socklen_t address_length;
unsigned char buffer[512];
struct timeval now, msg;
int cnt = 0;
long long diff = 0LL;

socket_fd = socket (PF_UNIX, SOCK_DGRAM, 0);
if (socket_fd < 0)
{
printf (“socket() failed\n”);
return 1;
}

/* start with a clean address structure */
memset (&address, 0, sizeof (struct sockaddr_un));

address.sun_family = AF_UNIX;
snprintf (address.sun_path, FILENAME_MAX, “./demo_socket”);
unlink (address.sun_path);

if (bind (socket_fd,
(struct sockaddr *) &address, sizeof (struct sockaddr_un)) != 0)
{
perror (“bind() failed”);
return 1;
}
while (read (socket_fd, &msg, sizeof (msg)) >= sizeof (msg))
{
long long t1, t2;

  t1 = msg.tv_sec * 1000000;
  t1 += msg.tv_usec;

  gettimeofday (&now, NULL);
  t2 = now.tv_sec * 1000000;
  t2 += now.tv_usec;
  diff += (t2 - t1);

  cnt++;
  if ((cnt % atoi (argv[1])) == 0)
{
  fprintf (stderr, "%f\n", (double) diff / (double) atoi (argv[1]));
  diff = 0LL;
}
}

close (socket_fd);
unlink (“./demo_socket”);
return 0;
}

And found no significant difference in peak and average latencies
between them.

The unixdomain_server takes a single command-line argument which tells
it how many samples
to average over before producing a printed result.

So this confirms my earlier assertion that I would be surprised to find
a significant latency difference
between Unix-domain sockets and FIFOs, since the interior kernel
mechanisms are broadly similar.
Basically–some chunk of memory is copied from one place to another,
there’s some housekeeping, and
the system-call interface is traversed a couple of times.


Principal Investigator
Shirleys Bay Radio Astronomy Consortium

On Tue, May 31, 2011 at 03:07, Marcus D. Leech [email protected]
wrote:

So this confirms my earlier assertion that I would be surprised to find
a significant latency difference
between Unix-domain sockets and FIFOs, since the interior kernel
mechanisms are broadly similar.
Basically–some chunk of memory is copied from one place to another,
there’s some housekeeping, and
the system-call interface is traversed a couple of times.

Let me contradict. Maximum latency is much better - less then 80us
instead of 3ms.

Data attached as usual. Tests were run with ‘chrt 80 ./runtest_sh’

PS I test on Core 2 Duo 1.6 GHz with all the GUI stuff running.

On 05/31/2011 10:54 AM, Alexander C. wrote:

Data attached as usual. Tests were run with ‘chrt 80 ./runtest_sh’

PS I test on Core 2 Duo 1.6 GHz with all the GUI stuff running.

Just did both tests on my machine here, Intel Core i5 notebook.

I couldn’t find such a high latency (3ms). Indeed, both are performing
pretty much the same. I would even say that on my box, the pipe
mechanism does even better in terms of jitter. I didn’t calculate mean
and standard deviation but from the plot it looks like the values are
closer together.

Cheers,
Andre

On Tue, May 31, 2011 at 15:24, Andre P.
[email protected] wrote:

to average over before producing a printed result.
instead of 3ms.
and standard deviation but from the plot it looks like the values are
closer together.

So this once again means that there is no single answer. :slight_smile:
We should put together a test which everyone could run and choose.


Regards,
Alexander C…