SlideShare a Scribd company logo
Hands-on Go
Profiling
Daniel	Ammar
•‫ה‬Be the Central Hub of All
Mobility Services Exposures and Consumption
Agenda
• Introduction
• Profiling in Golang
- CPU Profiler
- Memory Profiler
- Block Profiler
- Goroutine Profiler
- Mutex Profiler
• runtime/trace
• Community additions
• D e m o
• Caveats
• Q&A*
01
Profiling
Profiling
https://stackify.com/what-is-code-profiling/
Diagnostics
• Profiling: analyze the complexity and costs of a program such as its
memory, cpu cycles etc
• Tracing: Tracing is a way to instrument code to analyze latency
throughout the lifecycle of a request
• Debugging: Debugging allows us to pause a Go program and examine
its execution.
• Runtime statistics and events: Collection and analysis of runtime
stats and events provides a high-level overview of the health of a
program.
Profiling in Go
https://blog.golang.org/profiling-go-programs
Profiling in Go
• go test flags
• Manual profile collection
• Import “net/http/pprof” package for
side effect
• go tool pprof/trace command
• Community tools
DatacollectionDatavisualization
Built-in Profiles
• CPU
Determines where a program spends its time while actively consuming CPU cycles (as
opposed to while sleeping or waiting for I/O)
Sampled 100 times per second (default)
• Memory
Reports current and historical memory allocation samples
Sampled per allocation of 512KB (default)
• Block
Reports where goroutines block waiting on synchronization primitives (including
timer channels)
Not enabled by default (runtime.SetBlockProfileRate(ns))
• Goroutine
Reports the stack traces of all current goroutines
• Mutex
Reports lock contentions
Not enabled by default (runtime.SetMutexProfileFraction)
Runtime/trace
The tracer captures a wide range of execution events by looking directly
into the goroutine scheduling implementation.
In contrast to runtime/pprof sampling nature, the tracer gets notified
when an event occurs:
• goroutine creation/blocking/unblocking,
• syscall enter/exit/block
• GC-related events
• changes of heap size
• processor start/stop
• …
Community tools
• pprof - a tool for visualization and analysis of profiling data -
https://github.com/google/pprof
• Go-torch flame graph - https://github.com/uber/go-torch
• Gom a visual interface - https://github.com/rakyll/gom
02
Demo
Fibonacci example
package main
import (
"log"
"os"
"runtime/pprof"
)
func main() {
f, err := os.Create("cpu.pprof")
if err != nil {
log.Fatal("Error creating file")
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("Failed to start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
z := Fibonacci(42)
log.Printf("The 42nd Fibonacci number is %dn", z)
}
func Fibonacci(n int) int {
if n < 2 {
return n
}
return Fibonacci(n-1) + Fibonacci(n-2)
}
Fibonacci with 10Mb example
package main
import (
"log"
"net/http"
_ "net/http/pprof"
)
func bigBytes() *[]byte {
s := make([]byte, 1000000)
return &s
}
func main() {
go func() {
log.Fatal(http.ListenAndServe(":6060", nil))
}()
for i := 0; i < 10; i++ {
s := bigBytes()
if s == nil {
log.Println("can't allocate memory")
}
}
z := Fibonacci(42)
log.Printf("The 42nd Fibonacci number is %dn", z)
select {}
}
func Fibonacci(n int) int {
if n < 2 {
return n
}
return Fibonacci(n-1) + Fibonacci(n-2)
}
Regex Example
Move method variable to by initialized once, i.e., outside the function
var regx = regexp.MustCompile(`^w*$`)
Runtime/trace Example
In theory many small & short-lived goroutines are excellent way to
handle CPU intensive tasks.
In practice our program will stay most of its time in context switching
Caveats
• Profilers affect service!
• Profiles are only as good as the kernel support used to generate
them
• Can interfere with each other
• runtime.pprof is sample based
• Benchmarking not on a shared host (noisy neighbors problem)
• Known bugs - https://github.com/golang/go/issues/13841
Q&A
Thank you

More Related Content

Similar to Hands-on go profiling

PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
Graham Dumpleton
 
Where is the bottleneck
Where is the bottleneckWhere is the bottleneck
Where is the bottleneck
Manuel Miranda de Cid
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
Steve Caron
 
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes with ...
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes  with ...GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes  with ...
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes with ...
KAI CHU CHUNG
 
Introduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdfIntroduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdf
Yomna Mahmoud Ibrahim Hassan
 
Continuous Go Profiling & Observability
Continuous Go Profiling & ObservabilityContinuous Go Profiling & Observability
Continuous Go Profiling & Observability
ScyllaDB
 
Ladypy 01
Ladypy 01Ladypy 01
Ladypy 01
Calvin Cheng
 
Reproducibility and automation of machine learning process
Reproducibility and automation of machine learning processReproducibility and automation of machine learning process
Reproducibility and automation of machine learning process
Denis Dus
 
Swift profiling middleware and tools
Swift profiling middleware and toolsSwift profiling middleware and tools
Swift profiling middleware and toolszhang hua
 
Scaling python webapps from 0 to 50 million users - A top-down approach
Scaling python webapps from 0 to 50 million users - A top-down approachScaling python webapps from 0 to 50 million users - A top-down approach
Scaling python webapps from 0 to 50 million users - A top-down approach
Jinal Jhaveri
 
HPC Application Profiling and Analysis
HPC Application Profiling and AnalysisHPC Application Profiling and Analysis
HPC Application Profiling and AnalysisRishi Pathak
 
Optimizing and Profiling Golang Rest Api
Optimizing and Profiling Golang Rest ApiOptimizing and Profiling Golang Rest Api
Optimizing and Profiling Golang Rest Api
Iman Syahputra Situmorang
 
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
Cloud Foundry Monitoring How-To: Collecting Metrics and LogsCloud Foundry Monitoring How-To: Collecting Metrics and Logs
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
Altoros
 
How to improve ELK log pipeline performance
How to improve ELK log pipeline performanceHow to improve ELK log pipeline performance
How to improve ELK log pipeline performance
Steven Shim
 
HPC Application Profiling & Analysis
HPC Application Profiling & AnalysisHPC Application Profiling & Analysis
HPC Application Profiling & AnalysisRishi Pathak
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
vnwzympx
 
Guider: An Integrated Runtime Performance Analyzer on AGL
Guider: An Integrated Runtime Performance Analyzer on AGLGuider: An Integrated Runtime Performance Analyzer on AGL
Guider: An Integrated Runtime Performance Analyzer on AGL
Peace Lee
 
A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node
ibmwebspheresoftware
 
Django dev-env-my-way
Django dev-env-my-wayDjango dev-env-my-way
Django dev-env-my-way
Robert Lujo
 
DWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHubDWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHub
Marc Müller
 

Similar to Hands-on go profiling (20)

PyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web ApplicationsPyCon AU 2012 - Debugging Live Python Web Applications
PyCon AU 2012 - Debugging Live Python Web Applications
 
Where is the bottleneck
Where is the bottleneckWhere is the bottleneck
Where is the bottleneck
 
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
[ CNCF Q1 2024 ] Intro to Continuous Profiling and Grafana Pyroscope.pdf
 
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes with ...
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes  with ...GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes  with ...
GDG Cloud Taipei meetup #50 - Build go kit microservices at kubernetes with ...
 
Introduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdfIntroduction to Google Colaboratory.pdf
Introduction to Google Colaboratory.pdf
 
Continuous Go Profiling & Observability
Continuous Go Profiling & ObservabilityContinuous Go Profiling & Observability
Continuous Go Profiling & Observability
 
Ladypy 01
Ladypy 01Ladypy 01
Ladypy 01
 
Reproducibility and automation of machine learning process
Reproducibility and automation of machine learning processReproducibility and automation of machine learning process
Reproducibility and automation of machine learning process
 
Swift profiling middleware and tools
Swift profiling middleware and toolsSwift profiling middleware and tools
Swift profiling middleware and tools
 
Scaling python webapps from 0 to 50 million users - A top-down approach
Scaling python webapps from 0 to 50 million users - A top-down approachScaling python webapps from 0 to 50 million users - A top-down approach
Scaling python webapps from 0 to 50 million users - A top-down approach
 
HPC Application Profiling and Analysis
HPC Application Profiling and AnalysisHPC Application Profiling and Analysis
HPC Application Profiling and Analysis
 
Optimizing and Profiling Golang Rest Api
Optimizing and Profiling Golang Rest ApiOptimizing and Profiling Golang Rest Api
Optimizing and Profiling Golang Rest Api
 
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
Cloud Foundry Monitoring How-To: Collecting Metrics and LogsCloud Foundry Monitoring How-To: Collecting Metrics and Logs
Cloud Foundry Monitoring How-To: Collecting Metrics and Logs
 
How to improve ELK log pipeline performance
How to improve ELK log pipeline performanceHow to improve ELK log pipeline performance
How to improve ELK log pipeline performance
 
HPC Application Profiling & Analysis
HPC Application Profiling & AnalysisHPC Application Profiling & Analysis
HPC Application Profiling & Analysis
 
04_ForkPipe.pptx
04_ForkPipe.pptx04_ForkPipe.pptx
04_ForkPipe.pptx
 
Guider: An Integrated Runtime Performance Analyzer on AGL
Guider: An Integrated Runtime Performance Analyzer on AGLGuider: An Integrated Runtime Performance Analyzer on AGL
Guider: An Integrated Runtime Performance Analyzer on AGL
 
A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node A Taste of Monitoring and Post Mortem Debugging with Node
A Taste of Monitoring and Post Mortem Debugging with Node
 
Django dev-env-my-way
Django dev-env-my-wayDjango dev-env-my-way
Django dev-env-my-way
 
DWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHubDWX 2022 - DevSecOps mit GitHub
DWX 2022 - DevSecOps mit GitHub
 

Recently uploaded

BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
abdulrafaychaudhry
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 

Recently uploaded (20)

BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)Introduction to Pygame (Lecture 7 Python Game Development)
Introduction to Pygame (Lecture 7 Python Game Development)
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 

Hands-on go profiling

  • 2. •‫ה‬Be the Central Hub of All Mobility Services Exposures and Consumption
  • 3.
  • 4. Agenda • Introduction • Profiling in Golang - CPU Profiler - Memory Profiler - Block Profiler - Goroutine Profiler - Mutex Profiler • runtime/trace • Community additions • D e m o • Caveats • Q&A*
  • 7. Diagnostics • Profiling: analyze the complexity and costs of a program such as its memory, cpu cycles etc • Tracing: Tracing is a way to instrument code to analyze latency throughout the lifecycle of a request • Debugging: Debugging allows us to pause a Go program and examine its execution. • Runtime statistics and events: Collection and analysis of runtime stats and events provides a high-level overview of the health of a program.
  • 9. Profiling in Go • go test flags • Manual profile collection • Import “net/http/pprof” package for side effect • go tool pprof/trace command • Community tools DatacollectionDatavisualization
  • 10. Built-in Profiles • CPU Determines where a program spends its time while actively consuming CPU cycles (as opposed to while sleeping or waiting for I/O) Sampled 100 times per second (default) • Memory Reports current and historical memory allocation samples Sampled per allocation of 512KB (default) • Block Reports where goroutines block waiting on synchronization primitives (including timer channels) Not enabled by default (runtime.SetBlockProfileRate(ns)) • Goroutine Reports the stack traces of all current goroutines • Mutex Reports lock contentions Not enabled by default (runtime.SetMutexProfileFraction)
  • 11. Runtime/trace The tracer captures a wide range of execution events by looking directly into the goroutine scheduling implementation. In contrast to runtime/pprof sampling nature, the tracer gets notified when an event occurs: • goroutine creation/blocking/unblocking, • syscall enter/exit/block • GC-related events • changes of heap size • processor start/stop • …
  • 12. Community tools • pprof - a tool for visualization and analysis of profiling data - https://github.com/google/pprof • Go-torch flame graph - https://github.com/uber/go-torch • Gom a visual interface - https://github.com/rakyll/gom
  • 14. Fibonacci example package main import ( "log" "os" "runtime/pprof" ) func main() { f, err := os.Create("cpu.pprof") if err != nil { log.Fatal("Error creating file") } if err := pprof.StartCPUProfile(f); err != nil { log.Fatal("Failed to start CPU profile: ", err) } defer pprof.StopCPUProfile() z := Fibonacci(42) log.Printf("The 42nd Fibonacci number is %dn", z) } func Fibonacci(n int) int { if n < 2 { return n } return Fibonacci(n-1) + Fibonacci(n-2) }
  • 15. Fibonacci with 10Mb example package main import ( "log" "net/http" _ "net/http/pprof" ) func bigBytes() *[]byte { s := make([]byte, 1000000) return &s } func main() { go func() { log.Fatal(http.ListenAndServe(":6060", nil)) }() for i := 0; i < 10; i++ { s := bigBytes() if s == nil { log.Println("can't allocate memory") } } z := Fibonacci(42) log.Printf("The 42nd Fibonacci number is %dn", z) select {} } func Fibonacci(n int) int { if n < 2 { return n } return Fibonacci(n-1) + Fibonacci(n-2) }
  • 16. Regex Example Move method variable to by initialized once, i.e., outside the function var regx = regexp.MustCompile(`^w*$`)
  • 17. Runtime/trace Example In theory many small & short-lived goroutines are excellent way to handle CPU intensive tasks. In practice our program will stay most of its time in context switching
  • 18. Caveats • Profilers affect service! • Profiles are only as good as the kernel support used to generate them • Can interfere with each other • runtime.pprof is sample based • Benchmarking not on a shared host (noisy neighbors problem) • Known bugs - https://github.com/golang/go/issues/13841
  • 19. Q&A