]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libtest/src/libtest_porting_abstraction_layer/libtest_porting_abstraction_layer_get_full_logical_processor_set.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / test_and_benchmark / libtest / src / libtest_porting_abstraction_layer / libtest_porting_abstraction_layer_get_full_logical_processor_set.c
1 /***** includes *****/
2 #include "libtest_porting_abstraction_layer_internal.h"
3
4
5
6
7
8 /****************************************************************************/
9 #if( defined _WIN32 && !defined KERNEL_MODE && NTDDI_VERSION >= NTDDI_WIN7 )
10
11   #ifdef LIBTEST_PAL_GET_LOGICAL_CORE_IDS
12     #error More than one porting abstraction layer matches current platform in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
13   #endif
14
15   #define LIBTEST_PAL_GET_LOGICAL_CORE_IDS
16
17   void libtest_pal_get_full_logical_processor_set( struct lfds710_list_asu_state *list_of_logical_processors,
18                                                    struct libshared_memory_state *ms )
19   {
20     BOOL
21       rv;
22
23     DWORD
24       offset = 0,
25       slpie_length = 0;
26
27     lfds710_pal_uint_t
28       bitmask,
29       logical_processor_number,
30       windows_processor_group_number;
31
32     SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
33       *slpie,
34       *slpie_buffer = NULL;
35
36     LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
37     LFDS710_PAL_ASSERT( ms != NULL );
38
39     lfds710_list_asu_init_valid_on_current_logical_core( list_of_logical_processors, NULL );
40
41     rv = GetLogicalProcessorInformationEx( RelationGroup, slpie_buffer, &slpie_length );
42     slpie_buffer = libshared_memory_alloc_from_most_free_space_node( ms, slpie_length, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
43     rv = GetLogicalProcessorInformationEx( RelationGroup, slpie_buffer, &slpie_length );
44
45     while( offset < slpie_length )
46     {
47       slpie = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *) ( (char unsigned *) slpie_buffer + offset );
48
49       offset += slpie->Size;
50
51       if( slpie->Relationship == RelationGroup )
52         for( windows_processor_group_number = 0 ; windows_processor_group_number < slpie->Group.ActiveGroupCount ; windows_processor_group_number++ )
53           for( logical_processor_number = 0 ; logical_processor_number < sizeof(KAFFINITY) * BITS_PER_BYTE ; logical_processor_number++ )
54           {
55             bitmask = (lfds710_pal_uint_t) 1 << logical_processor_number;
56
57             // TRD : if we've found a processor for this group, add it to the list
58             if( slpie->Group.GroupInfo[windows_processor_group_number].ActiveProcessorMask & bitmask )
59               libtest_misc_pal_helper_add_logical_processor_to_list_of_logical_processors( list_of_logical_processors, ms, logical_processor_number, windows_processor_group_number );
60           }
61     }
62
63     return;
64   }
65
66 #endif
67
68
69
70
71
72 /****************************************************************************/
73 #if( defined _WIN32 && !defined KERNEL_MODE && NTDDI_VERSION >= NTDDI_WINXP && NTDDI_VERSION < NTDDI_WIN7 )
74
75   #ifdef LIBTEST_PAL_GET_LOGICAL_CORE_IDS
76     #error More than one porting abstraction layer matches current platform in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
77   #endif
78
79   #define LIBTEST_PAL_GET_LOGICAL_CORE_IDS
80
81   void libtest_pal_get_full_logical_processor_set( struct lfds710_list_asu_state *list_of_logical_processors,
82                                                    struct libshared_memory_state *ms )
83   {
84     DWORD
85       slpi_length = 0;
86
87     lfds710_pal_uint_t
88       number_slpi,
89       loop;
90
91     SYSTEM_LOGICAL_PROCESSOR_INFORMATION
92       *slpi = NULL;
93
94     ULONG_PTR
95       mask;
96
97     LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
98     LFDS710_PAL_ASSERT( ms != NULL );
99
100     lfds710_list_asu_init_valid_on_current_logical_core( list_of_logical_processors, NULL, NULL );
101
102     *number_logical_processors = 0;
103
104     GetLogicalProcessorInformation( slpi, &slpi_length );
105     slpi = libshared_memory_alloc_from_most_free_space_node( ms, slpi_length, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
106     GetLogicalProcessorInformation( slpi, &slpi_length );
107     number_slpi = slpi_length / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
108
109     for( loop = 0 ; loop < number_slpi ; loop++ )
110       if( (slpi+loop)->Relationship == RelationProcessorCore )
111         for( logical_processor_number = 0 ; logical_processor_number < sizeof(ULONG_PTR) * BITS_PER_BYTE ; logical_processor_number++ )
112         {
113           bitmask = 1 << logical_processor_number;
114
115           if( (slpi+loop)->ProcessorMask & bitmask )
116             libtest_misc_pal_helper_add_logical_processor_to_list_of_logical_processors( list_of_logical_processors, ms, logical_processor_number, windows_processor_group_number );
117
118     return;
119   }
120
121 #endif
122
123
124
125
126
127 /****************************************************************************/
128 #if( defined _WIN32 && defined KERNEL_MODE && NTDDI_VERSION >= NTDDI_WIN7 )
129
130   #ifdef LIBTEST_PAL_GET_LOGICAL_CORE_IDS
131     #error More than one porting abstraction layer matches current platform in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
132   #endif
133
134   #define LIBTEST_PAL_GET_LOGICAL_CORE_IDS
135
136   void libtest_pal_get_full_logical_processor_set( struct lfds710_list_asu_state *list_of_logical_processors,
137                                                    struct libshared_memory_state *ms )
138   {
139     lfds710_pal_uint_t
140       bitmask,
141       logical_processor_number,
142       windows_processor_group_number;
143
144     NTSTATUS
145       rv;
146
147     SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX
148       *slpie,
149       *slpie_buffer = NULL;
150
151     ULONG
152       offset = 0,
153       slpie_length = 0;
154
155     LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
156     LFDS710_PAL_ASSERT( ms != NULL );
157
158     lfds710_list_asu_init_valid_on_current_logical_core( list_of_logical_processors, NULL );
159
160     rv = KeQueryLogicalProcessorRelationship( NULL, RelationGroup, slpie_buffer, &slpie_length );
161     slpie_buffer = libshared_memory_alloc_from_most_free_space_node( ms, slpie_length, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
162     rv = KeQueryLogicalProcessorRelationship( NULL, RelationGroup, slpie_buffer, &slpie_length );
163
164     while( offset < slpie_length )
165     {
166       slpie = (SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *) ( (char unsigned *) slpie_buffer + offset );
167
168       offset += slpie->Size;
169
170       if( slpie->Relationship == RelationGroup )
171         for( windows_processor_group_number = 0 ; windows_processor_group_number < slpie->Group.ActiveGroupCount ; windows_processor_group_number++ )
172           for( logical_processor_number = 0 ; logical_processor_number < sizeof(KAFFINITY) * BITS_PER_BYTE ; logical_processor_number++ )
173           {
174             bitmask = (lfds710_pal_uint_t) 1 << logical_processor_number;
175
176             // TRD : if we've found a processor for this group, add it to the list
177             if( slpie->Group.GroupInfo[windows_processor_group_number].ActiveProcessorMask & bitmask )
178               libtest_misc_pal_helper_add_logical_processor_to_list_of_logical_processors( list_of_logical_processors, ms, logical_processor_number, windows_processor_group_number );
179           }
180     }
181
182     return;
183   }
184
185 #endif
186
187
188
189
190
191 /****************************************************************************/
192 #if( defined _WIN32 && defined KERNEL_MODE && NTDDI_VERSION >= NTDDI_WINXP && NTDDI_VERSION < NTDDI_WIN7 )
193
194   #ifdef LIBTEST_PAL_GET_LOGICAL_CORE_IDS
195     #error More than one porting abstraction layer matches current platform in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
196   #endif
197
198   #define LIBTEST_PAL_GET_LOGICAL_CORE_IDS
199
200   void libtest_pal_get_full_logical_processor_set( struct lfds710_list_asu_state *list_of_logical_processors,
201                                                    struct libshared_memory_state *ms )
202   {
203     CCHAR
204       loop;
205
206     LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
207     LFDS710_PAL_ASSERT( ms != NULL );
208
209     /* TRD : in XP, KeNumberProcessors is a CCHAR indicating the number of processors
210              the docs say nothing about whether the actual logical processor numbers are contigious or not...
211              ...which is absolutely normal for MS docs on anything to do with CPU topology - bloody useless
212              just to make the point about bloody useless, this same variable is only a CCHAR in XP
213              prior to XP, it is a pointer to a CCHAR, where that CCHAR holds the same data
214
215              jesus...*facepalm*
216     */
217
218     for( loop = 0 ; loop < KeNumberProcessors ; loop++ )
219       libtest_misc_pal_helper_add_logical_processor_to_list_of_logical_processors( list_of_logical_processors, ms, loop, 0 );
220
221     return;
222   }
223
224 #endif
225
226
227
228
229
230 /****************************************************************************/
231 #if( defined __linux__ && defined __STDC__ and __STDC_HOSTED__ == 1 )
232
233   #ifdef LIBTEST_PAL_GET_LOGICAL_CORE_IDS
234     #error More than one porting abstraction layer matches current platform in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
235   #endif
236
237   #define LIBTEST_PAL_GET_LOGICAL_CORE_IDS
238
239   void libtest_pal_get_full_logical_processor_set( struct lfds710_list_asu_state *list_of_logical_processors,
240                                                    struct libshared_memory_state *ms )
241   {
242     char
243       diskbuffer[BUFSIZ],
244       string[1024];
245
246     FILE
247       *diskfile;
248
249     int long long unsigned
250       logical_processor_number;
251
252     LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
253     LFDS710_PAL_ASSERT( ms != NULL );
254
255     lfds710_list_asu_init_valid_on_current_logical_core( list_of_logical_processors, NULL );
256
257     diskfile = fopen( "/proc/cpuinfo", "r" );
258
259     if( diskfile != NULL )
260     {
261       setbuf( diskfile, diskbuffer );
262
263       while( NULL != fgets(string, 1024, diskfile) )
264         if( 1 == sscanf(string, "processor : %llu", &logical_processor_number) )
265           libtest_misc_pal_helper_add_logical_processor_to_list_of_logical_processors( list_of_logical_processors, ms, logical_processor_number, 0 );
266
267       fclose( diskfile );
268     }
269
270     return;
271   }
272
273 #endif
274
275
276
277
278
279 /****************************************************************************/
280 #if( !defined LIBTEST_PAL_GET_LOGICAL_CORE_IDS )
281
282     #error No matching porting abstraction layer in "libtest_porting_abstraction_layer_get_full_logical_processor_set.c".
283
284 #endif
285