1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
/*
** GNU Pth - The GNU Portable Threads
** Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
**
** This file is part of GNU Pth, a non-preemptive thread scheduling
** library which can be found at http://www.gnu.org/software/pth/.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; either
** version 2.1 of the License, or (at your option) any later version.
**
** This library is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with this library; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
** USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
**
** test_uctx.c: Pth test program (user-space context switching)
*/
/* ``Engineering does not require science.
Science helps a lot, but people built
perfectly good brick walls long before
they knew why cement works.''
-- Alan Cox */
#include <stdio.h>
#include <time.h>
#include "pth.h"
volatile pth_uctx_t uctx[10];
/*
* Test 1: master and worker "threads"
*/
volatile int worker_done[10];
static void worker(void *ctx)
{
volatile int n = (int)ctx;
volatile int i = 0;
fprintf(stderr, "worker #%d: enter\n", n);
for (i = 0; i < 100; i++) {
fprintf(stderr, "worker #%d: working (step %d)\n", n, i);
pth_uctx_switch(uctx[n], uctx[0]);
}
worker_done[n] = TRUE;
fprintf(stderr, "worker #%d: exit\n", n);
return;
}
static void test_working(void)
{
volatile int i;
volatile int todo;
fprintf(stderr, "master: startup\n");
fprintf(stderr, "master: create contexts\n");
pth_uctx_create((pth_uctx_t *)&uctx[0]);
worker_done[0] = FALSE;
for (i = 1; i < 10; i++) {
worker_done[i] = FALSE;
pth_uctx_create((pth_uctx_t *)&uctx[i]);
pth_uctx_make(uctx[i], NULL, 32*1024, NULL, worker, (void *)i, uctx[0]);
}
do {
todo = 0;
for (i = 1; i < 10; i++) {
if (!worker_done[i]) {
fprintf(stderr, "master: switching to worker #%d\n", i);
pth_uctx_switch(uctx[0], uctx[i]);
fprintf(stderr, "master: came back from worker #%d\n", i);
todo = 1;
}
}
} while (todo);
fprintf(stderr, "master: destroy contexts\n");
for (i = 1; i < 10; i++)
pth_uctx_destroy(uctx[i]);
pth_uctx_destroy(uctx[0]);
fprintf(stderr, "master: exit\n");
return;
}
/*
* Test 2: raw switching performance
*/
#define DO_SWITCHES 10000000
time_t stat_start;
time_t stat_end;
volatile int stat_switched;
static void dummy(void *ctx)
{
while (1) {
stat_switched++;
pth_uctx_switch(uctx[1], uctx[0]);
}
return;
}
static void test_performance(void)
{
volatile int i;
pth_uctx_create((pth_uctx_t *)&uctx[0]);
pth_uctx_create((pth_uctx_t *)&uctx[1]);
pth_uctx_make(uctx[1], NULL, 32*1024, NULL, dummy, NULL, uctx[0]);
fprintf(stderr, "\n");
fprintf(stderr, "Performing %d user-space context switches... "
"be patient!\n", DO_SWITCHES);
stat_start = time(NULL);
stat_switched = 0;
for (i = 0; i < DO_SWITCHES; i++) {
stat_switched++;
pth_uctx_switch(uctx[0], uctx[1]);
}
stat_end = time(NULL);
pth_uctx_destroy(uctx[0]);
pth_uctx_destroy(uctx[1]);
fprintf(stderr, "We required %d seconds for performing the test, "
"so this means we can\n", (int)(stat_end-stat_start));
fprintf(stderr, "perform %d user-space context switches per second "
"on this platform.\n", DO_SWITCHES/(int)(stat_end-stat_start));
fprintf(stderr, "\n");
return;
}
int main(int argc, char *argv[])
{
test_working();
test_performance();
return 0;
}
|