Goroutines priority (discussion)

818 views
Skip to first unread message

Sergey Shepelev

unread,
Sep 19, 2014, 11:27:24 AM9/19/14
to golan...@googlegroups.com
Hello.

So I was wondering about building embedded software, like smart house controller in Go with an architecture of running Go program as the only system process (PID 1).
And how Go scheduler could benefit from priorities akin to OS process priorities.
And it struck me that it's already possible with something like this:
  • by default, new goroutine runs with maximum, unlimited priority
  • it may later attempt to acquire chan struct{} semaphore, holds a slot in "token pool"
  • this acquisition may block until a slot is available which limits total number of goroutines running
  • multiple levels of semaphores implement "priority levels" in a simple and efficient way

Surely, this approach lacks in two departments:
  • it is cooperative by design, only "polite" goroutines may wish to decrease their priority
  • nobody limits actual CPU (or wall clock for that matter) time for each goroutine, but you can design levels and their corresponding expected resource loads
Combining these two restrictions, it may take a lot of thinking to achieve any benefits from cooperative priority assignment. Or, put it another way, this approach may bite you more often than benefit.

But it may be a place to start for some tasks.


Please, let me know what you think on this idea.

Best regards, Sergey Shepelev.

Dmitry Vyukov

unread,
Sep 19, 2014, 2:05:16 PM9/19/14
to Sergey Shepelev, golang-nuts
Hi,

This is still quite non-deterministic.
I would crete a single goroutine that implements scheduling policy and
knowns about all task types, their priorities, deadlines, starvation,
etc.

Basically every task/goroutine has an associated task descriptor, and
do something along the lines:

func (t *Task) Schedule() {
t.sched <- t // tell the scheduler that we are waiting
<-t.run // let the scheduler decide when we can run
}

The centralized scheduler would be a bad idea for a 128-core system.
But it should be perfectly OK for an embed 1 or 2 core system.

Sergey Shepelev

unread,
Sep 19, 2014, 3:24:15 PM9/19/14
to Dmitry Vyukov, golang-nuts

Thank you very much, Dmitry. Indeed, centralized scheduler is far more powerful solution.

Reply all
Reply to author
Forward
0 new messages