WO2015043528A1 - Parallel multi-thread message processing method and device - Google Patents

Parallel multi-thread message processing method and device Download PDF

Info

Publication number
WO2015043528A1
WO2015043528A1 PCT/CN2014/087733 CN2014087733W WO2015043528A1 WO 2015043528 A1 WO2015043528 A1 WO 2015043528A1 CN 2014087733 W CN2014087733 W CN 2014087733W WO 2015043528 A1 WO2015043528 A1 WO 2015043528A1
Authority
WO
WIPO (PCT)
Prior art keywords
token
amount
thread
bucket
token bucket
Prior art date
Application number
PCT/CN2014/087733
Other languages
French (fr)
Chinese (zh)
Inventor
张欢
沈伟锋
李纪先
Original Assignee
华为技术有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 华为技术有限公司 filed Critical 华为技术有限公司
Publication of WO2015043528A1 publication Critical patent/WO2015043528A1/en

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F13/00Interconnection of, or transfer of information or other signals between, memories, input/output devices or central processing units
    • G06F13/38Information transfer, e.g. on bus
    • G06F13/382Information transfer, e.g. on bus using universal interface adapter
    • G06F13/385Information transfer, e.g. on bus using universal interface adapter for adaptation of a particular data processing system to different peripheral devices

Definitions

  • the present invention relates to communication technologies, and in particular, to a method and apparatus for parallel multi-threaded message processing.
  • the token bucket locking method can be used to avoid data errors caused by data competition, for example, when a thread uses the token bucket, the token is used. The bucket is locked. At this time, only the thread can access the token bucket. If other threads access the token bucket, they must temporarily suspend until the thread finishes accessing the token bucket and unlock the token bucket, but this will make the original Parallel computing data is processed serially, greatly reducing system performance.
  • the present invention provides a method and apparatus for parallel multi-threaded message processing to improve system performance.
  • an embodiment of the present invention provides a parallel multi-thread processing method, including:
  • Determining the first token that is extracted with each of the thread requests The quantity is compared to determine whether the usable token amount is greater than or equal to the first token amount;
  • the token of the first token amount is extracted from the token bucket and allocated to the thread.
  • the determining, by the token bucket, the amount of usable tokens at the current moment includes:
  • the token consumption amount a token consumption amount of the token bucket for all the threads from an initial time to a current duration of the current time, the accumulated token amount being received by the token bucket within the first duration The result of adding the token amount to the initial value of the token bucket.
  • the acquiring, each thread, respectively, needs to request the first extracted from the token bucket Before the amount of tokens it also includes:
  • a counter is set for the thread, and the counter is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
  • the taking the first from the token bucket After the token token also includes:
  • the first amount of tokens is increased in the token consumption recorded by the counter.
  • the method further includes:
  • the amount of the added token is increased in the token bucket.
  • an embodiment of the present invention provides a parallel multi-thread processing apparatus, including:
  • An acquiring module configured to acquire a first token quantity that each thread needs to extract from the token bucket request, where the thread is a thread that is received by the token bucket at a current time;
  • a determining module configured to determine an amount of usable tokens of the token bucket at the current moment
  • a comparison module configured to compare the usable token amount with the first token amount extracted by each of the thread requests, and determine whether the usable token amount is greater than or equal to the first order Amount of card
  • an allocating module configured to: after the comparing module determines to be, extract the token of the first token amount from the token bucket, and allocate the token to the thread.
  • the comparing module is configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current moment, to obtain the token bucket in the The amount of the token that can be used at the current time, the token consumption being a token consumption amount of the token bucket for all the threads from the initial time to the current time of the current time,
  • the accumulated token amount is a result of adding the amount of the added token received by the token bucket within the first duration to the initial value of the token bucket.
  • the method further includes: a recording module;
  • the recording module is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
  • the recording module is further configured to: The amount of cards is increased in the amount of token consumption recorded by the counter.
  • the determining module is further configured to prepare the The amount of the added token issued by the card bucket is added to the amount of the usable token to obtain a first sum; determining whether the first sum is greater than or equal to the depth of the token bucket; if greater than, the The difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
  • the method and device for processing the parallel multi-threaded message obtained by the embodiment of the present invention obtains the first token amount that each thread needs to extract from the token bucket, and the thread is the thread that the token bucket receives at the current time.
  • the first token number is one or more tokens, and then the token token is determined at the current moment, and then the token amount is used separately with each token
  • the first number of tokens requested by the process is compared to determine whether the amount of available tokens is greater than or equal to the first token amount; if yes, the token of the first token amount is extracted from the token bucket and allocated to the token Thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
  • FIG. 1 is a flowchart of a parallel multi-thread processing method according to an embodiment of the present invention
  • FIG. 2 is a flowchart of a parallel multi-thread processing method according to another embodiment of the present invention.
  • FIG. 3 is a schematic diagram of simulation results of a parallel multi-thread processing method according to an embodiment of the present invention
  • FIG. 4 is a schematic diagram of simulation results of a parallel multi-thread processing method according to another embodiment of the present invention.
  • FIG. 5 is a schematic structural diagram of a parallel multi-thread processing apparatus according to an embodiment of the present invention.
  • FIG. 6 is a schematic structural diagram of a parallel multi-thread processing apparatus according to another embodiment of the present invention.
  • FIG. 7 is a schematic structural diagram of a parallel multi-thread processing device according to an embodiment of the present invention.
  • the parallel multi-thread processing method provided by the embodiment of the present invention can be specifically applied to a process of assigning tokens to different threads when the token bucket receives the access of multiple threads at the same time.
  • the parallel multi-thread processing method provided in this embodiment may be specifically implemented by a parallel multi-thread processing device.
  • the parallel multi-thread processing device can be integrated in a switch or a router, etc., and needs to control the number of network elements sent to the network.
  • the parallel multi-thread processing device can be implemented in software and/or hardware.
  • FIG. 1 is a flowchart of a parallel multi-thread processing method according to an embodiment of the present invention; as shown in FIG. 1 , the method in this embodiment includes:
  • Step 101 Acquire a first token quantity that each thread needs to extract from the token bucket, where the thread is a thread that the token bucket receives at the current time.
  • the parallel multi-thread processing device may determine that the token bucket receives at least one thread at the current time.
  • each thread in this embodiment corresponds to one core, that is, a central processing unit (CPU).
  • CPU central processing unit
  • Step 102 Determine the amount of usable tokens of the token bucket at the current moment.
  • the parallel multi-thread processing device acquires the usable token amount of the token bucket at the current moment, and at least includes the following two implementation manners:
  • the parallel multi-thread processing device can obtain the usable token amount of the token bucket itself by using the token bucket's own counting device.
  • the parallel multi-thread processing device can obtain the usable token amount of the current time of the token bucket by comparing the accumulated token amount of the token bucket with the token consumption of the current time, wherein the token The consumption is the token consumption of the token bucket in the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount and the token bucket received by the token bucket in the first duration.
  • the result of the initial value addition wherein the increase in the token amount is the product of the first duration and the issued token rate.
  • Step 103 Compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the usable token amount is greater than or equal to the first token amount.
  • the first token amount that each thread needs to extract from the token bucket may be separately compared to determine whether the available token amount is greater than or equal to The first token amount, if yes, step 104 is performed, and if no, step 105 is performed.
  • the token bucket receives two threads of access at the same time, wherein the first thread needs The amount of the first token to be extracted from the token bucket is N, and the amount of the first token that the second thread needs to extract from the token bucket is N+1, and then the amount of token that can be used is separately from each thread that needs to be
  • the first token amount extracted in the token bucket is compared, that is, when the token amount 2N at the current time of the token bucket is greater than the first token amount that the first thread needs to extract from the token bucket is N, then The N tokens are taken out from the token bucket and allocated to the first thread. Meanwhile, the token amount 2N of the current token bucket is greater than the first token amount that the second thread needs to extract from the token bucket is N+. At 1 o'clock, N+1 tokens are taken from the token bucket and assigned to the second thread.
  • the token bucket allocates 2N+1 tokens to 2 threads at the current time, there is no token in the token bucket, and a token on the pre-borrow is also assigned to the thread.
  • the token bucket needs to wait for its own number of tokens to reach the token amount at the next moment, and then perform the next allocation.
  • Step 104 Extract the token of the first token amount from the token bucket, and allocate the token to the thread.
  • the token of the first token number taken out in the token bucket is allocated to the thread for transmitting the data packet in the thread.
  • Step 105 The token allocation operation is not performed on the thread.
  • the token allocation operation may not be performed on the thread first, that is, the operation of the thread may be considered as the allowed traffic restriction.
  • the token is not assigned to the thread.
  • the data packet may be discarded or discharged in a queue to be transmitted when the token bucket has accumulated enough tokens, or continue to be sent but marked with special marks, and these special markers are added when the network is overloaded. The packet is dropped.
  • the parallel multi-thread processing method obtaineds a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token.
  • the number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine
  • the amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
  • the method may further include:
  • the method may further include:
  • the first token amount is increased in the token consumption recorded by the counter, wherein the counter is used to record the number of tokens in the token bucket consumed by the thread within the first duration.
  • FIG. 2 is a flowchart of a parallel multi-thread processing method according to another embodiment of the present invention; as shown in FIG. 2, before the step 101 of the foregoing embodiment, or after the step 101, the method in this embodiment may further include:
  • Step 201 Add the increased token amount to be issued to the token bucket and the available token amount to obtain the first sum.
  • the increase of the token amount in this embodiment may issue the above-mentioned increase token amount to the token bucket by program timing.
  • Step 202 Determine whether the first sum is greater than or equal to the token bucket depth.
  • the token bucket depth in this embodiment is the maximum number of tokens that can be stored in the token bucket.
  • the parallel multi-thread processing device determines whether the first sum is greater than or equal to the token bucket depth. If yes, step 203 is performed, and if no, step 204 is performed.
  • Step 203 Add a difference between the token bucket depth and the usable token amount in the token bucket.
  • Step 204 Increase the amount of the added token in the token bucket.
  • the parallel multi-thread processing method obtaineds the first sum by adding the increased token amount to be added in the token bucket and the usable token amount; and then determining the first sum Whether the token bucket depth is greater than or equal to; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increasing token amount is increased in the token bucket.
  • the operation of adding a token in a token bucket without locking is implemented.
  • FIG. 3 is a schematic diagram of a simulation result of a parallel multi-thread processing method according to an embodiment of the present invention; as shown in FIG. 3, on the basis of the parallel multi-thread processing method provided by the foregoing embodiment, simulating parallel multi-threading, Specifically, the upper limit of the counter processing data is set to 7Kpps, and 7 streams are simultaneously played, that is, the simulation performs data processing on 7 parallel threads simultaneously, and the rate of each stream is 1 Kpps. According to the simulation result of FIG. 3, according to the simulation result, according to In the method of the foregoing embodiment, in the case that multiple threads are simultaneously processed, the data packet can be effectively limited.
  • FIG. 4 is a schematic diagram of simulation results of a parallel multi-thread processing method according to another embodiment of the present invention; as shown in FIG. 4, the basis of the parallel multi-thread processing method provided by the foregoing embodiment
  • the parallel multi-thread processing is simulated.
  • the upper limit of the counter processing data is set to 7Kpps, and 7 streams are simultaneously played, that is, the simulation simultaneously performs data processing on 7 parallel threads, and the rate of each stream is 2Kpps, that is, 14 Kpps is required for data processing of 7 parallel threads at the same time.
  • the method according to the embodiment of the present invention can be used in a short time in the case of simultaneous processing by multiple threads.
  • the traffic shaping of the data packet is controlled, and the data can be effectively limited.
  • FIG. 5 is a schematic structural diagram of a parallel multi-thread processing apparatus according to an embodiment of the present invention. As shown in FIG. 5, the parallel multi-thread processing apparatus includes: an obtaining module 51, a determining module 52, a comparing module 53, and an allocating module 54. among them,
  • the obtaining module 51 is configured to obtain a first token quantity that each thread needs to extract from the token bucket request, and the thread is a thread that the token bucket receives at the current time;
  • a determining module 52 configured to determine an amount of usable tokens of the token bucket at the current moment
  • the comparing module 53 is configured to compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the usable token amount is greater than or equal to the first token amount;
  • the allocating module 54 is configured to extract the token of the first token amount from the token bucket after the comparison module determines to be yes, and allocate the token to the thread.
  • the parallel multi-thread processing method obtaineds a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token.
  • the number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine
  • the amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
  • the comparison module 53 is configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current time, and obtain the usable token amount of the token bucket at the current moment,
  • the card consumption is the token consumption of the token bucket within the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount and the token bucket received by the token bucket within the first time duration.
  • the initial values are added together.
  • FIG. 6 is a schematic structural diagram of a parallel multi-thread processing apparatus according to another embodiment of the present invention. As shown in FIG. 6, on the basis of the foregoing embodiment, the parallel multi-thread processing apparatus may further The recording module 55 is configured to record the number of tokens in the token bucket in the first time duration.
  • the recording module 55 is further configured to increase the first token amount in the token consumption recorded by the counter.
  • the determining module 52 is further configured to add the increased token amount to be issued to the token bucket and the usable token amount to obtain the first sum; determine whether the first sum is greater than Or equal to the token bucket depth; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
  • FIG. 7 is a schematic structural diagram of a parallel multi-thread processing device according to an embodiment of the present invention. As shown in FIG. 7, the parallel multi-thread processing device includes: an acquirer 71 and a processor 72. among them,
  • the acquirer 71 is configured to acquire a first token quantity that each thread needs to extract from the token bucket request, and the thread is a thread that the token bucket receives at the current time;
  • the processor 72 is configured to determine a usable token amount of the token bucket at the current moment; and compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the available token amount is The first token amount is greater than or equal to; if yes, the token of the first token amount is extracted from the token bucket and allocated to the thread.
  • the parallel multi-thread processing method obtaineds a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token.
  • the number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine
  • the amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
  • the processor 72 is specifically configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current time, and obtain the usable token amount of the token bucket at the current moment,
  • the card consumption is the token consumption amount of the token bucket in the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount received by the token bucket in the first time duration. The result of adding the initial value of the token bucket.
  • the processor 72 is further configured to record the first time duration The thread consumes the number of tokens in the token bucket.
  • the processor 72 is further configured to increase the first token amount in the token consumption recorded by the counter.
  • the processor 72 is further configured to add the increased token amount to be issued to the token bucket and the usable token amount to obtain a first sum; determine whether the first sum is greater than Or equal to the token bucket depth; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
  • the aforementioned program can be stored in a computer readable storage medium.
  • the program when executed, performs the steps including the foregoing method embodiments; and the foregoing storage medium includes various media that can store program codes, such as a ROM, a RAM, a magnetic disk, or an optical disk.

Landscapes

  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Data Exchanges In Wide-Area Networks (AREA)

Abstract

Provided is a parallel multi-thread processing method. The method comprises: acquiring a first token number of each thread which is respectively requested to be extracted from a token bucket, wherein the thread is a thread received by the token bucket at the current time, and the first token number indicates one or more tokens; next, determining the number of available tokens in the token bucket at the current time; and then, comparing the number of available tokens with the first token number requested to be extracted by each thread respectively, and determining whether the number of available tokens is greater than or equal to the first token number; if so, extracting tokens with the first token number from the token bucket, and allocating same to the thread; otherwise, not conducting a token allocation operation on the thread. Parallel processing on a plurality of threads is realized while a token bucket receives the plurality of threads at the same time, thereby improving the system performance.

Description

并行多线程报文处理的方法和装置Parallel multi-threaded message processing method and device 技术领域Technical field
本发明涉及通信技术,尤其涉及一种并行多线程报文处理的方法和装置。The present invention relates to communication technologies, and in particular, to a method and apparatus for parallel multi-threaded message processing.
背景技术Background technique
在现代通信中,人们通过令牌桶算法对网络上的数据流量进行控制,也就是说,通过从令牌桶取出令牌发送数据的方式对网络中数据的流量进行限制。In modern communication, people use the token bucket algorithm to control the data traffic on the network, that is, to limit the traffic of data in the network by extracting the token from the token bucket to send data.
在现有技术中,当一个需要m个字节的数据包到达时,若令牌桶中当前的令牌数大于或等于m个令牌时,则从令牌桶中取出m个令牌用于该数据包的发送,若令牌桶中当前的令牌数少于m个令牌时,则将数据包丢弃。In the prior art, when a data packet requiring m bytes arrives, if the current number of tokens in the token bucket is greater than or equal to m tokens, then m tokens are taken out from the token bucket. In the sending of the data packet, if the current number of tokens in the token bucket is less than m tokens, the data packet is discarded.
然而,在多线程同时访问同一个令牌桶的情况下,可以通过令牌桶锁定方式来避免因数据竞争而导致数据错误,例如,当某个线程使用该令牌桶时,将该令牌桶锁定,此时仅此线程能访问该令牌桶,其他线程若访问令牌桶必须暂时挂起,直到该线程对令牌桶访问结束并解除对令牌桶的锁定,但这会使得原本并行计算的数据被串行处理,大大降低了系统的性能。However, in the case where multiple threads access the same token bucket at the same time, the token bucket locking method can be used to avoid data errors caused by data competition, for example, when a thread uses the token bucket, the token is used. The bucket is locked. At this time, only the thread can access the token bucket. If other threads access the token bucket, they must temporarily suspend until the thread finishes accessing the token bucket and unlock the token bucket, but this will make the original Parallel computing data is processed serially, greatly reducing system performance.
发明内容Summary of the invention
本发明提供一种并行多线程报文处理的方法和装置,用以提高系统性能。The present invention provides a method and apparatus for parallel multi-threaded message processing to improve system performance.
第一方面,本发明实施例提供一种并行多线程处理方法,包括:In a first aspect, an embodiment of the present invention provides a parallel multi-thread processing method, including:
获取每个线程分别需要从所述令牌桶请求提取的第一令牌量,所述线程为所述令牌桶在当前时刻接收的线程;Obtaining a first token amount that each thread needs to extract from the token bucket request, where the thread is a thread that is received by the token bucket at a current time;
确定所述令牌桶在所述当前时刻的可使用令牌量;Determining an amount of usable tokens of the token bucket at the current moment;
将所述可使用令牌量分别与每个所述线程请求提取的所述第一令牌 量进行比较,确定所述可使用令牌量是否大于或等于所述第一令牌量;Determining the first token that is extracted with each of the thread requests The quantity is compared to determine whether the usable token amount is greater than or equal to the first token amount;
若是,则从所述令牌桶中提取所述第一令牌量的令牌,并分配给所述线程。If yes, the token of the first token amount is extracted from the token bucket and allocated to the thread.
在第一种可能的实现方式中,所述确定所述令牌桶在所述当前时刻的可使用令牌量,包括:In a first possible implementation manner, the determining, by the token bucket, the amount of usable tokens at the current moment includes:
将所述令牌桶的累计令牌量与所述当前时刻的令牌消耗量进行比较,获得所述令牌桶在所述当前时刻的所述可使用令牌量,所述令牌消耗量为所有所述线程从初始时刻至所述当前时刻的第一时长内对所述令牌桶的令牌消耗量,所述累计令牌量为所述令牌桶在所述第一时长内接收的增加令牌量与所述令牌桶的初始值相加的结果。Comparing the accumulated token amount of the token bucket with the token consumption amount of the current time to obtain the usable token amount of the token bucket at the current moment, the token consumption amount a token consumption amount of the token bucket for all the threads from an initial time to a current duration of the current time, the accumulated token amount being received by the token bucket within the first duration The result of adding the token amount to the initial value of the token bucket.
结合第一个方面或是第一个方面的第一种可能的实现方式,在第二种可能的实现方式中,所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之前,还包括:With reference to the first aspect or the first possible implementation manner of the first aspect, in a second possible implementation manner, the acquiring, each thread, respectively, needs to request the first extracted from the token bucket Before the amount of tokens, it also includes:
对所述线程设置一个计数器,所述计数器用于记录所述第一时长内所述线程消耗所述令牌桶中令牌的数量。A counter is set for the thread, and the counter is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
结合第一个方面至第一个方面的第二种可能的实现方式中的任意一种实现方式,在第三种可能的实现方式中,所述从所述令牌桶中取出所述第一令牌量的令牌之后,还包括:With reference to any one of the first aspect to the second possible implementation of the first aspect, in a third possible implementation, the taking the first from the token bucket After the token token, it also includes:
将所述第一令牌量增加在所述计数器记录的所述令牌消耗量中。The first amount of tokens is increased in the token consumption recorded by the counter.
结合第一个方面至第一个方面的第三种可能的实现方式中的任意一种实现方式,在第四种可能的实现方式中,所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之后,或所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之前,还包括:With reference to any one of the third aspect, the third possible implementation manner of the first aspect, in the fourth possible implementation, the acquiring each thread separately needs to be from the token bucket After the first token amount is requested to be extracted, or before the acquiring the first token amount that each thread needs to extract from the token bucket respectively, the method further includes:
将准备向所述令牌桶发放的增加令牌量与所述可使用令牌量进行相加,获得第一和数;Adding an increased token amount to be issued to the token bucket and adding the first token to the usable token amount to obtain a first sum;
确定所述第一和数是否大于或等于所述令牌桶深度;Determining whether the first sum is greater than or equal to the token bucket depth;
若大于,则将所述令牌桶深度与所述可使用令牌量的差值增加在所述令牌桶;If greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket;
若小于,则将所述增加令牌量增加在所述令牌桶中。If less than, the amount of the added token is increased in the token bucket.
第二方面,本发明实施例提供一种并行多线程处理装置,包括: In a second aspect, an embodiment of the present invention provides a parallel multi-thread processing apparatus, including:
获取模块,用于获取每个线程分别需要从所述令牌桶请求提取的第一令牌量,所述线程为所述令牌桶在当前时刻接收的线程;An acquiring module, configured to acquire a first token quantity that each thread needs to extract from the token bucket request, where the thread is a thread that is received by the token bucket at a current time;
确定模块,用于确定所述令牌桶在所述当前时刻的可使用令牌量;a determining module, configured to determine an amount of usable tokens of the token bucket at the current moment;
比较模块,用于将所述可使用令牌量分别与每个所述线程请求提取的所述第一令牌量进行比较,确定所述可使用令牌量是否大于或等于所述第一令牌量;a comparison module, configured to compare the usable token amount with the first token amount extracted by each of the thread requests, and determine whether the usable token amount is greater than or equal to the first order Amount of card
分配模块,用于在所述比较模块确定为是之后,从所述令牌桶中提取所述第一令牌量的令牌,并分配给所述线程。And an allocating module, configured to: after the comparing module determines to be, extract the token of the first token amount from the token bucket, and allocate the token to the thread.
在第一种可能的实现方式中,所述比较模块,具体用于将所述令牌桶的累计令牌量与所述当前时刻的令牌消耗量进行比较,获得所述令牌桶在所述当前时刻的所述可使用令牌量,所述令牌消耗量为所有所述线程从初始时刻至所述当前时刻的第一时长内对所述令牌桶的令牌消耗量,所述累计令牌量为所述令牌桶在所述第一时长内接收的增加令牌量与所述令牌桶的初始值相加的结果。In a first possible implementation manner, the comparing module is configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current moment, to obtain the token bucket in the The amount of the token that can be used at the current time, the token consumption being a token consumption amount of the token bucket for all the threads from the initial time to the current time of the current time, The accumulated token amount is a result of adding the amount of the added token received by the token bucket within the first duration to the initial value of the token bucket.
结合第二个方面或是第二个方面的第一种可能的实现方式,在第二种可能的实现方式中,还包括:记录模块;In combination with the second aspect or the first possible implementation manner of the second aspect, in a second possible implementation manner, the method further includes: a recording module;
所述记录模块,用于记录所述第一时长内所述线程消耗所述令牌桶中令牌的数量。The recording module is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
结合第二个方面至第二个方面的第二种可能的实现方式中的任意一种实现方式,在第三种可能的实现方式中,所述记录模块;还用于将所述第一令牌量增加在所述计数器记录的所述令牌消耗量中。With reference to any one of the second aspect to the second possible implementation of the second aspect, in a third possible implementation, the recording module is further configured to: The amount of cards is increased in the amount of token consumption recorded by the counter.
结合第二个方面至第二个方面的第三种可能的实现方式中的任意一种实现方式,在第四种可能的实现方式中,所述确定模块,还用于将准备向所述令牌桶发放的增加令牌量与所述可使用令牌量进行相加,获得第一和数;确定所述第一和数是否大于或等于所述令牌桶深度;若大于,则将所述令牌桶深度与所述可使用令牌量的差值增加在所述令牌桶;若小于,则将所述增加令牌量增加在所述令牌桶中。With reference to any one of the third aspect to the third possible implementation of the second aspect, in a fourth possible implementation, the determining module is further configured to prepare the The amount of the added token issued by the card bucket is added to the amount of the usable token to obtain a first sum; determining whether the first sum is greater than or equal to the depth of the token bucket; if greater than, the The difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
本发明实施例提供的并行多线程报文处理的方法和装置,通过获取每个线程分别需要从令牌桶中请求提取的第一令牌量,该线程为令牌桶在当前时刻接收的线程,第一令牌数为一个或多个令牌,接着,确定令牌桶在当前时刻的可使用令牌量,然后,将可使用令牌量分别与每个线 程请求提取的第一令牌数进行比较,确定可使用令牌量是否大于或等于第一令牌量;若是,则从令牌桶中提取第一令牌量的令牌,并分配给该线程。实现了令牌桶同时接收到多个线程时,对多个线程的并行处理,从而提高了系统的性能。The method and device for processing the parallel multi-threaded message provided by the embodiment of the present invention obtains the first token amount that each thread needs to extract from the token bucket, and the thread is the thread that the token bucket receives at the current time. The first token number is one or more tokens, and then the token token is determined at the current moment, and then the token amount is used separately with each token The first number of tokens requested by the process is compared to determine whether the amount of available tokens is greater than or equal to the first token amount; if yes, the token of the first token amount is extracted from the token bucket and allocated to the token Thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
附图说明DRAWINGS
为了更清楚地说明本发明实施例或现有技术中的技术方案,下面将对实施例或现有技术描述中所需要使用的附图作一简单地介绍,显而易见地,下面描述中的附图是本发明的一些实施例,对于本领域普通技术人员来讲,在不付出创造性劳动性的前提下,还可以根据这些附图获得其他的附图。In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, a brief description of the drawings used in the embodiments or the prior art description will be briefly described below. Obviously, the drawings in the following description It is a certain embodiment of the present invention, and other drawings can be obtained from those skilled in the art without any inventive labor.
图1为本发明一实施例提供的并行多线程处理方法的流程图;FIG. 1 is a flowchart of a parallel multi-thread processing method according to an embodiment of the present invention;
图2为本发明另一实施例提供的并行多线程处理方法的流程图;2 is a flowchart of a parallel multi-thread processing method according to another embodiment of the present invention;
图3为本发明一实施例提供的并行多线程处理方法的仿真结果示意图;FIG. 3 is a schematic diagram of simulation results of a parallel multi-thread processing method according to an embodiment of the present invention; FIG.
图4为本发明另一实施例提供的并行多线程处理方法的仿真结果示意图;FIG. 4 is a schematic diagram of simulation results of a parallel multi-thread processing method according to another embodiment of the present invention; FIG.
图5为本发明一实施例提供的并行多线程处理装置的结构示意图;FIG. 5 is a schematic structural diagram of a parallel multi-thread processing apparatus according to an embodiment of the present invention;
图6为本发明另一实施例提供的并行多线程处理装置的结构示意图;FIG. 6 is a schematic structural diagram of a parallel multi-thread processing apparatus according to another embodiment of the present invention;
图7为本发明一实施例提供的并行多线程处理设备的结构示意图。FIG. 7 is a schematic structural diagram of a parallel multi-thread processing device according to an embodiment of the present invention.
具体实施方式detailed description
为使本发明实施例的目的、技术方案和优点更加清楚,下面将结合本发明实施例中的附图,对本发明实施例中的技术方案进行清楚、完整地描述,显然,所描述的实施例是本发明一部分实施例,而不是全部的实施例。基于本发明中的实施例,本领域普通技术人员在没有作出创造性劳动前提下所获得的所有其他实施例,都属于本发明保护的范围。The technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the drawings in the embodiments of the present invention. It is a partial embodiment of the invention, and not all of the embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present invention without creative efforts are within the scope of the present invention.
本发明实施例提供的并行多线程处理方法具体可以应用于在令牌桶同时接收到多个线程的访问时,对不同线程分配令牌的过程。本实施例提供的并行多线程处理方法具体可以通过并行多线程处理装置来执行, 该并行多线程处理装置可以集成在交换机或是路由器等,需要控制发送到网络上数据的数目的网元中,该并行多线程处理装置可以采用软件和/或硬件的方式来实现。以下对本实施例提供的并行多线程处理方法及装置进行详细地说明。The parallel multi-thread processing method provided by the embodiment of the present invention can be specifically applied to a process of assigning tokens to different threads when the token bucket receives the access of multiple threads at the same time. The parallel multi-thread processing method provided in this embodiment may be specifically implemented by a parallel multi-thread processing device. The parallel multi-thread processing device can be integrated in a switch or a router, etc., and needs to control the number of network elements sent to the network. The parallel multi-thread processing device can be implemented in software and/or hardware. The parallel multi-thread processing method and apparatus provided in this embodiment will be described in detail below.
图1为本发明一实施例提供的并行多线程处理方法的流程图;如图1所示,本实施例的方法包括:FIG. 1 is a flowchart of a parallel multi-thread processing method according to an embodiment of the present invention; as shown in FIG. 1 , the method in this embodiment includes:
步骤101、获取每个线程分别需要从令牌桶中请求提取的第一令牌量,其中,线程为令牌桶在当前时刻接收的线程。Step 101: Acquire a first token quantity that each thread needs to extract from the token bucket, where the thread is a thread that the token bucket receives at the current time.
具体的,多核多线程环境下,在当前时刻,当多个线程同时访问令牌桶时,并行多线程处理装置可以确定令牌桶当前时刻接收到至少一个线程。Specifically, in the multi-core multi-thread environment, at the current moment, when multiple threads access the token bucket at the same time, the parallel multi-thread processing device may determine that the token bucket receives at least one thread at the current time.
需要说明的是,本实施例中的每个线程对应一个核心,即中央处理器(Central Processing Unit,以下简称CPU)。It should be noted that each thread in this embodiment corresponds to one core, that is, a central processing unit (CPU).
步骤102、确定该令牌桶在当前时刻的可使用令牌量。Step 102: Determine the amount of usable tokens of the token bucket at the current moment.
在本实施例中,并行多线程处理装置获取该令牌桶在当前时刻的可使用令牌量至少包括以下两种实现方式:In this embodiment, the parallel multi-thread processing device acquires the usable token amount of the token bucket at the current moment, and at least includes the following two implementation manners:
第一种实现方式,并行多线程处理装置可以通过该令牌桶自身的计数装置,获得该令牌桶自身的可使用令牌量。In a first implementation manner, the parallel multi-thread processing device can obtain the usable token amount of the token bucket itself by using the token bucket's own counting device.
第二种实现方式,并行多线程处理装置可以通过将令牌桶的累计令牌量与当前时刻的令牌消耗量进行比较,获得令牌桶当前时刻的可使用令牌量,其中,令牌消耗量为所有线程从初始时刻至当前时刻的第一时长内对令牌桶的令牌消耗量,累计令牌量为该令牌桶在第一时长内接收的增加令牌量与令牌桶的初始值相加的结果,其中,增加令牌量为第一时长与发放令牌速率的乘积。In a second implementation manner, the parallel multi-thread processing device can obtain the usable token amount of the current time of the token bucket by comparing the accumulated token amount of the token bucket with the token consumption of the current time, wherein the token The consumption is the token consumption of the token bucket in the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount and the token bucket received by the token bucket in the first duration. The result of the initial value addition, wherein the increase in the token amount is the product of the first duration and the issued token rate.
步骤103、将可使用令牌量分别与每个线程请求提取的第一令牌量进行比较,确定可使用令牌量是否大于或等于第一令牌量。Step 103: Compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the usable token amount is greater than or equal to the first token amount.
具体的,在令牌桶同时接收到多个线程的访问时,可以同时分别对每个线程需要从令牌桶中提取的第一令牌量进行比较,确定可使用令牌量是否大于或等于第一令牌量,若是,则执行步骤104,如否,则执行步骤105。Specifically, when the token bucket receives the access of the multiple threads at the same time, the first token amount that each thread needs to extract from the token bucket may be separately compared to determine whether the available token amount is greater than or equal to The first token amount, if yes, step 104 is performed, and if no, step 105 is performed.
举例来讲,令牌桶同时接收到2个线程的访问,其中,第一线程需 要从令牌桶提取的第一令牌量为N,第二线程需要从令牌桶提取的第一令牌量为N+1,接着,将可使用令牌量分别与每个线程需要从令牌桶中提取的第一令牌量进行比较,即,令牌桶当前时刻的可使用令牌量2N大于第一线程需要从令牌桶提取的第一令牌量为N时,则从令牌桶中取出N个令牌,并分配给第一线程;同时,令牌桶当前时刻的可使用令牌量2N大于第二线程需要从令牌桶提取的第一令牌量为N+1时,则从令牌桶中取出N+1个令牌,并分配给第二线程。For example, the token bucket receives two threads of access at the same time, wherein the first thread needs The amount of the first token to be extracted from the token bucket is N, and the amount of the first token that the second thread needs to extract from the token bucket is N+1, and then the amount of token that can be used is separately from each thread that needs to be The first token amount extracted in the token bucket is compared, that is, when the token amount 2N at the current time of the token bucket is greater than the first token amount that the first thread needs to extract from the token bucket is N, then The N tokens are taken out from the token bucket and allocated to the first thread. Meanwhile, the token amount 2N of the current token bucket is greater than the first token amount that the second thread needs to extract from the token bucket is N+. At 1 o'clock, N+1 tokens are taken from the token bucket and assigned to the second thread.
需要说明的是,在当前时刻令牌桶将2N+1个令牌分配给2个线程之后,令牌桶中已经没有令牌,同时还将预借上的一个令牌分配给了线程,因此,令牌桶需要等待自身的可使用令牌数达到下一时刻的令牌量,再进行下一次的分配。It should be noted that after the token bucket allocates 2N+1 tokens to 2 threads at the current time, there is no token in the token bucket, and a token on the pre-borrow is also assigned to the thread. The token bucket needs to wait for its own number of tokens to reach the token amount at the next moment, and then perform the next allocation.
步骤104、从令牌桶中提取所述第一令牌量的令牌,并分配给该线程。Step 104: Extract the token of the first token amount from the token bucket, and allocate the token to the thread.
在本实施例中,将令牌桶中取出的第一令牌数的令牌分配给该线程,用以发送该线程中的数据包。In this embodiment, the token of the first token number taken out in the token bucket is allocated to the thread for transmitting the data packet in the thread.
步骤105、对该线程不进行令牌分配操作。Step 105: The token allocation operation is not performed on the thread.
在本实施例中,若令牌桶当前时刻的可使用令牌量小于第一令牌量时,可以首先对该线程不进行令牌分配操作,即可以认为该线程的操作在允许的流量限制之外,从而对该线程不分配令牌。进一步的,可以对该数据包进行丢弃、或者排放在队列中以便当令牌桶中累积了足够多的令牌时再传输、或者继续发送但做特殊标记,一旦网络过载的时候将这些特殊标记的数据包丢弃。In this embodiment, if the token amount of the token bucket at the current time is less than the first token amount, the token allocation operation may not be performed on the thread first, that is, the operation of the thread may be considered as the allowed traffic restriction. In addition, the token is not assigned to the thread. Further, the data packet may be discarded or discharged in a queue to be transmitted when the token bucket has accumulated enough tokens, or continue to be sent but marked with special marks, and these special markers are added when the network is overloaded. The packet is dropped.
本发明实施例提供的并行多线程处理方法,通过获取每个线程分别需要从令牌桶中请求提取的第一令牌量,该线程为令牌桶在当前时刻接收的线程,第一令牌数为一个或多个令牌,接着,确定令牌桶在当前时刻的可使用令牌量,然后,将可使用令牌量分别与每个线程请求提取的第一令牌数进行比较,确定可使用令牌量是否大于或等于第一令牌量;若是,则从令牌桶中提取第一令牌量的令牌,并分配给该线程。实现了令牌桶同时接收到多个线程时,对多个线程的并行处理,从而提高了系统的性能。The parallel multi-thread processing method provided by the embodiment of the present invention obtains a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token. The number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine The amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
需要说明的是,在步骤101之前,还可以包括:It should be noted that, before step 101, the method may further include:
对每个线程设置一个计数器,该计数器用以累积记录第一时长内每 个线程的令牌消耗量。Set a counter for each thread that is used to accumulate records for each of the first durations The token consumption of threads.
进一步的,在上述实施例的基础上,在步骤104之后,还可以包括:Further, on the basis of the foregoing embodiment, after step 104, the method may further include:
将第一令牌量增加在计数器记录的令牌消耗量中,其中,该计数器用于记录第一时长内该线程消耗令牌桶中令牌的数量。The first token amount is increased in the token consumption recorded by the counter, wherein the counter is used to record the number of tokens in the token bucket consumed by the thread within the first duration.
图2为本发明另一实施例提供的并行多线程处理方法的流程图;如图2所示,在上述实施例的步骤101之前,或是步骤101之后,本实施例的方法还可以包括:2 is a flowchart of a parallel multi-thread processing method according to another embodiment of the present invention; as shown in FIG. 2, before the step 101 of the foregoing embodiment, or after the step 101, the method in this embodiment may further include:
步骤201、将准备向令牌桶发放的增加令牌量与可使用令牌量进行相加,获得的第一和数。Step 201: Add the increased token amount to be issued to the token bucket and the available token amount to obtain the first sum.
本实施例中的增加令牌量可以通过程序定时向令牌桶发放上述的增加令牌量。The increase of the token amount in this embodiment may issue the above-mentioned increase token amount to the token bucket by program timing.
步骤202、确定第一和数是否大于或等于令牌桶深度。Step 202: Determine whether the first sum is greater than or equal to the token bucket depth.
本实施例中的令牌桶深度为令牌桶最大可存储的令牌数量。The token bucket depth in this embodiment is the maximum number of tokens that can be stored in the token bucket.
具体的,在并行多线程处理装置确定第一和数是否大于或等于令牌桶深度,若是,则执行步骤203,若否,则执行步骤204。Specifically, the parallel multi-thread processing device determines whether the first sum is greater than or equal to the token bucket depth. If yes, step 203 is performed, and if no, step 204 is performed.
步骤203、将令牌桶深度与可使用令牌量的差值增加在令牌桶。Step 203: Add a difference between the token bucket depth and the usable token amount in the token bucket.
步骤204、将增加令牌量增加在令牌桶中。Step 204: Increase the amount of the added token in the token bucket.
本发明实施例提供的并行多线程处理方法,通过将准备在令牌桶中增加的增加令牌量与可使用令牌量进行相加,获得的第一和数;接着,确定第一和数是否大于或等于令牌桶深度;若大于,则将令牌桶深度与可使用令牌量的差值增加在令牌桶;若小于,则将增加令牌量增加在令牌桶中。实现了在没有加锁的令牌桶中进行增加令牌的操作。The parallel multi-thread processing method provided by the embodiment of the present invention obtains the first sum by adding the increased token amount to be added in the token bucket and the usable token amount; and then determining the first sum Whether the token bucket depth is greater than or equal to; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increasing token amount is increased in the token bucket. The operation of adding a token in a token bucket without locking is implemented.
图3为本发明一实施例提供的并行多线程处理方法的仿真结果示意图;如图3所示,在上述实施例所提供的并行多线程处理方法的基础上,对并行多线程进行模拟仿真,具体为,将计数器处理数据的上限设置为7Kpps,同时打7条流,即仿真对7个并行线程同时进行数据处理,并且,每条流的速率为1Kpps,根据图3的仿真结果可知,根据上述实施例的方法,在多线程同时处理的情况下,可以对数据包进行有效的限流。FIG. 3 is a schematic diagram of a simulation result of a parallel multi-thread processing method according to an embodiment of the present invention; as shown in FIG. 3, on the basis of the parallel multi-thread processing method provided by the foregoing embodiment, simulating parallel multi-threading, Specifically, the upper limit of the counter processing data is set to 7Kpps, and 7 streams are simultaneously played, that is, the simulation performs data processing on 7 parallel threads simultaneously, and the rate of each stream is 1 Kpps. According to the simulation result of FIG. 3, according to the simulation result, according to In the method of the foregoing embodiment, in the case that multiple threads are simultaneously processed, the data packet can be effectively limited.
图4为本发明另一实施例提供的并行多线程处理方法的仿真结果示意图;如图4所示,在上述实施例所提供的并行多线程处理方法的基础 上,对并行多线程处理进行模拟仿真,具体为,将计数器处理数据的上限设置为7Kpps,同时打7条流,即仿真对7个并行线程同时进行数据处理,并且,每条流的速率为2Kpps,也就是说,同时对7个并行线程进行数据处理时需要14 Kpps,根据图4的仿真结果可知,根据本发明实施例的方法,在多线程同时处理的情况下,可以在较短时间内对数据包进行流量整形的控制,并可以对数据进行有效的限流。4 is a schematic diagram of simulation results of a parallel multi-thread processing method according to another embodiment of the present invention; as shown in FIG. 4, the basis of the parallel multi-thread processing method provided by the foregoing embodiment On the other hand, the parallel multi-thread processing is simulated. Specifically, the upper limit of the counter processing data is set to 7Kpps, and 7 streams are simultaneously played, that is, the simulation simultaneously performs data processing on 7 parallel threads, and the rate of each stream is 2Kpps, that is, 14 Kpps is required for data processing of 7 parallel threads at the same time. According to the simulation result of FIG. 4, the method according to the embodiment of the present invention can be used in a short time in the case of simultaneous processing by multiple threads. The traffic shaping of the data packet is controlled, and the data can be effectively limited.
图5为本发明一实施例提供的并行多线程处理装置的结构示意图;如图5所示,该并行多线程处理装置,包括:获取模块51、确定模块52、比较模块53和分配模块54。其中,FIG. 5 is a schematic structural diagram of a parallel multi-thread processing apparatus according to an embodiment of the present invention; as shown in FIG. 5, the parallel multi-thread processing apparatus includes: an obtaining module 51, a determining module 52, a comparing module 53, and an allocating module 54. among them,
获取模块51,用于获取每个线程分别需要从令牌桶请求提取的第一令牌量,线程为令牌桶在当前时刻接收的线程;The obtaining module 51 is configured to obtain a first token quantity that each thread needs to extract from the token bucket request, and the thread is a thread that the token bucket receives at the current time;
确定模块52,用于确定令牌桶在当前时刻的可使用令牌量;a determining module 52, configured to determine an amount of usable tokens of the token bucket at the current moment;
比较模块53,用于将可使用令牌量分别与每个线程请求提取的第一令牌量进行比较,确定可使用令牌量是否大于或等于第一令牌量;The comparing module 53 is configured to compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the usable token amount is greater than or equal to the first token amount;
分配模块54,用于在比较模块确定为是之后,从令牌桶中提取第一令牌量的令牌,并分配给线程。The allocating module 54 is configured to extract the token of the first token amount from the token bucket after the comparison module determines to be yes, and allocate the token to the thread.
本发明实施例提供的并行多线程处理方法,通过获取每个线程分别需要从令牌桶中请求提取的第一令牌量,该线程为令牌桶在当前时刻接收的线程,第一令牌数为一个或多个令牌,接着,确定令牌桶在当前时刻的可使用令牌量,然后,将可使用令牌量分别与每个线程请求提取的第一令牌数进行比较,确定可使用令牌量是否大于或等于第一令牌量;若是,则从令牌桶中提取第一令牌量的令牌,并分配给该线程。实现了令牌桶同时接收到多个线程时,对多个线程的并行处理,从而提高了系统的性能。The parallel multi-thread processing method provided by the embodiment of the present invention obtains a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token. The number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine The amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
在上述实施例的基础上,比较模块53,具体用于将令牌桶的累计令牌量与当前时刻的令牌消耗量进行比较,获得令牌桶在当前时刻的可使用令牌量,令牌消耗量为所有线程从初始时刻至当前时刻的第一时长内对令牌桶的令牌消耗量,累计令牌量为令牌桶在第一时长内接收的增加令牌量与令牌桶的初始值相加的结果。On the basis of the foregoing embodiment, the comparison module 53 is configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current time, and obtain the usable token amount of the token bucket at the current moment, The card consumption is the token consumption of the token bucket within the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount and the token bucket received by the token bucket within the first time duration. The initial values are added together.
图6为本发明另一实施例提供的并行多线程处理装置的结构示意图;如图6所示,在上述实施例的基础上,该并行多线程处理装置,还可以 包括:记录模块55;其中,记录模块55,用于记录第一时长内线程消耗令牌桶中令牌的数量。FIG. 6 is a schematic structural diagram of a parallel multi-thread processing apparatus according to another embodiment of the present invention; as shown in FIG. 6, on the basis of the foregoing embodiment, the parallel multi-thread processing apparatus may further The recording module 55 is configured to record the number of tokens in the token bucket in the first time duration.
进一步的,在上述实施例的基础上,记录模块55;还用于将第一令牌量增加在计数器记录的令牌消耗量中。Further, based on the foregoing embodiment, the recording module 55 is further configured to increase the first token amount in the token consumption recorded by the counter.
在上述实施例的基础上,确定模块52,还用于将准备向令牌桶发放的增加令牌量与可使用令牌量进行相加,获得第一和数;确定第一和数是否大于或等于令牌桶深度;若大于,则将令牌桶深度与可使用令牌量的差值增加在令牌桶;若小于,则将增加令牌量增加在令牌桶中。On the basis of the foregoing embodiment, the determining module 52 is further configured to add the increased token amount to be issued to the token bucket and the usable token amount to obtain the first sum; determine whether the first sum is greater than Or equal to the token bucket depth; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
图7为本发明一实施例提供的并行多线程处理设备的结构示意图;如图7所示,该并行多线程处理设备,包括:获取器71和处理器72。其中,FIG. 7 is a schematic structural diagram of a parallel multi-thread processing device according to an embodiment of the present invention; as shown in FIG. 7, the parallel multi-thread processing device includes: an acquirer 71 and a processor 72. among them,
获取器71,用于获取每个线程分别需要从令牌桶请求提取的第一令牌量,线程为令牌桶在当前时刻接收的线程;The acquirer 71 is configured to acquire a first token quantity that each thread needs to extract from the token bucket request, and the thread is a thread that the token bucket receives at the current time;
处理器72,用于确定令牌桶在当前时刻的可使用令牌量;并将可使用令牌量分别与每个线程请求提取的第一令牌量进行比较,确定可使用令牌量是否大于或等于第一令牌量;若是,则从令牌桶中提取第一令牌量的令牌,并分配给线程。The processor 72 is configured to determine a usable token amount of the token bucket at the current moment; and compare the usable token amount with the first token amount that each thread requests to extract, and determine whether the available token amount is The first token amount is greater than or equal to; if yes, the token of the first token amount is extracted from the token bucket and allocated to the thread.
本发明实施例提供的并行多线程处理方法,通过获取每个线程分别需要从令牌桶中请求提取的第一令牌量,该线程为令牌桶在当前时刻接收的线程,第一令牌数为一个或多个令牌,接着,确定令牌桶在当前时刻的可使用令牌量,然后,将可使用令牌量分别与每个线程请求提取的第一令牌数进行比较,确定可使用令牌量是否大于或等于第一令牌量;若是,则从令牌桶中提取第一令牌量的令牌,并分配给该线程。实现了令牌桶同时接收到多个线程时,对多个线程的并行处理,从而提高了系统的性能。The parallel multi-thread processing method provided by the embodiment of the present invention obtains a first token amount that each thread needs to extract from the token bucket, and the thread is a thread that the token bucket receives at the current time, the first token. The number is one or more tokens, and then, the token amount of the token bucket at the current moment is determined, and then the token token amount is respectively compared with the first token number requested by each thread to determine The amount of tokens that can be used is greater than or equal to the first token amount; if so, the token of the first token amount is extracted from the token bucket and assigned to the thread. It realizes the parallel processing of multiple threads when the token bucket receives multiple threads at the same time, thus improving the performance of the system.
在上述实施例的基础上,处理器72,具体用于将令牌桶的累计令牌量与当前时刻的令牌消耗量进行比较,获得令牌桶在当前时刻的可使用令牌量,令牌消耗量为所有线程从初始时刻至当前时刻的第一时长内对令牌桶的令牌消耗量,累计令牌量为所述令牌桶在所述第一时长内接收的增加令牌量与所述令牌桶的初始值相加的结果。On the basis of the foregoing embodiment, the processor 72 is specifically configured to compare the accumulated token amount of the token bucket with the token consumption amount of the current time, and obtain the usable token amount of the token bucket at the current moment, The card consumption is the token consumption amount of the token bucket in the first time period from the initial time to the current time of all the threads, and the accumulated token amount is the increased token amount received by the token bucket in the first time duration. The result of adding the initial value of the token bucket.
在上述实施例的基础上,处理器72,还用于记录所述第一时长内所 述线程消耗所述令牌桶中令牌的数量。On the basis of the foregoing embodiment, the processor 72 is further configured to record the first time duration The thread consumes the number of tokens in the token bucket.
进一步的,在上述实施例的基础上,处理器72;还用于将第一令牌量增加在计数器记录的令牌消耗量中。Further, based on the foregoing embodiment, the processor 72 is further configured to increase the first token amount in the token consumption recorded by the counter.
在上述实施例的基础上,处理器72,还用于将准备向令牌桶发放的增加令牌量与可使用令牌量进行相加,获得第一和数;确定第一和数是否大于或等于令牌桶深度;若大于,则将令牌桶深度与可使用令牌量的差值增加在令牌桶;若小于,则将增加令牌量增加在令牌桶中。On the basis of the foregoing embodiment, the processor 72 is further configured to add the increased token amount to be issued to the token bucket and the usable token amount to obtain a first sum; determine whether the first sum is greater than Or equal to the token bucket depth; if greater than, the difference between the token bucket depth and the usable token amount is increased in the token bucket; if less, the increased token amount is added in the token bucket.
本领域普通技术人员可以理解:实现上述各方法实施例的全部或部分步骤可以通过程序指令相关的硬件来完成。前述的程序可以存储于一计算机可读取存储介质中。该程序在执行时,执行包括上述各方法实施例的步骤;而前述的存储介质包括:ROM、RAM、磁碟或者光盘等各种可以存储程序代码的介质。One of ordinary skill in the art will appreciate that all or part of the steps to implement the various method embodiments described above may be accomplished by hardware associated with the program instructions. The aforementioned program can be stored in a computer readable storage medium. The program, when executed, performs the steps including the foregoing method embodiments; and the foregoing storage medium includes various media that can store program codes, such as a ROM, a RAM, a magnetic disk, or an optical disk.
最后应说明的是:以上各实施例仅用以说明本发明的技术方案,而非对其限制;尽管参照前述各实施例对本发明进行了详细的说明,本领域的普通技术人员应当理解:其依然可以对前述各实施例所记载的技术方案进行修改,或者对其中部分或者全部技术特征进行等同替换;而这些修改或者替换,并不使相应技术方案的本质脱离本发明各实施例技术方案的范围。 Finally, it should be noted that the above embodiments are merely illustrative of the technical solutions of the present invention, and are not intended to be limiting; although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art will understand that The technical solutions described in the foregoing embodiments may be modified, or some or all of the technical features may be equivalently replaced; and the modifications or substitutions do not deviate from the technical solutions of the embodiments of the present invention. range.

Claims (10)

  1. 一种并行多线程处理方法,其特征在于,包括:A parallel multi-thread processing method, comprising:
    获取每个线程分别需要从所述令牌桶请求提取的第一令牌量,所述线程为所述令牌桶在当前时刻接收的线程;Obtaining a first token amount that each thread needs to extract from the token bucket request, where the thread is a thread that is received by the token bucket at a current time;
    确定所述令牌桶在所述当前时刻的可使用令牌量;Determining an amount of usable tokens of the token bucket at the current moment;
    将所述可使用令牌量分别与每个所述线程请求提取的所述第一令牌量进行比较,确定所述可使用令牌量是否大于或等于所述第一令牌量;Comparing the usable token amount with the first token amount extracted by each of the thread requests, and determining whether the usable token amount is greater than or equal to the first token amount;
    若是,则从所述令牌桶中提取所述第一令牌量的令牌,并分配给所述线程。If yes, the token of the first token amount is extracted from the token bucket and allocated to the thread.
  2. 根据权利要求1所述的方法,其特征在于,所述确定所述令牌桶在所述当前时刻的可使用令牌量,包括:The method according to claim 1, wherein the determining the amount of usable tokens of the token bucket at the current moment comprises:
    将所述令牌桶的累计令牌量与所述当前时刻的令牌消耗量进行比较,获得所述令牌桶在所述当前时刻的所述可使用令牌量,所述令牌消耗量为所有所述线程从初始时刻至所述当前时刻的第一时长内对所述令牌桶的令牌消耗量,所述累计令牌量为所述令牌桶在所述第一时长内接收的增加令牌量与所述令牌桶的初始值相加的结果。Comparing the accumulated token amount of the token bucket with the token consumption amount of the current time to obtain the usable token amount of the token bucket at the current moment, the token consumption amount a token consumption amount of the token bucket for all the threads from an initial time to a current duration of the current time, the accumulated token amount being received by the token bucket within the first duration The result of adding the token amount to the initial value of the token bucket.
  3. 根据权利要求1或2所述的方法,其特征在于,所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之前,还包括:The method according to claim 1 or 2, wherein before the acquiring the first token amount that each thread needs to extract from the token bucket, the method further includes:
    对所述线程设置一个计数器,所述计数器用于记录所述第一时长内所述线程消耗所述令牌桶中令牌的数量。A counter is set for the thread, and the counter is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
  4. 根据权利要求1-3任一项所述的方法,其特征在于,所述从所述令牌桶中取出所述第一令牌量的令牌之后,还包括:The method according to any one of claims 1-3, wherein after the taking out the token of the first token amount from the token bucket, the method further comprises:
    将所述第一令牌量增加在所述计数器记录的所述令牌消耗量中。The first amount of tokens is increased in the token consumption recorded by the counter.
  5. 根据权利要求1-4任一项所述的方法,其特征在于,所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之后,或所述获取每个线程分别需要从所述令牌桶中请求提取的第一令牌量之前,还包括:The method according to any one of claims 1 to 4, wherein the acquiring each thread requires a first token amount to be extracted from the token bucket, or acquiring each thread Before requesting the first token amount to be extracted from the token bucket respectively, the method further includes:
    将准备向所述令牌桶发放的增加令牌量与所述可使用令牌量进行相加,获得第一和数;Adding an increased token amount to be issued to the token bucket and adding the first token to the usable token amount to obtain a first sum;
    确定所述第一和数是否大于或等于所述令牌桶深度;Determining whether the first sum is greater than or equal to the token bucket depth;
    若大于,则将所述令牌桶深度与所述可使用令牌量的差值增加在所 述令牌桶;If greater than, the difference between the token bucket depth and the usable token amount is increased. a token bucket;
    若小于,则将所述增加令牌量增加在所述令牌桶中。If less than, the amount of the added token is increased in the token bucket.
  6. 一种并行多线程处理装置,其特征在于,包括:A parallel multi-thread processing device, comprising:
    获取模块,用于获取每个线程分别需要从所述令牌桶请求提取的第一令牌量,所述线程为所述令牌桶在当前时刻接收的线程;An acquiring module, configured to acquire a first token quantity that each thread needs to extract from the token bucket request, where the thread is a thread that is received by the token bucket at a current time;
    确定模块,用于确定所述令牌桶在所述当前时刻的可使用令牌量;a determining module, configured to determine an amount of usable tokens of the token bucket at the current moment;
    比较模块,用于将所述可使用令牌量分别与每个所述线程请求提取的所述第一令牌量进行比较,确定所述可使用令牌量是否大于或等于所述第一令牌量;a comparison module, configured to compare the usable token amount with the first token amount extracted by each of the thread requests, and determine whether the usable token amount is greater than or equal to the first order Amount of card
    分配模块,用于在所述比较模块确定为是之后,从所述令牌桶中提取所述第一令牌量的令牌,并分配给所述线程。And an allocating module, configured to: after the comparing module determines to be, extract the token of the first token amount from the token bucket, and allocate the token to the thread.
  7. 根据权利要求6所述的装置,其特征在于,所述比较模块,具体用于将所述令牌桶的累计令牌量与所述当前时刻的令牌消耗量进行比较,获得所述令牌桶在所述当前时刻的所述可使用令牌量,所述令牌消耗量为所有所述线程从初始时刻至所述当前时刻的第一时长内对所述令牌桶的令牌消耗量,所述累计令牌量为所述令牌桶在所述第一时长内接收的增加令牌量与所述令牌桶的初始值相加的结果。The device according to claim 6, wherein the comparing module is configured to compare the accumulated token amount of the token bucket with the token consumption of the current moment to obtain the token. The amount of the usable token of the bucket at the current moment, the token consumption being a token consumption amount of the token bucket within a first duration of all the threads from an initial moment to the current moment The cumulative token amount is a result of adding the amount of the increased token received by the token bucket within the first duration to the initial value of the token bucket.
  8. 根据权利要求6或7所述的装置,其特征在于,还包括:记录模块;The device according to claim 6 or 7, further comprising: a recording module;
    所述记录模块,用于记录所述第一时长内所述线程消耗所述令牌桶中令牌的数量。The recording module is configured to record the number of tokens in the token bucket consumed by the thread in the first duration.
  9. 根据权利要求6-8任一项所述的装置,其特征在于,所述记录模块;还用于将所述第一令牌量增加在所述计数器记录的所述令牌消耗量中。The apparatus according to any one of claims 6-8, wherein the recording module is further configured to increase the first token amount in the token consumption amount recorded by the counter.
  10. 根据权利要求6-9任一项所述的装置,其特征在于,所述确定模块,还用于将准备向所述令牌桶发放的增加令牌量与所述可使用令牌量进行相加,获得第一和数;确定所述第一和数是否大于或等于所述令牌桶深度;若大于,则将所述令牌桶深度与所述可使用令牌量的差值增加在所述令牌桶;若小于,则将所述增加令牌量增加在所述令牌桶中。 The device according to any one of claims 6-9, wherein the determining module is further configured to: compare an amount of increased tokens to be issued to the token bucket with the amount of usable tokens Adding, obtaining a first sum; determining whether the first sum is greater than or equal to the token bucket depth; if greater than, increasing a difference between the token bucket depth and the usable token amount The token bucket; if less than, adds the increased token amount in the token bucket.
PCT/CN2014/087733 2013-09-30 2014-09-29 Parallel multi-thread message processing method and device WO2015043528A1 (en)

Applications Claiming Priority (2)

Application Number Priority Date Filing Date Title
CN201310465494.6A CN104518987A (en) 2013-09-30 2013-09-30 Method and device for processing parallel multithreading messages
CN201310465494.6 2013-09-30

Publications (1)

Publication Number Publication Date
WO2015043528A1 true WO2015043528A1 (en) 2015-04-02

Family

ID=52742093

Family Applications (1)

Application Number Title Priority Date Filing Date
PCT/CN2014/087733 WO2015043528A1 (en) 2013-09-30 2014-09-29 Parallel multi-thread message processing method and device

Country Status (2)

Country Link
CN (1) CN104518987A (en)
WO (1) WO2015043528A1 (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111064676A (en) * 2018-10-16 2020-04-24 中兴通讯股份有限公司 Flow monitoring method, equipment, device and computer storage medium
CN113765818A (en) * 2020-06-28 2021-12-07 北京沃东天骏信息技术有限公司 Distributed current limiting method, device, equipment, storage medium and system
CN114726798A (en) * 2022-02-28 2022-07-08 福建星云电子股份有限公司 Lithium battery test channel current limiting method and system

Families Citing this family (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN105939286B (en) * 2016-03-28 2019-05-07 杭州迪普科技股份有限公司 Token bucket management method and device
CN108647082A (en) * 2018-04-13 2018-10-12 中国民航信息网络股份有限公司 Task processing method, device, equipment based on token mechanism and medium
CN110275780B (en) * 2019-06-26 2022-08-02 北京百度网讯科技有限公司 Method and apparatus for restricting flow
CN110852680A (en) * 2019-11-12 2020-02-28 金蝶蝶金云计算有限公司 Data processing method, ERP system and computer storage medium
CN113472681A (en) * 2020-03-30 2021-10-01 阿里巴巴集团控股有限公司 Flow rate limiting method and device
CN112367270B (en) * 2020-10-30 2023-01-10 锐捷网络股份有限公司 Method and equipment for sending message
CN112463716B (en) * 2020-11-27 2024-02-13 中船重工(武汉)凌久电子有限责任公司 Global semaphore implementation method based on multi-core multi-processor parallel system
CN113691461B (en) * 2021-08-23 2023-03-24 新华三信息安全技术有限公司 Token bucket management method and device for multi-core equipment

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20020138643A1 (en) * 2000-10-19 2002-09-26 Shin Kang G. Method and system for controlling network traffic to a network computer
CN102224722A (en) * 2008-11-25 2011-10-19 思杰系统有限公司 Systems and methods for object rate limiting
CN102714618A (en) * 2009-06-22 2012-10-03 思杰系统有限公司 Systems and methods for platform rate limiting

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20020138643A1 (en) * 2000-10-19 2002-09-26 Shin Kang G. Method and system for controlling network traffic to a network computer
CN102224722A (en) * 2008-11-25 2011-10-19 思杰系统有限公司 Systems and methods for object rate limiting
CN102714618A (en) * 2009-06-22 2012-10-03 思杰系统有限公司 Systems and methods for platform rate limiting

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN111064676A (en) * 2018-10-16 2020-04-24 中兴通讯股份有限公司 Flow monitoring method, equipment, device and computer storage medium
CN113765818A (en) * 2020-06-28 2021-12-07 北京沃东天骏信息技术有限公司 Distributed current limiting method, device, equipment, storage medium and system
CN114726798A (en) * 2022-02-28 2022-07-08 福建星云电子股份有限公司 Lithium battery test channel current limiting method and system
CN114726798B (en) * 2022-02-28 2023-07-18 福建星云电子股份有限公司 Lithium battery test channel current limiting method and system

Also Published As

Publication number Publication date
CN104518987A (en) 2015-04-15

Similar Documents

Publication Publication Date Title
WO2015043528A1 (en) Parallel multi-thread message processing method and device
CN107086908B (en) Quantum key distribution method and device
CN108776934B (en) Distributed data calculation method and device, computer equipment and readable storage medium
US20130074091A1 (en) Techniques for ensuring resources achieve performance metrics in a multi-tenant storage controller
JP6480642B2 (en) Stochastic bandwidth adjustment
WO2017166643A1 (en) Method and device for quantifying task resources
JP2021503643A (en) Computer-readable storage media containing methods, devices, computer programs and computer programs for optimizing high-volume transaction performance on the blockchain.
Yao et al. LsPS: A job size-based scheduler for efficient task assignments in Hadoop
CN104331659A (en) Design method for system resource application isolation of critical application host
US20170185454A1 (en) Method and Electronic Device for Determining Resource Consumption of Task
US20060288397A1 (en) Stream controller
CN101931775A (en) Video recording method and device
WO2016101799A1 (en) Service allocation method and device based on distributed system
WO2022095358A1 (en) Task scheduling method and apparatus, electronic device, and readable storage medium
CN105022668B (en) Job scheduling method and system
WO2015176689A1 (en) Data processing method and device
CN106970841A (en) A kind of resource constraint and dynamic adjusting method based on cloud computing
US20230037783A1 (en) Resource scheduling method and related apparatus
WO2016008338A1 (en) I/o request processing method and storage system
CN110278104A (en) The technology that service quality for optimization accelerates
Martin et al. Notifying memories: A case-study on data-flow applications with NoC interfaces implementation
Liu et al. High-responsive scheduling with MapReduce performance prediction on hadoop YARN
CN113032410A (en) Data processing method and device, electronic equipment and computer storage medium
WO2015090044A1 (en) Data scanning method and device
CN112506632A (en) Method and system for scheduling batch tasks in distributed system

Legal Events

Date Code Title Description
121 Ep: the epo has been informed by wipo that ep was designated in this application

Ref document number: 14849743

Country of ref document: EP

Kind code of ref document: A1

NENP Non-entry into the national phase

Ref country code: DE

122 Ep: pct application non-entry in european phase

Ref document number: 14849743

Country of ref document: EP

Kind code of ref document: A1