当前位置: 代码网 > it编程>前端脚本>Golang > 从源码解析golang Timer定时器体系

从源码解析golang Timer定时器体系

2025年02月13日 Golang 我要评论
timer、ticker使用及其注意事项在刚开始学习golang语言的时候就听说timer、ticker的使用要尤其注意,很容易出现问题,这次就来一探究竟。本文主要脉络:介绍定时器体系,并介绍常用使用

timer、ticker使用及其注意事项

在刚开始学习golang语言的时候就听说timer、ticker的使用要尤其注意,很容易出现问题,这次就来一探究竟。

本文主要脉络:

  • 介绍定时器体系,并介绍常用使用方式和错误使用方式
  • 源码解读

timer、ticker是什么?

timer和ticker都是定时器,不同的是:

  • timer是一次性的定时器
  • ticker是循环定时器,在底层实现上是timer触发后又重新设置下一次触发时间来实现的

正确的使用姿势

timer

对于timer,可以通过三种函数创建:time.newtimer​、time.afterfunc​、time.after​。

其使用范例如下:

// fntimer1 timer的使用用法
func fntimer1() {
	timer := time.newtimer(time.second * 5) //返回生成的timer
	fmt.printf("timer 的启动时间为:%v\n", time.now())

	expire := <-timer.c //注意这个channel是一个容量为1的channel!
	fmt.printf("timer 的触发时间为:%v\n", expire)
}

func fntimer2() {
	ch1 := make(chan int, 1)
	select {
	case e1 := <-ch1:
		//如果ch1通道成功读取数据,则执行该case处理语句
		fmt.printf("1th case is selected. e1=%v",e1)
	case <- time.after(2 * time.second): //time.after直接返回生成的timer的channel,所以一般用于超时控制
		fmt.println("timed out")
	}
}

func fntimer3() {
	_ = time.afterfunc(time.second*5, func() { //返回的也是timer,不过可以自己传入函数进行执行
		fmt.printf("定时器触发了,触发时间为%v\n", time.now())
	})

	fmt.printf("timer 的启动时间为:%v\n", time.now())
	time.sleep(10 * time.second) // 确保定时器触发
}

在底层原理上,三种不同的创建方式都是调用的time.newtimer​,不同的是:

  • time.after​直接返回的是生成的timer的channel,而time.newtimer​是返回生成的timer。
  • time.newtimer​定时触发channel的原理就是定时调用一个sendtime​函数,这个函数负责向channel中发送触发时间;time.afterfunc​就是将定时调用的sendtime​函数换成了一个自定义的函数。

补充:

  • 返回的channel的容量为1,因此是一个asynchronous的channel,即在定时器fired(触发)、stop(停止)、reset(重启)之后,仍然有可能能收到数据~

  • 垃圾回收:在go1.23之前,对于处于active(没有触发且没有显式调用stop)的timer,gc是无法回收的,ticket也是同样的道理。因此在高并发的场景下需要显式搭配​​defer time.stop()​​来解决暂时的“内存泄露”的问题。

  • 上述两点在golang 1.23得到了解决,且可能在未来的版本中channel的容量会改为0(由asynchronous改成sync),在go 1.23相关源码的注释部分有对应的说明。

ticket

ticket相比于timer,其会循环触发,因此通常用于循环的干一些事情,like:

// fnticket2 ticker的正确的使用方法!!
func fnticket2() {
	ticker := time.newticker(1 * time.second)
	stopticker := make(chan struct{})
	defer ticker.stop()
	go func() {
		for {
			select {
			case now := <-ticker.c:
				// do something
				fmt.printf("ticker 的触发时间为:%v\n", now)
			case <-stopticker:
				fmt.println("ticker 结束")
				return
			}
		}
	}()

	time.sleep(4 * time.second)
	stopticker <- struct{}{}
}

注意:代码块中使用stopticker​ + select​的方式是必须的,由于stop函数只是修改定时器的状态,并不会主动关闭channel,因此如果直接使用循环(见我的github仓库)会直接导致ticker永远不退出而导致内存泄露!

补充:ticket = 触发时自动设置下一次时间的timer,因此上面提到的go1.23之前的timer存在的问题依然存在。

  • 返回的channel容量为1
  • 垃圾回收:由于ticket会一直循环触发,因此如果不显式调用time.stop方法的话,会永久内存泄露。

此外,对于ticket,还有一些额外的注意事项:

  • 需要使用一个额外的channel+select的方式来正确停止ticket。
  • reset函数的问题,由于比较长,单独整理在golang1.23版本之前 timer reset无法正确使用的问题,这里说个结论 :​reset​​函数由于内部非原子,因此无法完美的使用,建议使用goroutine+timer的方式替代!

使用的注意事项

由于golang 1.23版本对定时器timer、ticker做了很大的改进,因此要分成1.23之前和1.23及其之后的版本分开考虑:

以下是1.23版本关于timer、ticker部分的修改:

未停止的定时器和不再被引用的计时器可以进行垃圾回收。在 go 1.23 之前,未停止的定时器无法被垃圾回收,直到定时器超时,而未停止的计时器永远无法被垃圾回收。go 1.23 的实现避免了在不使用 t.stop​ 的程序中出现资源泄漏。

定时器通道现在是同步的(无缓冲的),这使 t.reset​ 和 t.stop​ 方法具有更强的保证:在其中一个方法返回后,将来从定时器通道接收到的任何值都不会观察到与旧定时器配置相对应的陈旧时间值。在 go 1.23 之前,无法使用 t.reset​ 避免陈旧值,而使用 t.stop​ 避免陈旧值需要仔细使用 t.stop​ 的返回值。go 1.23 的实现完全消除了这种担忧。

总结一下:1.23版本改进了timer、ticker的垃圾回收和 停止、重置的相关方法(reset、stop)。

这也就意味着在1.23版本之前,我们在使用的时候要注意:垃圾回收和停止、重置相关方法的使用
由于reset、stop方法的外在表现本质上上是跟缓冲区由 有缓冲 改为 无缓冲 相关,因此如果有涉及读取缓冲区我们也需要注意相关特性。

具体来说,对于1.23之前:

  • 垃圾回收:timer的回收只会在定时器触发(expired)或者stop​之后;ticker只显式触发stop​之后才会回收;
  • reset​、stop​使用:对于timer,没有完美的做法,无论怎么样reset​和stop​都可能存在一些问题;对于ticker,记得使用完之后显式的stop​;

源码解读

源码解读版本:release-branch.go1.8

运作原理

timer(ticket)的运作依赖于struct p​,源码位置:src/runtime/runtime2.go:603

所有的计时器timer都以最小四叉堆的形式存储在struct p​的timers​字段中。并且也是交给了计时器都交由处理器的网络轮询器和调度器触发,这种方式能够充分利用本地性、减少上下文的切换开销,也是目前性能最好的实现方式。

一般来说管理定时器的结构有3种:双向链表、最小堆、时间轮(很少)。

双向链表:插入|修改时间:需要遍历去寻找插入|修改的位置,时间复杂度o(n);触发:链表头触发即可,时间复杂度o(1)。

最小堆:插入|修改时间:o(logn)的时间复杂度;触发:队头触发,但是触发之后需要调整堆以保证堆的特性,o(logn)的时间复杂度。

时间轮:插入|修改时间|触发的时间复杂度都是o(1)。但是需要额外维护时间轮的结构,其占据空间随着需要维护的未来时间长度、时间精度增加。

涉及结构体

定时器体系有两种timer:一次性触发的timer​和循环触发的ticker​,这两种的底层结构是相同的,触发逻辑是类似的。

毕竟ticker​的功能包含timer​的功能。

定时器在源码中使用的结构体是timer​,其定义为:

// package time knows the layout of this structure.
// if this struct changes, adjust ../time/sleep.go:/runtimetimer.
type timer struct {
	// if this timer is on a heap, which p's heap it is on.
	// puintptr rather than *p to match uintptr in the versions
	// of this struct defined in other packages.
	pp puintptr //指针,指向持有当前timer的p

	// timer wakes up at when, and then at when+period, ... (period > 0 only)
	// each time calling f(arg, now) in the timer goroutine, so f must be
	// a well-behaved function and not block.
	//
	// when must be positive on an active timer.
	when   int64              //当前计时器被唤醒的时间;
	period int64              //两次被唤醒的间隔;
	f      func(any, uintptr) //唤醒时要执行的函数
	arg    any                // 计时器被唤醒时调用 f 传入的参数;
	seq    uintptr

	// what to set the when field to in timermodifiedxx status.
	nextwhen int64 //当定时器状态变为timermodifiedxx的时,when 字段下一次要设置的值

	// the status field holds one of the values below.
	status uint32 //计时器的状态,最重要的字段之一
}

timer​在p​中的相关字段为:

type p struct {
	// lock for timers. we normally access the timers while running
	// on this p, but the scheduler can also do it from a different p.
	timerslock mutex //操作timers的时候加锁

	// actions to take at some time. this is used to implement the
	// standard library's time package.
	// must hold timerslock to access.
	timers []*timer //timers数组

	// number of timers in p's heap.
	// modified using atomic instructions.
	numtimers uint32 //总timers的数量

	// number of timerdeleted timers in p's heap.
	// modified using atomic instructions.
	deletedtimers uint32 //处于deleted状态的timers的数量

	// race context used while executing timer functions.
	timerracectx uintptr //竞态检测相关
}

状态机

go内部的定时器的操作是并发安全的(新建定时器、停止定时器)等,为了支持并发安全和定时器的高效调度,在源码中设计了一套关于定时器的状态机,全部的状态为:

状态解释
timernostatus还没有设置状态
timerwaiting定时器等待触发
timerrunningtimer正在运行
timerdeleted定时器被标记删除
timerremoving定时器从标记删除到真正删除的中间态
timerremoved定时器真正被删除
timermodifying正在被修改的中间状态
timermodifiedearlier定时器被修改到了更早的触发时间
timermodifiedlater定时器被修改到了更晚的触发时间
timermoving已经被修改正在被移动

修改定时器的状态机涉及如下所示的 7 种不同操作,它们分别承担了不同的职责:

  • runtime.addtimer​ — 向当前处理器增加新的计时器
  • runtime.deltimer​ — 将计时器标记成 timerdeleted​ 删除处理器中的计时器
  • runtime.modtimer​ — 网络轮询器会调用该函数修改计时器
  • runtime.cleantimers​ — 清除队列头中的计时器,能够提升程序创建和删除计时器的性能
  • runtime.adjusttimers​ — 调整处理器持有的计时器堆,包括移动会稍后触发的计时器、删除标记为 timerdeleted​ 的计时器
  • runtime.runtimer​ — 检查队列头中的计时器,在其准备就绪时运行该计时器

状态机的变化流程图 可以大概帮我们看出timer​的不同状态的流转情况:

@startuml

[*] --> timernostatus : 运行时创建timer

timernostatus -->timerwaiting : addtimer

timerwaiting -->timermodifying : deltimer、modtimer

timermodifying -->timerdeleted : deltimer

timermodifiedlater -->timerdeleted : deltimer

timermodifiedearlier -->timermodifying : deltimer


timerdeleted -->timerremoving : cleantimers、adjusttimers、runtimer

timerremoving -->timerremoved : cleantimers、adjusttimers、runtimer

timermodifiedearlier --> timermoving : cleantimers、adjusttimers

timermodifiedlater --> timermoving : cleantimers、adjusttimers

timermoving --> timerwaiting : cleantimers

timerwaiting --> timerrunning : runtimer

timerrunning --> timerwaiting : runtimer

timerrunning --> timernostatus : runtimer

state timermodifiedxx {
  state timermodifiedearlier {
  }
  state timermodifiedlater {
  }
}

timermodifying --> timermodifiedxx : modtimer
timermodifiedxx --> timermodifying : modtimer

timernostatus   --> timermodifying : modtimer
timermodifying --> timerwaiting : modtimer
timerremoved --> timermodifying : modtimer
timerdeleted    --> timermodifying : modtimer

timerwaiting : 定时器等待触发
timermodifying : 定时器状态修改的中间态
timerdeleted : 定时器被标记删除的状态

timerremoving: 定时器从标记删除到真正被删除的中间态
timerremoved: 定时器真正被删除

timermodifiedearlier: 定时器被修改到了更早的触发时间
timermodifiedlater : 定时器被修改到了更晚的触发时间

timermoving: 定时器在堆上的位置正在重新排序
timerrunning: timer正在运行
timermodifiedxx: 定时器在堆上的位置等待重新排序

@enduml

实际上这些状态的流转都被完整的写在了golang的源码中,在后面逐个函数的讲解中也会涉及到:

// addtimer:
//   timernostatus   -> timerwaiting
//   anything else   -> panic: invalid value
// deltimer:
//   timerwaiting         -> timermodifying -> timerdeleted
//   timermodifiedearlier -> timermodifying -> timerdeleted
//   timermodifiedlater   -> timermodifying -> timerdeleted
//   timernostatus        -> do nothing
//   timerdeleted         -> do nothing
//   timerremoving        -> do nothing
//   timerremoved         -> do nothing
//   timerrunning         -> wait until status changes
//   timermoving          -> wait until status changes
//   timermodifying       -> wait until status changes
// modtimer:
//   timerwaiting    -> timermodifying -> timermodifiedxx
//   timermodifiedxx -> timermodifying -> timermodifiedyy
//   timernostatus   -> timermodifying -> timerwaiting
//   timerremoved    -> timermodifying -> timerwaiting
//   timerdeleted    -> timermodifying -> timermodifiedxx
//   timerrunning    -> wait until status changes
//   timermoving     -> wait until status changes
//   timerremoving   -> wait until status changes
//   timermodifying  -> wait until status changes
// cleantimers (looks in p's timer heap):
//   timerdeleted    -> timerremoving -> timerremoved
//   timermodifiedxx -> timermoving -> timerwaiting
// adjusttimers (looks in p's timer heap):
//   timerdeleted    -> timerremoving -> timerremoved
//   timermodifiedxx -> timermoving -> timerwaiting
// runtimer (looks in p's timer heap):
//   timernostatus   -> panic: uninitialized timer
//   timerwaiting    -> timerwaiting or
//   timerwaiting    -> timerrunning -> timernostatus or
//   timerwaiting    -> timerrunning -> timerwaiting
//   timermodifying  -> wait until status changes
//   timermodifiedxx -> timermoving -> timerwaiting
//   timerdeleted    -> timerremoving -> timerremoved
//   timerrunning    -> panic: concurrent runtimer calls
//   timerremoved    -> panic: inconsistent timer heap
//   timerremoving   -> panic: inconsistent timer heap
//   timermoving     -> panic: inconsistent timer heap

addtimer源码

addtimer对于状态的操作:

  • timernostatus -> timerwaiting
  • anything else -> panic: invalid value

addtimer的主要功能:向当前p的定时器堆中添加当前定时器,并尝试唤醒网络轮训器(定时器的执行依赖于网络轮训器处理)。

这里提到的网络轮训器可能让人有点疑惑,定时器和网络有什么关系?实际上确实也没什么关系,这里提到的网络轮训器重点在于轮训器,指的更多的是select、poll、epoll那套东西。

func addtimer(t *timer) {
	 // xxx
	if t.status != timernostatus {
		throw("addtimer called with initialized timer")
	}
	t.status = timerwaiting

	when := t.when

	// disable preemption while using pp to avoid changing another p's heap.
	mp := acquirem()

	pp := getg().m.p.ptr()
	lock(&pp.timerslock)
	cleantimers(pp)       //尝试清除timers堆中堆头的元素,以加速定时器添加
	doaddtimer(pp, t)
	unlock(&pp.timerslock)

	wakenetpoller(when)

	releasem(mp)
}
  • cleantimers真的能加速吗?为什么?

deltimer源码

time.stoptimer​的底层实际调用就是deltimer​。

deltimer​对于状态的操作:

// timerwaiting -> timermodifying -> timerdeleted
// timermodifiedearlier -> timermodifying -> timerdeleted
// timermodifiedlater -> timermodifying -> timerdeleted
// timernostatus -> do nothing
// timerdeleted -> do nothing
// timerremoving -> do nothing
// timerremoved -> do nothing
// timerrunning -> wait until status changes
// timermoving -> wait until status changes
// timermodifying -> wait until status changes

deltimer​的主要功能:对于传入的定时器进行标记删除(状态status设置为timerdeleted​)。

// deltimer deletes the timer t. it may be on some other p, so we can't
// actually remove it from the timers heap. we can only mark it as deleted.
// it will be removed in due course by the p whose heap it is on.
// reports whether the timer was removed before it was run.
func deltimer(t *timer) bool {
	for {
		switch s := atomic.load(&t.status); s {
		case timerwaiting, timermodifiedlater:
			// prevent preemption while the timer is in timermodifying.
			// this could lead to a self-deadlock. see #38070.
			mp := acquirem()
			if atomic.cas(&t.status, s, timermodifying) {
				// 必须要先拿到tpp,因为当状态设置为timerdeleted之后,timer就有可能被清除(cleantimers函数),就拿不到tpp了
				tpp := t.pp.ptr()
				if !atomic.cas(&t.status, timermodifying, timerdeleted) {
					badtimer()
				}
				releasem(mp)
				atomic.xadd(&tpp.deletedtimers, 1)
				// timer was not yet run.
				return true
			} else {
				releasem(mp)
			}
		case timermodifiedearlier:
			// 这里和上面case的代码在源码中除了注释少一点其他一模一样,暂不清楚为什么
			mp := acquirem()
			if atomic.cas(&t.status, s, timermodifying) {
				tpp := t.pp.ptr() //先拿到tpp,原理同上
				if !atomic.cas(&t.status, timermodifying, timerdeleted) {
					badtimer()
				}
				releasem(mp)
				atomic.xadd(&tpp.deletedtimers, 1)
				// timer was not yet run.
				return true
			} else {
				releasem(mp)
			}
		case timerdeleted, timerremoving, timerremoved:
			// timer was already run.
			return false
		case timerrunning, timermoving:
			// the timer is being run or moved, by a different p.
			// wait for it to complete.
			osyield()
		case timernostatus:
			// removing timer that was never added or
			// has already been run. also see issue 21874.
			return false
		case timermodifying:
			// simultaneous calls to deltimer and modtimer.
			// wait for the other call to complete.
			osyield()
		default:
			badtimer()
		}
	}
}

modtimer源码

time.reset​方法底层实际上调用就是modtimer​方法。

modtimer​对于状态的修改,可以简单的归纳为:

  • 当前timer还在heap中:修改为timermodifiedxx​状态(等待重新排序触发)
  • 当前timer不在heap中:修改为等待调度timerwaiting​状态
  • 当前timer在修改的中间态(xxing状态):xxing相当于是被锁定的状态,因此等待状态发生变动
//   timerwaiting    -> timermodifying -> timermodifiedxx
//   timermodifiedxx -> timermodifying -> timermodifiedyy
//   timernostatus   -> timermodifying -> timerwaiting
//   timerremoved    -> timermodifying -> timerwaiting
//   timerdeleted    -> timermodifying -> timermodifiedxx
//   timerrunning    -> wait until status changes
//   timermoving     -> wait until status changes
//   timerremoving   -> wait until status changes
//   timermodifying  -> wait until status changes

modtimer的主要功能:重置定时器。具体来说,首先判断timer还在不在heap中

  • 还在:修改状态timermodifiedxx​,等待重新触发
  • 不在:重新添加到heap中,等待重新触发

resettimer源码

底层调用的就是modtimer,这里不多赘述了。

// resettimer resets the time when a timer should fire.
// if used for an inactive timer, the timer will become active.
// this should be called instead of addtimer if the timer value has been,
// or may have been, used previously.
// reports whether the timer was modified before it was run.
func resettimer(t *timer, when int64) bool {
	return modtimer(t, when, t.period, t.f, t.arg, t.seq)
}

cleantimers源码

在addtimer中有调用,具体会在添加新定时器之前调用(addtimer源码)。

函数作用为:尝试清理heap头(第一个元素)的定时器:移除或者调整到正确的位置,可以加速addtimer​添加定时器。

// cleantimers cleans up the head of the timer queue. this speeds up
// programs that create and delete timers; leaving them in the heap
// slows down addtimer. reports whether no timer problems were found.
// the caller must have locked the timers for pp.
func cleantimers(pp *p) {
	gp := getg()
	for {
		if len(pp.timers) == 0 {
			return
		}

		t := pp.timers[0]

		switch s := atomic.load(&t.status); s {
		case timerdeleted: //被标记删除,现在正式移除
			if !atomic.cas(&t.status, s, timerremoving) {
				continue
			}
			dodeltimer0(pp)
			if !atomic.cas(&t.status, timerremoving, timerremoved) {
				badtimer()
			}
			atomic.xadd(&pp.deletedtimers, -1)
		case timermodifiedearlier, timermodifiedlater: //定时器被调整,移动其到正确的位置
			if !atomic.cas(&t.status, s, timermoving) {
				continue
			}
			// now we can change the when field.
			t.when = t.nextwhen
			// move t to the right position.
			dodeltimer0(pp)
			doaddtimer(pp, t)
			if !atomic.cas(&t.status, timermoving, timerwaiting) {
				badtimer()
			}
		default:
			// head of timers does not need adjustment.
			return
		}
	}
}

adjusttimers源码

adjusttimers​对状态的修改:

// adjusttimers (looks in p's timer heap):
//   timerdeleted    -> timerremoving -> timerremoved
//   timermodifiedxx -> timermoving -> timerwaiting

adjusttimers​的主要作用与cleantimers​相同:尝试清理heap的定时器:移除或者调整到正确的位置。

不同的是:cleantimers只会对堆头的元素进行处理,而adjusttimers是遍历堆中所有的元素进行处理。

很有意思的一点是:对于timermodifiedxx​状态的定时器,由于是触发时间修改了,因此需要调整其在堆中的位置,golang这边选择的做法是先删除(dodeltimer​)再添加(doaddtimer​)的方法调整位置。

runtimer

对状态的修改:

// runtimer (looks in p's timer heap):
//   timernostatus   -> panic: uninitialized timer
//   timerwaiting    -> timerwaiting or
//   timerwaiting    -> timerrunning -> timernostatus or
//   timerwaiting    -> timerrunning -> timerwaiting
//   timermodifying  -> wait until status changes
//   timermodifiedxx -> timermoving -> timerwaiting
//   timerdeleted    -> timerremoving -> timerremoved
//   timerrunning    -> panic: concurrent runtimer calls
//   timerremoved    -> panic: inconsistent timer heap
//   timerremoving   -> panic: inconsistent timer heap
//   timermoving     -> panic: inconsistent timer heap

主要作用:循环遍历堆中第一个定时器并操作:

  • 如果第一个定时器为timerwaiting​状态:已经到达触发时间久运行并调整时间,然后返回;未到触发时间久直接返回。
  • 其它状态:进行对应的操作并再次循环。对应的操作举例:timermodifiedxx​-》调整时间;timerdeleted​-》从堆中移除定时器。

为了保证正确性,runtimer​肯定会在adjusttimers​之后运行:

if len(pp.timers) > 0 {
		adjusttimers(pp, now)
		for len(pp.timers) > 0 {
			// note that runtimer may temporarily unlock
			// pp.timerslock.
			if tw := runtimer(pp, now); tw != 0 {
				if tw > 0 {
					polluntil = tw
				}
				break
			}
			ran = true
		}
}

状态机规律总结

  • active​和inactive​:如果阅读了源码,会发现对于定时器的状态,还有active、inactive的分类,实际上active状态的定时器是等待未来触发的定时器(包括但不限与timewaiting​状态),而正常不会再触发的定时器则为inactive(timeremoved​、timerdeleted​等)。

  • 在heap中和不在heap中:下方会解释状态机的管理和堆有什么关系?

  • xxing状态相当于是一个锁定状态,不允许其他goroutine并发操作,可以理解成锁。

定时器的触发

在上面的部分中,讲解了timers体系中不同函数对于不同状态的流转。

这里将分析器的触发过程,go 语言会在两个模块触发计时器,运行计时器中保存的函数:

  • 调度器调度时会检查处理器中的计时器是否准备就绪;
  • 系统监控会检查是否有未执行的到期计时器;

我们将依次分析上述这两个触发过程。

调度器调度

runtime.checktimers​ 是调度器用来运行处理器中计时器的函数,它会在发生以下情况时被调用:

  • 调度器调用 runtime.schedule​ 执行调度时;
  • 调度器调用 runtime.findrunnable​ 获取可执行的 goroutine 时;
  • 调度器调用 runtime.findrunnable​ 从其他处理器窃取计时器时;

这里不展开介绍 runtime.schedule​ 和 runtime.findrunnable​ 的实现了,重点分析用于执行计时器的runtime.checktimers​,我们将该函数的实现分成调整计时器、运行计时器和删除计时器三个部分:

// checktimers runs any timers for the p that are ready.
// if now is not 0 it is the current time.
// it returns the passed time or the current time if now was passed as 0.
// and the time when the next timer should run or 0 if there is no next timer,
// and reports whether it ran any timers.
// if the time when the next timer should run is not 0,
// it is always larger than the returned time.
// we pass now in and out to avoid extra calls of nanotime.
//go:yeswritebarrierrec
func checktimers(pp *p, now int64) (rnow, polluntil int64, ran bool) {
	// if it's not yet time for the first timer, or the first adjusted
	// timer, then there is nothing to do.
	next := int64(atomic.load64(&pp.timer0when))
	nextadj := int64(atomic.load64(&pp.timermodifiedearliest))
	if next == 0 || (nextadj != 0 && nextadj < next) {
		next = nextadj
	}

	if next == 0 {
		// no timers to run or adjust.
		return now, 0, false
	}

	if now == 0 {
		now = nanotime()
	}
	if now < next {
		// next timer is not ready to run, but keep going
		// if we would clear deleted timers.
		// this corresponds to the condition below where
		// we decide whether to call cleardeletedtimers.
		//	当前并没有到触发时间,这个检查的目的就是为了查看位于deletedtimers状态的 定时器 的比例,如果比例过大,就要清理
        // 	清理就是调用cleardeletedtimers函数。
		if pp != getg().m.p.ptr() || int(atomic.load(&pp.deletedtimers)) <= int(atomic.load(&pp.numtimers)/4) {
			return now, next, false
		}
	}

	lock(&pp.timerslock)

	if len(pp.timers) > 0 {
		adjusttimers(pp, now)  //上面生成的now会传下来,因此不用担心函数执行导致的时间流逝
		for len(pp.timers) > 0 {
			// note that runtimer may temporarily unlock
			// pp.timerslock.
			if tw := runtimer(pp, now); tw != 0 { //上面生成的now会传下来,因此不用担心函数执行导致的时间流逝
				if tw > 0 {
					polluntil = tw
				}
				break
			}
			ran = true
		}
	}

	// if this is the local p, and there are a lot of deleted timers,
	// clear them out. we only do this for the local p to reduce
	// lock contention on timerslock.
	//如果运行当前goroutine的p是持有timers数组的p 且 处于deletedtimers状态的定时器 比例超过1/4,就清理掉这部分的定时器。
	if pp == getg().m.p.ptr() && int(atomic.load(&pp.deletedtimers)) > len(pp.timers)/4 {
		cleardeletedtimers(pp)
	}

	unlock(&pp.timerslock)

	return now, polluntil, ran
}

runtime.cleardeletedtimers​ 能够避免堆中出现大量长时间运行的计时器,该函数和 runtime.movetimers​ 也是唯二会遍历计时器堆的函数(movetimers​ which only runs when the world is stopped)。

具体可见cleardeletedtimers​的注释:

// this is the only function that walks through the entire timer heap,
// other than movetimers which only runs when the world is stopped.
func cleardeletedtimers(pp *p) {

系统监控

系统监控中也会触发调度器的执行,大概率是因为有时候m中可能存在不能被强占的情况,就有可能会导致timer的触发时间滞后。需要注意的是,虽然有系统监控,可以帮助timers及时触发,但是timers的触发并不能达到严格的实时性(系统监控检查timers调度延迟的阈值是10ms)。

这里我也对这个过程理解的不是很深刻,这里推荐去draveness大佬的在线图书中搜索【系统监控】关键词进行全面学习

补充问题

状态机的管理和堆有什么关系?

首先需要明确这里的堆指的是struct p​管理定时器所用的四叉堆,而不是 内存管理涉及的栈和堆。

一个定时器创建之后是timernostatus​状态,其并不在堆上,需要放在p​的堆上之后才能进行调度和触发!

比如如下语句中的堆字眼:

  • timernostatus​ 和 timerremoved​ — 计时器不在堆上;
  • timermodifiedearlier​ 和 timermodifiedlater​ — 计时器虽然在堆上,但是可能位于错误的位置上,需要重新排序;
// active timers live in heaps attached to p, in the timers field.
// inactive timers live there too temporarily, until they are removed.

衍生问题:哪些状态的timer在堆中?哪些状态在?

回答:可从源码中adjusttimers​函数中一窥究竟,timernostatus, timerrunning, timerremoving, timerremoved, timermoving​状态的定时器是不在堆上的。

此外,有些同学可能有疑惑,定时器从堆中移除的过程(可以参考cleantimers​),是先标记成timermoving​然后再从堆中移除,这两步不是原子的,如果状态已经是timermoving​但是还没从堆中移除,遇上adjusttimers,岂不是会出现panic。

实际上这个问题并不会出现,因为只要涉及对p​的timers​数组操作(更改持续、加减元素)的地方都会加上锁(lock(&pp.timerslock)​),而且每个p​也只会修改自己的timers​数组,不会修改其它p​持有的timers​数组,但是同样如果不涉及数组更改,只设计状态变更的话就不需要加上锁(比如标记删除元素)。

为什么time.go和sleep.go中有接近相同的结构体?

相同的结构体示例:

time.go中:

// package time knows the layout of this structure.
// if this struct changes, adjust ../time/sleep.go:/runtimetimer.
// for goos=nacl, package syscall knows the layout of this structure.
// if this struct changes, adjust ../syscall/net_nacl.go:/runtimetimer.
type timer struct {
	i int // heap index

	// timer wakes up at when, and then at when+period, ... (period > 0 only)
	// each time calling f(arg, now) in the timer goroutine, so f must be
	// a well-behaved function and not block.
	when   int64                      //当前计时器被唤醒的时间
	period int64                      //两次被唤醒的间隔;
	f      func(interface{}, uintptr) //每当计时器被唤醒时都会调用的函数;
	arg    interface{}                //计时器被唤醒时调用 f 传入的参数;
	seq    uintptr
}

sleep.go中:

// interface to timers implemented in package runtime.
// must be in sync with ../runtime/time.go:/^type timer
type runtimetimer struct {
	i      int
	when   int64
	period int64
	f      func(interface{}, uintptr) // note: must not be closure
	arg    interface{}
	seq    uintptr
}

回答:实际上这两个结构体一个是运行时一个是编译时,不过作者目前对这块也不是特别清楚,也欢迎大家指点指点。

为什么deltimer​函数只是标记删除,并不直接删除timer?

回答:因为定时器体系中,对于timers数组的更改需要加锁,如果没有更改的话就不需要加锁,为了能快速的stoptimer,因此标记删除并不需要拿锁,效率很高。什么时候加锁可参考:struct p中的定时器加锁

acquirem()​和releasem()​的作用

这个问题和go的调度模型gmp息息相关,这里就做一个不严谨的解释:acquirem​ 的作用之一是 为了保证当前 p不会被切换,粗暴理解就是对p而言,其相当于不会被“打断”,从而可以保证此时修改的p是当前goroutine所属的p。

	// disable preemption while using pp to avoid changing another p's heap.
	mp := acquirem()

	pp := getg().m.p.ptr()
	lock(&pp.timerslock)
	cleantimers(pp)
	doaddtimer(pp, t)
	unlock(&pp.timerslock)

	wakenetpoller(when)

	releasem(mp)

定时器的状态机中为什么有这么多中间状态?

相信这篇文章读完之后,这个已经不是问题了。

nextwhen​字段的作用,其存在的必要性

首先我们要明白nextwhen​字段的作用:用于记录下一次要设置when​字段为什么值

那么既然其用于标识下一次when​字段的值,那为什么不直接修改when​字段呢?

这是因为在当前的设计中,p只会修改自己的timers数组,如果当前p​修改了其他p​的when​字段,timers数组就无法正常排序了。所以需要使用nextwhen​来记录when​需要修改的值,等timers​数组对应的p​来修改when​的值。

这里涉及跨p操作定时器的问题。

每个p都会存放在其上创建的timer​,但是不同的goroutine​可能会在不同的p​上面,因此可能操作timer​的goroutine​所在的p​与存放timer​所在的p​并不是同一个p​。

// the timer is in some other p's heap, so we can't change
		// the when field. if we did, the other p's heap would
		// be out of order. so we put the new when value in the
		// nextwhen field, and let the other p set the when field
		// when it is prepared to resort the heap.

为什么要用atomic相关变量,而不直接使用锁

猜测主要原因还是性能,锁可能还是太重了。而且实际上对于有重试的代码,atomic相关的设置可能更加优雅,比如下面代码:

if !atomic.cas(&t.status, s, timermoving) {
	continue //continue用于等待重试
}

如果使用锁的话,那么伪代码如下,就算使用双重校验,可能还是很重

for {
		if t.status == s{ //双重校验,延迟加锁
			t.mu.lock() // 锁定
			if t.status == s {
				t.status = timermoving // 修改状态
				t.mu.unlock()           // 释放锁
				break                    // 修改成功,退出
			}
			t.mu.unlock() // 如果没有修改成功,解锁
		}
		// 继续等待重试
	}

编程风格的学习

什么时候校验值:在每一次调用的入口。虽然函数值之前已经校验过。取之:src/runtime/time.go:255

func addtimer(t *timer) {
	// when must be positive. a negative value will cause runtimer to
	// overflow during its delta calculation and never expire other runtime
	// timers. zero will cause checktimers to fail to notice the timer.
	if t.when <= 0 {
		throw("timer when must be positive")
	}
	if t.period < 0 {
		throw("timer period must be non-negative")
	}
	if t.status != timernostatus {
		throw("addtimer called with initialized timer")
	}
xxx
}

函数的分层设计

// starttimer adds t to the timer heap.
//
//go:linkname starttimer time.starttimer
func starttimer(t *timer) {
	if raceenabled {
		racerelease(unsafe.pointer(t))
	}
	addtimer(t)
}

函数与方法:

可以思考下下面这里为什么是方法而不是p的函数。

个人认为因为这里并不设计直接修改结构体p​的值,所以设计成方法可读性更强。换言之,如果要修改对应的结构体的值,才创建函数,否则优先使用方法。

// updatetimermodifiedearliest updates the recorded nextwhen field of the
// earlier timermodifiedearier value.
// the timers for pp will not be locked.
func updatetimermodifiedearliest(pp *p, nextwhen int64) {
	for {
		old := atomic.load64(&pp.timermodifiedearliest)
		if old != 0 && int64(old) < nextwhen {
			return
		}
		if atomic.cas64(&pp.timermodifiedearliest, old, uint64(nextwhen)) {
			return
		}
	}
}

可以用注释表明当前函数必须被什么锁给锁定住

// doaddtimer adds t to the current p's heap.
// the caller must have locked the timers for pp.   //注释说明,这个函数必须锁定之后才能进入
func doaddtimer(pp *p, t *timer) {

xxx
}

总结

本文解开了timer体系相关的状态流转,但是对于现在timer中存在的问题(reset、垃圾回收)在golang1.23怎么得到解决的机制还没有探究,这个可以等待后续研究研究。

参考资料:

https://draveness.me/golang/docs/part3-runtime/ch06-concurrency/golang-timer/

计时器 | go 语言进阶之旅

go中定时器实现原理及源码解析-腾讯云开发者社区-腾讯云

golang 定时器(timer 和 ticker ),这篇文章就够了定时器是什么golang原生time包下可以用来执 - 掘金

golang源码

到此这篇关于从源码解析golang timer定时器体系的文章就介绍到这了,更多相关golang timer定时器体系解析内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com