You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

117 lines
3.4 KiB

  1. // Copyright 2018 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package profiler
  15. import (
  16. "bytes"
  17. "context"
  18. "fmt"
  19. "runtime"
  20. "time"
  21. "github.com/google/pprof/profile"
  22. )
  23. // heapProfile collects an in-use heap profile. The heap profiles returned by
  24. // the runtime also include the heap allocation metrics. We zero those out
  25. // since all allocations since program start are recorded, so these make the
  26. // profile large and there is a separate alloc profile type which shows
  27. // allocations from a set duration.
  28. func heapProfile(prof *bytes.Buffer) error {
  29. p, err := goHeapProfile()
  30. if err != nil {
  31. return err
  32. }
  33. for _, s := range p.Sample {
  34. s.Value[0] = 0
  35. s.Value[1] = 0
  36. }
  37. // Merge profile with itself to remove samples with only zero values and
  38. // reduce profile size.
  39. p, err = profile.Merge([]*profile.Profile{p})
  40. if err != nil {
  41. return err
  42. }
  43. return p.Write(prof)
  44. }
  45. // deltaAllocProfile collects an allocation profile by gathering a heap profile,
  46. // sleeping for the specified duration, gathering another heap profile and
  47. // subtracting the initial one from that. It then drops the in-use metrics from
  48. // the profile. If requested, it forces the GC before taking each of the heap
  49. // profiles, which improves the profile accuracy (see docs in
  50. // https://golang.org/src/runtime/mprof.go on why).
  51. func deltaAllocProfile(ctx context.Context, duration time.Duration, forceGC bool, prof *bytes.Buffer) error {
  52. p1, err := allocProfile(forceGC)
  53. if err != nil {
  54. return err
  55. }
  56. sleep(ctx, duration)
  57. p2, err := allocProfile(forceGC)
  58. if err != nil {
  59. return err
  60. }
  61. p1.Scale(-1)
  62. p, err := profile.Merge([]*profile.Profile{p1, p2})
  63. if err != nil {
  64. return err
  65. }
  66. p.DurationNanos = duration.Nanoseconds()
  67. return p.Write(prof)
  68. }
  69. // allocProfile collects a single heap profile, and removes all metrics but
  70. // allocation metrics.
  71. func allocProfile(forceGC bool) (*profile.Profile, error) {
  72. if forceGC {
  73. runtime.GC()
  74. }
  75. p, err := goHeapProfile()
  76. if err != nil {
  77. return nil, err
  78. }
  79. p.SampleType = p.SampleType[:2]
  80. for _, s := range p.Sample {
  81. s.Value = s.Value[:2]
  82. }
  83. return p, nil
  84. }
  85. // goHeapProfile collects a heap profile. It returns an error if the metrics
  86. // in the collected heap profile do not match the expected metrics.
  87. func goHeapProfile() (*profile.Profile, error) {
  88. var prof bytes.Buffer
  89. if err := writeHeapProfile(&prof); err != nil {
  90. return nil, fmt.Errorf("failed to write heap profile: %v", err)
  91. }
  92. p, err := profile.Parse(&prof)
  93. if err != nil {
  94. return nil, err
  95. }
  96. if got := len(p.SampleType); got != 4 {
  97. return nil, fmt.Errorf("invalid heap profile: got %d sample types, want 4", got)
  98. }
  99. for i, want := range []string{"alloc_objects", "alloc_space", "inuse_objects", "inuse_space"} {
  100. if got := p.SampleType[i].Type; got != want {
  101. return nil, fmt.Errorf("invalid heap profile: got %q sample type at index %d, want %q", got, i, want)
  102. }
  103. }
  104. return p, nil
  105. }