github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/sys/linux/sys_linux.go (about)

     1  // Copyright 2020 WHTCORPS INC, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // +build linux
    14  
    15  package linux
    16  
    17  import (
    18  	"syscall"
    19  
    20  	"golang.org/x/sys/unix"
    21  )
    22  
    23  // OSVersion returns version info of operation system.
    24  // e.g. Linux 4.15.0-45-generic.x86_64
    25  func OSVersion() (osVersion string, err error) {
    26  	var un syscall.Utsname
    27  	err = syscall.Uname(&un)
    28  	if err != nil {
    29  		return
    30  	}
    31  	charsToString := func(ca []int8) string {
    32  		s := make([]byte, len(ca))
    33  		var lens int
    34  		for ; lens < len(ca); lens++ {
    35  			if ca[lens] == 0 {
    36  				break
    37  			}
    38  			s[lens] = uint8(ca[lens])
    39  		}
    40  		return string(s[0:lens])
    41  	}
    42  	osVersion = charsToString(un.Sysname[:]) + " " + charsToString(un.Release[:]) + "." + charsToString(un.Machine[:])
    43  	return
    44  }
    45  
    46  // SetAffinity sets cpu affinity.
    47  func SetAffinity(cpus []int) error {
    48  	var cpuSet unix.CPUSet
    49  	cpuSet.Zero()
    50  	for _, c := range cpus {
    51  		cpuSet.Set(c)
    52  	}
    53  	return unix.SchedSetaffinity(unix.Getpid(), &cpuSet)
    54  }