win-pvdrivers

view liblfds.6/test/src/test_stack.c @ 1106:2d392ecdd366

Fix race is xenvbd causing 30 second freeze under high load
author James Harper <james.harper@bendigoit.com.au>
date Tue Nov 11 23:08:11 2014 +1100 (2014-11-11)
parents 6300617040e0
children
line source
1 #include "internal.h"
7 /****************************************************************************/
8 void test_stack( void )
9 {
10 unsigned int
11 loop,
12 cpu_count;
14 struct stack_state
15 *ss;
17 thread_state_t
18 *thread_handles;
20 /* TRD : there are 5 tests
22 1. single reader thread per CPU
23 - stack always empty
24 2. single writer thread per CPU
25 - stack always full
26 3. one reader and one writer thread per CPU
27 - stack balanced
28 4. one reader and two writer threads per CPU
29 - stack grows
30 5. two reader and one writer thread per CPU
31 - stack tends to empty
32 */
34 cpu_count = abstraction_cpu_count();
36 printf( "\n"
37 "Stack Test\n"
38 "==========\n" );
40 // TRD : 1. single reader thread per CPU
42 printf( "\n"
43 "1. single reader thread per CPU\n"
44 "===============================\n" );
46 stack_new( &ss, 10000 );
48 thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 1 );
50 for( loop = 0 ; loop < cpu_count ; loop++ )
51 abstraction_thread_start( &thread_handles[loop], loop, stack_internal_thread_reader, ss );
53 for( loop = 0 ; loop < cpu_count ; loop++ )
54 abstraction_thread_wait( thread_handles[loop] );
56 stack_delete( ss, NULL, NULL );
58 free( thread_handles );
60 // TRD : 2. single writer thread per CPU
62 printf( "\n"
63 "2. single writer thread per CPU\n"
64 "===============================\n" );
66 stack_new( &ss, 10000 );
68 thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 1 );
70 for( loop = 0 ; loop < cpu_count ; loop++ )
71 abstraction_thread_start( &thread_handles[loop], loop, stack_internal_thread_writer, ss );
73 for( loop = 0 ; loop < cpu_count ; loop++ )
74 abstraction_thread_wait( thread_handles[loop] );
76 stack_delete( ss, NULL, NULL );
78 free( thread_handles );
80 // TRD : 3. one reader and one writer thread per CPU
82 printf( "\n"
83 "3. one reader and one writer thread per CPU\n"
84 "===========================================\n" );
86 stack_new( &ss, 10000 );
88 thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 2 );
90 for( loop = 0 ; loop < cpu_count ; loop++ )
91 {
92 abstraction_thread_start( &thread_handles[loop], loop, stack_internal_thread_reader, ss );
93 abstraction_thread_start( &thread_handles[loop+cpu_count], loop, stack_internal_thread_writer, ss );
94 }
96 for( loop = 0 ; loop < cpu_count * 2 ; loop++ )
97 abstraction_thread_wait( thread_handles[loop] );
99 stack_delete( ss, NULL, NULL );
101 free( thread_handles );
103 // TRD : 4. one reader and two writer threads per CPU
105 printf( "\n"
106 "4. one reader and two writer threads per CPU\n"
107 "============================================\n" );
109 stack_new( &ss, 10000 );
111 thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 3 );
113 for( loop = 0 ; loop < cpu_count ; loop++ )
114 {
115 abstraction_thread_start( &thread_handles[loop], loop, stack_internal_thread_reader, ss );
116 abstraction_thread_start( &thread_handles[loop+cpu_count], loop, stack_internal_thread_writer, ss );
117 abstraction_thread_start( &thread_handles[loop+cpu_count*2], loop, stack_internal_thread_writer, ss );
118 }
120 for( loop = 0 ; loop < cpu_count * 3 ; loop++ )
121 abstraction_thread_wait( thread_handles[loop] );
123 stack_delete( ss, NULL, NULL );
125 free( thread_handles );
127 // TRD : 5. two reader and one writer thread per CPU
129 printf( "\n"
130 "5. two reader and one writer thread per CPU\n"
131 "===========================================\n" );
133 stack_new( &ss, 10000 );
135 thread_handles = (thread_state_t *) malloc( sizeof(thread_state_t) * cpu_count * 3 );
137 for( loop = 0 ; loop < cpu_count ; loop++ )
138 {
139 abstraction_thread_start( &thread_handles[loop], loop, stack_internal_thread_reader, ss );
140 abstraction_thread_start( &thread_handles[loop+cpu_count], loop, stack_internal_thread_reader, ss );
141 abstraction_thread_start( &thread_handles[loop+cpu_count*2], loop, stack_internal_thread_writer, ss );
142 }
144 for( loop = 0 ; loop < cpu_count * 3 ; loop++ )
145 abstraction_thread_wait( thread_handles[loop] );
147 stack_delete( ss, NULL, NULL );
149 free( thread_handles );
151 return;
152 }
158 /****************************************************************************/
159 thread_return_t CALLING_CONVENTION stack_internal_thread_reader( void *stack_state )
160 {
161 struct stack_state
162 *ss;
164 void
165 *user_data;
167 time_t
168 start_time;
170 unsigned long long int
171 count = 0;
173 assert( stack_state != NULL );
175 ss = (struct stack_state *) stack_state;
177 time( &start_time );
179 while( time(NULL) < start_time + 10 )
180 {
181 if( stack_pop(ss, &user_data) )
182 count++;
183 }
185 printf( "read count = %llu\n", count );
187 return( (thread_return_t) EXIT_SUCCESS );
188 }
194 /****************************************************************************/
195 thread_return_t CALLING_CONVENTION stack_internal_thread_writer( void *stack_state )
196 {
197 struct stack_state
198 *ss;
200 time_t
201 start_time;
203 unsigned long long int
204 count = 0;
206 assert( stack_state != NULL );
208 ss = (struct stack_state *) stack_state;
210 time( &start_time );
212 while( time(NULL) < start_time + 10 )
213 {
214 // TRD : we don't store any user data
215 if( stack_push(ss, NULL) )
216 count++;
217 }
219 printf( "write count = %llu\n", count );
221 return( (thread_return_t) EXIT_SUCCESS );
222 }