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