Request processing method and apparatus

By allocating a smaller initial thread space to a thread while it is in a waiting state and switching to a larger target thread space when processing a request, the problems of memory waste and low cache hit rate in the thread waiting state are solved, thus saving memory resources and improving performance.

CN114968574BActive Publication Date: 2025-11-04BEIJING OCEANBASE TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210571445.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-05-24
Publication Date
2025-11-04
Estimated Expiration
2042-05-24

AI Technical Summary

Technical Problem

In existing technologies, the memory waste and low cache hit rate caused by threads in a waiting state lead to a decrease in program performance.

Method used

By allocating initial thread space to threads and dynamically switching to a target thread space larger than the initial thread space when processing requests, thread stack memory waste is reduced and cache hit rate is improved.

Benefits of technology

This effectively avoids memory waste, improves cache hit rate, and enhances program performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114968574B_ABST
    Figure CN114968574B_ABST
Patent Text Reader

Abstract

Embodiments of the present specification provide a request processing method and device, wherein the request processing method comprises: assigning a corresponding target thread to an object processing request from at least two initial threads; determining an initial thread space in memory corresponding to the target thread, and determining current state information of a preset thread space in the memory, wherein the preset thread space is larger than the initial thread space; determining a corresponding target thread space for the target thread from the preset thread space based on the current state information; replacing the initial thread space with the target thread space, and processing the object processing request based on the target thread and the corresponding target thread space, thereby avoiding memory waste and saving memory resources.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] Embodiments of the present specification relate to the technical field of computer technology, and particularly relate to a request processing method. BACKGROUND

[0002] With the continuous development of computer technology, the system will allocate corresponding stack memory for each thread processing request. And in order to prevent the thread from processing the request to carry out a large number of stack memory allocation, which leads to the problem of stack explosion, the stack memory of the thread will be set to be relatively large. However, when the thread is in the waiting state, the memory usage of the stack is very small, and in actual application, most of the threads are in the waiting state at the same time, so there is a lot of memory waste in the thread stack memory. SUMMARY

[0003] Therefore, the embodiments of the present specification provide a request processing method. One or more embodiments of the present specification also relate to a request processing apparatus, a computing device, a computer readable storage medium, and a computer program to solve the technical defects in the prior art.

[0004] According to a first aspect of the embodiments of the present specification, a request processing method is provided, comprising:

[0005] allocating a corresponding target thread for an object processing request from at least two initial threads;

[0006] determining the initial thread space in the memory corresponding to the target thread, and determining the current state information of the preset thread space in the memory, wherein the preset thread space is larger than the initial thread space;

[0007] determining the corresponding target thread space for the target thread from the preset thread space based on the current state information;

[0008] replacing the initial thread space with the target thread space, and processing the object processing request based on the target thread and the corresponding target thread space.

[0009] According to a second aspect of the embodiments of the present specification, a request processing apparatus is provided, comprising:

[0010] The allocation module is configured to allocate a corresponding target thread for an object processing request from at least two initial threads;

[0011] The first determination module is configured to determine the initial thread space in the memory corresponding to the target thread, and determine the current state information of the preset thread space in the memory, wherein the preset thread space is larger than the initial thread space;

[0012] a second determining module, configured to determine a corresponding target thread space for the target thread from the preset thread space based on the current state information;

[0013] a processing module, configured to replace the initial thread space with the target thread space, and process the object processing request based on the target thread and the corresponding target thread space.

[0014] According to a third aspect of the embodiments of the present specification, a computing device is provided, comprising:

[0015] a memory and a processor;

[0016] The memory is configured to store computer executable instructions, and the processor is configured to execute the computer executable instructions, and the computer executable instructions, when executed by the processor, implement the steps of the request processing method.

[0017] According to a fourth aspect of the embodiments of the present specification, a computer readable storage medium is provided, which stores computer executable instructions, and the computer executable instructions, when executed by a processor, implement the steps of the request processing method.

[0018] According to a fifth aspect of the embodiments of the present specification, a computer program is provided, and when the computer program is executed in a computer, the computer program causes the computer to execute the steps of the request processing method.

[0019] The request processing method provided by the present specification comprises assigning a corresponding target thread for an object processing request from at least two initial threads; determining an initial thread space in memory corresponding to the target thread, and determining current state information of a preset thread space in the memory, wherein the preset thread space is greater than the initial thread space; determining a corresponding target thread space for the target thread from the preset thread space based on the current state information; replacing the initial thread space with the target thread space, and processing the object processing request based on the target thread and the corresponding target thread space.

[0020] Specifically, after the method assigns a target thread with an initial thread space for an object processing request, it determines a corresponding target thread space for the target thread from a preset thread space greater than the initial thread space, and flexibly replaces the initial thread space with the target thread space in the process of processing the object processing request, thereby realizing processing of the object processing request based on the target thread and the target thread space greater than the initial thread space, avoiding memory waste, and saving memory resources. BRIEF DESCRIPTION OF DRAWINGS

[0021] Figure 1is a schematic diagram of an application scenario of a request processing method provided by an embodiment of the present specification;

[0022] Figure 2 is a flowchart of a request processing method provided by an embodiment of the present specification;

[0023] Figure 3 is a process flowchart of a request processing method provided by an embodiment of the present specification;

[0024] Figure 4 is a structural schematic diagram of a request processing apparatus provided by an embodiment of the present specification;

[0025] Figure 5 is a structural block diagram of a computing device provided by an embodiment of the present specification. DETAILED DESCRIPTION

[0026] In the following description, numerous specific details are set forth in order to provide a thorough understanding of the present specification. However, the present specification can be practiced without the specific details, other than in the examples, and it is understood that the scope of the present specification is not limited to the details below.

[0027] The terminology used in one or more embodiments of the present specification is for the purpose of describing particular embodiments only and is not intended to be limiting of one or more embodiments of the present specification. As used in one or more embodiments of the present specification and the accompanying claims, the singular forms "a," "an," and "the" are intended to include the plural forms as well, unless the context clearly indicates otherwise. It will be further understood that the terms "comprises" and / or "comprising," when used in one or more embodiments of the present specification, specify the presence of stated features, integers, steps, operations, elements, and / or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, and / or groups thereof.

[0028] It will be understood that, although the terms first, second, etc. can be employed in describing various information, such information should not be limited by these terms. These terms are only used to differentiate one piece of information from another. For example, a first can be termed a second, and, similarly, a second can be termed a first, without departing from the scope of one or more embodiments of the present specification. Depending on the context, the word "if" as used herein can be interpreted to mean "when" or "in response to determining" or "in response to a determination."

[0029] First, the noun terms related to one or more embodiments of the present specification are explained.

[0030] Thread: The smallest unit of operation scheduling by the operating system. It is contained in a process and is the actual unit of operation in the process.

[0031] Stack: In computer science, a space that stores messages about running subprograms.

[0032] Cache: A high-speed memory that is accessed faster than general random access memory (RAM).

[0033] pthread: A standard thread library under Linux, which defines a set of thread-related APIs.

[0034] For a thread that processes user requests, the memory usage of the stack is small when the thread is in a waiting state, but once the request arrives to start processing, these threads enter an active state (processing request state) and often start a large amount of memory allocation on the stack, so in order to prevent stack overflow, the thread stack memory is set to be relatively large.

[0035] However, in the scenario where the number of threads is much larger than the number of CPU cores, most threads are in a waiting state at the same time, so the thread stack memory actually has a lot of memory waste. Frequent switching of threads and a large amount of memory usage can cause the page where the stack space is located to be frequently replaced out of the cache, thereby causing a low cache hit rate and affecting the performance of the program. A way needs to be found to reduce the waste of thread stack memory, thereby improving the cache hit rate and improving the performance of the program.

[0036] Based on this, the present specification provides a scheme for reducing the number of threads, which can reduce the number of thread stack memories by limiting the number of threads, thereby reducing unnecessary cache replacement. However, this scheme has certain defects, reducing the number of threads means that each thread needs to be able to handle completely different logic, which greatly limits the software architecture design. Therefore, the present specification provides a request processing method, in the case of receiving an object processing request, first allocating a corresponding target thread for the object processing request from at least two initial threads; second, determining the initial thread space in the memory corresponding to the target thread, and determining the current state information of the preset thread space in the memory, it should be noted that the preset thread space is larger than the initial thread space; and based on the current state information of the preset thread space, determine the corresponding target thread space for the target thread from the preset thread space; finally, replace the initial thread space with the target thread space, and process the object processing request based on the target thread and the corresponding target thread space. Thus, a smaller initial thread space is allocated to the target thread when it is idle, and the smaller initial thread space is flexibly switched to a larger target thread space when the target thread is processing the object processing request, thereby avoiding memory waste and saving memory resources.

[0037] And, since the target thread is allocated a corresponding target thread space from a preset thread space during processing of the object processing request, the preset thread space is consistent with the number of CPU cores, so that the page where the stack space is located is not frequently replaced out of the cache during thread switching and a large amount of memory usage, thereby causing a low cache hit rate, affecting the performance of the program. That is, by reducing the thread stack memory usage, the thread stack cache hit rate is indirectly improved, thereby improving the performance.

[0038] Specifically, in the present specification, a request processing method is provided, and the present specification also relates to a request processing device, a computing device, a computer readable storage medium and a computer program, which are described in detail one by one in the following embodiments.

[0039] Figure 1 An application scenario schematic diagram of a request processing method provided by an embodiment of the present specification is shown, referring to FIG. 1. Figure 1 As shown, the terminal is in communication with the server and can send an object processing request to the server. The object processing request can be any request that needs to be processed by the server, and the present specification does not make specific limitations thereon, for example, a write request, a read request, a query request, an IO request, and the like.

[0040] After receiving the request (i.e., the object processing request), the server can select a thread in an idle state (i.e., a waiting state) from the pre-created threads to serve as a thread for processing the request. Then the request is processed by the thread allocated for the request. During processing of the request, the current stack of the thread needs to be saved first. In actual application, the request processing method provided by the present specification can allocate a smaller thread stack space for the thread in the waiting state from the memory. And during processing of the request by the thread, a larger stack space is allocated for the thread from the multiple larger stack spaces pre-divided. Finally, after determining that the request processing is completed, the large stack (i.e., the large stack space) corresponding to the thread is switched to the small stack (i.e., the current stack pre-saved). Based on this, the request processing method provided by the present specification is limited to the modification of the underlying framework, does not involve complex upper code modification, and has no any influence on subsequent newly added threads and design. Therefore, the cache hit rate is improved, and the cost of the high-frequency operation of accessing the thread stack is reduced, thereby improving the performance.

[0041] Specifically, Figure 2 A flowchart of a request processing method provided by an embodiment of the present specification is shown, specifically including the following steps.

[0042] Step 202: allocating a corresponding target thread for the object processing request from at least two initial threads.

[0043] The initial thread can be understood as a thread created in advance by the server for processing a request. The object processing request can refer to the above explanation, and the target thread can be understood as a thread in the initial thread for processing the object processing request.

[0044] Specifically, the server can assign a corresponding target thread for the object processing request from at least two initial threads created in advance when receiving the object processing request, and subsequently process the object processing request through the target thread.

[0045] The application of the request processing method in improving the stack memory cache hit rate in a multi-thread scenario is taken as an example for further description of assigning a corresponding target thread for an object processing request from at least two initial threads. The object processing request can be understood as a write request. Based on this, the server can assign a thread in a waiting state for the write request from a plurality of threads created in advance, and subsequently process the write request based on the thread.

[0046] Further, in the embodiments provided in the specification, it should be noted that since the call stack (i.e., thread stack space) when waiting for a request is short, the default thread stack size of all threads can be adjusted to be very small, and the thread stack is expanded until the request arrives. Based on this, before the object processing request is assigned to a corresponding target thread from at least two initial threads, it further includes:

[0047] The at least two initial threads are created, and each initial thread is assigned a corresponding initial thread space from the memory.

[0048] The initial thread space can be understood as a stack space allocated in advance from the memory for the initial thread. In actual application, the part actually effective in the request processing method provided in the specification is to reduce the size of the thread stack (i.e., thread stack space, stack space). The way to reduce the size of the thread stack can be achieved by any way that can scale the thread stack, which is not limited in the specification, for example, the stack space used by the thread can be modified by pthread_attr_setstack (a function).

[0049] Specifically, the server creates at least two initial threads for the object processing request after receiving the object processing request, and assigns a corresponding initial thread space for each initial thread from the memory, thereby reducing the waste of memory by the thread in a waiting state and saving memory resources.

[0050] Step 204: determining an initial thread space in memory corresponding to the target thread, and determining current state information of a preset thread space in the memory, wherein the preset thread space is greater than the initial thread space.

[0051] The preset thread space can be understood as a thread stack space pre-allocated from the memory for multiple threads, wherein the preset thread space is greater than the initial thread space.

[0052] The current state information can be understood as information representing whether the preset thread space is in an idle state (i.e. unused state), including but not limited to utilization of the preset thread space, state identifier of the preset thread space, etc.

[0053] Specifically, after determining the corresponding target thread for the object processing request, the server determines the initial thread space pre-allocated from the memory for the target thread, and determines the current state information of the preset thread space pre-divided in the memory. This facilitates subsequent determination of the corresponding target thread space for the target thread from the preset thread space based on the current state information.

[0054] In the above example, the initial thread space is the current stack space (small stack space) of the thread, and the preset thread space is the large stack space. Based on this, after determining the corresponding thread for the write request, the server can determine the current stack of the thread and determine the current state information of the pre-divided large stack space.

[0055] Step 206: determining the corresponding target thread space for the target thread from the preset thread space based on the current state information.

[0056] In the above example, the server determines a corresponding large stack space for the thread from multiple large stack spaces based on the current state information of the large stack space.

[0057] In the embodiments of the present disclosure, the determination of the corresponding target thread space for the target thread from the preset thread space based on the current state information comprises:

[0058] determining an idle preset thread space from the preset thread space based on the current state information;

[0059] determining the corresponding target thread space for the target thread based on the idle preset thread space.

[0060] Specifically, the server determines an idle preset thread space in the preset thread spaces based on the previous state information after determining the current state information representing whether each preset thread space is currently in an idle state, and determines a corresponding target thread space for the target thread based on the idle preset thread space, so that the subsequent target thread can process the object processing request based on a larger target thread space, thereby avoiding the problem of stack overflow.

[0061] It should be noted that when the idle preset thread space is one, the idle preset thread space can be used as the target thread to determine the corresponding target thread space. When the idle preset thread space is multiple, any one of the multiple idle preset thread spaces can be selected as the target thread to determine the corresponding target thread space.

[0062] Step 208: Replacing the initial thread space with the target thread space, and processing the object processing request based on the target thread and the corresponding target thread space.

[0063] Specifically, after determining the corresponding target thread space for the target thread, the server can save the initial thread space previously allocated for the target thread, and replace the initial thread space with the target thread space, that is, cancel the correspondence between the thread and the initial thread space, and establish an association between the target thread space and the target thread. Then, the object processing request is processed based on the target thread and the target thread space.

[0064] Continuing with the above example, after determining the corresponding large stack space for the thread, the server can save the current stack of the thread, switch the current stack to the larger stack space, and process the write request based on the thread and the corresponding larger stack space.

[0065] In the embodiments provided in the present specification, in order to ensure that the thread stack space corresponding to the thread does not waste memory resources, after the processing request is executed, the target thread space is replaced with the initial thread space, so that the thread space corresponding to the target thread in the waiting state does not waste too much memory resources. Specifically, after the object processing request is processed based on the target thread and the corresponding target thread space, the method further includes:

[0066] Determining the initial thread space in the memory corresponding to the target thread;

[0067] Replacing the target thread space with the initial thread space, and releasing the target thread space in the memory.

[0068] Continuing with the above example, after the server finishes executing the object processing request, the server can determine the current stack (small stack space) in which the thread was previously saved, and replace the large stack space with the small stack space.

[0069] Further, in the embodiments provided in the specification, the processing of the object processing request based on the target thread and the corresponding target thread space includes:

[0070] determining request running data corresponding to the object processing request, and sending the request running data to the target thread space;

[0071] running the request running data in the target thread space through the target thread to process the object processing request.

[0072] The request running data can be understood as data used to process the object processing request, including but not limited to variables, functions, parameters, etc. It should be noted that the request running data can be data carried in the object processing request and / or data obtained from the local server based on the object processing request. The specification does not make specific limitations on this.

[0073] Specifically, after the server replaces the initial thread space of the target thread with the target thread space, the server can obtain request running parameters corresponding to the object processing request from the data possessed by the server and / or data carried in the object processing request, and store the request running parameters in the target thread space. Running the request running data in the target thread space through the target thread to process the object processing request. Thus, while ensuring that memory resources are not wasted and stack overflow requests do not occur, the processing of the object processing request is completed.

[0074] In the embodiments provided in the specification, if a large number of thread-local variables are used in the project, frequently switching threads to access their respective thread stack memories will also affect the cache hit rate. Therefore, a relatively simple approach is to try to reuse a small number of memory segments for all thread-local variables, thereby improving the cache hit rate of thread-local variables. Specifically, the processing of the object processing request based on the target thread and the corresponding target thread space includes:

[0075] determining a target variable space corresponding to the target thread from at least two variable spaces in the memory;

[0076] processing the object processing request based on the target thread, the target variable space, and the target thread space.

[0077] The at least two variable spaces can be understood as spaces pre-divided for storing thread-local variables of each thread.

[0078] It should be noted that the multiplexed memory can be constructed separately, or can be created by creating several dummy threads (a kind of thread) as a memory pool for use, but no matter which way is adopted, the requirement that the thread local variable must be constructed before use must be met. Therefore, the most simple way is actually to use the existing thread, and after all threads are completed, the thread local variable space of all threads is sorted according to the access order, and the thread local variable space that is used most recently is always provided for new requests.

[0079] That is to say, the thread local variable space of each thread is no longer owned by the thread itself after the construction is completed, but can be used as other thread local variable space at any time.

[0080] Specifically, in order to improve the cache hit rate of the thread variable, the server can determine a corresponding target variable space for a target thread from at least two variable spaces in the memory, and process an object processing request based on the target thread, the target variable space, and the target thread space.

[0081] Further, in the embodiments provided in the specification, since the thread local variable is continuously stored at the head of the thread stack, and the local variable is accessed by adding a fixed offset to the base address recorded in the segment register, only the base address recorded in the segment register needs to be modified when switching the thread stack, so that different variable spaces can be switched at the same time when the thread stack is switched. Based on this, the determination of the corresponding target variable space for the target thread from the at least two variable spaces in the memory includes:

[0082] Determine at least two variable spaces in the memory and space state information of each variable space;

[0083] Determine the corresponding to-be-processed variable space for the target thread from the at least two variable spaces based on the space state information of the variable space;

[0084] Determine address information of the to-be-processed variable space, add the address information to an address storage unit corresponding to the target thread, and obtain a target variable space corresponding to the target thread.

[0085] The space state information can be understood as a request indicating whether the variable space is in an idle state, for example, utilization rate of the variable space, an idle identifier of the variable space, and the like.

[0086] The to-be-processed variable space can be understood as a variable space that needs to be allocated to the target thread.

[0087] The address information of the to-be-processed variable space can be understood as information representing the address of the to-be-processed variable space in the memory. For example, a base address.

[0088] The address storage unit can be understood as a unit for storing the address of the space (including but not limited to a variable space, a thread space, etc.) corresponding to the target thread, for example, a segment register.

[0089] Specifically, the server can determine at least two variable spaces in the memory and space state information of each variable space, determine a to-be-processed variable space corresponding to the target thread based on the space state information of the variable space from the at least two variable spaces, determine the address information of the to-be-processed variable space, add the address information to the address storage unit corresponding to the target thread, obtain a target variable space corresponding to the target thread, and thereby improve the cache hit rate of the thread local variable.

[0090] Specifically, the server can determine at least two variable spaces in the memory and space state information of each variable space, determine a to-be-processed variable space corresponding to the target thread based on the space state information of the variable space from the at least two variable spaces, determine the address information of the to-be-processed variable space, add the address information to the address storage unit corresponding to the target thread, obtain a target variable space corresponding to the target thread, and thereby improve the cache hit rate of the thread local variable.

[0091] Based on the historical access frequencies of the at least two variable spaces, the variable space with the highest historical access frequency among the at least two variable spaces is determined as the to-be-processed variable space corresponding to the target thread.

[0092] In actual application, since all thread local variable spaces of all threads are sorted according to the access order after all threads are completed, the thread local variable space that is used most recently is always provided for new requests, based on which the server can determine the historical access frequencies of the current multiple variable spaces, and determine the variable space with the highest historical access frequency among the at least two variable spaces as the to-be-processed variable space corresponding to the target thread.

[0093] In the embodiments provided in the specification, the processing of the object processing request based on the target thread, the target variable space, and the target thread space includes:

[0094] determining request running data corresponding to the object processing request and request variables corresponding to the object processing request;

[0095] sending the request running data to the target thread space and sending the request variables to the target variable space;

[0096] running the request running data in the target thread space by the target thread and running the request variables in the target variable space by the target thread, to process the object processing request.

[0097] In this context, the request variable can be understood as a variable required by the target thread when processing the request. This variable is stored locally on the server. For example, this request variable can be understood as a thread-local variable.

[0098] Specifically, after replacing the initial thread space of the target thread with the target thread space and determining the target variable space, the server can determine the request execution parameters corresponding to the object processing request from the data it holds and / or from the data carried by the object processing request, store the request execution parameters in the target thread space, and store the variables required by the object processing request in the target variable space.

[0099] The request to process the object is executed by running the data in the target thread space and by running the request variables in the target variable space. This ensures that memory resources are not wasted and that stack overflow requests do not occur, while completing the processing of the object's processing request. Furthermore, by prioritizing the use of thread-local variable space in the cache, the thread stack cache hit rate is improved, thereby enhancing performance.

[0100] In the request processing method provided in this specification, after allocating a target thread with an initial thread space for the object processing request, a corresponding target thread space is determined for the target thread from a preset thread space larger than the initial thread space. Furthermore, during the process of processing the object processing request, the initial thread space is flexibly replaced with the target thread space, thereby realizing the processing of object processing requests based on the target thread and the target thread space larger than the initial thread space, avoiding memory waste and saving memory resources.

[0101] The following is in conjunction with the appendix Figure 3 Taking the application of the request processing method provided in this specification in improving the stack memory cache hit rate in a multi-threaded scenario as an example, the request processing method will be further explained. Figure 3 The present specification illustrates a flowchart of a request processing method according to an embodiment, which includes the following steps.

[0102] Step 302: Create a thread.

[0103] Specifically, the server pre-creates multiple threads for subsequent request processing. During thread creation, a small storage area is allocated from memory for each thread as its stack space (small stack space).

[0104] Step 304: Waiting for a request.

[0105] Specifically, the server creates multiple threads and waits for receiving a request sent by a terminal. During this process, the multiple threads are in a waiting state.

[0106] Step 306: The request arrives.

[0107] Specifically, the server receives a request sent by a terminal, wherein the request includes but is not limited to a write request, a read request, and the like.

[0108] Step 308: Process the request.

[0109] Specifically, the server selects a thread from the threads in the waiting state, and subsequently processes the request based on the thread.

[0110] Step 310: Save the current stack.

[0111] Specifically, after determining the corresponding thread for the received request, the server can determine the current stack of the thread (i.e., the small stack space allocated to the thread when the thread is created), and save the current stack of the thread.

[0112] Step 312: Allocate a larger stack space and switch to a large stack.

[0113] Specifically, the server allocates a larger stack space for the thread from the multiple large stack spaces pre-divided, and switches the current stack of the thread to the large stack space.

[0114] In actual applications, in order to reduce the waste of thread stack memory and improve the cache hit rate, the server allocates a smaller stack space for the thread in the waiting state. However, in order to prevent the thread from entering an active state and causing a large amount of memory allocation on the stack, which leads to a stack explosion problem, multiple larger large stack spaces are divided from the memory, and a larger large stack space is allocated for the thread from the multiple large stack spaces during the process of processing the request by the thread, so that the request is processed based on the larger large stack space, thereby avoiding the stack explosion problem.

[0115] Meanwhile, in the request processing method provided in the specification, by allocating a larger stack space for the thread, the cache hit rate can also be improved. For example, in actual applications, assuming that there are 1000 threads in the server and there is one CPU, the CPU will switch between the 1000 threads, and the stack memory (i.e., memory page or thread space) corresponding to each thread also needs to be replaced out of the cache, thereby resulting in a poor cache hit rate.

[0116] Therefore, in the embodiments provided in the specification, the large stack space is divided in advance, and the large stack space is used to process the request when multiple threads are active, so that the stack memory corresponding to each thread does not need to be replaced out of the cache frequently, because multiple threads use the same large stack space, thereby improving the cache hit rate.

[0117] Step 314: processing is completed.

[0118] Specifically, after the server switches the large stack space for the thread, the data required for processing the write request is stored in the large stack space. The data required for processing the request includes but is not limited to the function of the thread, the variable of the thread, the parameter carried in the write request, and the like.

[0119] Then, the server runs the data required for processing the request in the large stack space through the CPU, thereby realizing processing of the write request.

[0120] In actual application, if a large number of thread local variables are used in the project, frequent switching of threads to access their respective thread stack memories will also affect the cache hit rate. Therefore, in the embodiments provided in the specification, all thread local variables are reused in a small number of memory segments. After the construction of the reused memory (i.e., the thread local variable space) is completed, the memory no longer belongs to the thread itself, but can be used as other thread local variable space at any time.

[0121] Based on this, in the process of processing the write request by the thread, the idle thread local variable space needs to be determined from the created multiple thread local variable spaces, and the local variable of the thread is stored in the idle thread local variable space. Subsequently, the CPU runs the local variable required for processing the request in the thread local variable space, thereby realizing processing of the write request.

[0122] In addition, because the thread needs to call the local variable of other threads in the process of processing the request, the server sorts the thread local variable spaces that are accessed more frequently in the order of access, and always provides the thread local variable space that is used most recently for new requests.

[0123] Step 316: return to the small stack.

[0124] Specifically, after the server determines that the thread completes processing of the request, the server determines the small stack space corresponding to the thread that is saved in advance, and switches the large stack space corresponding to the thread to the small stack space.

[0125] Step 318: release the large stack.

[0126] Specifically, in order to ensure that the large stack space can be used by other threads, the server will release the large stack space after the switching is completed. Thus, when the server performs step 304 subsequently, the released large stack can be continuously used.

[0127] It should be noted that in the request processing method provided in the present specification, before all thread stacks are reduced, the stack explosion that may occur when processing a request must be handled, otherwise the thread stacks cannot be reduced.

[0128] Meanwhile, for threads in a non-waiting request state, there are two states: one is in a running state, and the number of threads in the running state at the same time will not exceed the number of CPU cores; the other is in a block state, and the number of threads waiting for a lock or an IO thread will also not be too large. Therefore, in the case that the sum of the number of threads in the two states is much smaller than the total number of threads, the present solution can save a large amount of thread stack memory, and ultimately achieve the purpose of improving the cache hit rate.

[0129] In the request processing method provided in the present specification, a large stack is used when processing a request to avoid stack explosion; and the default thread stack memory size of all threads is reduced to reduce the use of thread stack memory; at the same time, by reducing the use of thread stack memory, the thread stack cache hit rate is indirectly improved, and the performance is further improved; and by preferentially using the thread local variable space in the cache, the thread stack cache hit rate is indirectly improved, and the performance is further improved.

[0130] Corresponding to the method embodiments described above, the present specification also provides request processing device embodiments, Figure 4 A structure schematic diagram of a request processing device provided by one embodiment of the present specification is shown. As shown in the figure, Figure 4 The device comprises:

[0131] The allocation module 402 is configured to allocate a corresponding target thread for an object processing request from at least two initial threads;

[0132] The first determination module 404 is configured to determine an initial thread space in memory corresponding to the target thread, and determine current state information of a preset thread space in the memory, wherein the preset thread space is larger than the initial thread space;

[0133] The second determination module 406 is configured to determine a corresponding target thread space for the target thread from the preset thread space based on the current state information;

[0134] The processing module 408 is configured to replace the initial thread space with the target thread space, and process the object processing request based on the target thread and the corresponding target thread space.

[0135] Optionally, the request processing apparatus further comprises a thread creation module configured to:

[0136] create the at least two initial threads, and allocate a corresponding initial thread space for each initial thread from the memory respectively.

[0137] Optionally, the second determination module 406 is further configured to:

[0138] determine an idle preset thread space from the preset thread spaces based on the current state information;

[0139] determine a corresponding target thread space for the target thread based on the idle preset thread space.

[0140] Optionally, the request processing apparatus further comprises a space release module configured to:

[0141] determine an initial thread space corresponding to the target thread in the memory;

[0142] replace the target thread space with the initial thread space and release the target thread space in the memory.

[0143] Optionally, the processing module 408 is further configured to:

[0144] determine request running data corresponding to the object processing request and send the request running data to the target thread space;

[0145] run the request running data by the target thread in the target thread space to process the object processing request.

[0146] Optionally, the processing module 408 is further configured to:

[0147] determine a corresponding target variable space for the target thread from at least two variable spaces in the memory;

[0148] process the object processing request based on the target thread, the target variable space and the target thread space.

[0149] Optionally, the processing module 408 is further configured to:

[0150] determine at least two variable spaces in the memory and space state information of each variable space;

[0151] determine a corresponding to-be-processed variable space for the target thread from at least two variable spaces based on the space state information of the variable space;

[0152] determining address information of the to-be-processed variable space, adding the address information to an address storage unit corresponding to the target thread, and obtaining a target variable space corresponding to the target thread.

[0153] Optionally, the processing module 408 is further configured to:

[0154] determining request running data corresponding to the object processing request and request variables corresponding to the object processing request;

[0155] sending the request running data to the target thread space and sending the request variables to the target variable space;

[0156] running the request running data by the target thread in the target thread space and running the request variables by the target thread in the target variable space, so as to process the object processing request.

[0157] Optionally, the processing module 408 is further configured to:

[0158] based on the historical access frequency of the variable space, determining a variable space with the highest historical access frequency among at least two variable spaces as the to-be-processed variable space corresponding to the target thread.

[0159] The request processing apparatus provided in the specification is capable of, after assigning a target thread with an initial thread space for an object processing request, determining a target thread space corresponding to the target thread from a preset thread space larger than the initial thread space, and flexibly replacing the initial thread space with the target thread space in the process of processing the object processing request, so as to process the object processing request based on the target thread and the target thread space larger than the initial thread space, thereby avoiding memory waste and saving memory resources.

[0160] The above is a schematic scheme of the request processing apparatus of the embodiment. It should be noted that the technical scheme of the request processing apparatus and the technical scheme of the request processing method described above belong to the same concept, and the details of the technical scheme of the request processing apparatus that are not described in detail can be referred to the description of the technical scheme of the request processing method.

[0161] Figure 5 A structural block diagram of a computing device 500 according to an embodiment of the specification is shown. The components of the computing device 500 include but are not limited to a memory 510 and a processor 520. The processor 520 is connected to the memory 510 through a bus 530, and a database 550 is used to save data.

[0162] The computing device 500 also includes an access device 540 that enables the computing device 500 to communicate via one or more networks 560. Examples of such networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or combinations of such networks, such as the Internet. The access device 540 can include one or more of any type of network interface (for example, a network interface card (NIC)) such as an IEEE 802.11 wireless local area network (WLAN) wireless interface, a global microwave interconnection access (Wi-MAX) interface, an Ethernet interface, a universal serial bus (USB) interface, a cellular network interface, a Bluetooth interface, a near field communication (NFC) interface, and so on.

[0163] In an embodiment of the present specification, the above-described components of the computing device 500 and other components not shown in the Figure 5 may be connected to each other, for example, through a bus. It should be understood that Figure 5 The computing device structure diagram shown is merely for the purpose of example, and is not a limitation on the scope of the present specification. Other components can be added or replaced by those skilled in the art as needed.

[0164] The computing device 500 can be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (for example, a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, and so on), a mobile phone (for example, a smartphone), a wearable computing device (for example, a smart watch, smart glasses, and so on), or other types of mobile devices, or a stationary computing device such as a desktop computer or a PC. The computing device 500 can also be a mobile or stationary server.

[0165] The processor 520 is configured to execute computer-executable instructions, which, when executed by the processor 520, implement the steps of the above-mentioned request processing method.

[0166] The above is a schematic scheme of a computing device according to an embodiment of the present specification. It should be noted that the technical scheme of the computing device belongs to the same concept as the technical scheme of the above-mentioned request processing method, and the details of the technical scheme of the computing device that are not described in detail can be referred to the description of the technical scheme of the above-mentioned request processing method.

[0167] An embodiment of the present specification also provides a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the steps of the above-mentioned request processing method.

[0168] The above is a schematic solution of the computer readable storage medium of the embodiment. It should be noted that the technical solution of the storage medium and the technical solution of the request processing method described above belong to the same concept, and the details of the technical solution of the storage medium that are not described in detail can be referred to the description of the technical solution of the request processing method.

[0169] An embodiment of the present specification also provides a computer program, wherein the computer program causes a computer to execute the steps of the request processing method when the computer program is executed in the computer.

[0170] The above is a schematic solution of the computer program of the embodiment. It should be noted that the technical solution of the computer program and the technical solution of the request processing method described above belong to the same concept, and the details of the technical solution of the computer program that are not described in detail can be referred to the description of the technical solution of the request processing method.

[0171] The above describes specific embodiments of the present specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in a different order than the order in which they are recited in the embodiments and still achieve desirable results. In addition, the processes depicted in the figures do not necessarily require the particular order shown, or sequential order, to achieve the desired results. In some implementations, multitasking and parallel processing can be advantageous or necessary.

[0172] The computer instructions include computer program code, which can be in the form of source code, object code, executable code, or some intermediate form. The computer readable medium can include any entity or apparatus capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc. It should be noted that the computer readable medium does not include electrical carrier signals and telecommunication signals.

[0173] It should be noted that for each of the above method embodiments, in order to facilitate description, each is described as a combination of a series of actions, but those skilled in the art should know that the embodiments of the present specification are not limited to the order of the actions described, because according to the embodiments of the present specification, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should know that the embodiments described in the specification all belong to preferred embodiments, and the actions and modules involved are not necessarily all the embodiments of the present specification.

[0174] In the above-described embodiments, the description of each embodiment focuses on different aspects, and the parts not described in detail in a certain embodiment can be referred to the relevant description of other embodiments.

[0175] The preferred embodiments of the present specification disclosed above are only used to help explain the present specification. The alternative embodiments do not describe all the details and limit the present application to the specific embodiments described. Obviously, according to the content of the embodiments of the present specification, many modifications and changes can be made. The present specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of the present specification, so that those skilled in the art can well understand and utilize the present specification. The present specification is limited only by the claims and their full scope and equivalents.

Claims

1. A request processing method, comprising: allocating a corresponding target thread to an object processing request from at least two initial threads; determining an initial thread space in a memory corresponding to the target thread, and determining current state information of a preset thread space in the memory, wherein the preset thread space is larger than the initial thread space, and the target thread corresponding to the initial thread space is in a waiting state; determining a corresponding target thread space for the target thread from the preset thread space based on the current state information; replacing the initial thread space with the target thread space, and processing the object processing request based on the target thread and the corresponding target thread space, wherein the processing the object processing request based on the target thread and the corresponding target thread space comprises determining a corresponding target variable space for the target thread from at least two variable spaces in the memory, and processing the object processing request based on the target thread, the target variable space and the target thread space, wherein the at least two variable spaces specifically represent spaces pre-divided for storing thread local variables of each thread. 2.The request processing method of claim 1, further comprising, before the allocating a corresponding target thread to an object processing request from at least two initial threads: creating the at least two initial threads, and allocating a corresponding initial thread space to each initial thread from the memory. 3.The request processing method of claim 1, wherein the determining a corresponding target thread space for the target thread from the preset thread space based on the current state information comprises: determining an idle preset thread space from the preset thread space based on the current state information; and determining the corresponding target thread space for the target thread based on the idle preset thread space. 4.The request processing method of claim 1, further comprising, after the processing the object processing request based on the target thread and the corresponding target thread space: determining an initial thread space in the memory corresponding to the target thread; replacing the target thread space with the initial thread space, and releasing the target thread space in the memory. 5.The request processing method of claim 1, wherein the processing the object processing request based on the target thread and the corresponding target thread space comprises: determining request running data corresponding to the object processing request, and sending the request running data to the target thread space; and running the request running data by the target thread in the target thread space to process the object processing request. 6.The request processing method of claim 1, wherein the determining a corresponding target variable space for the target thread from at least two variable spaces in the memory comprises: determining at least two variable spaces in the memory, and space state information of each variable space; and determining a to-be-processed variable space for the target thread from the at least two variable spaces based on the space state information of the variable spaces. determining address information of the to-be-processed variable space, adding the address information to an address storage unit corresponding to the target thread, and obtaining a target variable space corresponding to the target thread.

7. The request processing method of claim 1, wherein the processing of the object processing request based on the target thread, the target variable space, and the target thread space comprises: determining request running data corresponding to the object processing request and request variables corresponding to the object processing request; sending the request running data to the target thread space and the request variables to the target variable space; processing the object processing request by running the request running data in the target thread space and running the request variables in the target variable space.

8. The request processing method of claim 6, wherein the determining of the to-be-processed variable space corresponding to the target thread based on the space state information of the variable space comprises: determining, based on the space state information of the at least two variable spaces, an idle variable space in the at least two variable spaces as the to-be-processed variable space corresponding to the target thread.

9. A request processing apparatus, comprising: an allocation module configured to allocate a target thread corresponding to an object processing request from at least two initial threads; a first determination module configured to determine an initial thread space in a memory corresponding to the target thread and determine current state information of a preset thread space in the memory, wherein the preset thread space is larger than the initial thread space, and a target thread corresponding to the initial thread space is in a waiting state; a second determination module configured to determine a target thread space corresponding to the target thread from the preset thread space based on the current state information; a processing module configured to replace the initial thread space with the target thread space and process the object processing request based on the target thread and the target thread space corresponding to the target thread, wherein the processing of the object processing request based on the target thread and the target thread space corresponding to the target thread comprises: determining a target variable space corresponding to the target thread from at least two variable spaces in a memory, and processing the object processing request based on the target thread, the target variable space, and the target thread space, wherein the at least two variable spaces specifically represent a space pre-divided for storing thread-local variables of each thread.

10. A computing device, comprising: a memory and a processor; the memory is configured to store computer executable instructions, and the processor is configured to execute the computer executable instructions, which, when executed by the processor, implement the steps of the request processing method of any one of claims 1 to 8.

11. A computer readable storage medium storing computer executable instructions, which, when executed by a processor, implement the steps of the request processing method of any one of claims 1 to 8.

Citation Information

Patent Citations

  • Signal processing method and device

    CN103246591A

  • Reducing stack memory resources in a threaded computer system

    US6968557B1