Files
build
debian
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
tests
bookmarks
collate
gobject
markups
refcount
Makefile.am
array-test.c
asyncqueue-test.c
atomic-test.c
base64-test.c
bit-test.c
bookmarkfile-test.c
casefold.txt
casemap.txt
checksum-test.c
child-test.c
completion-test.c
convert-test.c
cxx-test.C
date-test.c
dirname-test.c
env-test.c
errorcheck-mutex-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-ls.c
gio-test.c
hash-test.c
iochannel-test-infile
iochannel-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
list-test.c
mainloop-test.c
makefile.msc.in
mapping-test.c
markup-collect.c
markup-escape-test.c
markup-test.c
memchunks.c
module-test.c
node-test.c
onceinit.c
patterntest.c
printf-test.c
qsort-test.c
queue-test.c
rand-test.c
regex-test.c
relation-test.c
run-bookmark-test.sh
run-collate-tests.sh
run-markup-tests.sh
scannerapi.c
sequence-test.c
shell-test.c
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
slist-test.c
spawn-test-win32-gui.c
spawn-test.c
string-test.c
testgdate.c
testgdateparser.c
testglib.c
testingbase64.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
tree-test.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
uri-test.c
utf8-pointer.c
utf8-validate.c
utf8.txt
.gitignore
AUTHORS
COPYING
ChangeLog
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-2
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL
INSTALL.in
MAINTAINERS
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.in
gio-2.0-uninstalled.pc.in
gio-2.0.pc.in
gio-unix-2.0-uninstalled.pc.in
gio-unix-2.0.pc.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
glibconfig.h.win32.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
glib/tests/timeloop-basic.c
Matthias Clasen 912027f0df Some file list updates (, Owen Taylor)
2007-01-19  Matthias Clasen  <mclasen@redhat.com>

        Some file list updates (, Owen Taylor)

        * docs/Changes-2.0.txt
        * docs/reference/README.cvs-commits
        * glib.spec.in: Remove obsolete files

        * tests/Makefile.am:
        * glib/libcharset/Makefile.am:
        * gobject/Makefile.am:
        * Makefile.am: Add some missing files to EXTRA_DIST

        * tests/timeloop-basic.c: Make it build
        * HACKING: Small updates


svn path=/trunk/; revision=5302
2007-01-19 15:50:30 +00:00

236 lines
4.2 KiB
C

#undef G_DISABLE_ASSERT
#undef G_LOG_DOMAIN
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/poll.h>
#define TRUE 1
#define FALSE 0
static int n_children = 3;
static int n_active_children;
static int n_iters = 10000;
static int write_fds[1024];
static struct pollfd poll_fds[1024];
void
my_pipe (int *fds)
{
if (pipe(fds) < 0)
{
fprintf (stderr, "Cannot create pipe %s\n", strerror (errno));
exit (1);
}
}
int
read_all (int fd, char *buf, int len)
{
size_t bytes_read = 0;
ssize_t count;
while (bytes_read < len)
{
count = read (fd, buf + bytes_read, len - bytes_read);
if (count < 0)
{
if (errno != EAGAIN)
return FALSE;
}
else if (count == 0)
return FALSE;
bytes_read += count;
}
return TRUE;
}
int
write_all (int fd, char *buf, int len)
{
size_t bytes_written = 0;
ssize_t count;
while (bytes_written < len)
{
count = write (fd, buf + bytes_written, len - bytes_written);
if (count < 0)
{
if (errno != EAGAIN)
return FALSE;
}
bytes_written += count;
}
return TRUE;
}
void
run_child (int in_fd, int out_fd)
{
int i;
int val = 1;
for (i = 0; i < n_iters; i++)
{
write_all (out_fd, (char *)&val, sizeof (val));
read_all (in_fd, (char *)&val, sizeof (val));
}
val = 0;
write_all (out_fd, (char *)&val, sizeof (val));
exit (0);
}
int
input_callback (int source, int dest)
{
int val;
if (!read_all (source, (char *)&val, sizeof(val)))
{
fprintf (stderr,"Unexpected EOF\n");
exit (1);
}
if (val)
{
write_all (dest, (char *)&val, sizeof(val));
return TRUE;
}
else
{
close (source);
close (dest);
n_active_children--;
return FALSE;
}
}
void
create_child (int pos)
{
int pid;
int in_fds[2];
int out_fds[2];
my_pipe (in_fds);
my_pipe (out_fds);
pid = fork ();
if (pid > 0) /* Parent */
{
close (in_fds[0]);
close (out_fds[1]);
write_fds[pos] = in_fds[1];
poll_fds[pos].fd = out_fds[0];
poll_fds[pos].events = POLLIN;
}
else if (pid == 0) /* Child */
{
close (in_fds[1]);
close (out_fds[0]);
setsid ();
run_child (in_fds[0], out_fds[1]);
}
else /* Error */
{
fprintf (stderr,"Cannot fork: %s\n", strerror (errno));
exit (1);
}
}
static double
difftimeval (struct timeval *old, struct timeval *new)
{
return
(new->tv_sec - old->tv_sec) * 1000. + (new->tv_usec - old->tv_usec) / 1000;
}
int
main (int argc, char **argv)
{
int i, j;
struct rusage old_usage;
struct rusage new_usage;
if (argc > 1)
n_children = atoi(argv[1]);
if (argc > 2)
n_iters = atoi(argv[2]);
printf ("Children: %d Iters: %d\n", n_children, n_iters);
n_active_children = n_children;
for (i = 0; i < n_children; i++)
create_child (i);
getrusage (RUSAGE_SELF, &old_usage);
while (n_active_children > 0)
{
int old_n_active_children = n_active_children;
poll (poll_fds, n_active_children, -1);
for (i=0; i<n_active_children; i++)
{
if (poll_fds[i].events & (POLLIN | POLLHUP))
{
if (!input_callback (poll_fds[i].fd, write_fds[i]))
write_fds[i] = -1;
}
}
if (old_n_active_children > n_active_children)
{
j = 0;
for (i=0; i<old_n_active_children; i++)
{
if (write_fds[i] != -1)
{
if (j < i)
{
poll_fds[j] = poll_fds[i];
write_fds[j] = write_fds[i];
}
j++;
}
}
}
}
getrusage (RUSAGE_SELF, &new_usage);
printf ("Elapsed user: %g\n",
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime));
printf ("Elapsed system: %g\n",
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
printf ("Elapsed total: %g\n",
difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime));
printf ("total / iteration: %g\n",
(difftimeval (&old_usage.ru_utime, &new_usage.ru_utime) +
difftimeval (&old_usage.ru_stime, &new_usage.ru_stime)) /
(n_iters * n_children));
return 0;
}