A memory segment allocation method, apparatus and electronic device
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-11-28
- Publication Date
- 2026-08-14
AI Technical Summary
[0041]As can be seen from the above technical solutions, this application provides a memory segment allocation method and apparatus. The method includes: analyzing a target code file to obtain an information configuration file; determining weight parameters of target information to be allocated to a memory segment based on the information configuration file; determining the contribution rate of the target information in each memory segment based on the weight parameters; and determining a target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result. This application can analyze the characteristics of the information to be stored and combine the efficiency of the microcontroller for different operations on different memory segments to adaptively allocate memory segments to the information to be stored, thereby improving the accuracy and efficiency of memory segment allocation.
Smart Images

Figure CN117407175B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of data processing technology, and more specifically to a memory segment allocation method, apparatus, and electronic device. Background Technology
[0002] In recent years, automotive electronics technology has developed rapidly, with increasingly complex logic and continuously increasing functions, which has raised the requirements for chips. Microcontrollers have also evolved from single-core to multi-core. The emergence of multi-core microcontrollers has led to the application of more memory segments (dividing the system chip's memory space into multiple memory segments). Each core has its own private memory segment and shared memory segment, and there are differences in the speed at which the microcontroller core accesses different memory segments. There are also differences in the speed at which the same core accesses the same memory segment. Even for the same memory segment, the speed of read and write operations on that area is different.
[0003] During the development of business systems based on microcontrollers, developers place global variables and code in different memory segments according to their development habits. During operation, the microcontroller continuously retrieves system instructions and data from different memory segments to complete corresponding logical operations and rewrites variables according to actual needs. The shorter the access time during the entire operation of the microcontroller, the more timely the microcontroller response. At the same time, the lower the load on the microcontroller, the better it can adapt to more complex logic. How to allocate memory segments for global variables and code has become an urgent problem to be solved. Summary of the Invention
[0004] In view of this, this application provides a memory segment allocation method, apparatus and electronic device, which realizes the adaptive allocation of functions and variables in corresponding memory segments, thereby improving the accuracy and efficiency of memory segment allocation.
[0005] To achieve the above objectives, the following solution is proposed:
[0006] A memory segment allocation method, comprising:
[0007] The target code file is analyzed to obtain an information configuration file, which includes at least a first file, a second file, and a third file. The first file is used to store the functions called by the microcontroller when executing the target code file and the frequency information of each function being called. The second file is used to store the variables read by the microcontroller when executing the target code file and the frequency information of each variable being read. The third file is used to store the variables modified by the microcontroller when executing the target code file and the frequency information of each variable being modified.
[0008] Based on the information configuration file, weight parameters of the target information to be allocated to the memory segment are determined. The target information includes at least functions and / or variables to be allocated to the memory segment. The weight parameters include a first weight parameter and a second weight parameter. The first weight parameter represents the application rate of each processing core of the microcontroller to the memory segment, and the second weight parameter represents the processing rate of each processing core of the microcontroller to the functions or variables.
[0009] Based on the weight parameters, the contribution rate of the target information in each memory segment is determined, and the contribution rate represents the probability parameter that the target information can be stored in the memory segment;
[0010] Based on the contribution rate of the target information in each memory segment, the target memory segment allocation result is determined so that the target information is stored in a memory segment that matches the target memory segment allocation result.
[0011] Optionally, the method further includes:
[0012] The target code file is analyzed to determine whether there are functions called via pointers;
[0013] If there are functions called via pointers, a fourth file of the information configuration file is generated. The fourth file is used to store the pointer functions and the number of times the pointer functions are called when the microcontroller executes the target code file.
[0014] Optionally, determining the weight parameters of the target information to be allocated to the memory segment based on the information configuration file includes:
[0015] Based on the information configuration file, first information is obtained, which includes a function call relationship tree, the number of times each function calls other functions, and the frequency of function reading and writing variables. The call relationship tree represents the call relationship between functions.
[0016] Based on the first information, a first weight parameter is determined. The first weight parameter includes the read weight sub-parameter of each processing core of the microcontroller relative to the memory segment, the write weight sub-parameter of each processing core relative to the memory segment, and the access speed weight sub-parameter of each processing core relative to the memory segment acquisition instruction.
[0017] Based on the information configuration file, the access frequency of the main function of each processing core cycle within the cycle is determined, and based on the access frequency, the second weight parameter is determined.
[0018] Optionally, determining the target memory segment based on the contribution rate of the target information in each memory segment includes:
[0019] Based on the contribution rate of the target information in each memory segment, the initial memory segment allocation result is determined;
[0020] In response to the replacement of the stored information in each memory segment of the initial memory segment allocation result, the contribution rate of the stored information in the replaced memory segment is calculated, and the stored information belongs to the target information;
[0021] Based on the contribution rate of the stored information in the replaced memory segment, the initial memory segment allocation result is adjusted to obtain the target memory segment allocation result.
[0022] Optionally, the target information includes at least one sub-information, wherein determining the initial memory segment allocation result based on the contribution rate of the target information in each memory segment includes:
[0023] The contribution rate of each sub-information in each memory segment is sorted to determine the sorting information of the memory segment corresponding to each sub-information. The sorting information is determined based on the contribution rate from low to high.
[0024] The memory segment with the first sorting position is selected as each sub-information of the memory segment. Based on the contribution rate of each sub-information in the memory segment with the first sorting position, the sorting information of each sub-information is determined.
[0025] Based on the sorting information of the memory segments and the sorting information of each sub-information, the initial memory segment allocation result is determined.
[0026] Optionally, determining the initial memory segment allocation result based on the sorting information of the memory segment and the sorting information of each sub-information includes:
[0027] Based on the sorting information of each sub-information, the first sorting position sub-information is determined. If the memory segment of the first sorting position meets the conditions for storing the first sorting position sub-information, the first sorting position sub-information is stored in the memory segment of the first sorting position.
[0028] After removing the first sorting position sub-information from the target information to be stored, the sorting information of the memory segment and the sorting information of the sub-information are recalculated until a corresponding memory segment is determined for each sub-information.
[0029] Optionally, adjusting the initial memory segment allocation result based on the contribution rate of the stored information in the replaced memory segment to obtain the target memory segment allocation result includes:
[0030] If the contribution rate of the stored information in the first memory segment before replacement is greater than the contribution rate in the second memory segment after replacement, the stored information is stored in the second memory segment.
[0031] If the contribution rate of the stored information in the first memory segment before replacement is not greater than the contribution rate of the second memory segment after replacement, determine whether the stored information is the last stored information in the second memory segment. If not, replace the memory segment corresponding to the next stored information until the replacement and adjustment of each stored information is completed, and obtain the target memory segment allocation result.
[0032] A memory segment allocation device, comprising:
[0033] An analysis unit is used to analyze the target code file and obtain an information configuration file. The information configuration file includes at least a first file, a second file, and a third file. The first file is used to store the functions called by the microcontroller when executing the target code file and the frequency information of each function being called. The second file is used to store the variables read by the microcontroller when executing the target code file and the frequency information of each variable being read. The third file is used to store the variables modified by the microcontroller when executing the target code file and the frequency information of each variable being modified.
[0034] The first determining unit is used to determine the weight parameters of the target information to be allocated to the memory segment based on the information configuration file, wherein the target information includes at least the functions and / or variables to be allocated to the memory segment, and the weight parameters include a first weight parameter and a second weight parameter, wherein the first weight parameter represents the application rate of each processing core of the microcontroller to the memory segment, and the second weight parameter represents the processing rate of each processing core of the microcontroller to the functions or variables.
[0035] The second determining unit is used to determine the contribution rate of the target information in each memory segment based on the weight parameters, wherein the contribution rate represents the probability parameter that the target information can be stored in the memory segment;
[0036] The third determining unit is used to determine the target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result.
[0037] An electronic device, comprising a memory and a processor;
[0038] The memory is used to store programs;
[0039] The processor is configured to execute the program to implement the memory segment allocation method as described in any of the preceding embodiments.
[0040] A storage medium having a computer program stored thereon, the computer program being executed by a processor using the memory segment allocation method described in any of the preceding claims.
[0041] As can be seen from the above technical solutions, this application provides a memory segment allocation method and apparatus. The method includes: analyzing a target code file to obtain an information configuration file; determining weight parameters of target information to be allocated to a memory segment based on the information configuration file; determining the contribution rate of the target information in each memory segment based on the weight parameters; and determining a target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result. This application can analyze the characteristics of the information to be stored and combine the efficiency of the microcontroller for different operations on different memory segments to adaptively allocate memory segments to the information to be stored, thereby improving the accuracy and efficiency of memory segment allocation. Attached Figure Description
[0042] To more clearly illustrate the technical solutions in the embodiments of this application 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 embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.
[0043] Figure 1 A flowchart illustrating a memory segment allocation method provided in an embodiment of this application;
[0044] Figure 2 This application provides an example diagram of a Core 0 function call and variable read / write operation.
[0045] Figure 3 An initial allocation flowchart for memory segment allocation for a function is provided for an embodiment of this application;
[0046] Figure 4 A flowchart for fine-tuning memory segment allocation for functions is provided for embodiments of this application;
[0047] Figure 5 An initial allocation flowchart for allocating memory segments for a Constant is provided in an embodiment of this application;
[0048] Figure 6 A flowchart for fine-tuning memory segment allocation for the Constant constant is provided for embodiments of this application;
[0049] Figure 7 An initial allocation flowchart for memory segment allocation for non-Const variables is provided for embodiments of this application;
[0050] Figure 8 A flowchart for fine-tuning memory segment allocation for non-Const variables is provided for embodiments of this application;
[0051] Figure 9 This is a schematic diagram of a memory segment allocation device provided in an embodiment of this application. Detailed Implementation
[0052] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0053] This application provides a memory segment allocation method that can be applied to a microcontroller when processing information, allocating the information it accesses or calls to the corresponding memory segment. Because it can achieve accurate memory segment allocation, the microcontroller can obtain instructions or data from the corresponding memory segment more efficiently during operation.
[0054] To facilitate the explanation of the embodiments of this application, the relevant terms in the embodiments of this application will now be explained.
[0055] Memory segments refer to contiguous memory-mapped address segments with uniform characteristics in a microcontroller.
[0056] Global variables: A type of data in embedded software that can be read and written by all cores in the chip;
[0057] Microcontroller system instructions: These refer to a set of binary instructions stored inside the microcontroller, used to control the microcontroller to perform various operations. These instructions can be written into programs by programmers using assembly language or other methods, loaded into the microcontroller's memory, and executed sequentially by the processor.
[0058] See Figure 1 This is a flowchart illustrating a memory allocation method provided in an embodiment of this application. The method may include the following steps:
[0059] S101. Analyze the target code file to obtain the information configuration file.
[0060] The target code files generated by the embedded software compiler can be analyzed using scripts. The compiler's built-in functions can be used to perform readability conversions on the target files. The compiler's built-in instructions can be used to obtain explicit identifiers for function calls and global variable read / write operations within a function. Based on this, an information configuration file can be generated for each function, including a first file, a second file, and a third file. The target code file can be the code file executed by the microcontroller when processing relevant data (such as business data).
[0061] The first file stores the functions called by the microcontroller when executing the target code file, along with the frequency of each function call. The second file stores the variables read by the microcontroller when executing the target code file, along with the frequency of each variable read. For arrays, this information is accurate to the dimension read and the frequency of that dimension read; for structures, it is accurate to the element read and the frequency of that element read. The third file stores the variables modified by the microcontroller when executing the target code file, along with the frequency of each variable modification. For arrays, this information is accurate to the dimension modified and the frequency of that dimension modified; for structures, it is accurate to the element modified and the frequency of that element modified.
[0062] Furthermore, the information configuration file also includes a fourth file, which is for scenarios where functions are called via pointers. The method further includes: analyzing the target code file to determine whether there are functions called via pointers; if there are functions called via pointers, generating the fourth file of the information configuration file, which is used to store the pointer functions and the number of times the pointer functions are called when the microcontroller executes the target code file.
[0063] S102. Based on the information configuration file, determine the weight parameters of the target information to be allocated to the memory segment.
[0064] In the embodiments of this application, the target information includes at least the functions and / or variables to be allocated to the memory segment. That is, the memory segment can store functions or variables (also known as global variables, or non-constant variables, etc.), or a combination of both.
[0065] For automotive electronic systems, during user startup, all functions except terminal functions are called directly or indirectly within the main cycle function. Information related to the main cycle function requires developer input and cannot be automatically obtained from analysis files unless the main cycle function follows a specific naming convention, and its information is embedded within that convention. By analyzing the call frequency of the main cycle function, relying on the function call tree, and the number of times each function calls other functions, the read / write frequency of each function and variable can be obtained, thus deriving weight parameters. These weight parameters include a first weight parameter and a second weight parameter. The first weight parameter characterizes the application rate of each microcontroller processing core to the memory segment, while the second weight parameter characterizes the processing rate of each microcontroller processing core to functions or variables.
[0066] In one implementation, determining the weight parameters of the target information to be allocated to the memory segment based on the information configuration file includes: obtaining first information based on the information configuration file, the first information including a function call tree, the number of times each function calls other functions, and the frequency of function reading and writing variables, wherein the call tree represents the call relationship between functions; determining a first weight parameter based on the first information, the first weight parameter including sub-parameters of read weight of each microcontroller processing core relative to the memory segment, sub-parameters of write weight of each processing core relative to the memory segment, and sub-parameters of access speed of each processing core relative to the memory segment instruction acquisition; determining the access frequency of the main function of each processing core within a period based on the information configuration file, and determining a second weight parameter based on the access frequency.
[0067] The function call tree and the number of times each function calls other functions / variables for reading and writing can be obtained from the first and fourth files in the information configuration file. The weighting coefficients are: P2_ReadCoren_XXX for the memory segment read / write rate of each microcontroller core and the instruction access rate; P2_WriteCoren_XXX for the memory segment write rate; and P2_FuncCoren_XXX for the function or instruction access rate (where XXX represents the memory segment name).
[0068] The weighting parameters corresponding to the reading and writing speeds of functions and variables for each core of the microcontroller are as follows: the weighting parameter for the reading speed of functions is P3_FuncCoren_XXX, the weighting parameter for the reading speed of variables is P3_ReadCoren_XXX, and the weighting parameter for the writing speed of variables is P3_WriteCoren_XXX (XXX represents the variable or function name).
[0069] It should be noted that the weight parameter P2XX is fixed, and its specific value is determined by the microcontroller chip. The specific value can be obtained from the microcontroller's manual. For example, the weight values of the DSPR0 memory segment of Core0 of a certain microcontroller model are as follows:
[0070] P2_ReadCore0DSPR0 = 0;
[0071] P2_WriteCore0DSPR0 = 0;
[0072] P2_FuncCore0DSPR0 = 6.
[0073] The weighting coefficient P3XX is a value related to environmental engineering. It can be obtained from the static analysis stage products, map files, and the access frequency of the main function in each kernel cycle within 1ms. For non-array, non-structure variables, their weight in each kernel (read / write) = the variable's access (read / write) frequency in that kernel; for array variables, their weight in each kernel (read / write) = (access (read / write) frequency of array variable dimension 1 in that kernel + ... + access (read / write) frequency of array variable dimension m in that kernel) (where m represents the total dimension of the array variable) / m; for structure variables, their weight in each kernel (read / write) = (access (read / write) frequency of structure variable element 1 in that kernel + ... + access (read / write) frequency of structure variable element o in that kernel) (where o represents the number of structure elements) / o.
[0074] The access frequency (read / write) of a variable in kernel B = the total number of times the function A0 that reads / writes the variable directly or indirectly is called in the main functions of kernel B within an average of 1 ms * the number of times the variable is read / written in function A0 + ... + the total number of times the function Ap (read / writes the variable) that reads / writes the variable directly or indirectly is called in the main functions of kernel B within an average of 1 ms * the number of times the variable Ap is read / written in that function.
[0075] See Figure 2 This is an example diagram of function calls and variable read / write operations of a single-chip microcomputer processing core Core0 provided in an embodiment of this application. Figure 2 In this process, Core0 calls function A 1 / 10 times and function B 2 / 10 + 1 / 100 times per 1ms on average. d calls function C 1 / 100 times, reads variable a 1*1 / 10 times, reads variable b 1*1 / 10 + 2*2*2 / 10 + 1*6*6 / 100 times, reads variable c 2*3*3 / 10 times, reads variable d 1*2*2 / 100 times, writes to variable a 1*1 / 10 times, writes to variable b 1*2 / 10 + 2*2*4 / 10 times, writes to variable c 0 times, and writes to variable d 0 times. All variables here are neither arrays nor structures.
[0076] They only have read and call permissions for functions, but no modification permissions. Furthermore, the execution of functions is based on their logical conditions, unlike arrays or structures which are called locally. Therefore, each function is treated as a separate whole.
[0077] For functions, there is only read and call permission, not modification permission. Furthermore, function execution follows its logical conditions, unlike arrays or structures which are called locally. Therefore, each function is treated as a separate unit. The total number of times a function is directly or indirectly called within each main function of core B within a 1ms access weight on each core is considered. Since interrupt execution requires specific triggering conditions and its call behavior cannot be confirmed through static analysis, the aforementioned scheduling functions only focus on periodically scheduling functions that are directly or indirectly called.
[0078] S103. Based on the weight parameters, determine the contribution rate of the target information in each memory segment.
[0079] S104. Based on the contribution rate of the target information in each memory segment, determine the target memory segment allocation result.
[0080] The contribution rate represents the probability that the target information can be stored in the memory segment; a lower contribution rate indicates a higher probability of storage in that memory segment. In this embodiment, when determining the target memory segment allocation result, an initial allocation can be performed first, followed by further fine-tuning to seek a better allocation strategy. In one implementation, determining the target memory segment based on the contribution rate of the target information in each memory segment includes: determining an initial memory segment allocation result based on the contribution rate of the target information in each memory segment; in response to replacing the stored information in each memory segment of the initial memory segment allocation result, calculating the contribution rate of the stored information in the replaced memory segment, wherein the stored information belongs to the target information; and adjusting the initial memory segment allocation result based on the contribution rate of the stored information in the replaced memory segment to obtain the target memory segment allocation result.
[0081] The target information during the initialization allocation process includes at least one sub-information. The step of determining the initial memory segment allocation result based on the contribution rate of the target information in each memory segment includes:
[0082] The contribution rate of each sub-information in each memory segment is sorted to determine the sorting information of the memory segment corresponding to each sub-information. The sorting information is determined based on the contribution rate from low to high.
[0083] The memory segment with the first sorting position is selected as each sub-information of the memory segment. Based on the contribution rate of each sub-information in the memory segment with the first sorting position, the sorting information of each sub-information is determined.
[0084] Based on the sorting information of the memory segments and the sorting information of each sub-information, the initial memory segment allocation result is determined.
[0085] Further, determining the initial memory segment allocation result based on the sorting information of the memory segment and the sorting information of each sub-information includes: determining a first sorting position sub-information based on the sorting information of each sub-information; if the memory segment of the first sorting position meets the condition for storing the first sorting position sub-information, storing the first sorting position sub-information in the memory segment of the first sorting position; after removing the first sorting position sub-information from the target information to be stored, recalculating the sorting information of the memory segment and the sorting information of the sub-information until a corresponding memory segment is determined for each sub-information.
[0086] Correspondingly, the process of determining the initial memory segment allocation result based on the sorting information of the memory segment and the sorting information of each sub-information during the fine-tuning process includes: determining the first sorting position sub-information based on the sorting information of each sub-information; if the memory segment of the first sorting position meets the condition for storing the first sorting position sub-information, storing the first sorting position sub-information in the memory segment of the first sorting position; after removing the first sorting position sub-information from the target information to be stored, recalculating the sorting information of the memory segment and the sorting information of the sub-information until a corresponding memory segment is determined for each sub-information.
[0087] Since the target information stored in the memory segment can be either a function or a variable, the initialization allocation and fine-tuning process will be explained using functions and variables respectively.
[0088] For microcontroller systems, functions are code and must be stored in flash memory. First, initialization and allocation are performed. Then, the initialization and allocation results are further refined to find a more optimized allocation strategy, thus obtaining the target memory segment allocation result. See also... Figure 3 and Figure 4 , Figure 3 The flowchart shows the initial allocation process for the function. Figure 4 For fine-tuning the allocation flowchart.
[0089] For example, the contribution rate of the function FuncA in each MemmA memory segment is calculated, denoted as FuncA_MemmA_Load, as follows:
[0090] FuncA_MemmA_Load=P2_FuncCore0_MemmA*P3_FuncCore0_FuncA+…+P2_FuncCoren_MemmA*P3_FuncCoren_FuncA.
[0091] The specific process includes: calculating the contribution rate of each function in each flash memory segment, sorting the contribution rates of each function in each memory segment (from low to high), which are the 1st memory segment, 2nd memory segment, ..., qth memory segment for that function (assuming there are q memory segments in total). The total number of events in memory segments (F1...Fq) that are the first memory segment of the function is counted, and the memory segments are sorted accordingly to obtain memory segment Fa. Functions using memory segment Fa as their first memory segment are selected, and the difference between the contribution rates of the first and second memory segments for each function is calculated. Functions are then sorted from largest to smallest contribution rate, and the function Aa is selected as the first function in the current sort. After placing function Aa into memory segment Fa, it is determined whether memory segment Fa exceeds its limit. If not, function Aa is placed into memory segment Fa, and function Aa is removed from the list of unallocated functions. If memory segment Fa exceeds its limit, this memory segment is removed from the memory segment sort of each function, i.e., Fa is deleted, forming a new memory segment sort. At this point, the total number of memory segments is q-1. If q > 1 or there is no function with unplaced partitions, the initial allocation process ends. If q > 1 or there is no function with unplaced partitions, the process returns to the beginning. The total number of events in the memory segment (F1…Fq) is the first memory segment of the function. The memory segments are sorted according to this count, and the process continues until q > 1 or there is no function with unplaced partitions.
[0092] The fine-tuning process includes: replacing the address of the i-th function in the k-th memory segment with the address of the j-th function in the (k+1)...q (maximum value q, where q is the number of memory segments) memory segments (where k, i, and j are initially 1). Calculate the memory segment contribution rates P1 and P2 of the two functions before and after the replacement. If P1 > P2, replace the memory segments containing the two functions. If P1 is not greater than P2, determine if function j is the last function in the (k+1)...q memory segments. If yes, set j = j + 1 and re-execute the process. If no, set i = i + 1, determine if function i is the total number of functions stored in the k-th memory segment. If yes, set k = k + 1, determine if k is greater than q. If yes, determine the memory occupied by all functions allocated in the k-th memory segment, and determine if the occupied memory is greater than the maximum memory of the current memory segment. If yes, place the last allocated function back to its original area; otherwise, place it in the (k+1)-th memory segment, and then process the next memory segment.
[0093] The partitions can be fine-tuned multiple times. The more times the fine-tuning is iterated, the more perfect the partitioning strategy becomes and the higher the accuracy. However, the response time will also be longer.
[0094] For microcontroller systems, Const constants (also known as Const variables) are similar to code; they must be stored in flash memory and can only be read, not modified. The allocation process is the same as the function allocation process: first, initialization allocation is performed, and then the initialization allocation result is further fine-tuned to seek a better allocation strategy. See the flowchart below. Figure 5 and Figure 6 .
[0095] Calculate the contribution rate of ConstA in each MemmA memory segment, where ConstA_MemmA_Load represents the contribution rate of the Const variable in each MemmA memory segment. The specific calculation formula is as follows:
[0096] ConstA_MemmA_Load=P2_ReadCore0_MemmA*P3_ReadCore0_ConstA+…+P2_ReadCoren_MemmA*P3_ReadCoren_ConstA.
[0097] Among them, P2_ReadCore0_MemmA is the weight parameter corresponding to the read rate of MemmA of each memory segment by the processing core Core0, P3_ReadCore0_ConstA is the weight parameter of the read rate of the Const constant by the processing core Core0, and so on for subsequent parameters, which will not be explained in detail here.
[0098] The initial allocation process includes:
[0099] Calculate the contribution rate of each Const constant in each flash memory segment, and sort the constants by their contribution rate in each flash memory segment (from low to high), which are the 1st memory segment, the 2nd memory segment, ..., the qth memory segment for that constant (assuming there are q memory segments in total). Count the total number of events in memory segments (F1...Fq) that are the first memory segment of the Const constant, and sort the memory segments accordingly to obtain memory segment Fa. Select the constants that use memory segment Fa as their first memory segment, and calculate the difference between the contribution rate of each constant in the first and second memory segments. Sort the constants according to their contribution rate from large to small, and take the constant Aa that is first in the current sort. After adding constant Aa to memory segment Fa, check if memory segment Fa is overloaded. If not, add constant Aa to memory segment Fa and delete constant Aa from the list of unallocated Const constants. If memory segment Fa exceeds the limit, remove this memory segment from the sort of constant memory segments, i.e., delete Fa, and form a new memory segment sort. At this time, the total number of memory segments is q-1. If q>1 or there are no constants without unplaced partitions, the initial allocation process ends. If the condition q>1 or there are no constants without unplaced partitions is not met, return to the execution of counting the total number of events where memory segment (F1...Fq) is the first constant memory segment, and sort the memory segments according to this, obtaining the memory segment process, until q>1 or there are no constants without unplaced partitions.
[0100] The fine-tuning process includes: taking the address of the i-th constant in the k-th memory segment and replacing it with the address of the j-th constant in the (k+1...q)-th memory segment (where the maximum value is q, and q is the number of memory segments) (where k, i, and j are initially 1), calculating the memory segment contribution rates P1 and P2 of the two constants before and after the replacement. If P1 > P2, the memory segments where the two constants are located are replaced. If P1 is not greater than P2, determine whether constant j is the last constant in the (k+1...q)-th memory segment. If it is, j = j + 1, and the process is repeated. If not, i = i + 1, check if the constant i is greater than the total number of constants stored in the k-th memory segment. If yes, k = k + 1, check if k is greater than q, check the memory occupied by all the constants already allocated in the k memory segment, and check if the memory occupied is greater than the maximum memory of the current memory segment. If yes, place the last allocated constant back to its original area. If no, place it in the k + 1 memory segment. Then, if the current k is not greater than q, i.e., k is not the last memory segment, process the next memory segment.
[0101] For microcontroller systems, non-Const global variables (also referred to as non-Const variables) must be placed in RAM and can be read and modified. For the initial allocation process, please refer to [link to relevant documentation]. Figure 7 For detailed adjustment procedures, please refer to [link / reference]. Figure 8 , among which, Figure 7 and Figure 8In this context, non-Const variables are also referred to as non-const variables. The contribution rate of the non-Const global variable VariA in each RAM memory segment MemmA can be expressed by the following formula:
[0102] VariA_MemmA_Load=P2_ReadCore0_MemmA*P3_ReadCore0_VariA+…+P2_ReadCoren_MemmA*P3_ReadCoren_VariA+P2_WriteCore0_MemmA*P3_WriteCore0_VariA+…+P2_WriteCoren_MemmA*P3_WriteCoren_VariA
[0103] The initial allocation process for non-Const variables includes: calculating the contribution rate of each non-Const variable in each RAM memory segment; sorting the contribution rates of non-Const variables in each RAM memory segment (from low to high), which are respectively the 1st memory segment, 2nd memory segment, ..., qth memory segment for that non-Const variable (assuming there are q memory segments in total). The total number of events in the first memory segment of the non-Const variable (F1...Fq) is counted, and the memory segments are sorted accordingly to obtain memory segment Fa. Non-Constant variables with memory segment Fa as their first memory segment are selected, and the difference between the contribution rates of each non-Constant variable in the first and second memory segments is calculated. The non-Constant variables are then sorted from largest to smallest contribution rate, and the non-Constant variable Aa is selected as the first non-Constant variable in the current sort. After placing the non-Constant variable Aa into memory segment Fa, it is determined whether memory segment Fa exceeds its limit. If not, the non-Constant variable Aa is placed into memory segment Fa, and the non-Constant variable Aa is removed from the list of unallocated non-Constant variables. If memory segment Fa exceeds the limit, this memory segment is removed from the memory segment sort of each non-constant variable, i.e., Fa is deleted, forming a new memory segment sort. At this time, the total number of memory segments is q-1. If q>1 or there are no non-constant variables with unplaced partitions, the initial allocation process ends. If the condition q>1 or there are no non-constant variables with unplaced partitions is not met, the process returns to execute the total number of events for the memory segment (F1...Fq) being the first memory segment of the non-constant variable, and sorts the memory segments accordingly to obtain the memory segment process, until q>1 or there are no non-constant variables with unplaced partitions.
[0104] The fine-tuning process for non-Const variables includes: taking the address of the i-th non-constant variable in the k-th memory segment and replacing it with the address of the j-th non-constant variable in the (k+1...q)-th memory segment (where the maximum value is q, and q is the number of memory segments) (where k, i, and j are initially 1), calculating the memory segment contribution rates P1 and P2 of the two non-constant variables before and after the replacement. If P1 > P2, the memory segments where the two non-constant variables are located are replaced. If P1 is not greater than P2, determine whether the non-constant variable j is the last non-constant variable in the (k+1...q)-th memory segment. If it is, j = j + 1, and the process is repeated. If not, i = i + 1, check if i is greater than the total number of non-constant variables stored in the k-th memory segment. If yes, k = k + 1, check if k is greater than q, check the memory occupied by all non-constant variables allocated in the k-th memory segment, and check if the memory occupied is greater than the maximum memory of the current memory segment. If yes, place the last allocated non-constant variable back to its original area. If no, place it in the k + 1 memory segment. Then, if the current k is not greater than q, i.e., k is not the last memory segment, process the next memory segment.
[0105] This application provides a memory segment allocation method, which includes: analyzing a target code file to obtain an information configuration file; determining weight parameters of target information to be allocated to a memory segment based on the information configuration file; determining the contribution rate of the target information in each memory segment based on the weight parameters; and determining a target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result. This application can analyze the characteristics of the information to be stored and combine this with the efficiency of different operations of the microcontroller on different memory segments to adaptively allocate memory segments to the information to be stored, improving the accuracy and efficiency of memory segment allocation. Based on the divided memory segments and the actual project, variables can be reasonably partitioned, reducing CPU load without changing the developer's code logic.
[0106] Correspondingly, this application also provides a memory segment allocation device, see [link to relevant documentation]. Figure 9 ,include:
[0107] Analysis unit 201 is used to analyze the target code file and obtain an information configuration file. The information configuration file includes at least a first file, a second file, and a third file. The first file is used to store the functions called by the microcontroller when executing the target code file and the frequency information of each function being called. The second file is used to store the variables read by the microcontroller when executing the target code file and the frequency information of each variable being read. The third file is used to store the variables modified by the microcontroller when executing the target code file and the frequency information of each variable being modified.
[0108] The first determining unit 202 is used to determine the weight parameters of the target information to be allocated to the memory segment based on the information configuration file. The target information includes at least the functions and / or variables to be allocated to the memory segment. The weight parameters include a first weight parameter and a second weight parameter. The first weight parameter represents the application rate of each processing core of the microcontroller to the memory segment, and the second weight parameter represents the processing rate of each processing core of the microcontroller to the functions or variables.
[0109] The second determining unit 203 is used to determine the contribution rate of the target information in each memory segment based on the weight parameters, wherein the contribution rate represents the probability parameter that the target information can be stored in the memory segment;
[0110] The third determining unit 204 is used to determine the target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result.
[0111] Optionally, the device further includes:
[0112] The generation unit is used to analyze the target code file to determine whether there are functions called by pointers; if there are functions called by pointers, a fourth file of the information configuration file is generated, which is used to store the pointer functions and the number of times the pointer functions are called when the microcontroller executes the target code file.
[0113] Optionally, the first determining unit is specifically used for:
[0114] Based on the information configuration file, first information is obtained, which includes a function call relationship tree, the number of times each function calls other functions, and the frequency of function reading and writing variables. The call relationship tree represents the call relationship between functions.
[0115] Based on the first information, a first weight parameter is determined. The first weight parameter includes the read weight sub-parameter of each processing core of the microcontroller relative to the memory segment, the write weight sub-parameter of each processing core relative to the memory segment, and the access speed weight sub-parameter of each processing core relative to the memory segment acquisition instruction.
[0116] Based on the information configuration file, the access frequency of the main function of each processing core cycle within the cycle is determined, and based on the access frequency, the second weight parameter is determined.
[0117] Optionally, the third determining unit includes:
[0118] The first determining subunit is used to determine the initial memory segment allocation result based on the contribution rate of the target information in each memory segment;
[0119] A calculation subunit is configured to, in response to the replacement of stored information in each memory segment of the initial memory segment allocation result, calculate the contribution rate of the stored information in the replaced memory segment, wherein the stored information belongs to the target information;
[0120] The acquisition sub-unit is used to adjust the initial memory segment allocation result based on the contribution rate of the storage information in the replaced memory segment, so as to obtain the target memory segment allocation result.
[0121] Optionally, the target information includes at least one sub-information, wherein the first determining sub-unit is specifically used for:
[0122] The contribution rate of each sub-information in each memory segment is sorted to determine the sorting information of the memory segment corresponding to each sub-information. The sorting information is determined based on the contribution rate from low to high.
[0123] The memory segment with the first sorting position is selected as each sub-information of the memory segment. Based on the contribution rate of each sub-information in the memory segment with the first sorting position, the sorting information of each sub-information is determined.
[0124] Based on the sorting information of the memory segments and the sorting information of each sub-information, the initial memory segment allocation result is determined.
[0125] Optionally, determining the initial memory segment allocation result based on the sorting information of the memory segment and the sorting information of each sub-information includes:
[0126] Based on the sorting information of each sub-information, the first sorting position sub-information is determined. If the memory segment of the first sorting position meets the conditions for storing the first sorting position sub-information, the first sorting position sub-information is stored in the memory segment of the first sorting position.
[0127] After removing the first sorting position sub-information from the target information to be stored, the sorting information of the memory segment and the sorting information of the sub-information are recalculated until a corresponding memory segment is determined for each sub-information.
[0128] Optionally, the acquisition subunit is specifically used for:
[0129] If the contribution rate of the stored information in the first memory segment before replacement is greater than the contribution rate in the second memory segment after replacement, the stored information is stored in the second memory segment.
[0130] If the contribution rate of the stored information in the first memory segment before replacement is not greater than the contribution rate of the second memory segment after replacement, determine whether the stored information is the last stored information in the second memory segment. If not, replace the memory segment corresponding to the next stored information until the replacement and adjustment of each stored information is completed, and obtain the target memory segment allocation result.
[0131] This application also provides a storage medium that can store a program suitable for execution by a processor, the program being used to implement the aforementioned memory segment allocation method.
[0132] This application provides an electronic device, including a memory and a processor;
[0133] The memory is used to store programs;
[0134] The processor is used to execute the program and implement the memory segment allocation method as described above.
[0135] Finally, it should be noted that in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0136] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. The same or similar parts between the various embodiments can be referred to each other.
[0137] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A memory segment allocation method, characterized in that, include: The target code file is analyzed to obtain an information configuration file, which includes at least a first file, a second file, and a third file. The first file is used to store the functions called by the microcontroller when executing the target code file and the frequency information of each function being called. The second file is used to store the variables read by the microcontroller when executing the target code file and the frequency information of each variable being read. The third file is used to store the variables modified by the microcontroller when executing the target code file and the frequency information of each variable being modified. Based on the information configuration file, weight parameters of the target information to be allocated to the memory segment are determined. The target information includes at least functions and / or variables to be allocated to the memory segment. The weight parameters include a first weight parameter and a second weight parameter. The first weight parameter represents the application rate of each processing core of the microcontroller to the memory segment, and the second weight parameter represents the processing rate of each processing core of the microcontroller to the functions or variables. Based on the weight parameters, the contribution rate of the target information in each memory segment is determined, and the contribution rate represents the probability parameter that the target information can be stored in the memory segment; Based on the contribution rate of the target information in each memory segment, the target memory segment allocation result is determined so that the target information is stored in a memory segment that matches the target memory segment allocation result.
2. The memory segment allocation method according to claim 1, characterized in that, The method further includes: The target code file is analyzed to determine whether there are functions called via pointers; If there are functions called via pointers, a fourth file of the information configuration file is generated. The fourth file is used to store the pointer functions and the number of times the pointer functions are called when the microcontroller executes the target code file.
3. The memory segment allocation method according to claim 1, characterized in that, The step of determining the weight parameters of the target information to be allocated to the memory segment based on the information configuration file includes: Based on the information configuration file, first information is obtained, which includes a function call relationship tree, the number of times each function calls other functions, and the frequency of function reading and writing variables. The call relationship tree represents the call relationship between functions. Based on the first information, a first weight parameter is determined. The first weight parameter includes the read weight sub-parameter of each processing core of the microcontroller relative to the memory segment, the write weight sub-parameter of each processing core relative to the memory segment, and the access speed weight sub-parameter of each processing core relative to the memory segment acquisition instruction. Based on the information configuration file, the access frequency of the main function of each processing core cycle within the cycle is determined, and based on the access frequency, the second weight parameter is determined.
4. The memory segment allocation method according to claim 1, characterized in that, The step of determining the target memory segment based on the contribution rate of the target information in each memory segment includes: Based on the contribution rate of the target information in each memory segment, the initial memory segment allocation result is determined; In response to the replacement of the stored information in each memory segment of the initial memory segment allocation result, the contribution rate of the stored information in the replaced memory segment is calculated, and the stored information belongs to the target information; Based on the contribution rate of the stored information in the replaced memory segment, the initial memory segment allocation result is adjusted to obtain the target memory segment allocation result.
5. The memory segment allocation method according to claim 4, characterized in that, The target information includes at least one sub-information, wherein determining the initial memory segment allocation result based on the contribution rate of the target information in each memory segment includes: The contribution rate of each sub-information in each memory segment is sorted to determine the sorting information of the memory segment corresponding to each sub-information. The sorting information is determined based on the contribution rate from low to high. The memory segment with the first sorting position is selected as each sub-information of the memory segment. Based on the contribution rate of each sub-information in the memory segment with the first sorting position, the sorting information of each sub-information is determined. Based on the sorting information of the memory segments and the sorting information of each sub-information, the initial memory segment allocation result is determined.
6. The memory segment allocation method according to claim 5, characterized in that, The determination of the initial memory segment allocation result based on the sorting information of the memory segment and the sorting information of each sub-information includes: Based on the sorting information of each sub-information, the first sorting position sub-information is determined. If the memory segment of the first sorting position meets the conditions for storing the first sorting position sub-information, the first sorting position sub-information is stored in the memory segment of the first sorting position. After removing the first sorting position sub-information from the target information to be stored, the sorting information of the memory segment and the sorting information of the sub-information are recalculated until a corresponding memory segment is determined for each sub-information.
7. The memory segment allocation method according to claim 4, characterized in that, The step of adjusting the initial memory segment allocation result based on the contribution rate of the stored information in the replaced memory segment to obtain the target memory segment allocation result includes: If the contribution rate of the stored information in the first memory segment before replacement is greater than the contribution rate in the second memory segment after replacement, the stored information is stored in the second memory segment. If the contribution rate of the stored information in the first memory segment before replacement is not greater than the contribution rate of the second memory segment after replacement, determine whether the stored information is the last stored information in the second memory segment. If not, replace the memory segment corresponding to the next stored information until the replacement and adjustment of each stored information is completed, and obtain the target memory segment allocation result.
8. A memory segment allocation device, characterized in that, include: An analysis unit is used to analyze the target code file and obtain an information configuration file. The information configuration file includes at least a first file, a second file, and a third file. The first file is used to store the functions called by the microcontroller when executing the target code file and the frequency information of each function being called. The second file is used to store the variables read by the microcontroller when executing the target code file and the frequency information of each variable being read. The third file is used to store the variables modified by the microcontroller when executing the target code file and the frequency information of each variable being modified. The first determining unit is used to determine the weight parameters of the target information to be allocated to the memory segment based on the information configuration file, wherein the target information includes at least the functions and / or variables to be allocated to the memory segment, and the weight parameters include a first weight parameter and a second weight parameter, wherein the first weight parameter represents the application rate of each processing core of the microcontroller to the memory segment, and the second weight parameter represents the processing rate of each processing core of the microcontroller to the functions or variables. The second determining unit is used to determine the contribution rate of the target information in each memory segment based on the weight parameters, wherein the contribution rate represents the probability parameter that the target information can be stored in the memory segment; The third determining unit is used to determine the target memory segment allocation result based on the contribution rate of the target information in each memory segment, so that the target information is stored in a memory segment that matches the target memory segment allocation result.
9. An electronic device, characterized in that, Including memory and processor; The memory is used to store programs; The processor is configured to execute the program to implement the memory segment allocation method as described in any one of claims 1-7.
10. A storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the memory segment allocation method as described in any one of claims 1-7.
Citation Information
Patent Citations
Vulnerability detection method and device, electronic equipment and computer readable storage medium
CN111488579A
Source code analysis method and device, electronic equipment and storage medium
CN116303013A