os/ossrv/glib/tests/timeloop-basic.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* Portions copyright (c) 2009 Nokia Corporation.  All rights reserved.*/
sl@0
     2
#undef G_DISABLE_ASSERT
sl@0
     3
#undef G_LOG_DOMAIN
sl@0
     4
sl@0
     5
#include <errno.h>
sl@0
     6
#include <stdlib.h>
sl@0
     7
#include <stdio.h>
sl@0
     8
#include <string.h>
sl@0
     9
#include <unistd.h>
sl@0
    10
#include <sys/resource.h>
sl@0
    11
#include <sys/time.h>
sl@0
    12
#include <sys/poll.h>
sl@0
    13
sl@0
    14
#define TRUE 1
sl@0
    15
#define FALSE 0
sl@0
    16
sl@0
    17
static int n_children = 3;
sl@0
    18
static int n_active_children;
sl@0
    19
static int n_iters = 10000;
sl@0
    20
sl@0
    21
static int write_fds[1024];
sl@0
    22
static struct pollfd poll_fds[1024];
sl@0
    23
sl@0
    24
void
sl@0
    25
my_pipe (int *fds)
sl@0
    26
{
sl@0
    27
  if (pipe(fds) < 0)
sl@0
    28
    {
sl@0
    29
      fprintf (stderr, "Cannot create pipe %s\n", strerror (errno));
sl@0
    30
      exit (1);
sl@0
    31
    }
sl@0
    32
}
sl@0
    33
sl@0
    34
int
sl@0
    35
read_all (int fd, char *buf, int len)
sl@0
    36
{
sl@0
    37
  size_t bytes_read = 0;
sl@0
    38
  ssize_t count;
sl@0
    39
sl@0
    40
  while (bytes_read < len)
sl@0
    41
    {
sl@0
    42
      count = read (fd, buf + bytes_read, len - bytes_read);
sl@0
    43
      if (count < 0)
sl@0
    44
	{
sl@0
    45
	  if (errno != EAGAIN)
sl@0
    46
	    return FALSE;
sl@0
    47
	}
sl@0
    48
      else if (count == 0)
sl@0
    49
	return FALSE;
sl@0
    50
sl@0
    51
      bytes_read += count;
sl@0
    52
    }
sl@0
    53
sl@0
    54
  return TRUE;
sl@0
    55
}
sl@0
    56
sl@0
    57
int
sl@0
    58
write_all (int fd, char *buf, int len)
sl@0
    59
{
sl@0
    60
  size_t bytes_written = 0;
sl@0
    61
  ssize_t count;
sl@0
    62
sl@0
    63
  while (bytes_written < len)
sl@0
    64
    {
sl@0
    65
      count = write (fd, buf + bytes_written, len - bytes_written);
sl@0
    66
      if (count < 0)
sl@0
    67
	{
sl@0
    68
	  if (errno != EAGAIN)
sl@0
    69
	    return FALSE;
sl@0
    70
	}
sl@0
    71
sl@0
    72
      bytes_written += count;
sl@0
    73
    }
sl@0
    74
sl@0
    75
  return TRUE;
sl@0
    76
}
sl@0
    77
sl@0
    78
void
sl@0
    79
run_child (int in_fd, int out_fd)
sl@0
    80
{
sl@0
    81
  int i;
sl@0
    82
  int val = 1;
sl@0
    83
sl@0
    84
  for (i = 0; i < n_iters; i++)
sl@0
    85
    {
sl@0
    86
      write_all (out_fd, (char *)&val, sizeof (val));
sl@0
    87
      read_all (in_fd, (char *)&val, sizeof (val));
sl@0
    88
    }
sl@0
    89
sl@0
    90
  val = 0;
sl@0
    91
  write_all (out_fd, (char *)&val, sizeof (val));
sl@0
    92
sl@0
    93
  exit (0);
sl@0
    94
}
sl@0
    95
sl@0
    96
int
sl@0
    97
input_callback (int source, int dest)
sl@0
    98
{
sl@0
    99
  int val;
sl@0
   100
  
sl@0
   101
  if (!read_all (source, (char *)&val, sizeof(val)))
sl@0
   102
    {
sl@0
   103
      fprintf (stderr,"Unexpected EOF\n");
sl@0
   104
      exit (1);
sl@0
   105
    }
sl@0
   106
sl@0
   107
  if (val)
sl@0
   108
    {
sl@0
   109
      write_all (dest, (char *)&val, sizeof(val));
sl@0
   110
      return TRUE;
sl@0
   111
    }
sl@0
   112
  else
sl@0
   113
    {
sl@0
   114
      close (source);
sl@0
   115
      close (dest);
sl@0
   116
      
sl@0
   117
      n_active_children--;
sl@0
   118
      return FALSE;
sl@0
   119
    }
sl@0
   120
}
sl@0
   121
sl@0
   122
void
sl@0
   123
create_child (int pos)
sl@0
   124
{
sl@0
   125
  int pid;
sl@0
   126
  int in_fds[2];
sl@0
   127
  int out_fds[2];
sl@0
   128
  
sl@0
   129
  my_pipe (in_fds);
sl@0
   130
  my_pipe (out_fds);
sl@0
   131
sl@0
   132
  pid = fork ();
sl@0
   133
sl@0
   134
  if (pid > 0)			/* Parent */
sl@0
   135
    {
sl@0
   136
      close (in_fds[0]);
sl@0
   137
      close (out_fds[1]);
sl@0
   138
sl@0
   139
      write_fds[pos] = in_fds[1];
sl@0
   140
      poll_fds[pos].fd = out_fds[0];
sl@0
   141
      poll_fds[pos].events = POLLIN;
sl@0
   142
    }
sl@0
   143
  else if (pid == 0)		/* Child */
sl@0
   144
    {
sl@0
   145
      close (in_fds[1]);
sl@0
   146
      close (out_fds[0]);
sl@0
   147
sl@0
   148
      setsid ();
sl@0
   149
sl@0
   150
      run_child (in_fds[0], out_fds[1]);
sl@0
   151
    }
sl@0
   152
  else				/* Error */
sl@0
   153
    {
sl@0
   154
      fprintf (stderr,"Cannot fork: %s\n", strerror (errno));
sl@0
   155
      exit (1);
sl@0
   156
    }
sl@0
   157
}
sl@0
   158
sl@0
   159
static double 
sl@0
   160
difftimeval (struct timeval *old, struct timeval *new)
sl@0
   161
{
sl@0
   162
  return
sl@0
   163
    (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
sl@0
   164
}
sl@0
   165
sl@0
   166
int 
sl@0
   167
main (int argc, char **argv)
sl@0
   168
{
sl@0
   169
  int i, j;
sl@0
   170
  struct rusage old_usage;
sl@0
   171
  struct rusage new_usage;
sl@0
   172
  
sl@0
   173
  if (argc > 1)
sl@0
   174
    n_children = atoi(argv[1]);
sl@0
   175
sl@0
   176
  if (argc > 2)
sl@0
   177
    n_iters = atoi(argv[2]);
sl@0
   178
sl@0
   179
  printf ("Children: %d     Iters: %d\n", n_children, n_iters);
sl@0
   180
sl@0
   181
  n_active_children = n_children;
sl@0
   182
  for (i = 0; i < n_children; i++)
sl@0
   183
    create_child (i);
sl@0
   184
sl@0
   185
  getrusage (RUSAGE_SELF, &old_usage);
sl@0
   186
sl@0
   187
  while (n_active_children > 0)
sl@0
   188
    {
sl@0
   189
      int old_n_active_children = n_active_children;
sl@0
   190
sl@0
   191
      poll (poll_fds, n_active_children, -1);
sl@0
   192
sl@0
   193
      for (i=0; i<n_active_children; i++)
sl@0
   194
	{
sl@0
   195
	  if (poll_fds[i].events & (POLLIN | POLLHUP))
sl@0
   196
	    {
sl@0
   197
	      if (!input_callback (poll_fds[i].fd, write_fds[i]))
sl@0
   198
		write_fds[i] = -1;
sl@0
   199
	    }
sl@0
   200
	}
sl@0
   201
sl@0
   202
      if (old_n_active_children > n_active_children)
sl@0
   203
	{
sl@0
   204
	  j = 0;
sl@0
   205
	  for (i=0; i<old_n_active_children; i++)
sl@0
   206
	    {
sl@0
   207
	      if (write_fds[i] != -1)
sl@0
   208
		{
sl@0
   209
		  if (j < i)
sl@0
   210
		    {
sl@0
   211
		      poll_fds[j] = poll_fds[i];
sl@0
   212
		      write_fds[j] = write_fds[i];
sl@0
   213
		    }
sl@0
   214
		  j++;
sl@0
   215
		}
sl@0
   216
	    }
sl@0
   217
	}
sl@0
   218
    }
sl@0
   219
sl@0
   220
  getrusage (RUSAGE_SELF, &new_usage);
sl@0
   221
sl@0
   222
  printf ("Elapsed user: %g\n",
sl@0
   223
	   difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
sl@0
   224
  printf ("Elapsed system: %g\n",
sl@0
   225
	   difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
sl@0
   226
  printf ("Elapsed total: %g\n",
sl@0
   227
	  difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
sl@0
   228
	   difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
sl@0
   229
  printf ("total / iteration: %g\n",
sl@0
   230
	   (difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
sl@0
   231
	    difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
sl@0
   232
	   (n_iters * n_children));
sl@0
   233
sl@0
   234
  return 0;
sl@0
   235
}
sl@0
   236