Home  >  Article  >  Backend Development  >  golang scheduling settings

golang scheduling settings

WBOY
WBOYOriginal
2023-05-19 09:18:30465browse

Golang is an efficient and easy-to-use programming language, and its scheduling mechanism is one of the important factors for its excellent performance. In Golang, the scheduler controls all Goroutines. Goroutines are lightweight threads of the Go language. Each Goroutine can run independently to achieve high concurrency. In order to give full play to the performance of Golang, we need to optimize the scheduling settings. This article will introduce how to set up Golang's scheduling mechanism.

Golang’s scheduler is based on the M:N model. Normally, one operating system thread (M) corresponds to multiple Goroutines (N). The scheduler will allocate Goroutines to available threads to achieve concurrent execution. The scheduler will schedule tasks according to the status of each Goroutine to ensure optimal utilization of computer resources.

Parameters you need to know when setting Golang's scheduler

Before setting up Golang's scheduler, you need to know the following parameters:

  1. GOMAXPROCS

GOMAXPROCS is a parameter used to set the maximum number of CPUs that a Golang program can use. By default, the value of GOMAXPROCS is the number of CPU cores of the machine. Setting this parameter too large or too small will affect Golang's performance.

If GOMAXPROCS is set too large, it will cause the number of threads to increase, which will cause the CPU to frequently switch between different threads, causing increased system burden and even affecting application execution efficiency.

If GOMAXPROCS is set too small, on the one hand, the program will not be able to fully utilize the performance of the multi-core CPU; on the other hand, since Golang's scheduler is based on preemptive scheduling, if GOMAXPROCS is set too small, This will cause Goroutine to be unable to be scheduled in time, resulting in Goroutine blocking and low program execution efficiency.

  1. GOGC

GOGC is a parameter used to control the triggering time and frequency of Golang's garbage collection. Golang's garbage collector is implemented based on the mark-and-sweep algorithm, which means that the program needs to pause for a period of time during operation for garbage collection. The default value of GOGC is 100, that is, when the heap memory usage in the system reaches 100% of the total memory, garbage collection will be triggered.

If GOGC is set too low, the system will frequently perform garbage collection, resulting in a decrease in program execution efficiency. If GOGC is set too high, it will take a long time for the garbage collector to be triggered, which will affect the memory usage efficiency of the program.

  1. GoSchedTimeout

GoSchedTimeout is a parameter used to set the scheduling interval of Goroutine. By default, Goroutine scheduling is based on competition for computer resources, rather than task scheduling according to a predetermined timeline.

If GoSchedTimeout is set to a very small value, it will cause Goroutine to be scheduled frequently, thereby reducing the execution efficiency of the program. On the contrary, if GoSchedTimeout is set to an excessively large value, Goroutine will not be scheduled in time, thus affecting the execution efficiency of the program.

  1. MaxBackgroundGC

MaxBackgroundGC is a parameter used to set the maximum number of concurrent garbage collections in Golang. Golang's garbage collection mechanism is performed in the background by default. MaxBackgroundGC is used to set the proportion of simultaneous garbage collection. The default value of this value is 1, that is, if there are currently 5 CPU cores, then a maximum of one garbage collection task can be performed in parallel in the background at the same time.

If MaxBackgroundGC is set too large, it will cause excessive use of system resources and affect the execution efficiency of the program. If MaxBackgroundGC is set too small, it will affect the efficiency of garbage collection.

Set Golang’s scheduling mechanism optimization

According to the above parameters, Golang’s scheduling mechanism can be optimized:

  1. Set GOMAXPROCS

In actual use, the optimal GOMAXPROCS value can be determined by testing different settings. This parameter can be set when the program starts:

import "runtime"
func main() {
    runtime.GOMAXPROCS(8)
    //...
}
  1. Set GOGC

When the program is initialized, the frequency and time point of garbage collection can be controlled by setting the value of GOGC :

import (
    "runtime"
    "time"
)
func main() {
    runtime.GC()
    //设置触发时机
    runtime.SetGCPercent(10)
    //设置触发时机和时间
    debug.SetGCPercent(20)
    debug.SetGCPercent(-1)
    //...
}
  1. Set GoSchedTimeout

Golang's scheduler is asynchronous by default, and determines the time Goroutine is scheduled based on CPU competition. Therefore, the default value of GoSchedTimeout is set relatively large. If you need to adjust it, you can set it in the program:

runtime.Gosched()
  1. Run GC regularly

Since Golang’s garbage collection mechanism is It is implemented based on the mark-and-sweep algorithm. It needs to pause the running of the program during garbage collection, which affects the execution efficiency of the program. Therefore, the impact of a long one-time garbage collection can be avoided by running the GC regularly. In the program, you can set it like this:

func gc() {
        for {
              select {
              case <-time.After(time.Minute):
                   runtime.GC()
              }
        }
}

Summary

Golang is a high-performance programming language, and its concurrency mechanism is one of the important factors for its excellent performance. This article details how to set up Golang's scheduling mechanism, including setting parameters such as GOMAXPROCS, GOGC, GoSchedTimeout and MaxBackgroundGC. By setting these parameters reasonably, the performance of Golang can be fully utilized and the execution efficiency of the program can be improved.

The above is the detailed content of golang scheduling settings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:golang image rotationNext article:golang image rotation