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