6 #include <sys/resource.h>
9 #include <glib-object.h>
11 static int n_children = 3;
12 static int n_active_children;
13 static int n_iters = 10000;
14 static GMainLoop *loop;
17 io_pipe (GIOChannel **channels)
23 fprintf (stderr, "Cannot create pipe %s\n", g_strerror (errno));
27 channels[0] = g_io_channel_unix_new (fds[0]);
28 channels[1] = g_io_channel_unix_new (fds[1]);
32 read_all (GIOChannel *channel, char *buf, int len)
38 while (bytes_read < len)
40 err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
43 if (err != G_IO_ERROR_AGAIN)
56 write_all (GIOChannel *channel, char *buf, int len)
58 gsize bytes_written = 0;
62 while (bytes_written < len)
64 err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
65 if (err && err != G_IO_ERROR_AGAIN)
68 bytes_written += count;
75 run_child (GIOChannel *in_channel, GIOChannel *out_channel)
79 GTimer *timer = g_timer_new();
81 for (i = 0; i < n_iters; i++)
83 write_all (out_channel, (char *)&val, sizeof (val));
84 read_all (in_channel, (char *)&val, sizeof (val));
88 write_all (out_channel, (char *)&val, sizeof (val));
90 val = g_timer_elapsed (timer, NULL) * 1000;
92 write_all (out_channel, (char *)&val, sizeof (val));
93 g_timer_destroy (timer);
99 input_callback (GIOChannel *source,
100 GIOCondition condition,
104 GIOChannel *dest = (GIOChannel *)data;
106 if (!read_all (source, (char *)&val, sizeof(val)))
108 fprintf (stderr, "Unexpected EOF\n");
114 write_all (dest, (char *)&val, sizeof(val));
120 g_io_channel_close (source);
121 g_io_channel_close (dest);
123 g_io_channel_unref (source);
124 g_io_channel_unref (dest);
127 if (n_active_children == 0)
128 g_main_loop_quit (loop);
138 GIOChannel *in_channels[2];
139 GIOChannel *out_channels[2];
142 io_pipe (in_channels);
143 io_pipe (out_channels);
147 if (pid > 0) /* Parent */
149 g_io_channel_close (in_channels[0]);
150 g_io_channel_close (out_channels[1]);
152 source = g_io_create_watch (out_channels[0], G_IO_IN | G_IO_HUP);
153 g_source_set_closure (source,
154 g_cclosure_new (G_CALLBACK (input_callback), in_channels[1], NULL));
155 g_source_attach (source, NULL);
157 else if (pid == 0) /* Child */
159 g_io_channel_close (in_channels[1]);
160 g_io_channel_close (out_channels[0]);
164 run_child (in_channels[0], out_channels[1]);
168 fprintf (stderr, "Cannot fork: %s\n", g_strerror (errno));
174 difftimeval (struct timeval *old, struct timeval *new)
177 (new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
181 main (int argc, char **argv)
184 struct rusage old_usage;
185 struct rusage new_usage;
190 n_children = atoi(argv[1]);
193 n_iters = atoi(argv[2]);
195 printf ("Children: %d Iters: %d\n", n_children, n_iters);
197 n_active_children = n_children;
198 for (i = 0; i < n_children; i++)
201 getrusage (RUSAGE_SELF, &old_usage);
202 loop = g_main_loop_new (NULL, FALSE);
203 g_main_loop_run (loop);
204 getrusage (RUSAGE_SELF, &new_usage);
206 printf ("Elapsed user: %g\n",
207 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
208 printf ("Elapsed system: %g\n",
209 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
210 printf ("Elapsed total: %g\n",
211 difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
212 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
213 printf ("total / iteration: %g\n",
214 (difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
215 difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
216 (n_iters * n_children));