cubefs

Форк
0
114 строк · 3.2 Кб
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
var logger = grpclog.Component("core")
36

37
// GetCPUTime returns the how much CPU time has passed since the start of this process.
38
func GetCPUTime() int64 {
39
	var ts unix.Timespec
40
	if err := unix.ClockGettime(unix.CLOCK_PROCESS_CPUTIME_ID, &ts); err != nil {
41
		logger.Fatal(err)
42
	}
43
	return ts.Nano()
44
}
45

46
// Rusage is an alias for syscall.Rusage under linux environment.
47
type Rusage = syscall.Rusage
48

49
// GetRusage returns the resource usage of current process.
50
func GetRusage() *Rusage {
51
	rusage := new(Rusage)
52
	syscall.Getrusage(syscall.RUSAGE_SELF, rusage)
53
	return rusage
54
}
55

56
// CPUTimeDiff returns the differences of user CPU time and system CPU time used
57
// between two Rusage structs.
58
func CPUTimeDiff(first *Rusage, latest *Rusage) (float64, float64) {
59
	var (
60
		utimeDiffs  = latest.Utime.Sec - first.Utime.Sec
61
		utimeDiffus = latest.Utime.Usec - first.Utime.Usec
62
		stimeDiffs  = latest.Stime.Sec - first.Stime.Sec
63
		stimeDiffus = latest.Stime.Usec - first.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
}
115

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.