garbage_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package debug
  5. import (
  6. "runtime"
  7. "testing"
  8. "time"
  9. )
  10. func TestReadGCStats(t *testing.T) {
  11. defer SetGCPercent(SetGCPercent(-1))
  12. var stats GCStats
  13. var mstats runtime.MemStats
  14. var min, max time.Duration
  15. // First ReadGCStats will allocate, second should not,
  16. // especially if we follow up with an explicit garbage collection.
  17. stats.PauseQuantiles = make([]time.Duration, 10)
  18. ReadGCStats(&stats)
  19. runtime.GC()
  20. // Assume these will return same data: no GC during ReadGCStats.
  21. ReadGCStats(&stats)
  22. runtime.ReadMemStats(&mstats)
  23. if stats.NumGC != int64(mstats.NumGC) {
  24. t.Errorf("stats.NumGC = %d, but mstats.NumGC = %d", stats.NumGC, mstats.NumGC)
  25. }
  26. if stats.PauseTotal != time.Duration(mstats.PauseTotalNs) {
  27. t.Errorf("stats.PauseTotal = %d, but mstats.PauseTotalNs = %d", stats.PauseTotal, mstats.PauseTotalNs)
  28. }
  29. if stats.LastGC.UnixNano() != int64(mstats.LastGC) {
  30. t.Errorf("stats.LastGC.UnixNano = %d, but mstats.LastGC = %d", stats.LastGC.UnixNano(), mstats.LastGC)
  31. }
  32. n := int(mstats.NumGC)
  33. if n > len(mstats.PauseNs) {
  34. n = len(mstats.PauseNs)
  35. }
  36. if len(stats.Pause) != n {
  37. t.Errorf("len(stats.Pause) = %d, want %d", len(stats.Pause), n)
  38. } else {
  39. off := (int(mstats.NumGC) + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
  40. for i := 0; i < n; i++ {
  41. dt := stats.Pause[i]
  42. if dt != time.Duration(mstats.PauseNs[off]) {
  43. t.Errorf("stats.Pause[%d] = %d, want %d", i, dt, mstats.PauseNs[off])
  44. }
  45. if max < dt {
  46. max = dt
  47. }
  48. if min > dt || i == 0 {
  49. min = dt
  50. }
  51. off = (off + len(mstats.PauseNs) - 1) % len(mstats.PauseNs)
  52. }
  53. }
  54. q := stats.PauseQuantiles
  55. nq := len(q)
  56. if q[0] != min || q[nq-1] != max {
  57. t.Errorf("stats.PauseQuantiles = [%d, ..., %d], want [%d, ..., %d]", q[0], q[nq-1], min, max)
  58. }
  59. for i := 0; i < nq-1; i++ {
  60. if q[i] > q[i+1] {
  61. t.Errorf("stats.PauseQuantiles[%d]=%d > stats.PauseQuantiles[%d]=%d", i, q[i], i+1, q[i+1])
  62. }
  63. }
  64. }
  65. var big = make([]byte, 1<<20)
  66. func TestFreeOSMemory(t *testing.T) {
  67. var ms1, ms2 runtime.MemStats
  68. if big == nil {
  69. t.Skip("test is not reliable when run multiple times")
  70. }
  71. big = nil
  72. runtime.GC()
  73. runtime.ReadMemStats(&ms1)
  74. FreeOSMemory()
  75. runtime.ReadMemStats(&ms2)
  76. if ms1.HeapReleased >= ms2.HeapReleased {
  77. t.Errorf("released before=%d; released after=%d; did not go up", ms1.HeapReleased, ms2.HeapReleased)
  78. }
  79. }
  80. func TestSetGCPercent(t *testing.T) {
  81. // Test that the variable is being set and returned correctly.
  82. // Assume the percentage itself is implemented fine during GC,
  83. // which is harder to test.
  84. old := SetGCPercent(123)
  85. new := SetGCPercent(old)
  86. if new != 123 {
  87. t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new)
  88. }
  89. }