Auto-adapting the Runtime State
If different parameters need to be set based on chip types or different code logic paths need to be used for the upper-layer service code, query the chip type in real time to determine the chip type. In this way, the code implements automatic adaptation of the chip type. Different from the compilation macro control method, the code is irrelevant to compilation and can be compiled in different chip architectures.
Procedure
Query the CPU type in real time and determine the CPU type based on the query result. For example, you can run the lscpu command in the Linux operating system:
Command: lscpu | grep Archit | awk '{print $2}'
- The following information is displayed on the Kunpeng server:
[root@16u ~]# lscpu | grep Archit | awk '{print $2}' aarch64 - The following information is displayed on the x86 server:
[root@16u ~]# lscpu | grep Archit | awk '{print $2}' x86_64
The programming language has function similar to system() function. The function can be used to execute the preceding shell command and determine the code fork execution path based on the execution result.
//Pseudocode used to automatically identify the chip type.
strCmdLine = "lscpu | grep Archit | awk '{print $2}'";
strExeResult = system(strCmdLine);
if ("aarch64" == strExeResult) //Stores the service code of the Kunpeng chip here.
{
...
}
if ("x86_64" == strExeResult) //Stores the service code of the x86 chip here.
{
...
}
In addition to the lscpu command, there are many other methods for determining the underlying chip type. You can use the command based on service requirements.