Skip to content

Commit

Permalink
util: add affinity-cpus starting argument (#10773)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored and tiancaiamao committed Jul 1, 2019
1 parent 1ddb317 commit 5409406
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ require (
go.uber.org/atomic v1.3.2
go.uber.org/zap v1.9.1
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb
golang.org/x/text v0.3.0
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
golang.org/x/tools v0.0.0-20190130214255-bb1329dc71a0
Expand Down
32 changes: 29 additions & 3 deletions tidb-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"os"
"runtime"
"strconv"
"strings"
"sync/atomic"
"time"

Expand Down Expand Up @@ -53,6 +54,7 @@ import (
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/printer"
"github.com/pingcap/tidb/util/signal"
"github.com/pingcap/tidb/util/sys/linux"
"github.com/pingcap/tidb/util/systimemon"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
Expand Down Expand Up @@ -90,6 +92,7 @@ const (

nmProxyProtocolNetworks = "proxy-protocol-networks"
nmProxyProtocolHeaderTimeout = "proxy-protocol-header-timeout"
nmAffinityCPU = "affinity-cpus"
)

var (
Expand All @@ -112,6 +115,7 @@ var (
tokenLimit = flag.Int(nmTokenLimit, 1000, "the limit of concurrent executed sessions")
pluginDir = flag.String(nmPluginDir, "/data/deploy/plugin", "the folder that hold plugin")
pluginLoad = flag.String(nmPluginLoad, "", "wait load plugin name(separated by comma)")
affinityCPU = flag.String(nmAffinityCPU, "", "affinity cpu (cpu-no. separated by comma, e.g. 1,2,3)")

// Log
logLevel = flag.String(nmLogLevel, "info", "log level: info, debug, warn, error, fatal")
Expand Down Expand Up @@ -157,6 +161,7 @@ func main() {
os.Exit(0)
}
setGlobalVars()
setCPUAffinity()
setupLog()
// If configStrict had been specified, and there had been an error, the server would already
// have exited by now. If configWarning is not an empty string, write it to the log now that
Expand Down Expand Up @@ -184,6 +189,30 @@ func exit() {
os.Exit(0)
}

func setCPUAffinity() {
if affinityCPU == nil || len(*affinityCPU) == 0 {
return
}
var cpu []int
for _, af := range strings.Split(*affinityCPU, ",") {
af = strings.TrimSpace(af)
if len(af) > 0 {
c, err := strconv.Atoi(af)
if err != nil {
fmt.Fprintf(os.Stderr, "wrong affinity cpu config: %s", *affinityCPU)
exit()
}
cpu = append(cpu, c)
}
}
err := linux.SetAffinity(cpu)
if err != nil {
fmt.Fprintf(os.Stderr, "set cpu affinity failure: %v", err)
exit()
}
runtime.GOMAXPROCS(len(cpu))
}

func registerStores() {
err := kvstore.Register("tikv", tikv.Driver{})
terror.MustNil(err)
Expand Down Expand Up @@ -331,9 +360,6 @@ func reloadConfig(nc, c *config.Config) {
// like config.GetGlobalConfig().OOMAction.
// These config items will become available naturally after the global config pointer
// is updated in function ReloadGlobalConfig.
if nc.Performance.MaxProcs != c.Performance.MaxProcs {
runtime.GOMAXPROCS(int(nc.Performance.MaxProcs))
}
if nc.Performance.MaxMemory != c.Performance.MaxMemory {
plannercore.PreparedPlanCacheMaxMemory.Store(nc.Performance.MaxMemory)
}
Expand Down
15 changes: 14 additions & 1 deletion util/sys/linux/sys_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

package linux

import "syscall"
import (
"golang.org/x/sys/unix"
"syscall"
)

// OSVersion returns version info of operation system.
// e.g. Linux 4.15.0-45-generic.x86_64
Expand All @@ -38,3 +41,13 @@ func OSVersion() (osVersion string, err error) {
osVersion = charsToString(un.Sysname[:]) + " " + charsToString(un.Release[:]) + "." + charsToString(un.Machine[:])
return
}

// SetAffinity sets cpu affinity.
func SetAffinity(cpus []int) error {
var cpuSet unix.CPUSet
cpuSet.Zero()
for _, c := range cpus {
cpuSet.Set(c)
}
return unix.SchedSetaffinity(unix.Getpid(), &cpuSet)
}
5 changes: 5 additions & 0 deletions util/sys/linux/sys_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ func OSVersion() (osVersion string, err error) {
osVersion = runtime.GOOS + "." + runtime.GOARCH
return
}

// SetAffinity sets cpu affinity.
func SetAffinity(cpus []int) error {
return nil
}

0 comments on commit 5409406

Please sign in to comment.