Update contrib.
     1 /* Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.*/
 
     2 #undef G_DISABLE_ASSERT
 
    10 #include <sys/resource.h>
 
    17 static int n_children = 3;
 
    18 static int n_active_children;
 
    19 static int n_iters = 10000;
 
    21 static int write_fds[1024];
 
    22 static struct pollfd poll_fds[1024];
 
    29       fprintf (stderr, "Cannot create pipe %s\n", strerror (errno));
 
    35 read_all (int fd, char *buf, int len)
 
    37   size_t bytes_read = 0;
 
    40   while (bytes_read < len)
 
    42       count = read (fd, buf + bytes_read, len - bytes_read);
 
    58 write_all (int fd, char *buf, int len)
 
    60   size_t bytes_written = 0;
 
    63   while (bytes_written < len)
 
    65       count = write (fd, buf + bytes_written, len - bytes_written);
 
    72       bytes_written += count;
 
    79 run_child (int in_fd, int out_fd)
 
    84   for (i = 0; i < n_iters; i++)
 
    86       write_all (out_fd, (char *)&val, sizeof (val));
 
    87       read_all (in_fd, (char *)&val, sizeof (val));
 
    91   write_all (out_fd, (char *)&val, sizeof (val));
 
    97 input_callback (int source, int dest)
 
   101   if (!read_all (source, (char *)&val, sizeof(val)))
 
   103       fprintf (stderr,"Unexpected EOF\n");
 
   109       write_all (dest, (char *)&val, sizeof(val));
 
   123 create_child (int pos)
 
   134   if (pid > 0)			/* Parent */
 
   139       write_fds[pos] = in_fds[1];
 
   140       poll_fds[pos].fd = out_fds[0];
 
   141       poll_fds[pos].events = POLLIN;
 
   143   else if (pid == 0)		/* Child */
 
   150       run_child (in_fds[0], out_fds[1]);
 
   154       fprintf (stderr,"Cannot fork: %s\n", strerror (errno));
 
   160 difftimeval (struct timeval *old, struct timeval *new)
 
   163     (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
 
   167 main (int argc, char **argv)
 
   170   struct rusage old_usage;
 
   171   struct rusage new_usage;
 
   174     n_children = atoi(argv[1]);
 
   177     n_iters = atoi(argv[2]);
 
   179   printf ("Children: %d     Iters: %d\n", n_children, n_iters);
 
   181   n_active_children = n_children;
 
   182   for (i = 0; i < n_children; i++)
 
   185   getrusage (RUSAGE_SELF, &old_usage);
 
   187   while (n_active_children > 0)
 
   189       int old_n_active_children = n_active_children;
 
   191       poll (poll_fds, n_active_children, -1);
 
   193       for (i=0; i<n_active_children; i++)
 
   195 	  if (poll_fds[i].events & (POLLIN | POLLHUP))
 
   197 	      if (!input_callback (poll_fds[i].fd, write_fds[i]))
 
   202       if (old_n_active_children > n_active_children)
 
   205 	  for (i=0; i<old_n_active_children; i++)
 
   207 	      if (write_fds[i] != -1)
 
   211 		      poll_fds[j] = poll_fds[i];
 
   212 		      write_fds[j] = write_fds[i];
 
   220   getrusage (RUSAGE_SELF, &new_usage);
 
   222   printf ("Elapsed user: %g\n",
 
   223 	   difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
 
   224   printf ("Elapsed system: %g\n",
 
   225 	   difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
 
   226   printf ("Elapsed total: %g\n",
 
   227 	  difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
 
   228 	   difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
 
   229   printf ("total / iteration: %g\n",
 
   230 	   (difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
 
   231 	    difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
 
   232 	   (n_iters * n_children));