func setProgramLimits() { // Swarm runnable threads could be large when the number of nodes is large // or under request bursts. Most threads are occupied by network connections. // Increase max thread count from 10k default to 50k to accommodate it. const maxThreadCount int = 50 * 1000 debug.SetMaxThreads(maxThreadCount) }
func ThreadExhaustion() { debug.SetMaxThreads(10) c := make(chan int) for i := 0; i < 100; i++ { go func() { runtime.LockOSThread() c <- 0 select {} }() <-c } }
func NewSpider(policy CrawlPolicy, numDownloads uint, numWorkers uint) (Spider, error) { if numDownloads > MAX_DOWNLOADS || numWorkers > MAX_WORKERS { return nil, InvalidArgs } d := newSimpleDownloader("downloads", numDownloads) s := newSimpleScheduler(policy, numWorkers, d) // set the max number of threads debug.SetMaxThreads(MAX_WORKERS + MAX_DOWNLOADS + 1) return &engine{s, d}, nil }
// configureMaxThreads sets the Go runtime max threads threshold // which is 90% of the kernel setting from /proc/sys/kernel/threads-max func configureMaxThreads(config *Config) error { mt, err := ioutil.ReadFile("/proc/sys/kernel/threads-max") if err != nil { return err } mtint, err := strconv.Atoi(strings.TrimSpace(string(mt))) if err != nil { return err } maxThreads := (mtint / 100) * 90 debug.SetMaxThreads(maxThreads) logrus.Debugf("Golang's threads limit set to %d", maxThreads) return nil }
func main() { select { case <-time.After(time.Second): fmt.Println(time.Minute.Seconds(), time.Second) } select { case <-time.After(time.Minute): fmt.Println(time.Minute.Seconds(), time.Second) } var kvlist []int kvlist = make([]int, 0, 10) kvlist = append(kvlist, 1) fmt.Println(kvlist) m := make(map[int]int, 10) m[1] += 1 fmt.Println("aaaa:", string(runtime.CPUProfile()), m, cap(kvlist)) for i := 0; ; i++ { pc, file, line, ok := runtime.Caller(i) if ok == false { break } fmt.Println(pc, file, line, ok) //runtime.Breakpoint() } fmt.Println(runtime.Version()) buf := make([]byte, 1025) len := runtime.Stack(buf, true) fmt.Println(string(runtime.CPUProfile())) fmt.Println(len, string(buf[:len])) //debug.PrintStack() fmt.Println(debug.SetMaxThreads(10)) i := 100 fmt.Println(reflect.TypeOf((*int32)(nil)).Elem()) fmt.Println(reflect.ValueOf(&i).Kind()) fmt.Println(reflect.TypeOf(&i).Kind()) RoundPos(3, func(r, x, y int32) { println(r, x, y) }) }