mirror of
https://github.com/kubeshark/kubeshark.git
synced 2025-08-01 08:50:27 +00:00
Add option to memory profiling and limit page cache (#158)
This commit is contained in:
parent
69a9deab4b
commit
1e726e381b
@ -1,6 +1,7 @@
|
|||||||
package tap
|
package tap
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/romana/rlog"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -27,12 +28,14 @@ func (cl *Cleaner) clean() {
|
|||||||
startCleanTime := time.Now()
|
startCleanTime := time.Now()
|
||||||
|
|
||||||
cl.assemblerMutex.Lock()
|
cl.assemblerMutex.Lock()
|
||||||
|
rlog.Debugf("Assembler Stats before cleaning %s", cl.assembler.Dump())
|
||||||
flushed, closed := cl.assembler.FlushCloseOlderThan(startCleanTime.Add(-cl.connectionTimeout))
|
flushed, closed := cl.assembler.FlushCloseOlderThan(startCleanTime.Add(-cl.connectionTimeout))
|
||||||
cl.assemblerMutex.Unlock()
|
cl.assemblerMutex.Unlock()
|
||||||
|
|
||||||
deleted := cl.matcher.deleteOlderThan(startCleanTime.Add(-cl.connectionTimeout))
|
deleted := cl.matcher.deleteOlderThan(startCleanTime.Add(-cl.connectionTimeout))
|
||||||
|
|
||||||
cl.statsMutex.Lock()
|
cl.statsMutex.Lock()
|
||||||
|
rlog.Debugf("Assembler Stats after cleaning %s", cl.assembler.Dump())
|
||||||
cl.stats.flushed += flushed
|
cl.stats.flushed += flushed
|
||||||
cl.stats.closed += closed
|
cl.stats.closed += closed
|
||||||
cl.stats.deleted += deleted
|
cl.stats.deleted += deleted
|
||||||
|
@ -35,6 +35,7 @@ const AppPortsEnvVar = "APP_PORTS"
|
|||||||
const maxHTTP2DataLenEnvVar = "HTTP2_DATA_SIZE_LIMIT"
|
const maxHTTP2DataLenEnvVar = "HTTP2_DATA_SIZE_LIMIT"
|
||||||
const maxHTTP2DataLenDefault = 1 * 1024 * 1024 // 1MB
|
const maxHTTP2DataLenDefault = 1 * 1024 * 1024 // 1MB
|
||||||
const cleanPeriod = time.Second * 10
|
const cleanPeriod = time.Second * 10
|
||||||
|
|
||||||
var remoteOnlyOutboundPorts = []int{80, 443}
|
var remoteOnlyOutboundPorts = []int{80, 443}
|
||||||
|
|
||||||
func parseAppPorts(appPortsList string) []int {
|
func parseAppPorts(appPortsList string) []int {
|
||||||
@ -196,6 +197,37 @@ func StartPassiveTapper(opts *TapOpts) (<-chan *OutputChannelItem, <-chan *Outbo
|
|||||||
return nil, outboundLinkWriter.OutChan
|
return nil, outboundLinkWriter.OutChan
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func startMemoryProfiler() {
|
||||||
|
dirname := "/app/pprof"
|
||||||
|
rlog.Info("Profiling is on, results will be written to %s", dirname)
|
||||||
|
go func() {
|
||||||
|
if _, err := os.Stat(dirname); os.IsNotExist(err) {
|
||||||
|
if err := os.Mkdir(dirname, 0777); err != nil {
|
||||||
|
log.Fatal("could not create directory for profile: ", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for true {
|
||||||
|
t := time.Now()
|
||||||
|
|
||||||
|
filename := fmt.Sprintf("%s/%s__mem.prof", dirname, t.Format("15_04_05"))
|
||||||
|
|
||||||
|
rlog.Info("Writing memory profile to %s\n", filename)
|
||||||
|
|
||||||
|
f, err := os.Create(filename)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("could not create memory profile: ", err)
|
||||||
|
}
|
||||||
|
runtime.GC() // get up-to-date statistics
|
||||||
|
if err := pprof.WriteHeapProfile(f); err != nil {
|
||||||
|
log.Fatal("could not write memory profile: ", err)
|
||||||
|
}
|
||||||
|
_ = f.Close()
|
||||||
|
time.Sleep(time.Minute)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func startPassiveTapper(harWriter *HarWriter, outboundLinkWriter *OutboundLinkWriter) {
|
func startPassiveTapper(harWriter *HarWriter, outboundLinkWriter *OutboundLinkWriter) {
|
||||||
log.SetFlags(log.LstdFlags | log.LUTC | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.LUTC | log.Lshortfile)
|
||||||
|
|
||||||
@ -313,10 +345,16 @@ func startPassiveTapper(harWriter *HarWriter, outboundLinkWriter *OutboundLinkWr
|
|||||||
doHTTP: !*nohttp,
|
doHTTP: !*nohttp,
|
||||||
harWriter: harWriter,
|
harWriter: harWriter,
|
||||||
outbountLinkWriter: outboundLinkWriter,
|
outbountLinkWriter: outboundLinkWriter,
|
||||||
|
|
||||||
}
|
}
|
||||||
streamPool := reassembly.NewStreamPool(streamFactory)
|
streamPool := reassembly.NewStreamPool(streamFactory)
|
||||||
assembler := reassembly.NewAssembler(streamPool)
|
assembler := reassembly.NewAssembler(streamPool)
|
||||||
|
|
||||||
|
maxBufferedPagesTotal := GetMaxBufferedPagesPerConnection()
|
||||||
|
maxBufferedPagesPerConnection := GetMaxBufferedPagesTotal()
|
||||||
|
rlog.Infof("Assembler options: maxBufferedPagesTotal=%d, maxBufferedPagesPerConnection=%d", maxBufferedPagesTotal, maxBufferedPagesPerConnection)
|
||||||
|
assembler.AssemblerOptions.MaxBufferedPagesTotal = maxBufferedPagesTotal
|
||||||
|
assembler.AssemblerOptions.MaxBufferedPagesPerConnection = maxBufferedPagesPerConnection
|
||||||
|
|
||||||
var assemblerMutex sync.Mutex
|
var assemblerMutex sync.Mutex
|
||||||
|
|
||||||
signalChan := make(chan os.Signal, 1)
|
signalChan := make(chan os.Signal, 1)
|
||||||
@ -376,6 +414,10 @@ func startPassiveTapper(harWriter *HarWriter, outboundLinkWriter *OutboundLinkWr
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
if GetMemoryProfilingEnabled() {
|
||||||
|
startMemoryProfiler()
|
||||||
|
}
|
||||||
|
|
||||||
for packet := range source.Packets() {
|
for packet := range source.Packets() {
|
||||||
count++
|
count++
|
||||||
rlog.Debugf("PACKET #%d", count)
|
rlog.Debugf("PACKET #%d", count)
|
||||||
|
@ -1,5 +1,18 @@
|
|||||||
package tap
|
package tap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
MemoryProfilingEnabledEnvVarName = "MEMORY_PROFILING_ENABLED"
|
||||||
|
MaxBufferedPagesTotalEnvVarName = "MAX_BUFFERED_PAGES_TOTAL"
|
||||||
|
MaxBufferedPagesPerConnectionEnvVarName = "MAX_BUFFERED_PAGES_PER_CONNECTION"
|
||||||
|
MaxBufferedPagesTotalDefaultValue = 5000
|
||||||
|
MaxBufferedPagesPerConnectionDefaultValue = 5000
|
||||||
|
)
|
||||||
|
|
||||||
type globalSettings struct {
|
type globalSettings struct {
|
||||||
filterPorts []int
|
filterPorts []int
|
||||||
filterAuthorities []string
|
filterAuthorities []string
|
||||||
@ -29,3 +42,23 @@ func GetFilterIPs() []string {
|
|||||||
copy(addresses, gSettings.filterAuthorities)
|
copy(addresses, gSettings.filterAuthorities)
|
||||||
return addresses
|
return addresses
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetMaxBufferedPagesTotal() int {
|
||||||
|
valueFromEnv, err := strconv.Atoi(os.Getenv(MaxBufferedPagesTotalEnvVarName))
|
||||||
|
if err != nil {
|
||||||
|
return MaxBufferedPagesTotalDefaultValue
|
||||||
|
}
|
||||||
|
return valueFromEnv
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMaxBufferedPagesPerConnection() int {
|
||||||
|
valueFromEnv, err := strconv.Atoi(os.Getenv(MaxBufferedPagesPerConnectionEnvVarName))
|
||||||
|
if err != nil {
|
||||||
|
return MaxBufferedPagesPerConnectionDefaultValue
|
||||||
|
}
|
||||||
|
return valueFromEnv
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetMemoryProfilingEnabled() bool {
|
||||||
|
return os.Getenv(MemoryProfilingEnabledEnvVarName) == "1"
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user