Skip to content

Commit

Permalink
user: custom mapSize flag. improve memory usage #433 .
Browse files Browse the repository at this point in the history
used `--map-size` to set mapSize perCPU in cli. default:10240KB,

Signed-off-by: cfc4n <[email protected]>
  • Loading branch information
cfc4n committed Dec 2, 2023
1 parent c7bdd3e commit abda47c
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 27 deletions.
1 change: 1 addition & 0 deletions cli/cmd/bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func bashCommandFunc(command *cobra.Command, args []string) {
bc.Uid = gConf.Uid
bc.Debug = gConf.Debug
bc.IsHex = gConf.IsHex
bc.SetPerCpuMapSize(gConf.mapSizeKB)

logger.Printf("ECAPTURE :: pid info :%d", os.Getpid())
//bc.Pid = globalFlags.Pid
Expand Down
6 changes: 6 additions & 0 deletions cli/cmd/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type GlobalFlags struct {
Uid uint64 // UID
NoSearch bool // No lib search
LoggerAddr string // save file
mapSizeKB int // ebpf map size per CPU
addrType uint8 // 0:stdout, 1:file, 2:tcp
address string
writer io.Writer
Expand Down Expand Up @@ -68,6 +69,11 @@ func getGlobalConf(command *cobra.Command) (conf GlobalFlags, err error) {
return
}

conf.mapSizeKB, err = command.Flags().GetInt("map-size")
if err != nil {
return
}

conf.LoggerAddr, err = command.Flags().GetString("log-addr")
if err != nil {
return
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/gnutls.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func gnuTlsCommandFunc(command *cobra.Command, args []string) {
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)
conf.SetPerCpuMapSize(gConf.mapSizeKB)

err = conf.Check()

Expand Down
1 change: 1 addition & 0 deletions cli/cmd/gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func goTLSCommandFunc(command *cobra.Command, args []string) {
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)
conf.SetPerCpuMapSize(gConf.mapSizeKB)
//conf.SetNoSearch(gConf.NoSearch)

err = conf.Check()
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/mysqld.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func mysqldCommandFunc(command *cobra.Command, args []string) {
mysqldConfig.Pid = gConf.Pid
mysqldConfig.Debug = gConf.Debug
mysqldConfig.IsHex = gConf.IsHex
mysqldConfig.SetPerCpuMapSize(gConf.mapSizeKB)

log.Printf("ECAPTURE :: pid info :%d", os.Getpid())
//bc.Pid = globalFlags.Pid
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/nss.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ func nssCommandFunc(command *cobra.Command, args []string) {
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)
conf.SetPerCpuMapSize(gConf.mapSizeKB)

err = conf.Check()

Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func postgresCommandFunc(command *cobra.Command, args []string) {
postgresConfig.Pid = gConf.Pid
postgresConfig.Debug = gConf.Debug
postgresConfig.IsHex = gConf.IsHex

postgresConfig.SetPerCpuMapSize(gConf.mapSizeKB)
log.Printf("ECAPTURE :: pid info: %d", os.Getpid())
//bc.Pid = globalFlags.Pid
if e := postgresConfig.Check(); e != nil {
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func init() {
rootCmd.PersistentFlags().BoolVarP(&globalFlags.Debug, "debug", "d", false, "enable debug logging")
rootCmd.PersistentFlags().BoolVar(&globalFlags.IsHex, "hex", false, "print byte strings as hex encoded strings")
rootCmd.PersistentFlags().BoolVar(&globalFlags.NoSearch, "nosearch", false, "no lib search")
rootCmd.PersistentFlags().IntVar(&globalFlags.mapSizeKB, "map-size", 10240, "eBPF map size per CPU,for events buffer. default:10240. (KB)")
rootCmd.PersistentFlags().Uint64VarP(&globalFlags.Pid, "pid", "p", defaultPid, "if pid is 0 then we target all pids")
rootCmd.PersistentFlags().Uint64VarP(&globalFlags.Uid, "uid", "u", defaultUid, "if uid is 0 then we target all users")
rootCmd.PersistentFlags().StringVarP(&globalFlags.LoggerAddr, "log-addr", "l", "", "-l /tmp/ecapture.log or -l tcp://127.0.0.1:8080")
Expand Down
2 changes: 1 addition & 1 deletion cli/cmd/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func openSSLCommandFunc(command *cobra.Command, args []string) {
conf.SetUid(gConf.Uid)
conf.SetDebug(gConf.Debug)
conf.SetHex(gConf.IsHex)

conf.SetPerCpuMapSize(gConf.mapSizeKB)
err = conf.Check()

if err != nil {
Expand Down
1 change: 1 addition & 0 deletions user/config/common_androidgki.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
"/apex/com.android.conscrypt/lib64",
"/apex/com.android.runtime/lib64/bionic",
}
DefaultMapSizePerCpu = os.Getpagesize() * 512
)

const ElfArchIsandroid = true
Expand Down
8 changes: 7 additions & 1 deletion user/config/common_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package config

import "log"
import (
"log"
"os"
)

const (
LdLoadPath = "/etc/ld.so.conf"
Expand All @@ -40,6 +43,9 @@ var (
"/usr/lib64",
"/lib64",
}

// default: 4MB
DefaultMapSizePerCpu = os.Getpagesize() * 1024
)

func GetDynLibDirs() []string {
Expand Down
1 change: 1 addition & 0 deletions user/config/config_bash.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type BashConfig struct {

func NewBashConfig() *BashConfig {
config := &BashConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}

Expand Down
1 change: 1 addition & 0 deletions user/config/config_gnutls.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ type GnutlsConfig struct {

func NewGnutlsConfig() *GnutlsConfig {
config := &GnutlsConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}
4 changes: 3 additions & 1 deletion user/config/config_gotls.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ type GoTLSConfig struct {

// NewGoTLSConfig creates a new config for Go SSL
func NewGoTLSConfig() *GoTLSConfig {
return &GoTLSConfig{}
gc := &GoTLSConfig{}
gc.PerCpuMapSize = DefaultMapSizePerCpu
return gc
}

func (gc *GoTLSConfig) Check() error {
Expand Down
1 change: 1 addition & 0 deletions user/config/config_mysqld.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type MysqldConfig struct {

func NewMysqldConfig() *MysqldConfig {
config := &MysqldConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}

Expand Down
1 change: 1 addition & 0 deletions user/config/config_nspr.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ type NsprConfig struct {

func NewNsprConfig() *NsprConfig {
config := &NsprConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}
1 change: 1 addition & 0 deletions user/config/config_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type OpensslConfig struct {

func NewOpensslConfig() *OpensslConfig {
config := &OpensslConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}

Expand Down
1 change: 1 addition & 0 deletions user/config/config_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type PostgresConfig struct {

func NewPostgresConfig() *PostgresConfig {
config := &PostgresConfig{}
config.PerCpuMapSize = DefaultMapSizePerCpu
return config
}

Expand Down
23 changes: 18 additions & 5 deletions user/config/iconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package config

import "ecapture/pkg/util/kernel"
import (
"ecapture/pkg/util/kernel"
)

type IConfig interface {
Check() error //检测配置合法性
Expand All @@ -26,14 +28,17 @@ type IConfig interface {
SetUid(uint64)
SetHex(bool)
SetDebug(bool)
GetPerCpuMapSize() int
SetPerCpuMapSize(int)
EnableGlobalVar() bool //
}

type eConfig struct {
Pid uint64
Uid uint64
IsHex bool
Debug bool
Pid uint64
Uid uint64
PerCpuMapSize int // ebpf map size for per Cpu. see https://github.com/gojue/ecapture/issues/433 .
IsHex bool
Debug bool
}

func (c *eConfig) GetPid() uint64 {
Expand Down Expand Up @@ -68,6 +73,14 @@ func (c *eConfig) SetHex(isHex bool) {
c.IsHex = isHex
}

func (c *eConfig) GetPerCpuMapSize() int {
return c.PerCpuMapSize
}

func (c *eConfig) SetPerCpuMapSize(size int) {
c.PerCpuMapSize = size
}

func (c *eConfig) EnableGlobalVar() bool {
kv, err := kernel.HostVersion()
if err != nil {
Expand Down
8 changes: 0 additions & 8 deletions user/module/const_androidgki.go

This file was deleted.

8 changes: 0 additions & 8 deletions user/module/const_linux.go

This file was deleted.

3 changes: 1 addition & 2 deletions user/module/imodule.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"github.com/cilium/ebpf/perf"
"github.com/cilium/ebpf/ringbuf"
"log"
"os"
"strings"
)

Expand Down Expand Up @@ -190,7 +189,7 @@ func (m *Module) readEvents() error {
}

func (m *Module) perfEventReader(errChan chan error, em *ebpf.Map) {
rd, err := perf.NewReader(em, os.Getpagesize()*BufferSizeOfEbpfMap)
rd, err := perf.NewReader(em, m.conf.GetPerCpuMapSize())
if err != nil {
errChan <- fmt.Errorf("creating %s reader dns: %s", em.String(), err)
return
Expand Down

0 comments on commit abda47c

Please sign in to comment.