Skip to content

Commit 589c74a

Browse files
committed
Use systemcfg APIs for CPU detection on AIX
AIX libc already provides ready access to an integer that contains a bit identifying the CPU it's running on, so there's no need to call a program and grep its output. Additionally, prtconf is not available in the PASE runtime, which provides an AIX emulation layer on the IBM i operating system. The AIX systemcfg.h also provides macro definitions like POWER_8, POWER_9, etc for all the bits defining the CPUs as well as macros like __power_8(), __power_9_andup() that return booleans, but I did not use them. Since these macros depend on the level of the OS in which it is built, they may not be defined and instead the associated hex literals are used directly.
1 parent 104aa67 commit 589c74a

File tree

1 file changed

+13
-28
lines changed

1 file changed

+13
-28
lines changed

cpuid_power.c

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838

3939
#include <sys/utsname.h>
4040
#ifdef _AIX
41+
#include <sys/systemcfg.h>
4142
#include <sys/vminfo.h>
4243
#endif
4344
#ifdef __APPLE__
@@ -137,35 +138,19 @@ int detect(void){
137138
#endif
138139

139140
#ifdef _AIX
140-
FILE *infile;
141-
char buffer[512], *p;
142-
143-
p = (char *)NULL;
144-
infile = popen("prtconf|grep 'Processor Type'", "r");
145-
while (fgets(buffer, sizeof(buffer), infile)){
146-
if (!strncmp("Pro", buffer, 3)){
147-
p = strchr(buffer, ':') + 2;
148-
#if 0
149-
fprintf(stderr, "%s\n", p);
150-
#endif
151-
break;
152-
}
153-
}
154-
155-
pclose(infile);
141+
// Cast from int to unsigned to ensure comparisons work for all bits in
142+
// the bit mask, even the top bit
143+
unsigned implementation = (unsigned) _system_configuration.implementation;
156144

157-
if (strstr(p, "POWER3")) return CPUTYPE_POWER3;
158-
if (strstr(p, "POWER4")) return CPUTYPE_POWER4;
159-
if (strstr(p, "PPC970")) return CPUTYPE_PPC970;
160-
if (strstr(p, "POWER5")) return CPUTYPE_POWER5;
161-
if (strstr(p, "POWER6")) return CPUTYPE_POWER6;
162-
if (strstr(p, "POWER7")) return CPUTYPE_POWER6;
163-
if (strstr(p, "POWER8")) return CPUTYPE_POWER8;
164-
if (strstr(p, "POWER9")) return CPUTYPE_POWER9;
165-
if (strstr(p, "POWER10")) return CPUTYPE_POWER10;
166-
if (strstr(p, "Cell")) return CPUTYPE_CELL;
167-
if (strstr(p, "7447")) return CPUTYPE_PPCG4;
168-
return CPUTYPE_POWER5;
145+
if (implementation >= 0x40000u) return CPUTYPE_POWER10;
146+
else if (implementation & 0x20000) return CPUTYPE_POWER9;
147+
else if (implementation & 0x10000) return CPUTYPE_POWER8;
148+
else if (implementation & 0x08000) return CPUTYPE_POWER7; // POWER 7
149+
else if (implementation & 0x04000) return CPUTYPE_POWER6;
150+
else if (implementation & 0x02000) return CPUTYPE_POWER5;
151+
else if (implementation & 0x01000) return CPUTYPE_POWER4; // MPC7450
152+
else if (implementation & 0x00800) return CPUTYPE_POWER4;
153+
else return CPUTYPE_POWER3;
169154
#endif
170155

171156
#ifdef __APPLE__

0 commit comments

Comments
 (0)