Skip to content

Commit 2fd0a12

Browse files
authored
count and sort cpu models on big.little systems
1 parent 17803e7 commit 2fd0a12

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

cpuid_arm64.c

Lines changed: 28 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626
*****************************************************************************/
2727

28+
#include <stdlib.h>
2829
#include <string.h>
2930
#ifdef __APPLE__
3031
#include <sys/sysctl.h>
@@ -42,11 +43,9 @@ size_t length64=sizeof(value64);
4243
#define CPU_CORTEXA57 3
4344
#define CPU_CORTEXA72 4
4445
#define CPU_CORTEXA73 5
45-
#define CPU_CORTEXA76 23
4646
#define CPU_NEOVERSEN1 11
4747
#define CPU_NEOVERSEV1 16
4848
#define CPU_NEOVERSEN2 17
49-
#define CPU_NEOVERSEV2 24
5049
#define CPU_CORTEXX1 18
5150
#define CPU_CORTEXX2 19
5251
#define CPU_CORTEXA510 20
@@ -91,9 +90,7 @@ static char *cpuname[] = {
9190
"CORTEXX2",
9291
"CORTEXA510",
9392
"CORTEXA710",
94-
"FT2000",
95-
"CORTEXA76",
96-
"NEOVERSEV2"
93+
"FT2000"
9794
};
9895

9996
static char *cpuname_lower[] = {
@@ -119,15 +116,13 @@ static char *cpuname_lower[] = {
119116
"cortexx2",
120117
"cortexa510",
121118
"cortexa710",
122-
"ft2000",
123-
"cortexa76",
124-
"neoversev2"
119+
"ft2000"
125120
};
126121

127122
int get_feature(char *search)
128123
{
129124

130-
#if defined( __linux ) || defined( __NetBSD__ )
125+
#ifdef __linux
131126
FILE *infile;
132127
char buffer[2048], *p,*t;
133128
p = (char *) NULL ;
@@ -158,33 +153,49 @@ int get_feature(char *search)
158153
#endif
159154
return(0);
160155
}
161-
156+
static int cpusort(const void *model1, const void *model2)
157+
{
158+
return (*(int*)model2-*(int*)model1);
159+
}
162160

163161
int detect(void)
164162
{
165163

166-
#if defined( __linux ) || defined( __NetBSD__ )
167-
164+
#ifdef __linux
165+
int n,i;
166+
int cpucores[1024];
168167
FILE *infile;
169-
char buffer[512], *p, *cpu_part = NULL, *cpu_implementer = NULL;
168+
char cpupart[6];
169+
char buffer[2048], *p, *cpu_part = NULL, *cpu_implementer = NULL;
170170
p = (char *) NULL ;
171-
171+
infile = fopen("/proc/cpuinfo", "r"); while (fgets(buffer, sizeof(buffer), infile)) { if (!strncmp("processor", buffer, 9)) n++; }
172+
fclose(infile);
173+
for (i=0;i<1024;i++)cpucores[i]=0;
174+
cpu_implementer=NULL;
172175
infile = fopen("/proc/cpuinfo", "r");
176+
for (i=0;i<n-1;i++){
173177
while (fgets(buffer, sizeof(buffer), infile)) {
178+
cpu_part=NULL;//cpu_implementer=NULL;
174179
if ((cpu_part != NULL) && (cpu_implementer != NULL)) {
175180
break;
176181
}
177182

178183
if ((cpu_part == NULL) && !strncmp("CPU part", buffer, 8)) {
179184
cpu_part = strchr(buffer, ':') + 2;
180185
cpu_part = strdup(cpu_part);
186+
cpucores[i]=strtol(cpu_part,NULL,0);
187+
181188
} else if ((cpu_implementer == NULL) && !strncmp("CPU implementer", buffer, 15)) {
182189
cpu_implementer = strchr(buffer, ':') + 2;
183190
cpu_implementer = strdup(cpu_implementer);
184191
}
185-
}
186192

193+
}
194+
}
187195
fclose(infile);
196+
qsort(cpucores,1024,sizeof(int),cpusort);
197+
sprintf(cpupart,"0x%3x",cpucores[0]);
198+
cpu_part=strdup(cpupart);
188199
if(cpu_part != NULL && cpu_implementer != NULL) {
189200
// Arm
190201
if (strstr(cpu_implementer, "0x41")) {
@@ -216,10 +227,6 @@ int detect(void)
216227
return CPU_CORTEXX2;
217228
else if (strstr(cpu_part, "0xd4e")) //X3
218229
return CPU_CORTEXX2;
219-
else if (strstr(cpu_part, "0xd4f")) //NVIDIA Grace et al.
220-
return CPU_NEOVERSEV2;
221-
else if (strstr(cpu_part, "0xd0b"))
222-
return CPU_CORTEXA76;
223230
}
224231
// Qualcomm
225232
else if (strstr(cpu_implementer, "0x51") && strstr(cpu_part, "0xc00"))
@@ -280,8 +287,6 @@ int detect(void)
280287
sysctlbyname("hw.cpufamily",&value64,&length64,NULL,0);
281288
if (value64 ==131287967|| value64 == 458787763 ) return CPU_VORTEX; //A12/M1
282289
if (value64 == 3660830781) return CPU_VORTEX; //A15/M2
283-
if (value64 == 2271604202) return CPU_VORTEX; //A16/M3
284-
if (value64 == 1867590060) return CPU_VORTEX; //M4
285290
#endif
286291
return CPU_ARMV8;
287292
#endif
@@ -314,7 +319,7 @@ void get_cpucount(void)
314319
{
315320
int n=0;
316321

317-
#if defined( __linux ) || defined( __NetBSD__ )
322+
#ifdef __linux
318323
FILE *infile;
319324
char buffer[2048], *p,*t;
320325
p = (char *) NULL ;
@@ -347,7 +352,6 @@ void get_cpuconfig(void)
347352
printf("#define ARMV8\n");
348353
printf("#define HAVE_NEON\n"); // This shouldn't be necessary
349354
printf("#define HAVE_VFPV4\n"); // This shouldn't be necessary
350-
351355
int d = detect();
352356
switch (d)
353357
{
@@ -402,8 +406,6 @@ void get_cpuconfig(void)
402406
break;
403407

404408
case CPU_NEOVERSEV1:
405-
printf("#define HAVE_SVE 1\n");
406-
case CPU_CORTEXA76:
407409
printf("#define %s\n", cpuname[d]);
408410
printf("#define L1_CODE_SIZE 65536\n");
409411
printf("#define L1_CODE_LINESIZE 64\n");
@@ -431,32 +433,12 @@ void get_cpuconfig(void)
431433
printf("#define L2_ASSOCIATIVE 8\n");
432434
printf("#define DTB_DEFAULT_ENTRIES 48\n");
433435
printf("#define DTB_SIZE 4096\n");
434-
printf("#define HAVE_SVE 1\n");
435-
break;
436-
case CPU_NEOVERSEV2:
437-
printf("#define ARMV9\n");
438-
printf("#define HAVE_SVE 1\n");
439-
printf("#define %s\n", cpuname[d]);
440-
printf("#define L1_CODE_SIZE 65536\n");
441-
printf("#define L1_CODE_LINESIZE 64\n");
442-
printf("#define L1_CODE_ASSOCIATIVE 4\n");
443-
printf("#define L1_DATA_SIZE 65536\n");
444-
printf("#define L1_DATA_LINESIZE 64\n");
445-
printf("#define L1_DATA_ASSOCIATIVE 4\n");
446-
printf("#define L2_SIZE 1048576\n");
447-
printf("#define L2_LINESIZE 64\n");
448-
printf("#define L2_ASSOCIATIVE 8\n");
449-
// L1 Data TLB = 48 entries
450-
// L2 Data TLB = 2048 entries
451-
printf("#define DTB_DEFAULT_ENTRIES 48\n");
452-
printf("#define DTB_SIZE 4096\n"); // Set to 4096 for symmetry with other configs.
453436
break;
454437
case CPU_CORTEXA510:
455438
case CPU_CORTEXA710:
456439
case CPU_CORTEXX1:
457440
case CPU_CORTEXX2:
458441
printf("#define ARMV9\n");
459-
printf("#define HAVE_SVE 1\n");
460442
printf("#define %s\n", cpuname[d]);
461443
printf("#define L1_CODE_SIZE 65536\n");
462444
printf("#define L1_CODE_LINESIZE 64\n");
@@ -559,8 +541,6 @@ void get_cpuconfig(void)
559541
case CPU_VORTEX:
560542
printf("#define VORTEX \n");
561543
#ifdef __APPLE__
562-
sysctlbyname("hw.cpufamily",&value64,&length64,NULL,0);
563-
if (value64 == 1867590060) printf("#define HAVE_SME 1\n");; //M4
564544
sysctlbyname("hw.l1icachesize",&value64,&length64,NULL,0);
565545
printf("#define L1_CODE_SIZE %lld \n",value64);
566546
sysctlbyname("hw.cachelinesize",&value64,&length64,NULL,0);
@@ -575,7 +555,6 @@ void get_cpuconfig(void)
575555
break;
576556
case CPU_A64FX:
577557
printf("#define A64FX\n");
578-
printf("#define HAVE_SVE 1\n");
579558
printf("#define L1_CODE_SIZE 65535\n");
580559
printf("#define L1_DATA_SIZE 65535\n");
581560
printf("#define L1_DATA_LINESIZE 256\n");
@@ -608,7 +587,7 @@ void get_libname(void)
608587
void get_features(void)
609588
{
610589

611-
#if defined( __linux ) || defined( __NetBSD__ )
590+
#ifdef __linux
612591
FILE *infile;
613592
char buffer[2048], *p,*t;
614593
p = (char *) NULL ;

0 commit comments

Comments
 (0)