os/ossrv/glib/tests/timeloop.c
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/ossrv/glib/tests/timeloop.c	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,276 @@
     1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
     1.5 +#undef G_DISABLE_ASSERT
     1.6 +#undef G_LOG_DOMAIN
     1.7 +
     1.8 +#include <errno.h>
     1.9 +#include <stdlib.h>
    1.10 +#include <unistd.h>
    1.11 +#include <stdio.h>
    1.12 +#include <sys/time.h>
    1.13 +#include <sys/resource.h>
    1.14 +
    1.15 +#include <glib.h>
    1.16 +
    1.17 +#ifdef __SYMBIAN32__
    1.18 +#include <glib_global.h>
    1.19 +#include "mrt2_glib2_test.h"
    1.20 +#endif /*__SYMBIAN32__*/
    1.21 +
    1.22 +
    1.23 +static int n_children = 3;
    1.24 +static int n_active_children;
    1.25 +static int n_iters = 10000;
    1.26 +static GMainLoop *loop;
    1.27 +
    1.28 +static void
    1.29 +io_pipe (GIOChannel **channels)
    1.30 +{
    1.31 +  int fds[2];
    1.32 +
    1.33 +  if (pipe(fds) < 0)
    1.34 +    {
    1.35 +      g_print ("Cannot create pipe %s\n", g_strerror (errno));
    1.36 +      g_assert(FALSE && "timeloop failed");  
    1.37 +#ifdef __SYMBIAN32__
    1.38 +  testResultXml("timeloop");
    1.39 +#endif /* EMULATOR */
    1.40 +      exit (1);
    1.41 +    }
    1.42 +
    1.43 +  channels[0] = g_io_channel_unix_new (fds[0]);
    1.44 +  channels[1] = g_io_channel_unix_new (fds[1]);
    1.45 +}
    1.46 +
    1.47 +static gboolean
    1.48 +read_all (GIOChannel *channel, char *buf, int len)
    1.49 +{
    1.50 +  gsize bytes_read = 0;
    1.51 +  gsize count;
    1.52 +  GIOError err;
    1.53 +
    1.54 +  while (bytes_read < len)
    1.55 +    {
    1.56 +      err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
    1.57 +      if (err)
    1.58 +	{
    1.59 +	  if (err != G_IO_ERROR_AGAIN)
    1.60 +	  {
    1.61 +	  	g_assert(FALSE && "timeloop failed");
    1.62 +	    return FALSE;
    1.63 +	  }
    1.64 +	}
    1.65 +      else if (count == 0)
    1.66 +	return FALSE;
    1.67 +
    1.68 +      bytes_read += count;
    1.69 +    }
    1.70 +
    1.71 +  return TRUE;
    1.72 +}
    1.73 +
    1.74 +static gboolean
    1.75 +write_all (GIOChannel *channel, char *buf, int len)
    1.76 +{
    1.77 +  gsize bytes_written = 0;
    1.78 +  gsize count;
    1.79 +  GIOError err;
    1.80 +
    1.81 +  while (bytes_written < len)
    1.82 +    {
    1.83 +      err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
    1.84 +      if (err && err != G_IO_ERROR_AGAIN)
    1.85 +	return FALSE;
    1.86 +
    1.87 +      bytes_written += count;
    1.88 +    }
    1.89 +
    1.90 +  return TRUE;
    1.91 +}
    1.92 +
    1.93 +#ifndef __SYMBIAN32__
    1.94 +static void
    1.95 +run_child (GIOChannel *in_channel, GIOChannel *out_channel)
    1.96 +#else
    1.97 +gpointer
    1.98 +run_child (gpointer data)
    1.99 +#endif//__SYMBIAN32__
   1.100 +{
   1.101 +  int i;
   1.102 +  int val = 1;
   1.103 +#ifdef __SYMBIAN32__
   1.104 +GIOChannel *in_channel,*out_channel;
   1.105 +#endif//__SYMBIAN32__
   1.106 +  GTimer *timer = g_timer_new();
   1.107 +#ifdef __SYMBIAN32__  
   1.108 +  GIOChannel **channels = data;  
   1.109 +  in_channel = channels[0];
   1.110 +  out_channel = channels[1];
   1.111 +#endif//__SYMBIAN32__
   1.112 +  for (i = 0; i < n_iters; i++)
   1.113 +    {
   1.114 +      write_all (out_channel, (char *)&val, sizeof (val));
   1.115 +      read_all (in_channel, (char *)&val, sizeof (val));
   1.116 +    }
   1.117 +
   1.118 +  val = 0;
   1.119 +  write_all (out_channel, (char *)&val, sizeof (val));
   1.120 +
   1.121 +  val = g_timer_elapsed (timer, NULL) * 1000;
   1.122 +  
   1.123 +  write_all (out_channel, (char *)&val, sizeof (val));
   1.124 +  g_timer_destroy (timer);
   1.125 +#ifndef __SYMBIAN32__ 
   1.126 +  exit (0);
   1.127 +#else
   1.128 +  return NULL;
   1.129 +#endif//__SYMBIAN32__  
   1.130 +}
   1.131 +
   1.132 +static gboolean
   1.133 +input_callback (GIOChannel   *source,
   1.134 +		GIOCondition  condition,
   1.135 +		gpointer      data)
   1.136 +{
   1.137 +  int val;
   1.138 +  GIOChannel *dest = (GIOChannel *)data;
   1.139 +  
   1.140 +  if (!read_all (source, (char *)&val, sizeof(val)))
   1.141 +    {
   1.142 +      g_print("Unexpected EOF\n");
   1.143 +      g_assert(FALSE && "timeloop failed");
   1.144 +#ifdef __SYMBIAN32__
   1.145 +  testResultXml("timeloop");
   1.146 +#endif /* EMULATOR */
   1.147 +      exit (1);
   1.148 +    }
   1.149 +
   1.150 +  if (val)
   1.151 +    {
   1.152 +      write_all (dest, (char *)&val, sizeof(val));
   1.153 +      
   1.154 +      return TRUE;
   1.155 +    }
   1.156 +  else
   1.157 +    {
   1.158 +      g_io_channel_close (source);
   1.159 +      g_io_channel_close (dest);
   1.160 +      
   1.161 +      g_io_channel_unref (source);
   1.162 +      g_io_channel_unref (dest);
   1.163 +
   1.164 +      n_active_children--;
   1.165 +      if (n_active_children == 0)
   1.166 +	g_main_loop_quit (loop);
   1.167 +      
   1.168 +      return FALSE;
   1.169 +    }
   1.170 +}
   1.171 +
   1.172 +static void
   1.173 +create_child (void)
   1.174 +{
   1.175 +#ifndef __SYMBIAN32__
   1.176 +  int pid;
   1.177 +#else  
   1.178 +  GError *err = NULL;
   1.179 +#endif//__SYMBIAN32__    
   1.180 +  GIOChannel *in_channels[2];
   1.181 +  GIOChannel *out_channels[2];
   1.182 +#ifdef __SYMBIAN32__  
   1.183 +  GIOChannel **sub_channels;
   1.184 +  
   1.185 +  sub_channels = g_new (GIOChannel *, 2);
   1.186 +  
   1.187 +  io_pipe (in_channels);
   1.188 +  io_pipe (out_channels);
   1.189 +  sub_channels[0] = in_channels[0];
   1.190 +  sub_channels[1] = out_channels[1];
   1.191 +
   1.192 +  g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
   1.193 +		      input_callback, in_channels[1]);
   1.194 +  
   1.195 +  g_thread_create(run_child,sub_channels,FALSE,&err);
   1.196 +#else
   1.197 +  pid = fork ();
   1.198 +
   1.199 +  if (pid > 0)			/* Parent */
   1.200 +    {
   1.201 +      g_io_channel_close (in_channels[0]);
   1.202 +      g_io_channel_close (out_channels[1]);
   1.203 +
   1.204 +      g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
   1.205 +		      input_callback, in_channels[1]);
   1.206 +    }
   1.207 +  else if (pid == 0)		/* Child */
   1.208 +    {
   1.209 +      g_io_channel_close (in_channels[1]);
   1.210 +      g_io_channel_close (out_channels[0]);
   1.211 +
   1.212 +      setsid ();
   1.213 +
   1.214 +      run_child (in_channels[0], out_channels[1]);
   1.215 +    }
   1.216 +  else				/* Error */
   1.217 +    {
   1.218 +      fprintf (stderr, "Cannot fork: %s\n", g_strerror (errno));
   1.219 +      exit (1);
   1.220 +    }
   1.221 +#endif//__SYMBIAN32__	
   1.222 +}
   1.223 +
   1.224 +static double 
   1.225 +difftimeval (struct timeval *old, struct timeval *new)
   1.226 +{
   1.227 +  return
   1.228 +    (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
   1.229 +}
   1.230 +
   1.231 +int 
   1.232 +main (int argc, char **argv)
   1.233 +{
   1.234 +  int i;
   1.235 +  
   1.236 +  #ifdef __SYMBIAN32__
   1.237 +  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
   1.238 +  g_set_print_handler(mrtPrintHandler);
   1.239 +  g_thread_init(NULL);
   1.240 +  #endif /*__SYMBIAN32__*/
   1.241 +	  
   1.242 +  
   1.243 +  if (argc > 1)
   1.244 +    n_children = atoi(argv[1]);
   1.245 +
   1.246 +  if (argc > 2)
   1.247 +    n_iters = atoi(argv[2]);
   1.248 +
   1.249 +  printf ("Children: %d     Iters: %d\n", n_children, n_iters);
   1.250 +
   1.251 +  n_active_children = n_children;
   1.252 +  for (i = 0; i < n_children; i++)
   1.253 +    create_child ();
   1.254 +#ifndef __SYMBIAN32__
   1.255 +  getrusage (RUSAGE_SELF, &old_usage);
   1.256 +#endif  
   1.257 +  loop = g_main_loop_new (NULL, FALSE);
   1.258 +  g_main_loop_run (loop);
   1.259 +#ifndef __SYMBIAN32__   
   1.260 +  getrusage (RUSAGE_SELF, &new_usage);
   1.261 +
   1.262 +  printf ("Elapsed user: %g\n",
   1.263 +	  difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
   1.264 +  printf ("Elapsed system: %g\n",
   1.265 +	  difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
   1.266 +  printf ("Elapsed total: %g\n",
   1.267 +	  difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
   1.268 +	  difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
   1.269 +  printf ("total / iteration: %g\n",
   1.270 +	  (difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +	   
   1.271 +	   difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
   1.272 +	  (n_iters * n_children));
   1.273 +#endif	  
   1.274 +  #ifdef __SYMBIAN32__
   1.275 +  testResultXml("timeloop");
   1.276 +  #endif /* EMULATOR */
   1.277 +
   1.278 +  return 0;
   1.279 +}