]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/test_porting_abstraction_layer_get_logical_core_ids.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / test_porting_abstraction_layer_get_logical_core_ids.c
1 /***** includes *****/
2 #include "internal.h"
3
4
5
6
7
8 /****************************************************************************/
9 #if( defined _WIN32 && !defined _KERNEL_MODE && NTDDI_VERSION >= NTDDI_WIN7 )
10
11   /* TRD : _WIN32         indicates 64-bit or 32-bit Windows
12            !_KERNEL_MODE  indicates Windows user-mode
13            NTDDI_VERSION  indicates Windows version
14                             - GetLogicalProcessorInformationEx requires Windows 7
15   */
16
17   #ifdef TEST_PAL_GET_LOGICAL_CORE_IDS
18     #error More than one porting abstraction layer matches current platform in test_porting_abstraction_layer_get_logical_core_ids.c
19   #endif
20
21   #define TEST_PAL_GET_LOGICAL_CORE_IDS
22
23   void test_pal_get_logical_core_ids( struct lfds700_list_asu_state *lasus )
24   {
25     BOOL
26       rv;
27
28     DWORD
29       loop,
30       number_slpie,
31       slpie_length = 0;
32
33     lfds700_pal_uint_t
34       bitmask,
35       logical_processor_number,
36       windows_logical_processor_group_number;
37
38     struct lfds700_misc_prng_state
39       ps;
40
41     struct test_pal_logical_processor
42       *lp;
43
44     SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
45       *slpie = NULL;
46
47     assert( lasus != NULL );
48
49     lfds700_misc_prng_init( &ps );
50
51     lfds700_list_asu_init_valid_on_current_logical_core( lasus, NULL, NULL );
52
53     rv = GetLogicalProcessorInformationEx( RelationGroup, slpie, &slpie_length );
54     slpie = malloc( slpie_length );
55     rv = GetLogicalProcessorInformationEx( RelationGroup, slpie, &slpie_length );
56     number_slpie = slpie_length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX);
57
58     for( loop = 0 ; loop < number_slpie ; loop++ )
59       if( (slpie+loop)->Relationship == RelationGroup )
60         for( windows_logical_processor_group_number = 0 ; windows_logical_processor_group_number < (slpie+loop)->Group.ActiveGroupCount ; windows_logical_processor_group_number++ )
61           for( logical_processor_number = 0 ; logical_processor_number < sizeof(KAFFINITY) * BITS_PER_BYTE ; logical_processor_number++ )
62           {
63             bitmask = (lfds700_pal_uint_t) 1 << logical_processor_number;
64
65             // TRD : if we've found a processor for this group, add it to the list
66             if( (slpie+loop)->Group.GroupInfo[windows_logical_processor_group_number].ActiveProcessorMask & bitmask )
67             {
68               lp = util_aligned_malloc( sizeof(struct test_pal_logical_processor), LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
69
70               lp->logical_processor_number = logical_processor_number;
71               lp->windows_logical_processor_group_number = windows_logical_processor_group_number;
72
73               LFDS700_LIST_ASU_SET_VALUE_IN_ELEMENT( lp->lasue, lp );
74               lfds700_list_asu_insert_at_start( lasus, &lp->lasue, &ps );
75             }
76           }
77
78     free( slpie );
79
80     return;
81   }
82
83 #endif
84
85
86
87
88
89 /****************************************************************************/
90 #if( defined _WIN32 && !defined _KERNEL_MODE && NTDDI_VERSION >= NTDDI_WINXP && NTDDI_VERSION < NTDDI_WIN7 )
91
92   /* TRD : _WIN32         indicates 64-bit or 32-bit Windows
93            !_KERNEL_MODE  indicates Windows user-mode
94            NTDDI_VERSION  indicates Windows version
95                             - GetLogicalProcessorInformation requires XP SP3
96   */
97
98   #ifdef TEST_PAL_GET_LOGICAL_CORE_IDS
99     #error More than one porting abstraction layer matches current platform in test_porting_abstraction_layer_get_logical_core_ids.c
100   #endif
101
102   #define TEST_PAL_GET_LOGICAL_CORE_IDS
103
104   void test_pal_get_logical_core_ids( struct lfds700_list_asu_state *lasus )
105   {
106     DWORD
107       slpi_length = 0;
108
109     lfds700_pal_uint_t
110       number_slpi,
111       loop;
112
113     struct lfds700_misc_prng_state
114       ps;
115
116     struct test_pal_logical_processor
117       *lp;
118
119     SYSTEM_LOGICAL_PROCESSOR_INFORMATION
120       *slpi = NULL;
121
122     ULONG_PTR
123       mask;
124
125     assert( lasus != NULL );
126
127     lfds700_misc_prng_init( &ps );
128
129     lfds700_list_asu_init_valid_on_current_logical_core( lasus, NULL, NULL );
130
131     *number_logical_processors = 0;
132
133     GetLogicalProcessorInformation( slpi, &slpi_length );
134     slpi = malloc( slpi_length );
135     GetLogicalProcessorInformation( slpi, &slpi_length );
136     number_slpi = slpi_length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
137
138     for( loop = 0 ; loop < number_slpi ; loop++ )
139       if( (slpi+loop)->Relationship == RelationProcessorCore )
140         for( logical_processor_number = 0 ; logical_processor_number < sizeof(ULONG_PTR) * BITS_PER_BYTE ; logical_processor_number++ )
141         {
142           bitmask = 1 << logical_processor_number;
143
144           if( (slpi+loop)->ProcessorMask & bitmask )
145           {
146             lp = util_aligned_malloc( sizeof(struct test_pal_logical_processor), LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
147
148             lp->logical_processor_number = logical_processor_number;
149             lp->windows_logical_processor_group_number = 0;
150
151             LFDS700_LIST_ASU_SET_VALUE_IN_ELEMENT( lp->lasue, lp );
152             lfds700_list_asu_insert_at_start( lasus, &lp->lasue, &ps );
153           }
154
155     free( slpi );
156
157     return;
158   }
159
160 #endif
161
162
163
164
165
166 /****************************************************************************/
167 #if( defined __linux__ )
168
169   /* TRD : __linux__        indicates Linux
170            __STDC__         indicates Standard Library
171            __STDC_HOSTED__  indicates Standard Library hosted implementation
172                               - fopen requires a Standard Library hosted environment
173                               - setbuf requires a Standard Library hosted environment
174                               - fgets requires a Standard Library hosted environment
175                               - sscanf requires a Standard Library hosted environment
176                               - fclose requires a Standard Library hosted environment
177   */
178
179   #ifdef TEST_PAL_GET_LOGICAL_CORE_IDS
180     #error More than one porting abstraction layer matches current platform in test_porting_abstraction_layer_get_logical_core_ids.c
181   #endif
182
183   #define TEST_PAL_GET_LOGICAL_CORE_IDS
184
185   void test_pal_get_logical_core_ids( struct lfds700_list_asu_state *lasus )
186   {
187     char
188       diskbuffer[BUFSIZ],
189       string[1024];
190
191     FILE
192       *diskfile;
193
194     int long long unsigned
195       logical_processor_number;
196
197     struct lfds700_misc_prng_state
198       ps;
199
200     struct test_pal_logical_processor
201       *lp;
202
203     assert( lasus != NULL );
204
205     lfds700_misc_prng_init( &ps );
206
207     lfds700_list_asu_init_valid_on_current_logical_core( lasus, NULL, NULL );
208
209     diskfile = fopen( "/proc/cpuinfo", "r" );
210
211     if( diskfile != NULL )
212     {
213       setbuf( diskfile, diskbuffer );
214
215       while( NULL != fgets(string, 1024, diskfile) )
216         if( 1 == sscanf(string, "processor : %llu", &logical_processor_number) )
217         {
218           lp = util_aligned_malloc( sizeof(struct test_pal_logical_processor), LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
219
220           lp->logical_processor_number = (lfds700_pal_uint_t) logical_processor_number;
221           lp->windows_logical_processor_group_number = 0;
222
223           LFDS700_LIST_ASU_SET_VALUE_IN_ELEMENT( lp->lasue, lp );
224           lfds700_list_asu_insert_at_start( lasus, &lp->lasue, &ps );
225         }
226
227       fclose( diskfile );
228     }
229
230     return;
231   }
232
233 #endif
234
235
236
237
238
239 /****************************************************************************/
240 #if( !defined TEST_PAL_GET_LOGICAL_CORE_IDS )
241
242   #error test_pal_get_logical_core_ids() not implemented for this platform.
243
244 #endif
245