If you want to identify goroutines at runtime, you can find in the attachments patches for Golang 1.10 to allow to assign a name to a goroutine.
These are patches against Golang 1.10 because I have been using the gccgo runtime that comes with GCC 8.4. This could help you forward for what you want. Hugo On Tue, Jun 30, 2020 at 9:50 AM Jan Mercl <0xj...@gmail.com> wrote: > On Tue, Jun 30, 2020 at 9:36 AM Durga Someswararao G > <durgasomeswararao...@gmail.com> wrote: > > > Is it possible to get goroutine id when we creted? > > Is it possible to get goroutines count while we creating? > > Is it possible to terminate a goroutine based on ID like if any > goroutine has infinite loops? > > Is it possible to clean up memory of variable like we have free in C? > > The answer to all of the above questions, in the first approximation, is > no. > > Unsafe tricks may help to achieve some of those goals. What's the > purpose? Is this a XY problem? > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/golang-nuts/CAA40n-ULnCkc7hw0c%3DQCTSAvyP3ZO4rXmTEjm5fb6m2QTJC%3Duw%40mail.gmail.com > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAARrXCTo3W4s6ahPDNHktU6U_uefRbjnENiDNkfA6Smqb7BCqg%40mail.gmail.com.
From 99d77ef6698838e3c77178a2c719369b3cac9f25 Mon Sep 17 00:00:00 2001 From: Hugo Cornelis <hugo.corne...@essensium.com> Date: Tue, 2 Jun 2020 19:44:08 +0200 Subject: [PATCH 2/2] improved goroutine name reporting wrt parsing --- libgo/go/runtime/traceback_gccgo.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/libgo/go/runtime/traceback_gccgo.go b/libgo/go/runtime/traceback_gccgo.go index b53dc6f8e30..652d3d7ba71 100644 --- a/libgo/go/runtime/traceback_gccgo.go +++ b/libgo/go/runtime/traceback_gccgo.go @@ -8,6 +8,7 @@ package runtime import ( + "os" "runtime/internal/sys" "sync/atomic" "unsafe" @@ -15,6 +16,9 @@ import ( func GoroutineSetName(name string) { var length uint32 + + os.Stderr.WriteString("GoroutineSetName(" + name + ")\n") + if len(name) > GoroutineNameLengthMax { length = GoroutineNameLengthMax } else { @@ -28,6 +32,7 @@ func GoroutineSetName(name string) { atomic.StoreUint32(&_g_.nameLength, 0) copy(_g_.name[:], name[0:length]) atomic.StoreUint32(&_g_.nameLength, length) + } func printcreatedby(gp *g) { @@ -174,7 +179,7 @@ func goroutineheader(gp *g) { nameLength := atomic.LoadUint32(&gp.nameLength) if nameLength != 0 { - print("goroutine ", gp.goid, " '") + print("goroutine, name: ", gp.goid, " '") gwrite(gp.name[0:nameLength]) print("' [", status) } else { -- 2.17.1
From 89996ebc2d19c9a8e0ee7d567c95430d81f42b26 Mon Sep 17 00:00:00 2001 From: Hugo Cornelis <hugo.corne...@essensium.com> Date: Mon, 18 May 2020 14:49:03 +0200 Subject: [PATCH 1/2] added goroutine naming --- libgo/go/runtime/proc.go | 1 + libgo/go/runtime/runtime2.go | 7 +++++++ libgo/go/runtime/traceback_gccgo.go | 32 ++++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/libgo/go/runtime/proc.go b/libgo/go/runtime/proc.go index a6746c9f0a8..10a2ffc2f62 100644 --- a/libgo/go/runtime/proc.go +++ b/libgo/go/runtime/proc.go @@ -2614,6 +2614,7 @@ func goexit0(gp *g) { gp.param = nil gp.labels = nil gp.timer = nil + gp.nameLength = 0 if gcBlackenEnabled != 0 && gp.gcAssistBytes > 0 { // Flush assist credit to the global pool. This gives diff --git a/libgo/go/runtime/runtime2.go b/libgo/go/runtime/runtime2.go index 0299d5a788f..0ff04fa867a 100644 --- a/libgo/go/runtime/runtime2.go +++ b/libgo/go/runtime/runtime2.go @@ -88,6 +88,10 @@ const ( _Gscanwaiting = _Gscan + _Gwaiting // 0x1004 ) +const ( + GoroutineNameLengthMax = 30 +) + const ( // P status _Pidle = iota @@ -435,6 +439,9 @@ type g struct { context g_ucontext_t // saved context for setcontext stackcontext [10]uintptr // split-stack context + + nameLength uint32 + name[GoroutineNameLengthMax] uint8 } type m struct { diff --git a/libgo/go/runtime/traceback_gccgo.go b/libgo/go/runtime/traceback_gccgo.go index 8551ec19ac3..b53dc6f8e30 100644 --- a/libgo/go/runtime/traceback_gccgo.go +++ b/libgo/go/runtime/traceback_gccgo.go @@ -9,9 +9,27 @@ package runtime import ( "runtime/internal/sys" + "sync/atomic" "unsafe" ) +func GoroutineSetName(name string) { + var length uint32 + if len(name) > GoroutineNameLengthMax { + length = GoroutineNameLengthMax + } else { + length = uint32(len(name)) + } + + _g_ := getg() + + //! is locking required here? Play safe wrt goroutineheader(). + + atomic.StoreUint32(&_g_.nameLength, 0) + copy(_g_.name[:], name[0:length]) + atomic.StoreUint32(&_g_.nameLength, length) +} + func printcreatedby(gp *g) { // Show what created goroutine, except main goroutine (goid 1). pc := gp.gopc @@ -150,7 +168,19 @@ func goroutineheader(gp *g) { if (gpstatus == _Gwaiting || gpstatus == _Gsyscall) && gp.waitsince != 0 { waitfor = (nanotime() - gp.waitsince) / 60e9 } - print("goroutine ", gp.goid, " [", status) + + //! play safe wrt races with GoroutineSetName() + + nameLength := atomic.LoadUint32(&gp.nameLength) + + if nameLength != 0 { + print("goroutine ", gp.goid, " '") + gwrite(gp.name[0:nameLength]) + print("' [", status) + } else { + print("goroutine ", gp.goid, " [", status) + } + if isScan { print(" (scan)") } -- 2.17.1