Method and apparatus for detecting characteristics of a processor
By obtaining processor characteristic parameters during application compilation, determining whether characteristic detection instructions are supported, and directly using characteristic detection instructions or reading the cpuinfo file to obtain characteristic information, the problems of easy crashes and low efficiency in processor characteristic detection are solved, and efficient characteristic detection is achieved.
Patent Information
- Application Number
- CN202011568419.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-12-25
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2040-12-25
AI Technical Summary
Existing processor characteristic detection methods are prone to causing processor crashes or low detection efficiency.
When compiling the application, the first feature parameter is obtained. The parameter is used to determine whether the processor supports feature detection instructions. If it supports them, the feature detection instructions are used directly to obtain feature information. If it does not support them, the cpuinfo file is read to obtain feature information, thus avoiding crashes caused by blindly using feature detection instructions.
It improves the efficiency of processor feature detection, avoids processor crashes, and provides the fastest feature checking method.
Smart Images

Figure CN114691434B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer software technology, and in particular to a method and apparatus for detecting the characteristics of a processor. Background Technology
[0002] Processor updates are typically accompanied by new features or instruction sets. Newer processors are compatible with the features supported by older processors, but older processors cannot support the new features. Software developers often face the requirement that a single executable program needs to support multiple processors, meaning it needs to support various processor features. Therefore, software developers need to add runtime processor feature checks to their software and then select the appropriate implementation based on the feature support of each processor. For example, older processors only support 64-bit SIMD instructions, while newer processor platforms support 128-bit SIMD instructions in addition to 64-bit SIMD.
[0003] In related technologies, two methods for processor feature detection are provided to facilitate software developers in detecting processor features during runtime. The first method involves developers using processor detection instructions to check processor features. These instructions are compiled into the application of the developed software project. When the processor runs the application, it executes the detection instructions and returns information about the features it supports. Software developers can then determine which features the processor supports based on the returned results. The second method involves parsing the cpuinfo file to obtain the features supported by the processor.
[0004] However, the two methods mentioned above have the following problems: The feature detection instruction in the first method is itself similar to the feature of the processor, and not all processors can support it. If the software implementation uniformly forces the execution of this instruction to detect the features of all processors, it will cause the software to crash on some processors that do not support the feature detection instruction. Although the second method is highly adaptable and applicable to the detection of features of all processors, it is time-consuming and has very low detection efficiency. Summary of the Invention
[0005] This invention provides a method and apparatus for detecting processor characteristics, in order to solve the technical problems in the prior art where processor characteristic detection easily leads to processor crashes and the detection process is time-consuming.
[0006] A first aspect of this invention provides a method for detecting the characteristics of a processor, comprising:
[0007] Obtain the first characteristic parameter when compiling the application;
[0008] Based on the first characteristic parameter, it is determined whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction.
[0009] When the first detection is not required and it is clear that the first processor supports the feature detection instruction, a first executable file is generated based on the application source code and the judgment result. The first executable file includes binary code that uses the feature detection instruction to obtain processor feature information.
[0010] When the first detection is required, a second executable file is generated based on the application source code and the judgment result, so that when the first processor runs the second executable file, it determines whether the first processor supports the feature detection instruction and obtains processor feature information based on the determination result.
[0011] Optionally, the method further includes:
[0012] When the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result. The third executable file includes binary code that reads the cpuinfo file to obtain processor feature information.
[0013] Optionally, obtaining processor characteristic information based on the determination result includes:
[0014] If it is determined that the first processor supports the feature detection instruction, then the feature detection instruction is used to obtain processor feature information;
[0015] If it is determined that the first processor does not support the feature detection instruction, then the cpuinfo file is read to obtain processor feature information.
[0016] Optionally, obtaining processor characteristic information using the characteristic detection instruction includes:
[0017] Control the first processor to run the first executable file, so as to execute the feature detection instruction when running the first executable file;
[0018] Obtain the feature identifier generated when the feature detection instruction is executed;
[0019] The feature identifier is parsed to obtain the feature information supported by the first processor.
[0020] Optionally, the step of reading the cpuinfo file to obtain processor characteristic information includes:
[0021] The first processor is controlled to run the third executable file, so that the cpuinfo file is read and parsed when the third executable file is run to obtain feature information supported by the first processor.
[0022] A second aspect of the present invention provides a processor characteristic detection device, comprising:
[0023] The acquisition module is used to obtain the first characteristic parameter when compiling the application;
[0024] The judgment module is used to determine, based on the first characteristic parameter, whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction.
[0025] The detection module is used to generate a first executable file based on the application source code and the judgment result when the first detection is not required and it is clear that the first processor supports the feature detection instruction. The first executable file includes binary code that directly uses the feature detection instruction to obtain processor feature information.
[0026] The detection module is further configured to generate a second executable file based on the application source code and the judgment result when the first detection is required, so that the first processor can determine whether the first processor supports the feature detection instruction and obtain processor feature information based on the determination result when running the second executable file.
[0027] Optionally, the detection module is further configured to:
[0028] When the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result. The third executable file includes binary code that reads the cpuinfo file to obtain processor feature information.
[0029] Optionally, the detection module is specifically used for:
[0030] If it is determined that the first processor supports the feature detection instruction, then the feature detection instruction is used to obtain processor feature information;
[0031] If it is determined that the first processor does not support the feature detection instruction, then the cpuinfo file is read to obtain processor feature information.
[0032] Optionally, the detection module includes:
[0033] A first control unit is configured to control the first processor to run the first executable file, so as to execute the feature detection instruction when running the first executable file;
[0034] The acquisition unit is used to acquire the feature identifier generated when the feature detection instruction is executed;
[0035] The first parsing unit is used to parse the feature identifier to obtain feature information supported by the first processor.
[0036] Optionally, the detection module includes: a second control unit, used to control the first processor to run the third executable file;
[0037] The reading unit is configured to read the cpuinfo file when the first processor is running the third executable file;
[0038] The second parsing unit is used to parse the cpuinfo file to obtain feature information supported by the first processor.
[0039] This invention provides a method and apparatus for processor feature detection. The method first obtains first feature parameters during application compilation. These parameters are used to determine whether a first processor running the application needs to undergo a first detection, specifically whether it supports feature detection instructions. If no first detection is required, it indicates that the first processor either explicitly supports or does not support feature detection instructions. For processors that explicitly support feature detection instructions, a first executable file is generated. This first executable file includes binary code that uses the feature detection instructions to obtain processor feature information. Running the first executable file allows the first processor to directly use faster feature detection instructions to detect its supported feature information, providing the fastest feature checking method for cases where processor support is known, eliminating the need to check for feature detection instruction support in all cases. If a first detection is required, it indicates uncertainty regarding the processor's support for feature detection instructions. Therefore, a second executable file is generated. While running the second executable file, the first processor determines whether it supports the feature detection instructions and obtains processor feature information based on the determination result. In cases where it is unclear whether the processor supports feature detection instructions, we first determine whether the first processor supports feature detection instructions, and then select a feature detection method based on the determination result. This avoids the crash caused by blindly performing feature detection on the first processor when it does not support feature detection instructions. It can achieve processor feature detection in the fastest way without ensuring that the processor does not crash, thus improving feature detection efficiency. Attached Figure Description
[0040] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0041] Figure 1 This is an application scenario diagram illustrating a processor characteristic detection method according to an exemplary embodiment of the present invention;
[0042] Figure 2 This is a schematic flowchart illustrating a processor characteristic detection method according to an exemplary embodiment of the present invention;
[0043] Figure 3 This is a schematic flowchart illustrating a processor characteristic detection method according to another exemplary embodiment of the present invention;
[0044] Figure 4 This is a schematic flowchart illustrating a processor characteristic detection method according to another exemplary embodiment of the present invention;
[0045] Figure 5 This is a schematic flowchart illustrating a processor characteristic detection method according to another exemplary embodiment of the present invention;
[0046] Figure 6 This is a schematic flowchart illustrating a processor characteristic detection method according to another exemplary embodiment of the present invention;
[0047] Figure 7 This is a schematic diagram of the structure of a processor characteristic detection device according to an exemplary embodiment of the present invention;
[0048] Figure 8 This is a schematic diagram of the structure of the detection module in a processor characteristic detection device according to an exemplary embodiment of the present invention;
[0049] Figure 9 This is a schematic diagram of the structure of an electronic device shown in an exemplary embodiment of the present invention. Detailed Implementation
[0050] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0051] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification, claims, and accompanying drawings of this invention are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of the invention described herein can be implemented, for example, in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover a non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0052] Processor updates are typically accompanied by new features or instruction sets. Newer processors are compatible with the features supported by older processors, but older processors cannot support the new features. Software developers often face the requirement that a single executable program needs to support multiple processors, meaning it needs to support various processor features. Therefore, software developers need to add runtime processor feature checks to their software and then select the appropriate implementation based on the feature support of each processor. For example, older processors only support 64-bit SIMD instructions, while newer processor platforms support 128-bit SIMD instructions in addition to 64-bit SIMD.
[0053] In related technologies, to ensure software compatibility with most processors, runtime checks of processor characteristics need to be implemented in the software. Taking the Linux system environment as an example, there are generally two methods for checking. The first method is that developers can use processor detection instructions to check processor characteristics. That is, the characteristic detection instructions are compiled into the application of the developed software project. When the processor runs the application, it can execute the characteristic detection instructions and return information on the characteristics that the processor supports. The software developer can then determine which characteristics the processor supports based on the return results. However, although this method is fast, the characteristic detection instructions themselves are similar to processor characteristics, and not all processors support them. If the software implementation uniformly forces the execution of this instruction to detect all processor characteristics, it will cause the software to crash on some processors that do not support the characteristic detection instructions. The second method is to parse the cpuinfo file to obtain the characteristics supported by the processor. However, although this method is highly adaptable and applicable to the detection of all processor characteristics, it is time-consuming and has low detection efficiency.
[0054] To address this deficiency, the main technical solution of this invention is as follows: During application compilation, a first characteristic parameter is first obtained. This parameter is used to determine whether the first processor running the application needs to undergo a first detection. The first detection determines whether the first processor supports characteristic detection instructions. If no first detection is required, it indicates that the first processor either explicitly supports or explicitly does not support characteristic detection instructions. For a first processor that explicitly supports characteristic detection instructions, a first executable file is generated. This executable file includes binary code that directly uses the characteristic detection instructions to obtain processor characteristic information. By running the first executable file, the first processor can directly use the faster characteristic detection instructions to detect its supported characteristic information. This provides the fastest characteristic checking method for situations where it is known that the processor supports characteristic detection instructions, avoiding the need to check for support in all situations. If it is known that the first processor does not support characteristic detection instructions, the cpuinfo file is read to obtain the first processor's characteristic information, avoiding the possibility of the first processor crashing due to blindly using characteristic detection instructions. If a first detection is required on the first processor, it indicates that it is uncertain whether the first processor supports feature detection instructions. Therefore, a second executable file is generated. While running the second executable file, the first processor determines whether it supports the feature detection instructions and obtains processor feature information based on the determination result. For cases where it is unclear whether the processor supports feature detection instructions, the first processor's support for feature detection instructions is first determined, and a feature detection method is selected based on the determination result. This avoids crashes caused by blindly performing feature detection on the first processor when it does not support the instructions. It enables processor feature detection in the fastest way possible without causing processor crashes, thus improving feature detection efficiency.
[0055] Figure 1 This is an application scenario diagram illustrating a processor characteristic detection method according to an exemplary embodiment of the present invention.
[0056] like Figure 1As shown, the scenario includes a first client 101 and a second client 102. The processor in the first client 101 is an older version processor, while the processor in the second client 102 is a newer version processor. To compare and detect which features the newer version processor in the second client and the older version processor in the first client support, the cpuinfo file of the corresponding processor can be directly obtained to acquire the features supported by the processor. Alternatively, a feature detection instruction can be added to the developed software project (such as an application APP). When relevant technicians click on the application APP, the processor in the corresponding client executes the feature detection instruction while running the application. The processor then remotely sends the running results to the display terminal 103 for display. The running results include the feature information of the processor in the first client and the feature information of the processor in the second client.
[0057] Figure 2 This is a schematic flowchart illustrating a processor characteristic detection method according to an exemplary embodiment of the present invention. The execution entity of the method provided in this embodiment may be... Figure 1 The client in the illustrated embodiment can also be a server; this embodiment does not impose any specific limitations.
[0058] like Figure 2 As shown, the method provided in this embodiment mainly includes the following steps.
[0059] S201, when compiling the application, obtains the first characteristic parameter.
[0060] The first characteristic parameter is used to identify whether the corresponding processor explicitly supports characteristic detection instructions.
[0061] S202, based on the first characteristic parameter, determine whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction.
[0062] Specifically, when configuring a processor, technicians often know whether the processor they are configuring supports feature detection instructions. Therefore, technicians can pre-set some feature parameters in the processor through the operating terminal to identify whether the processor explicitly supports feature detection instructions.
[0063] In one possible embodiment, a technician can set feature detection parameters in processors that explicitly support feature detection instructions, and not set feature detection parameters in processors whose support for feature detection instructions is uncertain. When the first processor runs the compiled application, the first feature parameters of the first processor are first obtained. If the first feature parameters exist, it means that the first processor explicitly supports feature detection instructions, and therefore no first detection is needed for the first processor; if the first feature parameters do not exist, it means that it is uncertain whether the first processor supports feature detection instructions, and in order to select a fast and safe feature detection method, the first detection needs to be performed on the first processor to detect whether the first processor supports feature detection instructions.
[0064] For example, suppose there are three processors to be tested: processor A, processor B, and processor C. Processors A and B are configured by a testing technician who knows they support feature detection instructions. However, processor C is not configured by the technician, and the technician does not know whether processor C supports feature detection instructions. Therefore, the technician can pre-set feature parameters in processors A and B via an operating terminal, but not in processor C. When processors A, B, and C run their compiled applications, they can obtain the feature parameters of processors A and B, but not those of processor C. This indicates that processors A and B support feature detection instructions, but it is uncertain whether processor C supports them. Therefore, feature detection instructions cannot be directly used to test the features of processor C; a first test is needed to determine whether processor C supports feature detection instructions.
[0065] In another possible embodiment, a technician can set different characteristic parameters for processors that explicitly support characteristic detection instructions and processors whose support for characteristic detection instructions is uncertain. When the processor runs the compiled application, it determines whether to perform a first detection based on the value of the obtained characteristic parameter. Optionally, characteristic parameter 00 indicates that the processor explicitly supports characteristic detection instructions, and characteristic parameter 10 indicates that the processor cannot directly use characteristic detection instructions and needs to perform a first detection to further determine whether it supports characteristic detection instructions. When the first processor runs the compiled application, if the obtained characteristic parameter of the first processor is 00, it means that the first processor explicitly supports characteristic detection instructions and no first detection is required; if the obtained characteristic parameter of the first processor is 10, it means that it is uncertain whether the first processor supports characteristic detection instructions and a first detection is required.
[0066] S203, when the first detection is not required and it is clear that the first processor supports the feature detection instruction, a first executable file is generated based on the application source code and the judgment result. The first executable file includes binary code that uses the feature detection instruction to obtain processor feature information.
[0067] Specifically, if the first processor running the compiled application is determined not to require the first detection based on the first characteristic parameter, and the first characteristic parameter clearly indicates that the first processor supports characteristic detection instructions, then there is no need to further check its characteristic detection instruction support. It can be directly determined that the application will not crash due to compatibility issues when executing the characteristic detection instructions. Therefore, a fast characteristic detection instruction is selected to perform characteristic detection on the first processor to obtain the characteristic information supported by the first processor. The source code of the characteristic detection instruction can be added to the application's source code and compiled to generate a first executable file. When the first processor runs the first executable file, it will directly execute the characteristic detection instruction, obtaining the characteristic information supported by the first processor during the execution of the instruction.
[0068] S204, when the first detection is required, a second executable file is generated based on the application source code and the judgment result, so that when the first processor runs the second executable file, it determines whether the first processor supports the feature detection instruction and obtains processor feature information based on the determination result.
[0069] It should be noted that when the first processor needs to be tested, it means that it is uncertain whether the first processor supports the feature detection instruction. Therefore, it is necessary to further test the first processor to determine whether the first processor supports the feature detection instruction, so as to avoid forcing the first processor to execute the feature detection instruction if the first processor does not support the feature detection instruction and causing a crash.
[0070] In some embodiments, a second executable file is generated based on the application source code and the judgment result. The method for the first processor to perform a first detection by running the second executable file can include two types. One is to determine whether the first processor supports feature detection instructions by obtaining the value of a specific flag bit of the first processor. However, this method has requirements for the processor version. Some versions of processors do not have special flag bits, so this method cannot be used to determine this. The other is a more general method that does not have requirements for the processor version. Specifically, it involves creating a child process outside the application process running on the first processor, executing the feature detection instructions separately in the child process, and determining whether the first processor supports feature detection instructions by monitoring the execution of the child process.
[0071] Furthermore, by analyzing the results of the first detection of the first processor, the method for obtaining the feature information supported by the first processor is determined. If the first detection determines that the first processor supports feature detection instructions, then a feature detection instruction with a faster detection speed is used to obtain the feature information supported by the first processor. If the first detection determines that the first processor does not support feature detection instructions, then the feature information supported by the first processor is obtained by reading the cpuinfo file, thus avoiding the first processor from crashing due to forced execution of feature detection instructions.
[0072] In this embodiment, a first characteristic parameter is obtained during application compilation. This parameter is used to determine whether the first processor running the application needs to undergo a first detection. The first detection determines whether the first processor supports characteristic detection instructions. If no first detection is required, it indicates that the first processor either explicitly supports or explicitly does not support characteristic detection instructions. For a first processor that explicitly supports characteristic detection instructions, a first executable file is generated. This first executable file includes binary code that uses the characteristic detection instructions to obtain processor characteristic information. By running the first executable file, the first processor can directly use the faster characteristic detection instructions to detect its supported characteristic information. This provides the fastest characteristic checking method for cases where it is known that the processor supports characteristic detection instructions, avoiding the need to check for support in all cases. If a first detection is required, it indicates that it is uncertain whether the first processor supports characteristic detection instructions. Therefore, a second executable file is generated. While running the second executable file, the first processor determines whether it supports the characteristic detection instructions and obtains processor characteristic information based on the determination result. In cases where it is unclear whether the processor supports feature detection instructions, we first determine whether the first processor supports feature detection instructions, and then select a feature detection method based on the determination result. This avoids the crash caused by blindly performing feature detection on the first processor when it does not support feature detection instructions. It can achieve processor feature detection in the fastest way without ensuring that the processor does not crash, thus improving feature detection efficiency.
[0073] To better understand this application, the following will be... Figure 2 Based on the illustrated embodiments, combined with Figure 3 The complete implementation process of the processor characteristic detection method is described in detail.
[0074] like Figure 3 As shown, the method provided in this embodiment may include the following steps.
[0075] S31, when compiling the application, obtains the first characteristic parameter.
[0076] S32, based on the first characteristic parameter, determine whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction.
[0077] The first characteristic parameter is used to identify whether the corresponding processor explicitly supports characteristic detection instructions.
[0078] Specifically, when configuring a processor, technicians often know whether the processor they are configuring supports feature detection instructions. Therefore, technicians can pre-set some feature parameters in the processor through the operating terminal to identify whether the processor explicitly supports feature detection instructions.
[0079] In one possible embodiment, a technician can set different values for feature detection parameters in processors that explicitly support or explicitly do not support feature detection instructions, while no feature detection parameters are set for processors whose support for feature detection instructions is uncertain. When the first processor runs the compiled application, the first feature parameter of the first processor is first obtained. If the first feature parameter exists, it indicates that the first processor explicitly supports or explicitly does not support feature detection instructions. Then, based on the value of the obtained first feature parameter, it is further determined whether the first processor explicitly supports or explicitly does not support feature detection instructions. For example, feature parameter 00 can be used to indicate that the processor explicitly supports feature detection instructions, and feature parameter 01 can be used to indicate that the processor explicitly does not support feature detection instructions. Therefore, the first detection of the first processor is not required. If the first feature parameter does not exist, it indicates that it is uncertain whether the first processor supports feature detection instructions. In order to select a fast and safe feature detection method, the first detection of the first processor is required to detect whether the first processor supports feature detection instructions.
[0080] For example, suppose there are four processors to be tested: processor A, processor B, processor C, and processor D. Processors A, B, and D are configured by a testing technician who knows that processors A and B support feature detection instructions, while processor D does not. Processor C is not configured by the technician, and the technician does not know whether processor C supports feature detection instructions. Therefore, the technician can pre-set the feature parameters to 00 for processors A and B, 01 for processor D, and leave processor C unconfigured via an operating terminal. When processors A, B, C, and D run their compiled applications, if the feature parameters for processors A and B are found to be 00, it indicates that processors A and B explicitly support feature detection instructions. If the feature parameter for processor D is found to be 01, it indicates that processors D explicitly do not support feature detection instructions. However, if the feature parameters for processor C are not found, it means that it is uncertain whether processor C supports feature detection instructions. Therefore, feature detection instructions cannot be directly used to test the features of processor C; a first test is needed to determine whether processor C supports feature detection instructions.
[0081] In another possible embodiment, a technician can set different characteristic parameters for processors that explicitly support or explicitly do not support characteristic detection instructions, and for processors whose support for characteristic detection instructions is uncertain. When the processor runs the compiled application, it determines whether to perform a first detection based on the value of the obtained characteristic parameter. Optionally, characteristic parameter 00 indicates that the processor explicitly supports characteristic detection instructions, characteristic parameter 01 indicates that the processor explicitly does not support characteristic detection instructions, and characteristic parameter 10 indicates that the processor cannot directly use characteristic detection instructions and needs to perform a first detection to further determine whether it supports characteristic detection instructions. When the first processor runs the compiled application, if the obtained characteristic parameter of the first processor is 00, it means that the first processor explicitly supports characteristic detection instructions; if the obtained characteristic parameter of the first processor is 01, it means that the first processor explicitly does not support characteristic detection instructions and no first detection is required; if the obtained characteristic parameter of the first processor is 10, it means that it is uncertain whether the first processor supports characteristic detection instructions and a first detection is required.
[0082] S33, when the first detection is not required and it is clear that the first processor supports the feature detection instruction, a first executable file is generated based on the application source code and the judgment result, so that the first processor runs the first executable file.
[0083] The first executable file includes binary code that directly uses the feature detection instructions to obtain processor feature information.
[0084] Specifically, when it is determined from the first characteristic parameter that the first processor running the compiled application does not need to perform the first detection, and it is clear from the first characteristic parameter that the first processor supports characteristic detection instructions, then there is no need to check its characteristic detection instruction support. It can be directly determined that it will not crash due to compatibility issues when executing characteristic detection instructions. Therefore, a characteristic detection instruction with fast detection speed is selected to perform characteristic detection on the first processor in order to obtain the characteristic information supported by the first processor.
[0085] In one possible embodiment, the source code of the feature detection instruction can be added to the application's source code and compiled to generate a first executable file. When the first processor runs the first executable file, it will directly execute the feature detection instruction and obtain the feature information supported by the first processor during the execution of the feature detection instruction.
[0086] In one possible scenario of this embodiment, the steps performed by the first processor when running the first executable file are as follows: Figure 4 As shown, the process includes: S331, the first processor runs the first executable file to execute the feature detection instruction while running the first executable file. S332, the feature identifier generated when executing the feature detection instruction is obtained. S333, the feature identifier is parsed to obtain feature information supported by the first processor.
[0087] S34, when the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result, so that the first processor runs the third executable file.
[0088] The third executable file includes binary code that reads the cpuinfo file to obtain processor characteristic information.
[0089] It should be noted that when the first characteristic parameter determines that the first processor running the compiled application does not need to perform the first detection, and the first characteristic parameter clearly indicates that the first processor does not support characteristic detection instructions, then there is no need to further check its characteristic detection instruction support. Directly using characteristic detection instructions to detect characteristic information would cause the first processor to crash during application execution, resulting in the application failing to run properly. Therefore, a safer detection method is chosen to detect the first processor's characteristic information.
[0090] Specifically, the code for reading the cpuinfo file can be added to the application's source code and compiled to generate a third executable file. This third executable file includes binary code for reading the cpuinfo file to obtain processor feature information. When the first processor runs the third executable file, it will first read the cpuinfo file corresponding to the first processor and then parse the cpuinfo file to obtain the feature information supported by the first processor.
[0091] In one possible scenario of this embodiment, the steps performed by the first processor when running the third executable file are as follows: Figure 6 As shown, the process includes: S341, the first processor runs the third executable file; S342, the cpuinfo file is read; S343, the cpuinfo file is parsed to obtain feature information supported by the first processor.
[0092] S35, when the first detection is required, a second executable file is generated based on the application source code and the judgment result, so that when the first processor runs the second executable file, it determines whether the first processor supports the feature detection instruction and obtains processor feature information based on the determination result.
[0093] It should be noted that when the first processor needs to be tested, it means that it is uncertain whether the first processor supports the feature detection instruction. Therefore, it is necessary to further test the first processor to determine whether the first processor supports the feature detection instruction, so as to avoid forcing the first processor to execute the feature detection instruction if the first processor does not support the feature detection instruction and causing a crash.
[0094] In some embodiments, such as Figure 5 As shown, the execution flow of the second executable file includes the following steps:
[0095] S351, Perform a first detection on the first processor to determine whether the first processor supports the feature detection instruction. If the result is yes, proceed to steps S352 to S354; if the result is no, proceed to steps S355 to S356.
[0096] In some embodiments, the method for performing the first detection on the first processor may include two types. One method is to determine whether the first processor supports feature detection instructions by obtaining the value of a specific flag bit of the first processor. However, this method is required by the processor version; some processor versions do not have a special flag bit, so this method cannot be used to determine this. For example, for the 8086 and 8088 CPUs, their flag bits are only 16 bits long and do not reserve a special flag bit to indicate whether they support feature detection instructions. In the 80386 CPU, bit 21 is reserved and unused. If it supports special detection instructions, the value of bit 21 is 1. Therefore, by reading the value of bit 21, it can be determined whether feature detection instructions are supported.
[0097] Another, more general method, which is not limited by processor version, involves creating a child process outside the application process running on the first processor. This child process executes feature detection instructions independently, and the execution of the child process is monitored to determine if the first processor supports feature detection instructions. Specifically, while the first processor is running the compiled application process (which can be called the parent process), an additional child process is created. The first processor then executes feature detection instructions independently within this child process. If the entire execution of the child process proceeds smoothly without crashing, it indicates that the second processor supports feature detection instructions. If a crash occurs during the execution of the feature detection instructions, causing the child process to terminate, it indicates that the first processor does not support feature detection instructions.
[0098] In one possible embodiment, when the first processor executes the feature detection instruction in the child process, the parent process reads the running status of the child process in real time. If the parent process reads a normal status until the child process ends, it indicates that the first processor supports the feature detection instruction; if the parent process reads the child process status and then stops, it indicates that the first processor does not support the feature detection instruction.
[0099] In another possible embodiment, when the feature detection instruction is executed in the child process, the child process actively reports the execution status to the parent process. If the child process is interrupted, it will report a failure message to the parent process; if the child process can complete the execution, it will report a success message to the parent process. The processor can then determine whether the feature detection instruction is supported when running the application in the parent process.
[0100] S352, control the first processor to execute the feature detection instruction.
[0101] S353, Obtain the feature identifier generated when executing the feature detection instruction.
[0102] S354, parse the feature identifier to obtain the feature information supported by the first processor.
[0103] Specifically, the method for obtaining the feature information supported by the first processor is determined by the result of the first detection. If the first detection determines that the first processor supports feature detection instructions, then the feature detection instructions with faster detection speed are used to obtain the feature information supported by the first processor. Steps S352 to S354 correspond to the running process of the first executable program.
[0104] S355 reads the cpuinfo file.
[0105] S356, parse the cpuinfo file to obtain the feature information supported by the first processor.
[0106] Specifically, if the first detection determines that the first processor does not support the feature detection instruction, then the cpuinfo file is read to obtain the feature information supported by the first processor, thus preventing the first processor from crashing due to forced execution of the feature detection instruction. Steps S355 to S356 correspond to the process of running the third executable program.
[0107] Figure 7 This is a schematic diagram of the structure of a processor characteristic detection device shown in an exemplary embodiment of the present invention.
[0108] like Figure 7 As shown, the apparatus provided in this embodiment includes: an acquisition module 701, a judgment module 702, and a detection module 703; the acquisition module 701 is used to acquire a first characteristic parameter when compiling an application; the judgment module 702 is used to determine, based on the first characteristic parameter, whether a first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports a characteristic detection instruction; the detection module 703 is used to generate a first executable file based on the application source code and the judgment result when the first detection is not required and it is clear that the first processor supports the characteristic detection instruction, wherein the first executable file includes binary code that directly uses the characteristic detection instruction to obtain processor characteristic information; the detection module is further used to generate a second executable file based on the application source code and the judgment result when the first detection is required, so that the first processor determines whether the first processor supports the characteristic detection instruction and obtains processor characteristic information based on the determination result when running the second executable file.
[0109] Furthermore, the detection module is also used for:
[0110] When the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result. The third executable file includes binary code that reads the cpuinfo file to obtain processor feature information.
[0111] Furthermore, the detection module is specifically used for:
[0112] If it is determined that the first processor supports the feature detection instruction, then the feature detection instruction is used to obtain processor feature information;
[0113] If it is determined that the first processor does not support the feature detection instruction, then the cpuinfo file is read to obtain processor feature information.
[0114] Furthermore, such as Figure 8 As shown, the detection module 703 includes:
[0115] The first control unit 7031 is configured to control the first processor to run the first executable file, so as to execute the feature detection instruction when running the first executable file;
[0116] The acquisition unit 7032 is used to acquire the feature identifier generated when the feature detection instruction is executed;
[0117] The first parsing unit 7033 is used to parse the feature identifier to obtain feature information supported by the first processor.
[0118] Furthermore, the detection module includes: a second control unit 7034, used to control the first processor to run the third executable file;
[0119] The reading unit 7035 is used to read the cpuinfo file when the first processor runs the third executable file;
[0120] The second parsing unit 7036 is used to parse the cpuinfo file to obtain feature information supported by the first processor.
[0121] For a detailed description of the functions of each module in this embodiment, please refer to the description in the embodiments of the method; it will not be elaborated here.
[0122] Figure 9 This is a schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present invention. For example... Figure 9 As shown, the electronic device 900 provided in this embodiment includes at least one processor 901 and a memory 902. The processor 901 and the memory 902 are connected via a bus 903.
[0123] In a specific implementation, at least one processor 901 executes computer execution instructions stored in the memory 902, causing at least one processor 901 to execute the processor characteristic detection method in the above method embodiment.
[0124] The specific implementation process of processor 901 can be found in the above method embodiments, and its implementation principle and technical effect are similar. It will not be repeated here.
[0125] In the above Figure 9 In the illustrated embodiments, it should be understood that the processor can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), etc. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the method disclosed in this invention can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules within the processor.
[0126] The memory may include high-speed RAM, and may also include non-volatile storage (NVM), such as at least one disk storage.
[0127] The bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of illustration, the buses shown in the accompanying drawings are not limited to a single bus or a single type of bus.
[0128] Another embodiment of this application provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the processor characteristic detection method in the above method embodiment.
[0129] The aforementioned computer-readable storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0130] An exemplary readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the readable storage medium. Of course, the readable storage medium can also be a component of the processor. The processor and the readable storage medium can reside in an Application Specific Integrated Circuit (ASIC). Alternatively, the processor and the readable storage medium can exist as discrete components in the device.
[0131] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0132] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for detecting the characteristics of a processor, characterized in that, include: When compiling the application, a first characteristic parameter is obtained. The first characteristic parameter is set in the processor and is used to identify whether the corresponding processor explicitly supports the characteristic detection instruction. Based on the first characteristic parameter, it is determined whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction. When the first detection is not required and it is clear that the first processor supports the feature detection instruction, a first executable file is generated based on the application source code and the judgment result. The first executable file includes binary code that uses the feature detection instruction to obtain processor feature information. When the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result. The third executable file includes binary code for reading the cpuinfo file to obtain processor feature information. When the first detection is required, a second executable file is generated based on the application source code and the judgment result, so that the first processor can determine whether the first processor supports the feature detection instruction when running the second executable file; When the first processor supports the feature detection instruction, the feature detection instruction is used to obtain processor feature information; when the first processor does not support the feature detection instruction, the cpuinfo file is read to obtain processor feature information.
2. The method according to claim 1, characterized in that, The step of obtaining processor characteristic information using the characteristic detection instruction includes: Control the first processor to run the first executable file, so as to execute the feature detection instruction when running the first executable file; Obtain the feature identifier generated when the feature detection instruction is executed; The feature identifier is parsed to obtain the feature information supported by the first processor.
3. The method according to claim 1 or 2, characterized in that, The process of reading the cpuinfo file to obtain processor characteristic information includes: The first processor is controlled to run the third executable file, so that the cpuinfo file is read and parsed when the third executable file is run to obtain feature information supported by the first processor.
4. A processor characteristic detection device, characterized in that, include: The acquisition module is used to acquire a first characteristic parameter when compiling the application. The first characteristic parameter is set in the processor and is used to identify whether the corresponding processor explicitly supports the characteristic detection instruction. The judgment module is used to determine, based on the first characteristic parameter, whether the first processor running the application needs to perform a first detection, wherein the first detection is to detect whether the first processor supports the characteristic detection instruction. The detection module is used to generate a first executable file based on the application source code and the judgment result when the first detection is not required and it is clear that the first processor supports the feature detection instruction. The first executable file includes binary code that directly uses the feature detection instruction to obtain processor feature information. When the first detection is not required and it is clear that the first processor does not support the feature detection instruction, a third executable file is generated based on the application source code and the judgment result. The third executable file includes binary code for reading the cpuinfo file to obtain processor feature information. The detection module is also used to generate a second executable file based on the application source code and the judgment result when the first detection is required, so that the first processor can determine whether the first processor supports the feature detection instruction when running the second executable file; When the first processor supports the feature detection instruction, the feature detection instruction is used to obtain processor feature information; when the first processor does not support the feature detection instruction, the cpuinfo file is read to obtain processor feature information.
5. The apparatus according to claim 4, characterized in that, The detection module includes: A first control unit is configured to control the first processor to run the first executable file, so as to execute the feature detection instruction when running the first executable file; The acquisition unit is used to acquire the feature identifier generated when the feature detection instruction is executed; The first parsing unit is used to parse the feature identifier to obtain feature information supported by the first processor.
6. The apparatus according to claim 4 or 5, characterized in that, The detection module includes: a second control unit, used to control the first processor to run the third executable file; The reading unit is configured to read the cpuinfo file when the first processor is running the third executable file; The second parsing unit is used to parse the cpuinfo file to obtain feature information supported by the first processor.
Citation Information
Patent Citations
Memory fault detection method and device
CN110457150A