A Harvard architecture multi-level instruction cache measurement method based on random instruction set
By constructing a basic test unit and dynamically generating a test instruction set, monitoring performance change points, and calculating the size of the multi-level instruction cache of Harvard architecture chips, the problem of being unable to accurately measure the multi-level instruction cache in the existing technology is solved, and the program performance during loop expansion is improved. performance.
Patent Information
- Application Number
- CN202410899383.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-05
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2044-07-05
AI Technical Summary
Existing technology cannot accurately measure the size of the multi-level instruction cache under the Harvard architecture, making it difficult to avoid instruction cache overflow during loop unrolling and affecting program performance.
By constructing a basic test unit, dynamically generating a test instruction set, monitoring performance change points, and calculating the cache size corresponding to significant change points, software calculation of the instruction cache size of each level of the Harvard architecture chip is realized.
It provides quantifiable parameter support for compiler optimization strategies such as loop expansion, avoiding instruction buffer overflow and improving program performance.
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of computer software development, and in particular relates to a Harvard architecture multi-level instruction cache measurement method based on a random instruction set. Background Art
[0002] Loop Unrolling is a program optimization technique that is used to reduce loop control overhead during program execution and improve program execution efficiency. It reduces the number of loop iterations by copying the loop body code multiple times, thereby reducing loop control overhead, reducing the possibility of branch prediction errors, and improving instruction-level parallelism and CPU instruction scheduling capabilities.
[0003] The main difference between the von Neumann architecture and the Harvard architecture lies in the memory layout. In the von Neumann architecture, instructions and data use the same memory and bus structure, while in the Harvard architecture, the instruction memory and data memory are separate and each has an independent bus structure.
[0004] In the von Neumann architecture, since instructions and data share the same storage space, larger loop unrolling may increase the code size, thereby increasing the pressure on the instruction cache. If the loop is over-unrolled, the instruction cache hit rate may decrease, which will reduce program performance. Therefore, it is necessary to balance the code size and execution efficiency when unrolling the loop. In the Harvard architecture, instructions and data are separated, and the code size increased by loop unrolling only affects the instruction memory. This allows the loop to be unrolled to a greater extent without affecting the data cache, especially when the instruction memory capacity is large. The Harvard architecture can reduce the instruction bandwidth requirements caused by loop unrolling through independent instruction paths, so it is suitable for more aggressive loop unrolling. However, no matter which architecture is used, it is necessary to avoid the instruction cache overflow caused by over-unrolling, because after the overflow, the instructions need to be read from the memory, which will cause a sharp drop in performance.
[0005] Therefore, the size of the instruction cache needs to be considered when loop unrolling. For Harvard architecture processors, since their data cache and instruction cache are separate, the method of measuring the size of the instruction cache, especially the size of the multi-level instruction cache, is significantly different from that of the unified cache. The existing method cannot accurately measure the size of the multi-level instruction cache under the Harvard architecture. Summary of the invention
[0006] In view of this, the present invention provides a Harvard architecture multi-level instruction cache calculation method based on a random instruction set, which realizes the software calculation of the instruction cache size of each level of the Harvard architecture chip.
[0007] The present invention provides a Harvard architecture multi-level instruction cache measurement method based on a random instruction set, which specifically includes the following steps:
[0008] Step 1: construct a basic test unit according to the assembly instructions supported by the chip under test. The basic test unit consists of multiple assembly instructions that are only executed in registers, do not involve memory access, and are serialized. The last assembly instruction is a jump instruction. The size of the basic test unit is recorded as the basic length.
[0009] Step 2, start the test, allocate instruction space in the instruction address space, record it as code space, the size of the code space is an integer multiple of the basic length and is greater than the sum of the sizes of instruction caches at all levels; allocate data space in the data address space to save the jump address array, each member in the jump address array is the jump target address of the jump instruction in the basic test unit; randomly change the basic test unit, set the jump target address therein, and then use the basic test unit to fill the code space, and the data in the code space is the test instruction set to be executed;
[0010] Step 3, execute the test instruction set, and obtain the execution time and the performance of the chip under test as the monitoring results; after the execution is completed, determine the significant change points of the performance of the chip under test according to the monitoring results, obtain the executed code blocks corresponding to the significant change points, calculate the cache size occupied by the executed code blocks according to the executed code blocks, and obtain the corresponding relationship between the significant change points and the cache sizes. These cache sizes are the instruction cache sizes of each level of the chip under test.
[0011] Furthermore, there is no mutually dependent logical relationship between the assembly instructions in the basic test unit.
[0012] Furthermore, all computing tasks being executed in the chip under test are terminated before starting the test.
[0013] Furthermore, the step 2 of filling the code space with the basic test unit comprises the following steps:
[0014] Step 2.1, the code space contains multiple code blocks with the basic length, and the value of the current code block ID is set to 0;
[0015] Step 2.2, search for an unfilled code block, if it exists, use the code block ID to update the current code block ID, if it does not exist, end this process; randomly change the execution order of the assembly instructions in the basic test unit to form the current basic test unit;
[0016] Step 2.3, randomly generate a jump block ID, the jump block ID satisfies that the product of the absolute value of the difference between the jump block ID and the current code block ID and the basic length is not less than a threshold, and the code block corresponding to the jump block ID is not filled; the jump block ID corresponds to the code block to be jumped, and the starting address of the code block to be jumped is used as the jump target address of the jump instruction in the current basic test unit;
[0017] Step 2.4: Use the current basic test unit to fill the code block corresponding to the current code block ID, and mark the code block as filled; execute step 2.2.
[0018] Furthermore, the threshold is the size of the L1 instruction cache of the chip under test.
[0019] Furthermore, in step 2.3, the starting address of the code block to be jumped is saved in the jump address array, and its index in the jump address array is used as the jump target address of the jump instruction in the current basic test unit.
[0020] Furthermore, before executing the test instruction set, a plurality of loop reading operations on the jump address array are performed.
[0021] Furthermore, a plurality of no-operation instructions are added to the basic test unit. Beneficial Effects
[0022] The present invention selects specific assembly instructions for the chip under test to construct a basic test unit, and then randomly and dynamically generates a test instruction set according to the test process in the basic test unit. By monitoring the execution process of the test instruction set, significant change points in the performance of the chip under test are obtained. The cache size of the executed code block corresponding to the significant change point is the instruction cache size of the corresponding level, which realizes the software calculation of the instruction cache size of each level of the Harvard architecture chip, and provides quantifiable parameter support for the formulation of compiler optimization strategies such as loop unrolling. DETAILED DESCRIPTION
[0023] The present invention is described in detail with reference to the following embodiments.
[0024] In order to improve processing efficiency, DSPs or processors have established a variety of processing mechanisms, such as prefetching, cache replacement policies, etc. However, these mechanisms may affect the test results when testing the instruction cache. Therefore, when testing the cache, you should understand the above mechanisms of the processor as much as possible and consider the impact of these factors in the test. For example, the prefetch mechanism may cause some data that is not directly accessed to be loaded into the cache in advance, while the cache replacement policy determines which data will be replaced when the cache is full. When constructing a test program to test the cache, a special design is required to detect the boundaries of the cache hierarchy from performance changes to obtain the size of the instruction cache, while minimizing the impact of the above mechanisms on the test results.
[0025] The present invention provides a Harvard architecture multi-level instruction cache measurement method based on a random instruction set, the core idea of which is: selecting specific assembly instructions for a chip under test to construct a basic test unit, and then randomly and dynamically generating a test instruction set during the test according to the basic test unit, and obtaining significant change points in the performance of the chip under test by monitoring the execution process of the test instruction set, and the cache size of the executed code block corresponding to the significant change point is the instruction cache size of the corresponding level.
[0026] The present invention provides a Harvard architecture multi-level instruction cache measurement method based on a random instruction set, which specifically includes the following steps:
[0027] Step 1: construct a basic test unit of the chip under test according to the assembly instructions supported by the chip under test, wherein the basic test unit is composed of a plurality of assembly instructions that are executed only in registers, do not involve memory access, and are serialized, wherein the last assembly instruction is a jump instruction, and the size of the basic test unit is recorded as the basic length. In the present invention, the chip under test is a Harvard architecture chip.
[0028] In order to further counter the prefetch mechanism and reduce its influence on the test results, assembly instructions that do not have a mutually dependent logical relationship are selected to construct the basic test unit, thereby increasing the irregularity of the assembly instructions and making it difficult for the system to predict their instruction execution mode. Specifically, assembly instructions such as arithmetic operations, logical pre-operations, and bit operations can be selected.
[0029] Step 2: Further, before executing the test, in order to ensure the validity of the test results, all computing tasks being executed in the chip under test are terminated.
[0030] Step 3, start the test, and dynamically build the test instruction set at runtime. Specifically, start the test, allocate instruction space in the instruction address space, record the instruction space as the code space, record the starting address of the code space as the first address of the code space, the size of the code space is N times the basic length and greater than the sum of the sizes of instruction caches at all levels, which is equivalent to dividing the code space into N code blocks, and recording the size of the code space as the code space length; allocate data space in the data address space, the data space is used to store the jump address array, and each member in the jump address array is the jump target address of the last assembly instruction in the basic test unit; fill the code space by dynamically and randomly writing the basic test unit into N code blocks, and the data in the final code space is the test instruction set to be executed. Wherein, N is an integer.
[0031] Since the test instruction set is generated in real time, the instruction prefetching mechanism is difficult to predict in advance. Even if prefetching is implemented, the prefetched data content may become invalid before being executed.
[0032] In order to further reduce the correlation between instructions in the test instruction set to avoid the influence of the prefetch mechanism on the test results, the dynamic random generation method of the test instruction set in the present invention can also be:
[0033] Step 3.1: Set the value of the current code block ID to 0.
[0034] Step 3.2, search for an unfilled code block in the code space. If it exists, use the code block ID to update the current code block ID. If it does not exist, it means that the code space is filled, the test instruction set is generated, and this process ends; randomly change the execution order of the assembly instructions in the basic test unit to form the current basic test unit.
[0035] Step 3.3, randomly generate a jump block ID, satisfying that the product of the absolute value of the difference between the jump block ID and the current code block ID and the basic length is not less than the threshold, and the code block corresponding to the code block ID equal to the jump block ID is not filled; the code block corresponding to the jump block ID is the code block to be jumped, and the starting address of the code block to be jumped is used as the jump target address of the jump instruction in the current basic test unit. Among them, the threshold is generally the size of the first-level instruction cache of the chip under test, so the jump target address is larger than the first-level instruction cache, which can effectively avoid the pre-fetch mechanism from acting on the jump block. The starting address of the code block to be jumped is the product of the code block ID to be jumped and the basic length and the sum of the first address of the code space.
[0036] Furthermore, the present invention saves the jump target address into the jump address array, records its index in the jump address array as the jump target index, and uses the jump target index as the jump target address of the jump instruction in the current basic test unit. In this way, the indirect jump of the target address is realized, which can effectively prevent the system from reading the address first and then triggering the pre-fetch mechanism.
[0037] Step 3.4: Use the current basic test unit to fill the code block corresponding to the current code block ID, and mark the code block as filled.
[0038] Step 3.5: increment the jump target index by 1 and execute step 3.2.
[0039] Furthermore, since the data cache and the instruction cache are independent under the Harvard architecture, in order to minimize the impact of the last instruction in the basic test unit accessing the jump address array on the measured instruction time, it is necessary to ensure that the jump address array is completely stored in the data cache. Based on this consideration, the present invention first performs multiple loop reading operations on the jump address array before executing the test instruction set, for example, performing 100 loop readings.
[0040] Furthermore, in order to align the test instruction cache, a plurality of no-operation instructions are added to the basic test unit.
[0041] Step 4, execute the test instruction set, and use the hardware performance monitor to obtain the execution time of the test instruction set and the performance of the chip under test during the test, and use the instruction execution time and the performance of the chip under test as the monitoring results; when the test instruction set is executed, analyze the cache hit rate and miss rate during the execution of the test instruction set according to the monitoring results, and determine the significant change points in the performance of the chip under test, such as the change points where the instruction execution time suddenly increases, etc., obtain the executed code blocks corresponding to the significant change points, calculate the cache size occupied by the executed code blocks according to the executed code blocks, and obtain the corresponding relationship between the significant change points and the cache size, wherein the cache size corresponding to the significant change point of the first performance degradation is the first-level instruction cache size, and so on, to obtain the instruction cache sizes of all levels such as the second level and the third level.
[0042] In order to further improve the reliability of the results, it is possible to return to step 3 multiple times to construct and execute the test instruction set to obtain multiple instruction cache size values, and take the average value of the multiple test results as the final result.
[0043] In order to improve the accuracy of the test results, in the best case, the chip under test is a chip whose executable instructions can be accurately restricted, such as a computing-specific chip, such as a DSP, NPU, etc. General-purpose chips, such as CPUs, are usually affected by scheduling instructions such as interrupts in the operating system, that is, when the chip under test is a general-purpose chip, in addition to executing the instructions set by the present invention, it will also execute instructions specified by the operating system. The execution of these operating system-specified instructions will affect the measurement results of the instruction cache in the general-purpose chip by the present invention. In addition, in actual situations, there are relatively few Harvard architecture CPUs. Computing-specific chips usually only execute instructions involved in the currently scheduled computing tasks, and the operating system will not specify them to execute other instructions.
[0044] To prevent the system from crashing, the register states are restored at the end of the test. Example
[0045] In this embodiment, a DSP is used as a chip under test, and a Harvard architecture multi-level instruction cache measurement method based on a random instruction set provided by the present invention is used to measure the multi-level instruction cache of the DSP, which specifically includes the following steps:
[0046] S1. Eliminate interference: End all computing tasks being executed in the chip under test.
[0047] S2. Build the basic test unit of the chip under test. The program design of the basic test unit mainly considers two factors: one is that it does not involve memory data reading and writing operations, that is, reducing the impact of data cache hits on the test instruction execution time; the other is to avoid the impact of the prefetch mechanism, that is, the test instructions executed by the DSP cannot be prefetched into the instruction cache.
[0048] This embodiment takes the assembly pseudo code of TI's TMS320C6000 series as an example, covering a variety of assembly instructions that do not involve memory operations. Assuming that R0-R7 are general registers, the code is:
[0049] NOP; multiple no-ops for alignment testing instruction cache
[0050] NOP
[0051] …
[0052] MVK .S1 0,A1 ; Move immediate value 0 to register A1
[0053] MVK .S2 1,B1 ; Move immediate value 1 to register B1
[0054] ADD .L1 A1,B1,A2 ; A2 = A1 + B1
[0055] SUB .L2 A2,B1,A3 ; A3 = A2 - B1
[0056] MPY .M1 A1,B1,A4 ; A4 = A1 * B1
[0057] ABS .S1 A3,A5 ; A5 = |A3|
[0058] NEG .L2 A4,A4 ; A4 = -A4
[0059] MV .L1 A1,B2 ; B2 = A1
[0060] NOT .S2 B2,B2 ; B2 = ~B2
[0061] AND .S1 A1,B2,A1 ; A1 = A1 & B2
[0062] OR .L1 A1,A5,A1 ; A1 = A1 | A5
[0063] XOR .L2 A1,B1,A1 ; A1 = A1 ^ B1
[0064] ADDAB .L2 A2,B1 ; A2 = A2 + B1 accumulator based
[0065] SUBAB .L1 A2,B1 ; A2 = A2 - B1 accumulator based
[0066] MAX .S1 A1,A4,A1 ; A1 = max(A1, A4)
[0067] MIN .S2 A1,B1,A2 ; A2 = min(A1, B1)
[0068] SAT .L1 A2,A2 ; A2 = saturate(A2) to the permissible range
[0069] ROL .S1 A1,1,A3 ; A3 = rotate left A1 by 1 bit
[0070] CLR A1 ; Clear register A1
[0071] B .L2 target_label ; Long jump to label target_label
[0072] All instructions in the above code are executed in registers and the register status is restored at the end, and no memory operations are involved. The code uses a serialized instruction stream to execute instructions in a linear manner, effectively avoiding parallel execution of instructions and making it more difficult for the prefetch mechanism to determine effective prefetch opportunities.
[0073] In addition, the irregularity of the instruction working set is increased in the above code. By designing irregular and unpredictable memory access patterns, various instructions that do not involve memory access, such as arithmetic operations, logical pre-operations, bit operations, etc., are covered to prevent the prefetch mechanism from predicting the next access, thereby reducing the impact on the test results.
[0074] The last long jump instruction in the above code forces the instruction prefetch logic to reposition the prefetch window, thereby reducing the prefetch of continuous code streams.
[0075] S3, based on the basic test unit constructed in S2, randomly generates a test instruction set by dynamically modifying it at runtime, as follows:
[0076] S3.1. The size of the basic test unit is recorded as unitCodeSize, and the instructions in the basic test unit are the instructions generated after compilation.
[0077] S3.2. Allocate an instruction space in the instruction address space. The instruction space is a code space denoted as codeBuffer, and the starting address of the instruction space is codeBufferAddress. The instruction space is used to store the test instruction set. The size of the instruction space obviously exceeds the sum of the sizes of instruction caches at all levels. For example, the size of the instruction space is set to 128M, which is an integer multiple of unitCodeSize. The size of the instruction space is the length of the code space denoted as codeSize.
[0078] S3.3. Allocate a jump address array in the data address space. Each member of the array stores the jump target address of the last jump instruction in the basic test unit. The jump address array is recorded as arrayJumpTarget.
[0079] S3.4. Dynamically generate a test instruction set in codeBuffer. The generation method is as follows:
[0080] S3.4.1. Record the current code block ID as the current blockIndex, and set the value of the current blockIndex to 0;
[0081] S3.4.2. Find an unfilled code block. If it exists, record the index of the code block as currentBlockNo. If it does not exist, it means that (codeSize / unitCodeSize) code blocks are all filled and exit this process; randomly swap the order of 10 instructions between the initial line and the end line in the basic test unit to obtain the current basic test unit;
[0082] S3.4.3. Randomly generate a number and record it as jumpBlockNo, which satisfies the following formula:
[0083] |jumpBlockNo-currentBlockNo|*unitCodeSize > 4K
[0084] And the code block corresponding to jumpBlockNo is not filled, save the result of (jumpBlockNo*unitCodeSize+codeBuffer) to arrayJumpTarget[blockIndex], where blockIndex is the index in the jump address array, and modify the jump target address of the last instruction of the current code block to arrayJumpTarget[blockIndex];
[0085] arrayJumpTarget [blockIndex] is used as the jump target address to form an indirect jump. The jump target address saved by arrayJumpTarget [blockIndex] is greater than 4K, which ensures that the prefetch mechanism cannot act on the current code block, which means that the current code block is basically impossible to be prefetched. Indirect jumps read the address first, and then read the data according to the address to prevent the data from being prefetched.
[0086] S3.4.4, mark the current code block as filled;
[0087] S3.4.5. After incrementing blockIndex by 1, execute S3.4.2.
[0088] S4. Read arrayJumpTarget in a loop, and execute 100 times, thereby ensuring that arrayJumpTarget is saved in the data cache.
[0089] S5. Start executing the test instruction set and observe the performance degradation points step by step: Start with a smaller instruction working set, and gradually increase the instruction working set until performance degradation occurs. Performance degradation usually indicates that the instruction cache at the current level can no longer fully accommodate all execution instructions. Record the performance change point and the cache size occupied by the executed instruction working set corresponding to the performance change point. During the actual execution process, when the prefetch mechanism cannot work effectively, such as when the instruction working set size exceeds the cache capacity or the cache replacement strategy begins to frequently replace instructions, the program performance will be significantly reduced, usually manifested as an increase in the cache miss rate. In other words, a significant change in performance usually indicates that the test process has reached the boundary of the instruction cache capacity.
[0090] In this embodiment, a high-precision clock is used to monitor the execution time or instruction cycle count of the test instruction set, and then a hardware performance counter or other performance analysis tool is used to monitor the cache hit rate and miss rate during program execution.
[0091] S6. After determining the size of the first-level instruction cache, continue to expand the instruction working set. At this time, the first-level instruction cache will overflow. Use the same method to detect the capacity of the second-level instruction cache, and so on, until the measurement of all levels of cache is completed.
[0092] S8. Repeat the above process multiple times, calculate the average value of all the results, and use each average value as the size of all levels of cache of the DSP.
[0093] In summary, the above are only preferred embodiments of the present invention and are not intended to limit the protection scope of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A Harvard architecture multi-level instruction cache measurement method based on a random instruction set, characterized in that: The specific steps include: Step 1: construct a basic test unit according to the assembly instructions supported by the chip under test. The basic test unit consists of multiple assembly instructions that are only executed in registers, do not involve memory access, and are serialized. The last assembly instruction is a jump instruction. The size of the basic test unit is recorded as the basic length. Step 2, start the test, allocate instruction space in the instruction address space, record it as code space, the size of the code space is an integer multiple of the basic length and is greater than the sum of the sizes of instruction caches at all levels; allocate data space in the data address space to save the jump address array, each member in the jump address array is the jump target address of the jump instruction in the basic test unit; randomly change the basic test unit, set the jump target address therein, and then use the basic test unit to fill the code space, and the data in the code space is the test instruction set to be executed; Step 3, execute the test instruction set, and obtain the execution time and the performance of the chip under test as the monitoring results; after the execution is completed, determine the significant change points of the performance of the chip under test according to the monitoring results, obtain the executed code blocks corresponding to the significant change points, calculate the cache size occupied by the executed code blocks according to the executed code blocks, and obtain the corresponding relationship between the significant change points and the cache sizes. These cache sizes are the instruction cache sizes of each level of the chip under test; Filling the code space with the basic test unit in step 2 includes the following steps: Step 2.1, the code space contains multiple code blocks with the basic length, and the value of the current code block ID is set to 0; Step 2.2, search for an unfilled code block, if it exists, use the code block ID to update the current code block ID, if it does not exist, end this process; randomly change the execution order of the assembly instructions in the basic test unit to form the current basic test unit; Step 2.3, randomly generate a jump block ID, the jump block ID satisfies that the product of the absolute value of the difference between the jump block ID and the current code block ID and the basic length is not less than a threshold, and the code block corresponding to the jump block ID is not filled; the jump block ID corresponds to the code block to be jumped, and the starting address of the code block to be jumped is used as the jump target address of the jump instruction in the current basic test unit; Step 2.4: Use the current basic test unit to fill the code block corresponding to the current code block ID, and mark the code block as filled; execute step 2.
2.
2. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: There is no mutually dependent logical relationship between the assembly instructions in the basic test unit.
3. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: Before starting the test, all computing tasks currently being executed in the chip under test are terminated.
4. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: The threshold is the size of the L1 instruction cache of the chip under test.
5. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: In the step 2.3, the starting address of the code block to be jumped is saved in the jump address array, and its index in the jump address array is used as the jump target address of the jump instruction in the current basic test unit.
6. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: Before executing the test instruction set, a loop reading operation of the jump address array is performed multiple times.
7. The Harvard architecture multi-level instruction cache measurement method according to claim 1, characterized in that: Add multiple no-operation instructions in the basic test unit.
