Skip to content

Commit

Permalink
Merge pull request kubernetes#46033 from wojtek-t/reduce_memory_alloc…
Browse files Browse the repository at this point in the history
…ations_in_kube_proxy

Automatic merge from submit-queue

Reduce memory allocations in kube proxy

Memory allocation (and Go GarbageCollection) seems to be one of the most expensive operations in kube-proxy (I've seen profiles where it was more than 50%).

The commits are mostly independent from each other and all of them are mostly about reusing already allocated memory.

This PR is reducing memory allocation by ~5x (results below from 100-node load test):

before:
```
(pprof) top
38.64GB of 39.11GB total (98.79%)
Dropped 249 nodes (cum <= 0.20GB)
Showing top 10 nodes out of 61 (cum >= 0.20GB)
      flat  flat%   sum%        cum   cum%
   15.10GB 38.62% 38.62%    15.10GB 38.62%  bytes.makeSlice
    9.48GB 24.25% 62.87%     9.48GB 24.25%  runtime.rawstringtmp
    8.30GB 21.21% 84.07%    32.47GB 83.02%  k8s.io/kubernetes/pkg/proxy/iptables.(*Proxier).syncProxyRules
    2.08GB  5.31% 89.38%     2.08GB  5.31%  fmt.(*fmt).padString
    1.90GB  4.86% 94.24%     3.82GB  9.77%  strings.Join
    0.67GB  1.72% 95.96%     0.67GB  1.72%  runtime.hashGrow
    0.36GB  0.92% 96.88%     0.36GB  0.92%  runtime.stringtoslicebyte
    0.31GB  0.79% 97.67%     0.62GB  1.58%  encoding/base32.(*Encoding).EncodeToString
    0.24GB  0.62% 98.29%     0.24GB  0.62%  strings.genSplit
    0.20GB   0.5% 98.79%     0.20GB   0.5%  runtime.convT2E
```

after:
```
7.94GB of 8.13GB total (97.75%)
Dropped 311 nodes (cum <= 0.04GB)
Showing top 10 nodes out of 65 (cum >= 0.11GB)
      flat  flat%   sum%        cum   cum%
    3.32GB 40.87% 40.87%     8.05GB 99.05%  k8s.io/kubernetes/pkg/proxy/iptables.(*Proxier).syncProxyRules
    2.85GB 35.09% 75.95%     2.85GB 35.09%  runtime.rawstringtmp
    0.60GB  7.41% 83.37%     0.60GB  7.41%  runtime.hashGrow
    0.31GB  3.76% 87.13%     0.31GB  3.76%  runtime.stringtoslicebyte
    0.28GB  3.43% 90.56%     0.55GB  6.80%  encoding/base32.(*Encoding).EncodeToString
    0.19GB  2.29% 92.85%     0.19GB  2.29%  strings.genSplit
    0.18GB  2.17% 95.03%     0.18GB  2.17%  runtime.convT2E
    0.10GB  1.28% 96.31%     0.71GB  8.71%  runtime.mapassign
    0.10GB  1.21% 97.51%     0.10GB  1.21%  syscall.ByteSliceFromString
    0.02GB  0.23% 97.75%     0.11GB  1.38%  syscall.SlicePtrFromStrings
```
  • Loading branch information
Kubernetes Submit Queue authored May 20, 2017
2 parents 9f5849a + a3da8d7 commit 3456d4d
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 151 deletions.
33 changes: 12 additions & 21 deletions pkg/kubelet/network/hostport/fake_iptables.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,36 +228,27 @@ func saveChain(chain *fakeChain, data *bytes.Buffer) {
}

func (f *fakeIPTables) Save(tableName utiliptables.Table) ([]byte, error) {
data := bytes.NewBuffer(nil)
err := f.SaveInto(tableName, data)
return data.Bytes(), err
}

func (f *fakeIPTables) SaveInto(tableName utiliptables.Table, buffer *bytes.Buffer) error {
table, err := f.getTable(tableName)
if err != nil {
return nil, err
return err
}

data := bytes.NewBuffer(nil)
data.WriteString(fmt.Sprintf("*%s\n", table.name))
buffer.WriteString(fmt.Sprintf("*%s\n", table.name))

rules := bytes.NewBuffer(nil)
for _, chain := range table.chains {
data.WriteString(fmt.Sprintf(":%s - [0:0]\n", string(chain.name)))
buffer.WriteString(fmt.Sprintf(":%s - [0:0]\n", string(chain.name)))
saveChain(chain, rules)
}
data.Write(rules.Bytes())
data.WriteString("COMMIT\n")
return data.Bytes(), nil
}

func (f *fakeIPTables) SaveAll() ([]byte, error) {
data := bytes.NewBuffer(nil)
for _, table := range f.tables {
tableData, err := f.Save(table.name)
if err != nil {
return nil, err
}
if _, err = data.Write(tableData); err != nil {
return nil, err
}
}
return data.Bytes(), nil
buffer.Write(rules.Bytes())
buffer.WriteString("COMMIT\n")
return nil
}

func (f *fakeIPTables) restore(restoreTableName utiliptables.Table, data []byte, flush utiliptables.FlushFlag) error {
Expand Down
8 changes: 8 additions & 0 deletions pkg/kubelet/prober/prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ func (pb *prober) newExecInContainer(container v1.Container, containerID kubecon
}}
}

func (eic execInContainer) Run() error {
return fmt.Errorf("unimplemented")
}

func (eic execInContainer) CombinedOutput() ([]byte, error) {
return eic.run()
}
Expand All @@ -257,6 +261,10 @@ func (eic execInContainer) SetStdout(out io.Writer) {
//unimplemented
}

func (eic execInContainer) SetStderr(out io.Writer) {
//unimplemented
}

func (eic execInContainer) Stop() {
//unimplemented
}
6 changes: 6 additions & 0 deletions pkg/probe/exec/exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ type FakeCmd struct {
err error
}

func (f *FakeCmd) Run() error {
return nil
}

func (f *FakeCmd) CombinedOutput() ([]byte, error) {
return f.out, f.err
}
Expand All @@ -44,6 +48,8 @@ func (f *FakeCmd) SetStdin(in io.Reader) {}

func (f *FakeCmd) SetStdout(out io.Writer) {}

func (f *FakeCmd) SetStderr(out io.Writer) {}

func (f *FakeCmd) Stop() {}

type fakeExitError struct {
Expand Down
Loading

0 comments on commit 3456d4d

Please sign in to comment.