syscall_linux.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. var logger = grpclog.Component("core")
  31. // GetCPUTime returns the how much CPU time has passed since the start of this process.
  32. func GetCPUTime() int64 {
  33. var ts unix.Timespec
  34. if err := unix.ClockGettime(unix.CLOCK_PROCESS_CPUTIME_ID, &ts); err != nil {
  35. logger.Fatal(err)
  36. }
  37. return ts.Nano()
  38. }
  39. // Rusage is an alias for syscall.Rusage under linux non-appengine environment.
  40. type Rusage syscall.Rusage
  41. // GetRusage returns the resource usage of current process.
  42. func GetRusage() (rusage *Rusage) {
  43. rusage = new(Rusage)
  44. syscall.Getrusage(syscall.RUSAGE_SELF, (*syscall.Rusage)(rusage))
  45. return
  46. }
  47. // CPUTimeDiff returns the differences of user CPU time and system CPU time used
  48. // between two Rusage structs.
  49. func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {
  50. f := (*syscall.Rusage)(first)
  51. l := (*syscall.Rusage)(latest)
  52. var (
  53. utimeDiffs = l.Utime.Sec - f.Utime.Sec
  54. utimeDiffus = l.Utime.Usec - f.Utime.Usec
  55. stimeDiffs = l.Stime.Sec - f.Stime.Sec
  56. stimeDiffus = l.Stime.Usec - f.Stime.Usec
  57. )
  58. uTimeElapsed := float64(utimeDiffs) + float64(utimeDiffus)*1.0e-6
  59. sTimeElapsed := float64(stimeDiffs) + float64(stimeDiffus)*1.0e-6
  60. return uTimeElapsed, sTimeElapsed
  61. }
  62. // SetTCPUserTimeout sets the TCP user timeout on a connection's socket
  63. func SetTCPUserTimeout(conn net.Conn, timeout time.Duration) error {
  64. tcpconn, ok := conn.(*net.TCPConn)
  65. if !ok {
  66. // not a TCP connection. exit early
  67. return nil
  68. }
  69. rawConn, err := tcpconn.SyscallConn()
  70. if err != nil {
  71. return fmt.Errorf("error getting raw connection: %v", err)
  72. }
  73. err = rawConn.Control(func(fd uintptr) {
  74. err = syscall.SetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(timeout/time.Millisecond))
  75. })
  76. if err != nil {
  77. return fmt.Errorf("error setting option on socket: %v", err)
  78. }
  79. return nil
  80. }
  81. // GetTCPUserTimeout gets the TCP user timeout on a connection's socket
  82. func GetTCPUserTimeout(conn net.Conn) (opt int, err error) {
  83. tcpconn, ok := conn.(*net.TCPConn)
  84. if !ok {
  85. err = fmt.Errorf("conn is not *net.TCPConn. got %T", conn)
  86. return
  87. }
  88. rawConn, err := tcpconn.SyscallConn()
  89. if err != nil {
  90. err = fmt.Errorf("error getting raw connection: %v", err)
  91. return
  92. }
  93. err = rawConn.Control(func(fd uintptr) {
  94. opt, err = syscall.GetsockoptInt(int(fd), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT)
  95. })
  96. if err != nil {
  97. err = fmt.Errorf("error getting option on socket: %v", err)
  98. return
  99. }
  100. return
  101. }