Skip to content

Commit

Permalink
Remove -cpu flag (coredns#2793)
Browse files Browse the repository at this point in the history
The -cpu flag is a weird one (and copied originally from Caddy), it
basically sets GOMAXPROCS which can be *easily* done by just setting
that environment variable. Also with systemd and containerized env you
set this externally *anyway*, so there is little use to do this again in
the binary.

Also the option's help was confusing (i.e. percentage of what?). Remove
the option and supporting files.

Signed-off-by: Miek Gieben <miek@miek.nl>
  • Loading branch information
miekg authored Jul 3, 2019
1 parent e0c373e commit f5fe983
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 91 deletions.
3 changes: 0 additions & 3 deletions coredns.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ Available options:
: specify Corefile to load, if not given CoreDNS will look for a `Corefile` in the current
directory.

**-cpu** **CAP**
: specify maximum CPU capacity in percent.

**-dns.port** **PORT**
: override default port (53) to listen on.

Expand Down
44 changes: 0 additions & 44 deletions coremain/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
package coremain

import (
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"runtime"
"strconv"
"strings"

"github.com/coredns/coredns/core/dnsserver"
Expand All @@ -24,7 +22,6 @@ func init() {
setVersion()

flag.StringVar(&conf, "conf", "", "Corefile to load (default \""+caddy.DefaultConfigFile+"\")")
flag.StringVar(&cpu, "cpu", "100%", "CPU cap")
flag.BoolVar(&plugins, "plugins", false, "List installed plugins")
flag.StringVar(&caddy.PidFile, "pidfile", "", "Path to write pid file")
flag.BoolVar(&version, "version", false, "Show version")
Expand Down Expand Up @@ -73,11 +70,6 @@ func Run() {
os.Exit(0)
}

// Set CPU cap
if err := setCPU(cpu); err != nil {
mustLogFatal(err)
}

// Get Corefile input
corefile, err := caddy.LoadCaddyfile(serverType)
if err != nil {
Expand Down Expand Up @@ -194,45 +186,9 @@ func setVersion() {
}
}

// setCPU parses string cpu and sets GOMAXPROCS
// according to its value. It accepts either
// a number (e.g. 3) or a percent (e.g. 50%).
func setCPU(cpu string) error {
var numCPU int

availCPU := runtime.NumCPU()

if strings.HasSuffix(cpu, "%") {
// Percent
var percent float32
pctStr := cpu[:len(cpu)-1]
pctInt, err := strconv.Atoi(pctStr)
if err != nil || pctInt < 1 || pctInt > 100 {
return errors.New("invalid CPU value: percentage must be between 1-100")
}
percent = float32(pctInt) / 100
numCPU = int(float32(availCPU) * percent)
} else {
// Number
num, err := strconv.Atoi(cpu)
if err != nil || num < 1 {
return errors.New("invalid CPU value: provide a number or percent greater than 0")
}
numCPU = num
}

if numCPU > availCPU {
numCPU = availCPU
}

runtime.GOMAXPROCS(numCPU)
return nil
}

// Flags that control program flow or startup
var (
conf string
cpu string
logfile bool
version bool
plugins bool
Expand Down
44 changes: 0 additions & 44 deletions coremain/run_test.go

This file was deleted.

0 comments on commit f5fe983

Please sign in to comment.