syscall_linux.go 3.1 KB

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