fdtrack.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Package fdtrack logs statistics about open file descriptors.
  17. package fdtrack
  18. import (
  19. "fmt"
  20. "net"
  21. "sort"
  22. "sync"
  23. "time"
  24. "github.com/ethereum/go-ethereum/logger/glog"
  25. )
  26. var (
  27. mutex sync.Mutex
  28. all = make(map[string]int)
  29. )
  30. func Open(desc string) {
  31. mutex.Lock()
  32. all[desc] += 1
  33. mutex.Unlock()
  34. }
  35. func Close(desc string) {
  36. mutex.Lock()
  37. defer mutex.Unlock()
  38. if c, ok := all[desc]; ok {
  39. if c == 1 {
  40. delete(all, desc)
  41. } else {
  42. all[desc]--
  43. }
  44. }
  45. }
  46. func WrapListener(desc string, l net.Listener) net.Listener {
  47. Open(desc)
  48. return &wrappedListener{l, desc}
  49. }
  50. type wrappedListener struct {
  51. net.Listener
  52. desc string
  53. }
  54. func (w *wrappedListener) Accept() (net.Conn, error) {
  55. c, err := w.Listener.Accept()
  56. if err == nil {
  57. c = WrapConn(w.desc, c)
  58. }
  59. return c, err
  60. }
  61. func (w *wrappedListener) Close() error {
  62. err := w.Listener.Close()
  63. if err == nil {
  64. Close(w.desc)
  65. }
  66. return err
  67. }
  68. func WrapConn(desc string, conn net.Conn) net.Conn {
  69. Open(desc)
  70. return &wrappedConn{conn, desc}
  71. }
  72. type wrappedConn struct {
  73. net.Conn
  74. desc string
  75. }
  76. func (w *wrappedConn) Close() error {
  77. err := w.Conn.Close()
  78. if err == nil {
  79. Close(w.desc)
  80. }
  81. return err
  82. }
  83. func Start() {
  84. go func() {
  85. for range time.Tick(15 * time.Second) {
  86. mutex.Lock()
  87. var sum, tracked = 0, []string{}
  88. for what, n := range all {
  89. sum += n
  90. tracked = append(tracked, fmt.Sprintf("%s:%d", what, n))
  91. }
  92. mutex.Unlock()
  93. used, _ := fdusage()
  94. sort.Strings(tracked)
  95. glog.Infof("fd usage %d/%d, tracked %d %v", used, fdlimit(), sum, tracked)
  96. }
  97. }()
  98. }