Skip to content

Commit

Permalink
Cleaning up previous commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
strategicpause committed Sep 18, 2023
1 parent 8422730 commit c558daf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ This will turn off swap to make sure OOMs can be recreated at the expected memor
TCP Leak
~~~
$ podman run \
--memory=1024m \
--memory=128m \
--name=tcp-leak \
--rm \
localhost/memory-leak \
./leak tcp
./leak socket --size 16
~~~
The above command will allocate sockets, establish connections, and write 16 KiB.
14 changes: 2 additions & 12 deletions command/socket/leak.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,12 @@ const (
// defined by /proc/sys/net/core/somaxconn.
MaxConnections = 4096
StartPort = 9090
// By default each established socket connection will write 1 KiB to the buffer.
KiB = 1024
)

var (
LocalAddr = [4]byte{127, 0, 0, 1}
)

type Params struct {
NumSockets int64
NetworkAddressDomain int
ConnectionType int
CommunicationProtocol int
PauseTimeInSeconds time.Duration
}

func tcpLeak(params *Params) error {
PrintParams(params)

Expand Down Expand Up @@ -60,7 +50,7 @@ func tcpLeak(params *Params) error {
return err
}

_, err = unix.Write(clientFd, make([]byte, KiB))
_, err = unix.Write(clientFd, make([]byte, params.DataSize))

if i%100 == 0 {
metrics.PrintSocketStats()
Expand Down Expand Up @@ -106,7 +96,7 @@ func resetServer(params *Params, port int) (chan bool, error) {
}

func PrintParams(params *Params) {
fmt.Printf("NumSockets: %v\n", params.NumSockets)
fmt.Printf("NumSockets: %v\tSize: %v\n", params.NumSockets, params.DataSize)
}

func Must[T any](obj T, err error) T {
Expand Down
23 changes: 20 additions & 3 deletions command/socket/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

const (
NumSocketsName = "num-sockets"
CommunicationProtocolName = "comm-protocol"
PauseDurationName = "pause"
NumSocketsName = "num-sockets"
SizeName = "size"
PauseDurationName = "pause"

KiB = 1024
)

func Register() cli.Command {
Expand All @@ -33,15 +35,30 @@ func flags() []cli.Flag {
Usage: "Time between allocations in seconds.",
Value: time.Second,
},
cli.IntFlag{
Name: SizeName,
Usage: "Number of KiB to write per per socket.",
Value: 4,
},
}
}

type Params struct {
NumSockets int64
NetworkAddressDomain int
ConnectionType int
CommunicationProtocol int
DataSize int
PauseTimeInSeconds time.Duration
}

func action(ctx *cli.Context) error {
params := &Params{
NumSockets: ctx.Int64(NumSocketsName),
NetworkAddressDomain: unix.AF_INET,
ConnectionType: unix.SOCK_STREAM,
CommunicationProtocol: unix.IPPROTO_TCP,
DataSize: ctx.Int(SizeName) * KiB,
PauseTimeInSeconds: ctx.Duration(PauseDurationName),
}
return tcpLeak(params)
Expand Down

0 comments on commit c558daf

Please sign in to comment.