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.
 
 
 

115 lines
3.2 KiB

  1. // +build !appengine
  2. /*
  3. *
  4. * Copyright 2018 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. // Package syscall provides functionalities that grpc uses to get low-level operating system
  20. // stats/info.
  21. package syscall
  22. import (
  23. "fmt"
  24. "net"
  25. "syscall"
  26. "time"
  27. "golang.org/x/sys/unix"
  28. "google.golang.org/grpc/grpclog"
  29. )
  30. // GetCPUTime returns the how much CPU time has passed since the start of this process.
  31. func GetCPUTime() int64 {
  32. var ts unix.Timespec
  33. if err := unix.ClockGettime(unix.CLOCK_PROCESS_CPUTIME_ID, &ts); err != nil {
  34. grpclog.Fatal(err)
  35. }
  36. return ts.Nano()
  37. }
  38. // Rusage is an alias for syscall.Rusage under linux non-appengine environment.
  39. type Rusage syscall.Rusage
  40. // GetRusage returns the resource usage of current process.
  41. func GetRusage() (rusage *Rusage) {
  42. rusage = new(Rusage)
  43. syscall.Getrusage(syscall.RUSAGE_SELF, (*syscall.Rusage)(rusage))
  44. return
  45. }
  46. // CPUTimeDiff returns the differences of user CPU time and system CPU time used
  47. // between two Rusage structs.
  48. func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {
  49. f := (*syscall.Rusage)(first)
  50. l := (*syscall.Rusage)(latest)
  51. var (
  52. utimeDiffs = l.Utime.Sec - f.Utime.Sec
  53. utimeDiffus = l.Utime.Usec - f.Utime.Usec
  54. stimeDiffs = l.Stime.Sec - f.Stime.Sec
  55. stimeDiffus = l.Stime.Usec - f.Stime.Usec
  56. )
  57. uTimeElapsed := float64(utimeDiffs) + float64(utimeDiffus)*1.0e-6
  58. sTimeElapsed := float64(stimeDiffs) + float64(stimeDiffus)*1.0e-6
  59. return uTimeElapsed, sTimeElapsed
  60. }
  61. // SetTCPUserTimeout sets the TCP user timeout on a connection's socket
  62. func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
  63. tcpconn, ok := conn.(*net.TCPConn)
  64. if !ok {
  65. // not a TCP connection. exit early
  66. return nil
  67. }
  68. rawConn, err := tcpconn.SyscallConn()
  69. if err != nil {
  70. return fmt.Errorf("error getting raw connection: %v", err)
  71. }
  72. err = rawConn.Control(func(fd uintptr) {
  73. err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
  74. })
  75. if err != nil {
  76. return fmt.Errorf("error setting option on socket: %v", err)
  77. }
  78. return nil
  79. }
  80. // GetTCPUserTimeout gets the TCP user timeout on a connection's socket
  81. func GetTCPUserTimeout(conn net.Conn) (opt int, err error) {
  82. tcpconn, ok := conn.(*net.TCPConn)
  83. if !ok {
  84. err = fmt.Errorf("conn is not *net.TCPConn. got %T", conn)
  85. return
  86. }
  87. rawConn, err := tcpconn.SyscallConn()
  88. if err != nil {
  89. err = fmt.Errorf("error getting raw connection: %v", err)
  90. return
  91. }
  92. err = rawConn.Control(func(fd uintptr) {
  93. opt, err = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT)
  94. })
  95. if err != nil {
  96. err = fmt.Errorf("error getting option on socket: %v", err)
  97. return
  98. }
  99. return
  100. }