Skip to content

Commit

Permalink
Merge pull request #99 from inada-s/cloud-profiler
Browse files Browse the repository at this point in the history
install cloud profiler
  • Loading branch information
inada-s authored Feb 5, 2021
2 parents 7546b3b + 93d89b7 commit be4f870
Show file tree
Hide file tree
Showing 8 changed files with 457 additions and 34 deletions.
51 changes: 48 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,59 @@ Using only the `lbs` command to act as a standalone lobby and match server. (Thi


### Configulations
All settings are specified from environment variables.

#### Environment variables
- `GDXSV_LOBBY_PUBLIC_ADDR` : Specifies the TCP address that used when a mcs connects to a lbs.
- `GDXSV_LOBBY_ADDR` : Specifies the TCP address that the lbs listens on. Currently only the port number is used.
- `GDXSV_BATTLE_PUBLIC_ADDR` : Specifies the TCP/UDP address that a client will use to connect with TCP/UDP.
- `GDXSV_BATTLE_ADDR` : Specifies the TCP/UDP address that the mcs listens on. Currently only the port number is used.
- `GDXSV_BATTLE_LOG_PATH` : Specifies a file path that will be used to save battle log file.
- `GDXSV_MCSFUNC_KEY` : Specifies a GCP key that has permission to call mcsfunc.
- `GDXSV_MCSFUNC_URL` : Specifies an URL of mcsfunc that you deployed.
- `GDXSV_GCP_PROJECT_ID` : Specifies the project id of Google Cloud Platform. Required if you use mcsfunc or CloudProfiler.
- `GDXSV_GCP_KEY_PATH` : Specifies a GCP Service Account keyfile that have permission for following roles.
- `roles/cloudfunctions.invoker`
- `roles/cloudprofiler.agent`
- `GDXSV_MCSFUNC_URL` : Specifies a URL of mcsfunc that you deployed.

#### Commandline arguments
```
Usage: gdxsv <Flags...> [lbs, mcs, initdb, migratedb]
lbs: Serve lobby server and default battle server.
A lbs hosts PS2, DC1 and DC2 version, but their lobbies are separated internally.
mcs: Serve battle server.
The mcs attempts to register itself with a lbs.
When the mcs is vacant for a certain period, it will automatically end.
It is supposed to host mcs in a different location than the lobby server.
initdb: Initialize database.
It is supposed to run this command before you run lbs first time.
Note that if the database file already exists it will be permanently deleted.
migratedb: Update database schema.
It is supposed to run this command before you run updated gdxsv.
battlelog2json: Convert battle log file to json.
Flags:
-cprof int
0: disable cloud profiler, 1: enable cloud profiler, 2: also enable mtx profile
-cpu int
setting GOMAXPROCS (default 2)
-dump
enable var dump to dump.txt
-mcsdelay duration
mcs room delay for network lag emulation
-noban
not to check bad users
-pprof int
0: disable pprof, 1: enable http pprof, 2: enable blocking profile (default 1)
-prodlog
use production logging mode
-v int
logging level. 1:error, 2:info, 3:debug (default 2)
```

## Directory structures

Expand Down
8 changes: 4 additions & 4 deletions build_run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ set -eux

cd $(dirname "$0")

#make race
make

export GDXSV_LOBBY_PUBLIC_ADDR="192.168.1.10:9876"
export GDXSV_LOBBY_ADDR="localhost:9876"
export GDXSV_BATTLE_PUBLIC_ADDR="192.168.1.10:9877"
export GDXSV_BATTLE_ADDR="localhost:9877"
export GDXSV_MCSFUNC_KEY="${HOME}/keys/gdxsv-service-key.json"
export GDXSV_MCSFUNC_URL="https://asia-northeast1-gdxsv-274515.cloudfunctions.net/mcsfunc"

#./bin/gdxsv -v 3 lbs -profile 1 2>&1 | tee log.txt
export GDXSV_GCP_PROJECT_ID=""
export GDXSV_GCP_KEY_PATH=""
export GDXSV_MCSFUNC_URL=""

./bin/gdxsv -v 3 -noban lbs 2>&1 | tee log.txt

4 changes: 2 additions & 2 deletions gdxsv/lbs_mcsfunc.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func getMcsFuncClient() (*http.Client, error) {
return mcsFuncClientCache, nil
}

jsonKey, err := ioutil.ReadFile(conf.McsFuncKey)
jsonKey, err := ioutil.ReadFile(conf.GCPKeyPath)
if err != nil {
return nil, err
}
Expand All @@ -68,7 +68,7 @@ func getMcsFuncClient() (*http.Client, error) {
}

func McsFuncEnabled() bool {
return conf.McsFuncKey != "" && conf.McsFuncURL != ""
return conf.GCPKeyPath != "" && conf.McsFuncURL != ""
}

func McsFuncAlloc(region string) error {
Expand Down
53 changes: 43 additions & 10 deletions gdxsv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"gdxsv/gdxsv/proto"
pb "github.com/golang/protobuf/proto"
"google.golang.org/api/option"
"io/ioutil"
"log"
"math/rand"
Expand All @@ -16,6 +17,7 @@ import (
"syscall"
"time"

"cloud.google.com/go/profiler"
"github.com/caarlos0/env"
"github.com/davecgh/go-spew/spew"
"github.com/jmoiron/sqlx"
Expand All @@ -38,7 +40,8 @@ var (

dump = flag.Bool("dump", false, "enable var dump to dump.txt")
cpu = flag.Int("cpu", 2, "setting GOMAXPROCS")
profile = flag.Int("profile", 1, "0: no profile, 1: enable http pprof, 2: enable blocking profile")
pprof = flag.Int("pprof", 1, "0: disable pprof, 1: enable http pprof, 2: enable blocking profile")
cprof = flag.Int("cprof", 0, "0: disable cloud profiler, 1: enable cloud profiler, 2: also enable mtx profile")
prodlog = flag.Bool("prodlog", false, "use production logging mode")
loglevel = flag.Int("v", 2, "logging level. 1:error, 2:info, 3:debug")
mcsdelay = flag.Duration("mcsdelay", 0, "mcs room delay for network lag emulation")
Expand All @@ -58,9 +61,13 @@ type Config struct {
BattlePublicAddr string `env:"GDXSV_BATTLE_PUBLIC_ADDR" envDefault:"127.0.0.1:3334"`
BattleRegion string `env:"GDXSV_BATTLE_REGION" envDefault:""`
BattleLogPath string `env:"GDXSV_BATTLE_LOG_PATH" envDefault:"./battlelog"`
McsFuncKey string `env:"GDXSV_MCSFUNC_KEY" envDefault:""`
McsFuncURL string `env:"GDXSV_MCSFUNC_URL" envDefault:""`
DBName string `env:"GDXSV_DB_NAME" envDefault:"gdxsv.db"`

GCPProjectID string `env:"GDXSV_GCP_PROJECT_ID" envDefault:""`
GCPKeyPath string `env:"GDXSV_GCP_KEY_PATH" envDefault:""`
McsFuncKey string `env:"GDXSV_MCSFUNC_KEY" envDefault:""` // deprecated
McsFuncURL string `env:"GDXSV_MCSFUNC_URL" envDefault:""`

DBName string `env:"GDXSV_DB_NAME" envDefault:"gdxsv.db"`
}

func printHeader() {
Expand All @@ -72,7 +79,7 @@ func printHeader() {

func printUsage() {
fmt.Print(`
Usage: gdxsv [lbs, mcs, initdb, migratedb]
Usage: gdxsv <Flags...> [lbs, mcs, initdb, migratedb]
lbs: Serve lobby server and default battle server.
A lbs hosts PS2, DC1 and DC2 version, but their lobbies are separated internally.
Expand All @@ -90,7 +97,11 @@ Usage: gdxsv [lbs, mcs, initdb, migratedb]
It is supposed to run this command before you run updated gdxsv.
battlelog2json: Convert battle log file to json.
Flags:
`)
flag.PrintDefaults()
}

func loadConfig() {
Expand All @@ -99,6 +110,10 @@ func loadConfig() {
logger.Fatal("config load failed", zap.Error(err))
}

if c.GCPKeyPath == "" && c.McsFuncKey != "" {
c.GCPKeyPath = c.McsFuncKey
}

logger.Info("config loaded", zap.Any("config", c))
conf = c
}
Expand All @@ -116,18 +131,36 @@ func pprofPort(mode string) int {

func prepareOption(command string) {
runtime.GOMAXPROCS(*cpu)
if *profile >= 1 {

// http pprof
if 1 <= *pprof {
if 2 <= *pprof {
runtime.MemProfileRate = 1
runtime.SetBlockProfileRate(1)
logger.Warn("mem profile mode enabled")
}
go func() {
port := pprofPort(command)
addr := fmt.Sprintf(":%v", port)
err := http.ListenAndServe(addr, nil)
logger.Error("http.ListenAndServe error", zap.Error(err), zap.String("addr", addr))
}()
}
if *profile >= 2 {
runtime.MemProfileRate = 1
runtime.SetBlockProfileRate(1)
logger.Warn("mem profile mode enabled")

// google cloud profiler
if 1 <= *cprof {
cfg := profiler.Config{
Service: fmt.Sprintf("gdxsv-%s", command),
ServiceVersion: gdxsvVersion,
ProjectID: conf.GCPProjectID,
}
if 2 <= *cprof {
cfg.MutexProfiling = true
}
if err := profiler.Start(cfg, option.WithCredentialsFile(conf.GCPKeyPath)); err != nil {
logger.Error("failed to start cloud profiler", zap.Error(err), zap.Any("cfg", cfg))
}
logger.Info("profiler started")
}
}

Expand Down
20 changes: 10 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ module gdxsv
go 1.14

require (
cloud.google.com/go v0.38.0 // indirect
cloud.google.com/go v0.76.0
github.com/caarlos0/env v3.5.0+incompatible
github.com/davecgh/go-spew v1.1.1
github.com/golang/protobuf v1.4.2
github.com/golang/protobuf v1.4.3
github.com/jmoiron/sqlx v1.2.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.5.1 // indirect
github.com/tommy351/zap-stackdriver v0.1.3
go.opencensus.io v0.22.6 // indirect
go.uber.org/zap v1.15.0
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e
golang.org/x/net v0.0.0-20190923162816-aa69164e4478
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/text v0.3.2
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.25.0
golang.org/x/mod v0.4.1
golang.org/x/net v0.0.0-20210119194325-5f4716e94777
golang.org/x/oauth2 v0.0.0-20210201163806-010130855d6c
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a
golang.org/x/text v0.3.5
google.golang.org/api v0.39.0
google.golang.org/genproto v0.0.0-20210204154452-deb828366460 // indirect
)
Loading

0 comments on commit be4f870

Please sign in to comment.