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