| 400 | } |
| 401 | |
| 402 | func heapProfiling(ctx context.Context, interval time.Duration) { |
| 403 | ticker := time.NewTicker(interval) |
| 404 | defer ticker.Stop() |
| 405 | numGoroutines.Add(1) |
| 406 | defer numGoroutines.Add(-1) |
| 407 | for { |
| 408 | select { |
| 409 | case <-ctx.Done(): |
| 410 | return |
| 411 | case <-ticker.C: |
| 412 | fileName := fmt.Sprintf("heap-%s.prof", time.Now().Format(time.RFC3339)) |
| 413 | heapProfBuf := bytes.Buffer{} |
| 414 | err := pprof.WriteHeapProfile(&heapProfBuf) |
| 415 | if err != nil { |
| 416 | log.Printf("Error writing heap profile: %v", err) |
| 417 | return |
| 418 | } |
| 419 | |
| 420 | err = os.WriteFile(fileName, heapProfBuf.Bytes(), os.ModePerm) |
| 421 | if err != nil { |
| 422 | log.Printf("Error writing heap profile to file: %v", err) |
| 423 | return |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | func mutexProfiling(ctx context.Context, interval time.Duration) { |
| 430 | ticker := time.NewTicker(interval) |