A peak detection method, device, computer equipment and storage medium
By setting readable and writeable space in the thread's memory space and dividing the target space, the memory space peak of the thread is detected, which solves the memory waste problem caused by thread allocation of default size memory space, and achieves reasonable memory allocation and normal thread operation.
Patent Information
- Application Number
- CN202210364793.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-08
- Publication Date
- 2025-03-04
- Estimated Expiration
- 2042-04-08
AI Technical Summary
In large applications, when allocating memory space of default size to each application thread, there is a problem of wasting memory space, because in fact, many application threads only need memory space smaller than the default size to run normally.
By obtaining the thread's stack end address and start address, set the readable and writeable space in the memory space, and during the thread execution, determine whether the stack pointer points to the unreadable and writeable space. If so, an exception signal is generated, and the target space is divided as the newly added readable and writeable space until the thread is executed, and the peak value of the memory space used by the thread is determined.
It realizes the accurate detection of the peak memory space required for each application thread to run, improves the rationality of memory allocation, avoids the waste of memory space, and ensures the normal operation of threads.
Smart Images

Figure CN114706681B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a peak detection method, apparatus, computer equipment and storage medium. Background Art
[0002] In large applications, a large number of application threads are usually required to implement complex application logic. Each application thread needs to be allocated a certain amount of memory space for its operation. For the operation of allocating memory space to each application thread, a default size of memory space is usually allocated to each application thread, for example, 1MB of memory space is allocated to each thread.
[0003] However, in actual situations, there are a certain number of application threads that only need less (or even much less) than the default memory size to run normally. In this way, the method of allocating the default memory size to each application thread causes a waste of memory space. Therefore, there is an urgent need for a method to detect the peak value of the memory space required for each application thread to run, so as to improve the rationality of memory allocation and avoid the waste of memory space. Summary of the invention
[0004] The embodiments of the present disclosure at least provide a peak detection method, apparatus, computer equipment and storage medium.
[0005] In a first aspect, an embodiment of the present disclosure provides a peak detection method, including:
[0006] Obtaining a stack end address and a stack start address corresponding to a memory space allocated for a thread running in an application, and setting a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space is lower than the stack start address;
[0007] During the execution of the thread, determining whether a stack pointer of the thread points to a non-readable and non-writable space in the memory space;
[0008] If yes, an exception signal is generated, and based on the exception signal, a preset detection mechanism and a stack pointer address, a target space is segmented out from the non-readable and writable space, and the target space is used as a newly added readable and writable space;
[0009] Return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
[0010] In a possible implementation manner, after determining the peak value, the method further includes:
[0011] The peak value is fed back to the development end corresponding to the application, so that the development end can set a new memory space allocation program for the thread based on at least one of the received peak values, and the new memory space allocation program is used to allocate memory space matching at least one of the peak values to the newly created thread when the thread is newly created.
[0012] In a possible implementation manner, after determining the peak value, the method further includes:
[0013] Based on the peak value, a memory space allocation program for the thread is updated, and when the thread is newly created, a memory space matching the peak value is allocated to the newly created thread based on the updated memory space allocation program.
[0014] In a possible implementation manner, the step of dividing a target space in the unreadable and writable space based on the abnormal signal, a preset detection mechanism, and a stack pointer address includes:
[0015] Detecting the stack pointer address carried in the abnormal signal using the detection mechanism to determine whether the stack pointer address is between the highest address and the stack start address;
[0016] If so, the target space is partitioned out in the non-readable and writable space based on the stack pointer address and the highest address.
[0017] In a possible implementation, the method further includes:
[0018] When it is determined that the stack pointer address is not between the highest address and the stack starting address, return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and use the currently determined readable and writable space as the peak value of the memory space used by the thread.
[0019] In a possible implementation manner, the step of dividing the target space in the non-readable and writable space based on the stack pointer address and the highest address includes:
[0020] Determine the space size of a first memory space between the stack pointer address and the highest address;
[0021] When it is determined that the space size of the first memory space is an integer multiple of a second preset size, the first memory space in the unreadable space is used as the target space; the second preset size is a default space size of a memory page space.
[0022] In a possible implementation, the method further includes:
[0023] When it is determined that the space size of the first memory space does not match the integer multiple of the second preset size, starting from the highest address, a second memory space having a space size greater than the second preset size and being a minimum integer multiple of the second preset size is divided from the unreadable space;
[0024] The second memory space is used as the target space; wherein the stack pointer address pointed to by the stack pointer address is located in the target space.
[0025] In a possible implementation, determining the peak value of the memory space used by the thread based on the currently determined readable and writable space includes:
[0026] In a case where the size of the currently determined readable and writable space is greater than the first preset size, the size of the currently determined readable and writable space is used as the peak value.
[0027] In a possible implementation, determining the peak value of the memory space used by the thread based on the currently determined readable and writable space includes:
[0028] In a case where the size of the currently determined readable and writable space is equal to the first preset size, taking the size of the readable and writable space having the first preset size as the peak value; or
[0029] When the size of the currently determined readable and writable space is equal to the first preset size, determining a target stack pointer address pointed to by the stack pointer of the thread when the thread is executed;
[0030] The size of the memory space between the stack end address and the target stack pointer address is taken as the peak value.
[0031] In a second aspect, an embodiment of the present disclosure further provides a peak detection device, including:
[0032] an acquisition module, configured to acquire a stack end address and a stack start address corresponding to a memory space allocated for a thread running in an application, and to set a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space being lower than the stack start address;
[0033] A judgment module, used for determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space during the execution of the thread;
[0034] a segmentation module, configured to generate an exception signal if yes, segment a target space from the non-readable and writable space based on the exception signal, a preset detection mechanism and a stack pointer address, and use the target space as a newly added readable and writable space;
[0035] The loop module is used to return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
[0036] In a possible implementation, the device further includes:
[0037] A feedback module is used to feed back the peak value to the development end corresponding to the application after determining the peak value, so that the development end can set a new memory space allocation program for the thread based on at least one of the received peak values, and the new memory space allocation program is used to allocate memory space matching at least one of the peak values to the newly created thread when the thread is newly created.
[0038] In a possible implementation manner, the method further includes:
[0039] The update module is used to update the memory space allocation program for the thread based on the peak value after determining the peak value, and when the thread is newly created, allocate memory space matching the peak value to the newly created thread based on the updated memory space allocation program.
[0040] In a possible implementation, the segmentation module is used to detect the stack pointer address carried in the abnormal signal using the detection mechanism when segmenting the target space in the unreadable and writable space based on the abnormal signal, the pre-set detection mechanism and the stack pointer address, to determine whether the stack pointer address is between the highest address and the stack start address;
[0041] If so, the target space is partitioned out in the non-readable and writable space based on the stack pointer address and the highest address.
[0042] In one possible implementation, the segmentation module is also used to return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space when it is determined that the stack pointer address is not between the highest address and the stack starting address, until the thread is executed, and use the currently determined readable and writable space as the peak value of the memory space used by the thread.
[0043] In a possible implementation manner, the segmentation module, when segmenting the target space in the unreadable and writable space based on the stack pointer address and the highest address, is used to determine the space size of the first memory space located between the stack pointer address and the highest address;
[0044] When it is determined that the space size of the first memory space is an integer multiple of a second preset size, the first memory space in the unreadable space is used as the target space; the second preset size is a default space size of a memory page space.
[0045] In a possible implementation manner, the segmentation module is further configured to, when it is determined that the space size of the first memory space does not match the integer multiple of the second preset size, segment, starting from the highest address, a second memory space having a space size greater than the second preset size and being a minimum integer multiple of the second preset size in the unreadable space;
[0046] The second memory space is used as the target space; wherein the stack pointer address pointed to by the stack pointer address is located in the target space.
[0047] In one possible implementation, the loop module, when determining the peak value of the memory space used by the thread based on the currently determined readable and writable space, is used to use the space size of the currently determined readable and writable space as the peak value when the space size of the currently determined readable and writable space is greater than the first preset size.
[0048] In a possible implementation, the loop module, when determining the peak value of the memory space used by the thread based on the currently determined readable and writable space, is used to, when the space size of the currently determined readable and writable space is equal to the first preset size, use the space size of the readable and writable space having the first preset size as the peak value; or
[0049] When the size of the currently determined readable and writable space is equal to the first preset size, determining a target stack pointer address pointed to by the stack pointer of the thread when the thread is executed;
[0050] The size of the memory space between the stack end address and the target stack pointer address is taken as the peak value.
[0051] In a third aspect, an optional implementation of the present disclosure further provides a computer device, a processor, and a memory, wherein the memory stores machine-readable instructions executable by the processor, and the processor is used to execute the machine-readable instructions stored in the memory, and when the machine-readable instructions are executed by the processor, the machine-readable instructions perform the steps of the above-mentioned first aspect, or any possible implementation of the first aspect.
[0052] In a fourth aspect, an optional implementation of the present disclosure further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed, the steps of the above-mentioned first aspect, or any possible implementation of the first aspect are executed.
[0053] For a description of the effects of the peak detection device, computer equipment, and computer-readable storage medium, please refer to the description of the peak detection method above, which will not be repeated here.
[0054] The peak detection method, device, computer equipment and storage medium provided by the embodiments of the present disclosure set a readable and writable space in the memory space allocated for the thread, and when it is determined that the stack pointer of the thread points to the unreadable and writable space outside the readable and writable space in the memory space during the execution process, a new target space is divided in the unreadable space as a way to increase the readable and writable space for the thread to use, which does not affect the normal use of the thread, and can also accurately determine the peak value of the memory space used by the thread based on the currently determined readable and writable space when the thread is executed. Furthermore, when the thread is re-run, the memory space matching the peak value can be allocated to the thread based on the determined peak value, which not only ensures the normal operation of the thread, but also improves the rationality of the memory space allocation, and avoids the problem of memory space waste caused by allocating too large a memory space.
[0055] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the following is a brief introduction to the drawings required for use in the embodiments. The drawings herein are incorporated into the specification and constitute a part of the specification. These drawings illustrate embodiments consistent with the present disclosure and are used together with the specification to illustrate the technical solutions of the present disclosure. It should be understood that the following drawings only illustrate certain embodiments of the present disclosure and should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can also be obtained based on these drawings without creative work.
[0057] Figure 1A flow chart of a peak detection method provided by an embodiment of the present disclosure is shown;
[0058] Figure 2 A schematic diagram showing a memory space allocated to a thread provided by an embodiment of the present disclosure is shown;
[0059] Figure 3 A schematic diagram showing the moving direction of a stack pointer corresponding to a thread in a memory space provided by an embodiment of the present disclosure is shown;
[0060] Figure 4 A schematic diagram of a memory space with a readable and writable space provided in an embodiment of the present disclosure is shown;
[0061] Figure 5 A schematic diagram of a triggering anomaly detection mechanism provided by an embodiment of the present disclosure is shown;
[0062] Figure 6 A schematic diagram of a target space provided by an embodiment of the present disclosure is shown;
[0063] Figure 7 A schematic diagram of a peak detection device provided by an embodiment of the present disclosure is shown;
[0064] Figure 8 A schematic diagram of the structure of a computer device provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0065] In order to make the purpose, technical scheme and advantages of the embodiments of the present disclosure clearer, the technical scheme in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the drawings in the embodiments of the present disclosure. Obviously, the described embodiments are only part of the embodiments of the present disclosure, rather than all of the embodiments. The components of the embodiments of the present disclosure generally described and shown here can be arranged and designed in various different configurations. Therefore, the following detailed description of the embodiments of the present disclosure is not intended to limit the scope of the present disclosure claimed for protection, but merely represents the selected embodiments of the present disclosure. Based on the embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present disclosure.
[0066] In addition, the terms "first", "second", etc. in the description and claims of the embodiments of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchangeable where appropriate, so that the embodiments described herein can be implemented in an order other than that illustrated or described herein.
[0067] The "multiple or several" mentioned in this article refers to two or more. "And / or" describes the association relationship of the associated objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone. The character " / " generally indicates that the associated objects are in an "or" relationship.
[0068] It has been found through research that when allocating memory space for application threads in large applications, a method of allocating a default size of memory space to each application thread is usually adopted. However, in actual situations, there are a certain number of application threads that only need a memory space smaller than the default size to run normally. This not only causes the problem of low memory space utilization and low rationality of memory allocation, but also causes waste of memory space. In addition, unreasonable memory allocation will also lead to the problem of insufficient memory space when a large number of application threads are running at the same time, which will then cause the application to crash, seriously affecting the use of the application. Therefore, there is an urgent need for a method to detect the peak value of the memory space required for each application thread to run, so as to improve the rationality of memory allocation and avoid the above problems.
[0069] Based on the above research, the present disclosure provides a peak detection method, device, computer equipment and storage medium, which sets a readable and writable space in the memory space allocated for the thread, and when it is determined that the stack pointer of the thread points to the unreadable and writable space outside the readable and writable space in the memory space during the execution process, a new target space is divided in the unreadable space as a way to increase the readable and writable space for the thread to use, which does not affect the normal use of the thread, and can also accurately determine the peak value of the memory space used by the thread based on the currently determined readable and writable space when the thread is executed. Furthermore, when the thread is re-run, the memory space matching the peak value can be allocated to the thread based on the determined peak value, which not only ensures the normal operation of the thread, but also improves the rationality of the memory space allocation, and avoids the problem of memory space waste caused by allocating too large a memory space.
[0070] The defects existing in the above solutions are the results obtained by the inventor after practice and careful research. Therefore, the discovery process of the above problems and the solutions proposed by the present disclosure for the above problems below should be the contributions made by the inventor to the present disclosure during the disclosure process.
[0071] It should be noted that similar reference numerals and letters denote similar items in the following drawings, and therefore, once an item is defined in one drawing, further definition and explanation thereof is not required in subsequent drawings.
[0072] To facilitate understanding of this embodiment, a peak detection method disclosed in an embodiment of the present disclosure is first introduced in detail. The executor of the peak detection method provided in the embodiment of the present disclosure is generally a terminal device or other processing device with certain computing capabilities, wherein the terminal device may be a user equipment (UE), a mobile device, a user terminal, a terminal, a personal digital assistant (PDA), a handheld device, a computer device, etc.; in some possible implementations, the peak detection method may be implemented by a processor calling computer-readable instructions stored in a memory.
[0073] The peak detection method provided by the embodiment of the present disclosure is described below by taking a system in which an execution subject is an application running as an example.
[0074] like Figure 1 As shown, it is a flow chart of a peak detection method provided by an embodiment of the present disclosure, which may include the following steps:
[0075] S101: Obtain a stack end address and a stack start address corresponding to a memory space allocated for a thread running in an application, and set a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space may be lower than the stack start address.
[0076] Here, the system on which the application runs may be, for example, an Android system or an IOS system.
[0077] The above-mentioned thread is any thread running in the application. An application may include a large number of threads to implement complex business logic in the application. The threads included in the application can run concurrently.
[0078] The memory space allocated to the threads running in the application can be a memory space of a default size in the total memory space corresponding to the application. For example, the memory space allocated to the threads running in the application can be a 1MB memory space, a 2MB memory space, etc. in the corresponding 4G space. The memory space can be used for the running of the thread. Specifically, the memory space is the thread stack space corresponding to the thread, which is used to store the stack information called by the thread during the running process.
[0079] The memory space allocated for a thread can be identified by the stack end address (stack_end) and the stack start address (stack_begin). Specifically, the space between the stack end address and the stack start address is the memory space allocated for the thread. Figure 2As shown, it is a schematic diagram of a memory space allocated to a thread provided by an embodiment of the present disclosure, wherein a is the total memory space corresponding to the application, b is located between the stack end address and the stack start address, and is a memory space of a default size allocated to the thread, and b is located in a. The stack end address is the lowest address of the memory space, and the stack start address is the highest address of the memory space.
[0080] Since the memory space used by each function called during the thread's running starts from the stack end address and grows toward the stack start address, the address pointed to by the stack pointer (stack_pointer) corresponding to the thread can also move from the stack end address to the stack start address as the thread runs. Figure 3 FIG. 1 is a schematic diagram of a moving direction of a stack pointer corresponding to a thread in a memory space provided by an embodiment of the present disclosure, wherein the moving direction of the stack pointer in the memory space allocated for the thread is Figure 3 The arrow in the target indicates the direction.
[0081] In order to match the moving direction of the stack pointer, a readable and writable space of a first preset size can be set in the memory space starting from the end address of the stack during specific implementation. The readable and writable space is the space in the memory space whose access attribute is a readable and writable attribute. The first preset size can be a space size set by the developer based on experience, and the first preset size is smaller than the size of the memory space allocated for the thread. For example, when the size of the memory space allocated for the thread is 1MB, the first preset size can be 200KB. Since the first preset size is smaller than the size of the memory space allocated for the thread, the corresponding highest address of the allocated readable and writable space will be lower than the starting address of the stack. After allocating the readable and writable space, the space in the memory space except the readable and writable space can be used as an unreadable space, and the access attribute corresponding to the unreadable space can be set to an unreadable and writable attribute. Figure 4 FIG. 1 is a schematic diagram of a memory space with readable and writable space provided by an embodiment of the present disclosure, wherein the white area is the readable and writable space, and the black area is the non-readable and writable space.
[0082] For example, for any thread that starts running in the application, a default size of memory space can be allocated to the thread in the total memory space corresponding to the application, and the stack end address and stack start address corresponding to the default size of memory space can be determined. After that, starting from the stack end address, according to the moving direction of the stack pointer, a first preset size of readable and writable space can be set from the memory space, and the access attribute corresponding to the space other than the readable and writable space in the memory space can be set to a non-readable and non-writable attribute.
[0083] S102: During the execution of the thread, it is determined whether the stack pointer of the thread points to a non-readable and non-writable space in the memory space.
[0084] In specific implementation, during the execution of the thread, functions will be called continuously, and the memory space required for each called function will also increase. Specifically, the memory space used by each called function will be Figure 3 The stack pointer is used to indicate the address of the memory space used by the function called by the thread. Therefore, when the function is continuously called during the thread execution, the address pointed to by the stack pointer will also change. Specifically, the stack pointer will also change according to Figure 3 The address pointed to by the stack pointer will also increase according to the direction of movement.
[0085] Furthermore, as the thread executes, the stack pointer may point to the non-readable and writable space, which means that the function called by the thread needs to use the non-readable and writable space. Based on this, during the execution of the thread, it can be continuously determined whether the stack pointer of the thread points to the non-readable and writable space in the memory space.
[0086] If not, it means that the stack pointer has been pointing to the readable and writable space, and the thread can continue to run normally using the readable and writable space.
[0087] S103: If yes, an exception signal is generated, and based on the exception signal, a preset detection mechanism and a stack pointer address, a target space is separated from the non-readable and writable space, and the target space is used as a newly added readable and writable space.
[0088] Here, the abnormal signal is used to indicate that the thread is abnormal. Specifically, the abnormal signal may be a SIGSEGV signal, wherein the SIGSEGV signal is a signal sent when a process executes an invalid memory reference or a segmentation fault occurs.
[0089] The preset anomaly detection mechanism is used to perform anomaly detection. Specifically, the preset anomaly detection mechanism can be a pre-registered signal processing function, wherein the signal processing function is used to receive an abnormal signal and detect the abnormal information. Exemplarily, the signal processing function can be the handle_signal function in Linux.
[0090] The stack pointer address is the address pointed to by the stack pointer corresponding to the thread, and the address pointed to is the stack address corresponding to the position in the non-readable and writable space. The target space is the memory space separated from the non-readable and writable space, which can be used as a newly added readable and writable space.
[0091] In specific implementation, when the stack pointer points to a non-readable and writable space, the system can generate a SIGSEGV signal carrying the stack pointer address, and use the SIGSEGV signal to trigger the handle_signal function. Figure 5 As shown, it is a schematic diagram of a triggering exception detection mechanism provided by an embodiment of the present disclosure. When the SIGSEGV signal triggers the handle_signal function, the handle_signal function can be used to detect the stack pointer address in the SIGSEGV signal. When it is determined that the detection passes, the target space can be segmented from the non-readable and writable space according to the moving direction of the stack pointer starting from the highest address corresponding to the readable and writable space.
[0092] Exemplarily, the size of the target space for each segmentation may be preset. For example, the target space may be set to be the same size as the space corresponding to the readable and writable space for each segmentation, or for another example, a target space of 100KB may be set for each segmentation. The position corresponding to the stack pointer address is located in the segmented target space.
[0093] Furthermore, after the target space is segmented, the access attribute corresponding to the target space can be set to a read-write attribute, so that the target space is used as a newly added read-write space, that is, the read-write space is updated. Here, the highest address corresponding to the updated read-write space is the highest address corresponding to the target space. Figure 6 As shown, it is a schematic diagram of a target space provided by an embodiment of the present disclosure, wherein the memory space corresponding to c is the target space, the memory space corresponding to d is the readable and writable space, and the memory space corresponding to c plus the memory space corresponding to d is the updated readable and writable space.
[0094] S104: Return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
[0095] Here, the peak value is the maximum value of the memory space used by the thread during operation. After the target space is used to obtain the updated readable and writable space, since the thread can continue to execute, its corresponding stack pointer will also continue to move, so it is possible to return to execute the above S102 until the thread is executed.
[0096] When the thread is finished executing, the thread will no longer call the function, and the stack pointer corresponding to the thread will no longer move. At this time, the latest readable and writable space can be determined, and the space size corresponding to the latest readable and writable space is used as the peak value of the memory space used by the detected thread.
[0097] In this way, by setting a readable and writable space in the memory space allocated for the thread, and when it is determined that the stack pointer of the thread points to the unreadable and writable space other than the readable and writable space in the memory space during the execution process, a new target space is divided in the unreadable space as a way of increasing the readable and writable space for the thread to use, which does not affect the normal use of the thread, and can accurately determine the peak value of the memory space used by the thread based on the currently determined readable and writable space when the thread is executed. Furthermore, when the thread is re-run, the memory space matching the peak value can be allocated to the thread based on the determined peak value, which not only ensures the normal operation of the thread, but also improves the rationality of memory space allocation, and avoids the problem of memory space waste caused by allocating too large memory space.
[0098] In one embodiment, the step of dividing the target space in the non-readable and writable space based on the abnormal signal, the pre-set detection mechanism and the stack pointer address in S103 can be implemented according to the following steps:
[0099] S103-1: Use a detection mechanism to detect the stack pointer address carried in the abnormal signal to determine whether the stack pointer address is between the highest address and the stack start address.
[0100] Here, since there may be multiple types of exceptions in the system that can trigger the generation of an exception signal, after the detection mechanism is triggered by the exception signal, it is also necessary to determine whether the exception signal is a signal indicating that the stack pointer of the thread points to a non-readable and writable space.
[0101] In specific implementation, the detection mechanism can be used to detect the stack pointer address carried in the exception signal to determine whether the stack pointer address is between the highest address corresponding to the current readable and writable space and the stack starting address, that is, to determine whether the exception signal is a signal indicating that the stack pointer points to the non-readable and writable space in the memory space.
[0102] S103-2: If yes, then based on the stack pointer address and the highest address, a target space is partitioned out in the non-readable and writable space.
[0103] Exemplarily, when it is determined that the stack pointer address is between the highest address corresponding to the current readable and writable space and the stack start address, the target space can be segmented out from the unreadable and writable space between the highest address corresponding to the current readable and writable space and the stack pointer address.
[0104] In one embodiment, S103-2 may be implemented according to the following steps:
[0105] S103-2-1: Determine the size of the first memory space between the stack pointer address and the highest address.
[0106] In specific implementation, the non-readable and writable space between the stack pointer address and the highest address corresponding to the current readable and writable space can be used as the first memory space. Then, the space size corresponding to the first memory space can be determined.
[0107] S103-2-2: When it is determined that the space size of the first memory space is an integer multiple of the second preset size, the first memory space in the unreadable space is used as the target space; the second preset size is the default space size of a memory page space.
[0108] Here, the memory page space is a memory space with a default space size determined by a preset paging mechanism. The second preset size is the space size of a memory page space. Specifically, the second preset size can be 4KB. In specific implementation, the space size corresponding to the segmented target space needs to be an integer multiple of the second preset size, and the space size corresponding to the segmented minimum target space can be the second preset size.
[0109] Exemplarily, when the second preset size is 4 KB and the space size of the first memory space is 16 KB, the 4 KB first memory space may be used as the target space.
[0110] In another embodiment, when it is determined that the space size of the first memory space does not match an integer multiple of the second preset size, a second memory space whose space size is greater than the second preset size and is the minimum integer multiple of the second preset size is divided out from the unreadable space starting from the highest address.
[0111] In specific implementation, when the space size of the first memory space does not match the integer multiple of the second preset size, the second memory space with a space size greater than the second preset size and the minimum integer multiple of the second preset size can be split from the non-readable and writable space starting from the highest address corresponding to the current readable and writable space according to the moving direction of the stack pointer. For example, when the second preset size is 4KB and the space size of the first memory space is 13KB, the second memory space of 16KB can be split from the non-readable and writable space starting from the highest address corresponding to the current readable and writable space according to the moving direction of the stack pointer. Among them, the 16KB second memory space includes 13KB of the first memory space.
[0112] Furthermore, the access attribute corresponding to the second memory space can be set to a readable and writable attribute, that is, the second memory space is used as the target space; at this time, the stack pointer address pointed to by the stack pointer is located in the target space.
[0113] In another embodiment, when it is determined that the stack pointer address is not between the highest address and the stack start address, it can be determined that the signal currently triggering the detection mechanism is not a signal indicating that the stack pointer points to the unreadable and writable space, and then it can also be determined that the stack pointer does not point to the unreadable and writable space, and the thread can continue to run. Therefore, the abnormal signal can be ignored, and then the step of determining whether the stack pointer of the thread points to the unreadable and writable space in the memory space is returned to execute until the thread is executed, and the currently determined readable and writable space is used as the peak value of the memory space used by the thread.
[0114] In one embodiment, after determining the peak value, the peak value can also be fed back to the development end corresponding to the application, so that the development end can set a new memory space allocation program for the thread based on at least one peak value received. The new memory space allocation program is used to allocate memory space matching at least one peak value to the newly created thread when a new thread is created.
[0115] Exemplarily, after multiple detections of the peak values corresponding to the thread, the peak values obtained each time can be fed back to the development end corresponding to the application. The peak values detected multiple times can be the same or different. Afterwards, the development end user can determine the target space size of the memory space that needs to be allocated to the thread based on the multiple peak values obtained by the development end. For example, the maximum peak value among the multiple peak values can be used as the target space size of the memory space that needs to be allocated to the thread. Afterwards, the developer can set a new memory space allocation program on the development end for allocating memory space of the target space size to the thread.
[0116] Furthermore, when the thread is subsequently created, the new memory space allocation program can be used to allocate memory space of the target space size for the newly created thread, instead of allocating memory space of the default size (such as 1MB). This can not only ensure the normal operation of the thread, but also improve the rationality of memory space allocation and avoid the problem of memory space waste caused by allocating too large memory space.
[0117] In another embodiment, after determining the peak value corresponding to the thread, the system can also directly update the memory space allocation program for the thread based on the peak value. The updated memory space allocation program is used to allocate peak size memory space to the newly created thread. In this way, when the thread is newly created subsequently, the memory space matching the peak value can be allocated to the newly created thread based on the updated memory space allocation program. Alternatively, the system can also record multiple peak values detected, and then determine the most reasonable target space size based on the multiple peak values, and update the memory space allocation program corresponding to the thread based on the target space size. Afterwards, the updated memory space allocation program can be used to allocate memory space with the target space size to the newly created thread.
[0118] In this way, the system dynamically updates the memory space allocation program directly based on the determined peak value, thereby improving the efficiency of updating the memory space allocation program.
[0119] In one embodiment, for the step S104: determining the peak value of the memory space used by the thread based on the currently determined readable and writable space, it is also possible to determine whether the space size of the currently determined readable and writable space is greater than the first preset size. If so, the space size of the currently determined readable and writable space can be directly used as the peak value corresponding to the thread.
[0120] That is, when the thread is executed, it is determined whether the currently determined readable and writable space is a readable and writable space of the first preset size. If not, the size of the currently determined readable and writable space can be directly used as the peak value corresponding to the thread.
[0121] In another embodiment, when the size of the currently determined readable and writable space is equal to the first preset size, the size of the readable and writable space having the first preset size is used as the peak value. For example, if the size of the currently determined readable and writable space is 200KB when the thread is executed, it can be determined that the readable and writable space of 200KB initially divided can be used for the normal operation of the thread, and therefore, 200KB can be directly used as the peak value corresponding to the thread.
[0122] Alternatively, in another embodiment, when the size of the currently determined readable and writable space is equal to the first preset size, the target stack pointer address pointed to by the stack pointer of the thread when the thread is executed can be determined. Furthermore, the size of the memory space between the stack end address and the target stack pointer address can be used as the peak value corresponding to the thread.
[0123] Exemplarily, when the target stack pointer address corresponds to a position at 160 KB in the 200 KB readable and writable space initially divided, 160 KB may be used as the peak value corresponding to the thread.
[0124] Those skilled in the art will appreciate that, in the above method of specific implementation, the order in which the steps are written does not imply a strict execution order and does not constitute any limitation on the implementation process. The specific execution order of the steps should be determined by their functions and possible internal logic.
[0125] Based on the same inventive concept, a peak detection device corresponding to the peak detection method is also provided in the embodiment of the present disclosure. Since the principle of solving the problem by the device in the embodiment of the present disclosure is similar to the above-mentioned peak detection method in the embodiment of the present disclosure, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0126] like Figure 7FIG. 1 is a schematic diagram of a peak detection device provided by an embodiment of the present disclosure, comprising:
[0127] The acquisition module 701 is used to acquire a stack end address and a stack start address corresponding to the memory space allocated for the thread running in the application, and set a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space is lower than the stack start address;
[0128] A judgment module 702 is used to determine whether the stack pointer of the thread points to the non-readable and writable space in the memory space during the execution of the thread;
[0129] The segmentation module 703 is used for generating an abnormal signal if yes, segmenting a target space from the non-readable and writable space based on the abnormal signal, a preset detection mechanism and a stack pointer address, and using the target space as a newly added readable and writable space;
[0130] The loop module 704 is used to return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
[0131] In a possible implementation, the device further includes:
[0132] Feedback module 705 is used to feed back the peak value to the development end corresponding to the application after determining the peak value, so that the development end can set a new memory space allocation program for the thread based on at least one of the received peak values, and the new memory space allocation program is used to allocate memory space matching at least one of the peak values to the newly created thread when the thread is newly created.
[0133] In a possible implementation manner, the method further includes:
[0134] The update module 706 is used to update the memory space allocation program for the thread based on the peak value after determining the peak value, and allocate memory space matching the peak value to the newly created thread based on the updated memory space allocation program when the thread is newly created.
[0135] In a possible implementation, the segmentation module 703 is used to detect the stack pointer address carried in the abnormal signal using the detection mechanism when segmenting the target space in the unreadable and writable space based on the abnormal signal, the pre-set detection mechanism and the stack pointer address, to determine whether the stack pointer address is between the highest address and the stack start address;
[0136] If so, the target space is partitioned out in the non-readable and writable space based on the stack pointer address and the highest address.
[0137] In one possible implementation, the segmentation module 703 is also used to return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space when it is determined that the stack pointer address is not between the highest address and the stack starting address, until the thread is executed, and use the currently determined readable and writable space as the peak value of the memory space used by the thread.
[0138] In a possible implementation, the segmentation module 703, when segmenting the target space in the unreadable and writable space based on the stack pointer address and the highest address, is used to determine the space size of the first memory space between the stack pointer address and the highest address;
[0139] When it is determined that the space size of the first memory space is an integer multiple of a second preset size, the first memory space in the unreadable space is used as the target space; the second preset size is a default space size of a memory page space.
[0140] In a possible implementation manner, the segmentation module 703 is further configured to, when it is determined that the space size of the first memory space does not match the integer multiple of the second preset size, segment, starting from the highest address, a second memory space having a space size greater than the second preset size and being a minimum integer multiple of the second preset size in the unreadable space;
[0141] The second memory space is used as the target space; wherein the stack pointer address pointed to by the stack pointer address is located in the target space.
[0142] In one possible implementation, the loop module 704, when determining the peak value of the memory space used by the thread based on the currently determined readable and writable space, is used to use the space size of the currently determined readable and writable space as the peak value when the space size of the currently determined readable and writable space is greater than the first preset size.
[0143] In a possible implementation, the loop module 704, when determining the peak value of the memory space used by the thread based on the currently determined readable and writable space, is used to, when the space size of the currently determined readable and writable space is equal to the first preset size, use the space size of the readable and writable space having the first preset size as the peak value; or
[0144] When the size of the currently determined readable and writable space is equal to the first preset size, determining a target stack pointer address pointed to by the stack pointer of the thread when the thread is executed;
[0145] The size of the memory space between the stack end address and the target stack pointer address is taken as the peak value.
[0146] For descriptions of the processing flow of each module in the device and the interaction flow between each module, reference may be made to the relevant descriptions in the above method embodiment, which will not be described in detail here.
[0147] Based on the same technical concept, the embodiment of the present application also provides a computer device. Figure 8 FIG. 1 is a schematic diagram of a computer device provided in an embodiment of the present application, including:
[0148] Processor 81, memory 82 and bus 83. The memory 82 stores machine-readable instructions executable by the processor 81, and the processor 81 is used to execute the machine-readable instructions stored in the memory 82. When the machine-readable instructions are executed by the processor 81, the processor 81 executes the following steps: S101: obtain the stack end address and stack start address corresponding to the memory space allocated for the thread running in the application, and set a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space is lower than the stack start address; S102: during the execution of the thread, determine whether the stack pointer of the thread points to the unreadable and writable space in the memory space; S103: if so, generate an abnormal signal, and based on the abnormal signal, the preset detection mechanism and the stack pointer address, divide the target space in the unreadable and writable space, and use the target space as the newly added readable and writable space; and S104: return to the step of determining whether the stack pointer of the thread points to the unreadable and writable space in the memory space, until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
[0149] The above-mentioned memory 82 includes internal memory 821 and external memory 822; the memory 821 here is also called internal memory, which is used to temporarily store the calculation data in the processor 81, as well as the data exchanged with the external memory 822 such as the hard disk. The processor 81 exchanges data with the external memory 822 through the internal memory 821. When the computer device is running, the processor 81 and the memory 82 communicate through the bus 83, so that the processor 81 executes the execution instructions mentioned in the above-mentioned method embodiment.
[0150] The present disclosure also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the peak detection method described in the above method embodiment are executed. The storage medium can be a volatile or non-volatile computer-readable storage medium.
[0151] The computer program product of the peak detection method provided in the embodiment of the present disclosure includes a computer-readable storage medium storing program code, and the instructions included in the program code can be used to execute the steps of the peak detection method described in the above method embodiment. For details, please refer to the above method embodiment, which will not be repeated here.
[0152] The computer program product may be implemented in hardware, software or a combination thereof. In one optional embodiment, the computer program product is embodied as a computer storage medium, and in another optional embodiment, the computer program product is embodied as a software product, such as a software development kit (SDK) and the like.
[0153] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present disclosure, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, and the indirect coupling or communication connection of the device or unit can be electrical, mechanical or other forms.
[0154] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0155] In addition, each functional unit in each embodiment of the present disclosure may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0156] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: various media that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0157] If the technical solution of this application involves personal information, the product using the technical solution of this application has clearly informed the personal information processing rules and obtained the individual's voluntary consent before processing the personal information. If the technical solution of this application involves sensitive personal information, the product using the technical solution of this application has obtained the individual's separate consent before processing the sensitive personal information, and at the same time meets the "explicit consent" requirement. For example, on personal information collection devices such as cameras, clear and prominent signs are set to inform that the personal information collection scope has been entered and personal information will be collected. If the individual voluntarily enters the collection scope, it is deemed that he or she agrees to the collection of his or her personal information; or on the device that processes personal information, the personal information processing rules are notified by obvious signs / information, and the individual's authorization is obtained through pop-up information or by asking the individual to upload his or her personal information; among them, the personal information processing rules may include information such as the personal information processor, the purpose of personal information processing, the processing method, and the type of personal information processed.
[0158] Finally, it should be noted that the above-described embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The protection scope of the present disclosure is not limited thereto. Although the present disclosure is described in detail with reference to the above-described embodiments, ordinary technicians in the field should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above-described embodiments within the technical scope disclosed in the present disclosure, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.
Claims
1. A peak detection method, characterized in that: include: Obtaining a stack end address and a stack start address corresponding to a memory space allocated for a thread running in an application, and setting a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space is lower than the stack start address; During the execution of the thread, determining whether a stack pointer of the thread points to a non-readable and non-writable space in the memory space; If yes, an exception signal is generated, and based on the exception signal, a preset detection mechanism and a stack pointer address, a target space is segmented out from the non-readable and writable space, and the target space is used as a newly added readable and writable space; Return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
2. The method according to claim 1, characterized in that After determining the peak value, the method further comprises: The peak value is fed back to the development end corresponding to the application, so that the development end can set a new memory space allocation program for the thread based on at least one of the received peak values, and the new memory space allocation program is used to allocate memory space matching at least one of the peak values to the newly created thread when the thread is newly created.
3. The method according to claim 1, characterized in that After determining the peak value, the method further comprises: Based on the peak value, a memory space allocation program for the thread is updated, and when the thread is newly created, a memory space matching the peak value is allocated to the newly created thread based on the updated memory space allocation program.
4. The method according to claim 1, characterized in that The method of dividing a target space in the unreadable and writable space based on the abnormal signal, a preset detection mechanism and a stack pointer address includes: Detecting the stack pointer address carried in the abnormal signal using the detection mechanism to determine whether the stack pointer address is between the highest address and the stack start address; If so, the target space is partitioned out in the non-readable and writable space based on the stack pointer address and the highest address.
5. The method according to claim 4, characterized in that The method further comprises: When it is determined that the stack pointer address is not between the highest address and the stack starting address, return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and use the currently determined readable and writable space as the peak value of the memory space used by the thread.
6. The method according to claim 4, characterized in that The step of dividing the target space in the non-readable and writable space based on the stack pointer address and the highest address includes: Determine the space size of a first memory space between the stack pointer address and the highest address; When it is determined that the space size of the first memory space is an integer multiple of a second preset size, the first memory space in the non-readable and writable space is used as the target space; the second preset size is a default space size of a memory page space.
7. The method according to claim 6, characterized in that The method further comprises: When it is determined that the space size of the first memory space does not match the integer multiple of the second preset size, starting from the highest address, a second memory space having a space size greater than the second preset size and being a minimum integer multiple of the second preset size is divided from the unreadable and writable space; The second memory space is used as the target space; wherein the stack pointer address pointed to by the stack pointer address is located in the target space.
8. The method according to claim 1, characterized in that The determining the peak value of the memory space used by the thread based on the currently determined readable and writable space includes: In a case where the size of the currently determined readable and writable space is greater than the first preset size, the size of the currently determined readable and writable space is used as the peak value.
9. The method according to claim 1, characterized in that: The determining the peak value of the memory space used by the thread based on the currently determined readable and writable space includes: In a case where the size of the currently determined readable and writable space is equal to the first preset size, taking the size of the readable and writable space having the first preset size as the peak value; or When the size of the currently determined readable and writable space is equal to the first preset size, determining a target stack pointer address pointed to by the stack pointer of the thread when the thread is executed; The size of the memory space between the stack end address and the target stack pointer address is taken as the peak value.
10. A peak detection device, characterized in that: include: an acquisition module, configured to acquire a stack end address and a stack start address corresponding to a memory space allocated for a thread running in an application, and to set a readable and writable space of a first preset size in the memory space starting from the stack end address; the highest address corresponding to the readable and writable space being lower than the stack start address; A judgment module, used for determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space during the execution of the thread; a segmentation module, configured to generate an exception signal if yes, segment a target space from the non-readable and writable space based on the exception signal, a preset detection mechanism and a stack pointer address, and use the target space as a newly added readable and writable space; The loop module is used to return to the step of determining whether the stack pointer of the thread points to the non-readable and writable space in the memory space until the thread is executed, and determine the peak value of the memory space used by the thread based on the currently determined readable and writable space.
11. A computer device, characterized in that: include: A processor and a memory, wherein the memory stores machine-readable instructions executable by the processor, and the processor is used to execute the machine-readable instructions stored in the memory. When the machine-readable instructions are executed by the processor, the processor executes the steps of the peak detection method according to any one of claims 1 to 9.
12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program. When the computer program is executed by a computer device, the computer device performs the steps of the peak detection method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Method and device for realizing use condition of thread stack
CN104572448A
Dynamic stack memory management method and device
CN106776354A