/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Package syscall provides functionalities that grpc uses to get low-level operating system // stats/info.
package syscall import ( ) var logger = grpclog.Component("core") // GetCPUTime returns the how much CPU time has passed since the start of this process. func () int64 { var unix.Timespec if := unix.ClockGettime(unix.CLOCK_PROCESS_CPUTIME_ID, &); != nil { logger.Fatal() } return .Nano() } // Rusage is an alias for syscall.Rusage under linux environment. type Rusage = syscall.Rusage // GetRusage returns the resource usage of current process. func () *Rusage { := new(Rusage) syscall.Getrusage(syscall.RUSAGE_SELF, ) return } // CPUTimeDiff returns the differences of user CPU time and system CPU time used // between two Rusage structs. func ( *Rusage, *Rusage) (float64, float64) { var ( = .Utime.Sec - .Utime.Sec = .Utime.Usec - .Utime.Usec = .Stime.Sec - .Stime.Sec = .Stime.Usec - .Stime.Usec ) := float64() + float64()*1.0e-6 := float64() + float64()*1.0e-6 return , } // SetTCPUserTimeout sets the TCP user timeout on a connection's socket func ( net.Conn, time.Duration) error { , := .(*net.TCPConn) if ! { // not a TCP connection. exit early return nil } , := .SyscallConn() if != nil { return fmt.Errorf("error getting raw connection: %v", ) } = .Control(func( uintptr) { = syscall.SetsockoptInt(int(), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT, int(/time.Millisecond)) }) if != nil { return fmt.Errorf("error setting option on socket: %v", ) } return nil } // GetTCPUserTimeout gets the TCP user timeout on a connection's socket func ( net.Conn) ( int, error) { , := .(*net.TCPConn) if ! { = fmt.Errorf("conn is not *net.TCPConn. got %T", ) return } , := .SyscallConn() if != nil { = fmt.Errorf("error getting raw connection: %v", ) return } = .Control(func( uintptr) { , = syscall.GetsockoptInt(int(), syscall.IPPROTO_TCP, unix.TCP_USER_TIMEOUT) }) if != nil { = fmt.Errorf("error getting option on socket: %v", ) return } return }