sl@0
|
1 |
/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). 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 <unistd.h>
|
sl@0
|
8 |
#include <stdio.h>
|
sl@0
|
9 |
#include <sys/time.h>
|
sl@0
|
10 |
#include <sys/resource.h>
|
sl@0
|
11 |
|
sl@0
|
12 |
#include <glib.h>
|
sl@0
|
13 |
|
sl@0
|
14 |
#ifdef __SYMBIAN32__
|
sl@0
|
15 |
#include <glib_global.h>
|
sl@0
|
16 |
#include "mrt2_glib2_test.h"
|
sl@0
|
17 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
18 |
|
sl@0
|
19 |
|
sl@0
|
20 |
static int n_children = 3;
|
sl@0
|
21 |
static int n_active_children;
|
sl@0
|
22 |
static int n_iters = 10000;
|
sl@0
|
23 |
static GMainLoop *loop;
|
sl@0
|
24 |
|
sl@0
|
25 |
static void
|
sl@0
|
26 |
io_pipe (GIOChannel **channels)
|
sl@0
|
27 |
{
|
sl@0
|
28 |
int fds[2];
|
sl@0
|
29 |
|
sl@0
|
30 |
if (pipe(fds) < 0)
|
sl@0
|
31 |
{
|
sl@0
|
32 |
g_print ("Cannot create pipe %s\n", g_strerror (errno));
|
sl@0
|
33 |
g_assert(FALSE && "timeloop failed");
|
sl@0
|
34 |
#ifdef __SYMBIAN32__
|
sl@0
|
35 |
testResultXml("timeloop");
|
sl@0
|
36 |
#endif /* EMULATOR */
|
sl@0
|
37 |
exit (1);
|
sl@0
|
38 |
}
|
sl@0
|
39 |
|
sl@0
|
40 |
channels[0] = g_io_channel_unix_new (fds[0]);
|
sl@0
|
41 |
channels[1] = g_io_channel_unix_new (fds[1]);
|
sl@0
|
42 |
}
|
sl@0
|
43 |
|
sl@0
|
44 |
static gboolean
|
sl@0
|
45 |
read_all (GIOChannel *channel, char *buf, int len)
|
sl@0
|
46 |
{
|
sl@0
|
47 |
gsize bytes_read = 0;
|
sl@0
|
48 |
gsize count;
|
sl@0
|
49 |
GIOError err;
|
sl@0
|
50 |
|
sl@0
|
51 |
while (bytes_read < len)
|
sl@0
|
52 |
{
|
sl@0
|
53 |
err = g_io_channel_read (channel, buf + bytes_read, len - bytes_read, &count);
|
sl@0
|
54 |
if (err)
|
sl@0
|
55 |
{
|
sl@0
|
56 |
if (err != G_IO_ERROR_AGAIN)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
g_assert(FALSE && "timeloop failed");
|
sl@0
|
59 |
return FALSE;
|
sl@0
|
60 |
}
|
sl@0
|
61 |
}
|
sl@0
|
62 |
else if (count == 0)
|
sl@0
|
63 |
return FALSE;
|
sl@0
|
64 |
|
sl@0
|
65 |
bytes_read += count;
|
sl@0
|
66 |
}
|
sl@0
|
67 |
|
sl@0
|
68 |
return TRUE;
|
sl@0
|
69 |
}
|
sl@0
|
70 |
|
sl@0
|
71 |
static gboolean
|
sl@0
|
72 |
write_all (GIOChannel *channel, char *buf, int len)
|
sl@0
|
73 |
{
|
sl@0
|
74 |
gsize bytes_written = 0;
|
sl@0
|
75 |
gsize count;
|
sl@0
|
76 |
GIOError err;
|
sl@0
|
77 |
|
sl@0
|
78 |
while (bytes_written < len)
|
sl@0
|
79 |
{
|
sl@0
|
80 |
err = g_io_channel_write (channel, buf + bytes_written, len - bytes_written, &count);
|
sl@0
|
81 |
if (err && err != G_IO_ERROR_AGAIN)
|
sl@0
|
82 |
return FALSE;
|
sl@0
|
83 |
|
sl@0
|
84 |
bytes_written += count;
|
sl@0
|
85 |
}
|
sl@0
|
86 |
|
sl@0
|
87 |
return TRUE;
|
sl@0
|
88 |
}
|
sl@0
|
89 |
|
sl@0
|
90 |
#ifndef __SYMBIAN32__
|
sl@0
|
91 |
static void
|
sl@0
|
92 |
run_child (GIOChannel *in_channel, GIOChannel *out_channel)
|
sl@0
|
93 |
#else
|
sl@0
|
94 |
gpointer
|
sl@0
|
95 |
run_child (gpointer data)
|
sl@0
|
96 |
#endif//__SYMBIAN32__
|
sl@0
|
97 |
{
|
sl@0
|
98 |
int i;
|
sl@0
|
99 |
int val = 1;
|
sl@0
|
100 |
#ifdef __SYMBIAN32__
|
sl@0
|
101 |
GIOChannel *in_channel,*out_channel;
|
sl@0
|
102 |
#endif//__SYMBIAN32__
|
sl@0
|
103 |
GTimer *timer = g_timer_new();
|
sl@0
|
104 |
#ifdef __SYMBIAN32__
|
sl@0
|
105 |
GIOChannel **channels = data;
|
sl@0
|
106 |
in_channel = channels[0];
|
sl@0
|
107 |
out_channel = channels[1];
|
sl@0
|
108 |
#endif//__SYMBIAN32__
|
sl@0
|
109 |
for (i = 0; i < n_iters; i++)
|
sl@0
|
110 |
{
|
sl@0
|
111 |
write_all (out_channel, (char *)&val, sizeof (val));
|
sl@0
|
112 |
read_all (in_channel, (char *)&val, sizeof (val));
|
sl@0
|
113 |
}
|
sl@0
|
114 |
|
sl@0
|
115 |
val = 0;
|
sl@0
|
116 |
write_all (out_channel, (char *)&val, sizeof (val));
|
sl@0
|
117 |
|
sl@0
|
118 |
val = g_timer_elapsed (timer, NULL) * 1000;
|
sl@0
|
119 |
|
sl@0
|
120 |
write_all (out_channel, (char *)&val, sizeof (val));
|
sl@0
|
121 |
g_timer_destroy (timer);
|
sl@0
|
122 |
#ifndef __SYMBIAN32__
|
sl@0
|
123 |
exit (0);
|
sl@0
|
124 |
#else
|
sl@0
|
125 |
return NULL;
|
sl@0
|
126 |
#endif//__SYMBIAN32__
|
sl@0
|
127 |
}
|
sl@0
|
128 |
|
sl@0
|
129 |
static gboolean
|
sl@0
|
130 |
input_callback (GIOChannel *source,
|
sl@0
|
131 |
GIOCondition condition,
|
sl@0
|
132 |
gpointer data)
|
sl@0
|
133 |
{
|
sl@0
|
134 |
int val;
|
sl@0
|
135 |
GIOChannel *dest = (GIOChannel *)data;
|
sl@0
|
136 |
|
sl@0
|
137 |
if (!read_all (source, (char *)&val, sizeof(val)))
|
sl@0
|
138 |
{
|
sl@0
|
139 |
g_print("Unexpected EOF\n");
|
sl@0
|
140 |
g_assert(FALSE && "timeloop failed");
|
sl@0
|
141 |
#ifdef __SYMBIAN32__
|
sl@0
|
142 |
testResultXml("timeloop");
|
sl@0
|
143 |
#endif /* EMULATOR */
|
sl@0
|
144 |
exit (1);
|
sl@0
|
145 |
}
|
sl@0
|
146 |
|
sl@0
|
147 |
if (val)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
write_all (dest, (char *)&val, sizeof(val));
|
sl@0
|
150 |
|
sl@0
|
151 |
return TRUE;
|
sl@0
|
152 |
}
|
sl@0
|
153 |
else
|
sl@0
|
154 |
{
|
sl@0
|
155 |
g_io_channel_close (source);
|
sl@0
|
156 |
g_io_channel_close (dest);
|
sl@0
|
157 |
|
sl@0
|
158 |
g_io_channel_unref (source);
|
sl@0
|
159 |
g_io_channel_unref (dest);
|
sl@0
|
160 |
|
sl@0
|
161 |
n_active_children--;
|
sl@0
|
162 |
if (n_active_children == 0)
|
sl@0
|
163 |
g_main_loop_quit (loop);
|
sl@0
|
164 |
|
sl@0
|
165 |
return FALSE;
|
sl@0
|
166 |
}
|
sl@0
|
167 |
}
|
sl@0
|
168 |
|
sl@0
|
169 |
static void
|
sl@0
|
170 |
create_child (void)
|
sl@0
|
171 |
{
|
sl@0
|
172 |
#ifndef __SYMBIAN32__
|
sl@0
|
173 |
int pid;
|
sl@0
|
174 |
#else
|
sl@0
|
175 |
GError *err = NULL;
|
sl@0
|
176 |
#endif//__SYMBIAN32__
|
sl@0
|
177 |
GIOChannel *in_channels[2];
|
sl@0
|
178 |
GIOChannel *out_channels[2];
|
sl@0
|
179 |
#ifdef __SYMBIAN32__
|
sl@0
|
180 |
GIOChannel **sub_channels;
|
sl@0
|
181 |
|
sl@0
|
182 |
sub_channels = g_new (GIOChannel *, 2);
|
sl@0
|
183 |
|
sl@0
|
184 |
io_pipe (in_channels);
|
sl@0
|
185 |
io_pipe (out_channels);
|
sl@0
|
186 |
sub_channels[0] = in_channels[0];
|
sl@0
|
187 |
sub_channels[1] = out_channels[1];
|
sl@0
|
188 |
|
sl@0
|
189 |
g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
|
sl@0
|
190 |
input_callback, in_channels[1]);
|
sl@0
|
191 |
|
sl@0
|
192 |
g_thread_create(run_child,sub_channels,FALSE,&err);
|
sl@0
|
193 |
#else
|
sl@0
|
194 |
pid = fork ();
|
sl@0
|
195 |
|
sl@0
|
196 |
if (pid > 0) /* Parent */
|
sl@0
|
197 |
{
|
sl@0
|
198 |
g_io_channel_close (in_channels[0]);
|
sl@0
|
199 |
g_io_channel_close (out_channels[1]);
|
sl@0
|
200 |
|
sl@0
|
201 |
g_io_add_watch (out_channels[0], G_IO_IN | G_IO_HUP,
|
sl@0
|
202 |
input_callback, in_channels[1]);
|
sl@0
|
203 |
}
|
sl@0
|
204 |
else if (pid == 0) /* Child */
|
sl@0
|
205 |
{
|
sl@0
|
206 |
g_io_channel_close (in_channels[1]);
|
sl@0
|
207 |
g_io_channel_close (out_channels[0]);
|
sl@0
|
208 |
|
sl@0
|
209 |
setsid ();
|
sl@0
|
210 |
|
sl@0
|
211 |
run_child (in_channels[0], out_channels[1]);
|
sl@0
|
212 |
}
|
sl@0
|
213 |
else /* Error */
|
sl@0
|
214 |
{
|
sl@0
|
215 |
fprintf (stderr, "Cannot fork: %s\n", g_strerror (errno));
|
sl@0
|
216 |
exit (1);
|
sl@0
|
217 |
}
|
sl@0
|
218 |
#endif//__SYMBIAN32__
|
sl@0
|
219 |
}
|
sl@0
|
220 |
|
sl@0
|
221 |
static double
|
sl@0
|
222 |
difftimeval (struct timeval *old, struct timeval *new)
|
sl@0
|
223 |
{
|
sl@0
|
224 |
return
|
sl@0
|
225 |
(new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
|
sl@0
|
226 |
}
|
sl@0
|
227 |
|
sl@0
|
228 |
int
|
sl@0
|
229 |
main (int argc, char **argv)
|
sl@0
|
230 |
{
|
sl@0
|
231 |
int i;
|
sl@0
|
232 |
|
sl@0
|
233 |
#ifdef __SYMBIAN32__
|
sl@0
|
234 |
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);
|
sl@0
|
235 |
g_set_print_handler(mrtPrintHandler);
|
sl@0
|
236 |
g_thread_init(NULL);
|
sl@0
|
237 |
#endif /*__SYMBIAN32__*/
|
sl@0
|
238 |
|
sl@0
|
239 |
|
sl@0
|
240 |
if (argc > 1)
|
sl@0
|
241 |
n_children = atoi(argv[1]);
|
sl@0
|
242 |
|
sl@0
|
243 |
if (argc > 2)
|
sl@0
|
244 |
n_iters = atoi(argv[2]);
|
sl@0
|
245 |
|
sl@0
|
246 |
printf ("Children: %d Iters: %d\n", n_children, n_iters);
|
sl@0
|
247 |
|
sl@0
|
248 |
n_active_children = n_children;
|
sl@0
|
249 |
for (i = 0; i < n_children; i++)
|
sl@0
|
250 |
create_child ();
|
sl@0
|
251 |
#ifndef __SYMBIAN32__
|
sl@0
|
252 |
getrusage (RUSAGE_SELF, &old_usage);
|
sl@0
|
253 |
#endif
|
sl@0
|
254 |
loop = g_main_loop_new (NULL, FALSE);
|
sl@0
|
255 |
g_main_loop_run (loop);
|
sl@0
|
256 |
#ifndef __SYMBIAN32__
|
sl@0
|
257 |
getrusage (RUSAGE_SELF, &new_usage);
|
sl@0
|
258 |
|
sl@0
|
259 |
printf ("Elapsed user: %g\n",
|
sl@0
|
260 |
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
|
sl@0
|
261 |
printf ("Elapsed system: %g\n",
|
sl@0
|
262 |
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
|
sl@0
|
263 |
printf ("Elapsed total: %g\n",
|
sl@0
|
264 |
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
|
sl@0
|
265 |
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
|
sl@0
|
266 |
printf ("total / iteration: %g\n",
|
sl@0
|
267 |
(difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
|
sl@0
|
268 |
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
|
sl@0
|
269 |
(n_iters * n_children));
|
sl@0
|
270 |
#endif
|
sl@0
|
271 |
#ifdef __SYMBIAN32__
|
sl@0
|
272 |
testResultXml("timeloop");
|
sl@0
|
273 |
#endif /* EMULATOR */
|
sl@0
|
274 |
|
sl@0
|
275 |
return 0;
|
sl@0
|
276 |
}
|