Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[agent-smith] Reduce cpu and memory consumption #10356

Merged
merged 1 commit into from
May 30, 2022
Merged

Conversation

Furisto
Copy link
Member

@Furisto Furisto commented May 30, 2022

Description

We have cpu and memory spikes in agent-smith, with big contributors being reading the elf data of binaries. This data is reread for every signature that has been defined. With this change we cache the data for the duration of the match phase.

Related Issue(s)

Fixes ##10331

How to test

Profile agent-smith in preview environment

Release Notes

Reduce cpu and memory consumption of agent-smith

@Furisto Furisto requested a review from a team May 30, 2022 15:20
@github-actions github-actions bot added the team: workspace Issue belongs to the Workspace team label May 30, 2022
if err != nil {
return false, xerrors.Errorf("cannot read stream head: %w", err)
}
in.header = head
Copy link
Contributor

@utam0k utam0k May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache at first.

if err != nil {
return false, err
}
in.rodata = rodata
Copy link
Contributor

@utam0k utam0k May 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache at first.

Comment on lines +183 to +184
if len(in.rodata) > 0 {
rodata = in.rodata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a cache 👍

Copy link
Contributor

@utam0k utam0k left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sense

Comment on lines +125 to +126
if len(in.header) > 0 {
head = in.header
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a cache

Comment on lines +208 to +221
var symbols []string
if len(in.symbols) > 0 {
symbols = in.symbols
} else {
executable, err := elf.NewFile(in.Reader)
if err != nil {
return false, xerrors.Errorf("cannot anaylse ELF file: %w", err)
}

symbols, err := ExtractELFSymbols(executable)
if err != nil {
return false, err
symbols, err = ExtractELFSymbols(executable)
if err != nil {
return false, err
}
in.symbols = symbols
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

Comment on lines +246 to +255
symbols := make([]string, len(syms)+len(dynsyms))
i := 0
for _, s := range syms {
symbols[i] = s.Name
i += 1
}

for _, s := range dynsyms {
symbols = append(symbols, s.Name)
symbols[i] = s.Name
i += 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these refactoring?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We know the number of elements, so I allocate the underlying array once instead of appending to the slice which includes resizing i.e. reallocation of the array.

@roboquat roboquat merged commit 46fb0a0 into main May 30, 2022
@roboquat roboquat deleted the fo/smith-cpu-util branch May 30, 2022 23:48
@utam0k
Copy link
Contributor

utam0k commented May 30, 2022

@Furisto I'd like to know how you profile the agent-smith?

@Furisto
Copy link
Member Author

Furisto commented May 31, 2022

@Furisto I'd like to know how you profile the agent-smith?

https://www.notion.so/gitpod/Profiling-Go-Code-a0de0ddcb34a48ac9ebee7fa4f0faf52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note size/L team: workspace Issue belongs to the Workspace team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants