]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libtest/src/libtest_tests/libtest_tests_queue_unbounded_manyproducer_manyconsumer_enqueuing_with_malloc_and_dequeuing_with_free.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / test_and_benchmark / libtest / src / libtest_tests / libtest_tests_queue_unbounded_manyproducer_manyconsumer_enqueuing_with_malloc_and_dequeuing_with_free.c
1 /***** includes *****/
2 #include "libtest_tests_internal.h"
3
4 /***** structs *****/
5 struct test_per_thread_state
6 {
7   struct lfds710_queue_umm_state
8     *qs;
9 };
10
11 /***** private prototypes *****/
12 static libshared_pal_thread_return_t LIBSHARED_PAL_THREAD_CALLING_CONVENTION thread_enqueuer_with_malloc_and_dequeuer_with_free( void *libtest_threadset_per_thread_state );
13
14
15
16
17
18 /****************************************************************************/
19 void libtest_tests_queue_umm_enqueuing_with_malloc_and_dequeuing_with_free( struct lfds710_list_asu_state *list_of_logical_processors, struct libshared_memory_state *ms, enum lfds710_misc_validity *dvs )
20 {
21   lfds710_pal_uint_t
22     loop = 0,
23     number_logical_processors;
24
25   struct lfds710_list_asu_element
26     *lasue = NULL;
27
28   struct lfds710_queue_umm_element
29     dummy_element;
30
31   struct lfds710_queue_umm_state
32     qs;
33
34   struct lfds710_misc_validation_info
35     vi;
36
37   struct libtest_logical_processor
38     *lp;
39
40   struct libtest_threadset_per_thread_state
41     *pts;
42
43   struct libtest_threadset_state
44     ts;
45
46   struct test_per_thread_state
47     *tpts;
48
49   LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
50   LFDS710_PAL_ASSERT( ms != NULL );
51   LFDS710_PAL_ASSERT( dvs != NULL );
52
53   /* TRD : one thread per logical core
54            each thread loops for ten seconds
55            mallocs and enqueues 1k elements, then dequeues and frees 1k elements
56   */
57
58   *dvs = LFDS710_MISC_VALIDITY_VALID;
59
60   // TRD : allocate
61   lfds710_list_asu_query( list_of_logical_processors, LFDS710_LIST_ASU_QUERY_GET_POTENTIALLY_INACCURATE_COUNT, NULL, (void **) &number_logical_processors );
62   tpts = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct test_per_thread_state) * number_logical_processors, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
63   pts = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct libtest_threadset_per_thread_state) * number_logical_processors, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
64
65   lfds710_queue_umm_init_valid_on_current_logical_core( &qs, &dummy_element, NULL );
66
67   libtest_threadset_init( &ts, NULL );
68
69   while( LFDS710_LIST_ASU_GET_START_AND_THEN_NEXT(*list_of_logical_processors,lasue) )
70   {
71     lp = LFDS710_LIST_ASU_GET_VALUE_FROM_ELEMENT( *lasue );
72     (tpts+loop)->qs = &qs;
73
74     libtest_threadset_add_thread( &ts, &pts[loop], lp, thread_enqueuer_with_malloc_and_dequeuer_with_free, &tpts[loop] );
75     loop++;
76   }
77
78   LFDS710_MISC_BARRIER_STORE;
79
80   lfds710_misc_force_store();
81
82   // TRD : run the test
83   libtest_threadset_run( &ts );
84
85   libtest_threadset_cleanup( &ts );
86
87   // TRD : validate
88   LFDS710_MISC_BARRIER_LOAD;
89
90   vi.min_elements = vi.max_elements = 0;
91
92   lfds710_queue_umm_query( &qs, LFDS710_QUEUE_UMM_QUERY_SINGLETHREADED_VALIDATE, &vi, dvs );
93
94   lfds710_queue_umm_cleanup( &qs, NULL );
95
96   return;
97 }
98
99
100
101
102
103 /****************************************************************************/
104 static libshared_pal_thread_return_t LIBSHARED_PAL_THREAD_CALLING_CONVENTION thread_enqueuer_with_malloc_and_dequeuer_with_free( void *libtest_threadset_per_thread_state )
105 {
106   lfds710_pal_uint_t
107     loop,
108     time_loop = 0;
109
110   struct lfds710_queue_umm_element
111     *qe;
112
113   struct test_per_thread_state
114     *tpts;
115
116   struct libtest_threadset_per_thread_state
117     *pts;
118
119   time_t
120     current_time,
121     start_time;
122
123   LFDS710_MISC_MAKE_VALID_ON_CURRENT_LOGICAL_CORE_INITS_COMPLETED_BEFORE_NOW_ON_ANY_OTHER_LOGICAL_CORE;
124
125   LFDS710_PAL_ASSERT( libtest_threadset_per_thread_state != NULL );
126
127   pts = (struct libtest_threadset_per_thread_state *) libtest_threadset_per_thread_state;
128   tpts = LIBTEST_THREADSET_GET_USER_STATE_FROM_PER_THREAD_STATE( *pts );
129
130   libtest_threadset_thread_ready_and_wait( pts );
131
132   current_time = start_time = time( NULL );
133
134   while( current_time < start_time + TEST_DURATION_IN_SECONDS )
135   {
136     for( loop = 0 ; loop < 1000 ; loop++ )
137     {
138       qe = libtest_misc_aligned_malloc( sizeof(struct lfds710_queue_umm_element), LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
139       lfds710_queue_umm_enqueue( tpts->qs, qe );
140     }
141
142     for( loop = 0 ; loop < 1000 ; loop++ )
143     {
144       lfds710_queue_umm_dequeue( tpts->qs, &qe );
145       libtest_misc_aligned_free( qe );
146     }
147
148     if( time_loop++ == REDUCED_TIME_LOOP_COUNT )
149     {
150       time_loop = 0;
151       time( &current_time );
152     }
153   }
154
155   LFDS710_MISC_BARRIER_STORE;
156
157   lfds710_misc_force_store();
158
159   return LIBSHARED_PAL_THREAD_RETURN_CAST(RETURN_SUCCESS);
160 }
161