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

[process-agent] Remove the unsafe conversion and rely on Go's escape analysis to avoid allocation #21161

Merged
merged 1 commit into from
Dec 4, 2023

Conversation

wiyu
Copy link
Contributor

@wiyu wiyu commented Nov 28, 2023

What does this PR do?

This removes the unsafe calls to get a []byte to string to avoid copying as the go compiler is able to optimize this away as of 1.20. This will keep the code simpler and safer to work with.

Example code and the Go escape analysis. (notes: code is truncated imports, etc)

func convertToInt(b []byte) (int64, error) {
	val := string(b)
	return strconv.ParseInt(val, 10, 64)
}

The escape analysis while running go 1.19.13 shows the []byte is copied when converting to a string causing heap allocation.

❯ go build -gcflags="-m -m -l" bytes.go
# command-line-arguments
./bytes.go:8:15: string(b) escapes to heap:
./bytes.go:8:15:   flow: val = &{storage for string(b)}:
./bytes.go:8:15:     from string(b) (spill) at ./bytes.go:8:15
./bytes.go:8:15:     from val := string(b) (assign) at ./bytes.go:8:6
./bytes.go:8:15:   flow: {heap} = val:
./bytes.go:8:15:     from strconv.Atoi(val) (call parameter) at ./bytes.go:9:21
./bytes.go:7:19: b does not escape
./bytes.go:8:15: string(b) escapes to heap

Using go 1.20 it shows that no copy occurs to convert []byte to string and no heap escape happens which means allocation does not occur.

❯ go build -gcflags="-m -m -l" bytes.go
# command-line-arguments
./bytes.go:7:19: b does not escape
./bytes.go:8:16: string(b) does not escape

This is also demonstrated in a benchmark of the test code

func BenchmarkConvertToInt(b *testing.B) {
	in := "123"
	for i := 0; i < b.N; i++ {
		_, _ = convertToInt([]byte(in))
	}
}

Results

go v1.19.13

BenchmarkConvertToInt-10        56966983                19.92 ns/op            3 B/op          1 allocs/op

go v1.20

BenchmarkConvertToInt-10        83684193                14.19 ns/op            0 B/op          0 allocs/op

Benchmarks

❯ benchstat old.txt new.txt
name                old time/op    new time/op    delta
ParseStatusLine-4      836ns ±14%     838ns ±20%   ~     (p=0.912 n=10+10)
ParseStatContent-4    2.27µs ± 4%    2.26µs ± 2%   ~     (p=0.857 n=9+10)

name                old alloc/op   new alloc/op   delta
ParseStatusLine-4       296B ± 0%      296B ± 0%   ~     (all equal)
ParseStatContent-4     0.00B          0.00B        ~     (all equal)

name                old allocs/op  new allocs/op  delta
ParseStatusLine-4       6.00 ± 0%      6.00 ± 0%   ~     (all equal)
ParseStatContent-4      0.00           0.00        ~     (all equal)

Raw data (truncated from 10 runs)

old.txt
BenchmarkParseStatusLine-4       1331600               892.0 ns/op           296 B/op          6 allocs/op
BenchmarkParseStatusLine-4       1387952               830.7 ns/op           296 B/op          6 allocs/op
...
BenchmarkParseStatContent-4       517012              2364 ns/op               0 B/op          0 allocs/op
BenchmarkParseStatContent-4       508501              2302 ns/op               0 B/op          0 allocs/op
...
new.txt
BenchmarkParseStatusLine-4       1281456              1003 ns/op             296 B/op          6 allocs/op
BenchmarkParseStatusLine-4       1320164               834.2 ns/op           296 B/op          6 allocs/op

...
BenchmarkParseStatContent-4       508948              2286 ns/op               0 B/op          0 allocs/op
BenchmarkParseStatContent-4       498462              2293 ns/op               0 B/op          0 allocs/op
...

Motivation

Additional Notes

Possible Drawbacks / Trade-offs

Describe how to test/QA your changes

Reviewer's Checklist

  • If known, an appropriate milestone has been selected; otherwise the Triage milestone is set.
  • Use the major_change label if your change either has a major impact on the code base, is impacting multiple teams or is changing important well-established internals of the Agent. This label will be use during QA to make sure each team pay extra attention to the changed behavior. For any customer facing change use a releasenote.
  • A release note has been added or the changelog/no-changelog label has been applied.
  • Changed code has automated tests for its functionality.
  • Adequate QA/testing plan information is provided if the qa/skip-qa label is not applied.
  • At least one team/.. label has been applied, indicating the team(s) that should QA this change.
  • If applicable, docs team has been notified or an issue has been opened on the documentation repo.
  • If applicable, the need-change/operator and need-change/helm labels have been applied.
  • If applicable, the k8s/<min-version> label, indicating the lowest Kubernetes version compatible with this feature.
  • If applicable, the config template has been updated.

@pr-commenter
Copy link

pr-commenter bot commented Nov 28, 2023

Bloop Bleep... Dogbot Here

Regression Detector Results

Run ID: 075f9294-a59a-482a-8116-7c532301b767
Baseline: 5222280
Comparison: 334852a
Total datadog-agent CPUs: 7

Explanation

A regression test is an integrated performance test for datadog-agent in a repeatable rig, with varying configuration for datadog-agent. What follows is a statistical summary of a brief datadog-agent run for each configuration across SHAs given above. The goal of these tests are to determine quickly if datadog-agent performance is changed and to what degree by a pull request.

Because a target's optimization goal performance in each experiment will vary somewhat each time it is run, we can only estimate mean differences in optimization goal relative to the baseline target. We express these differences as a percentage change relative to the baseline target, denoted "Δ mean %". These estimates are made to a precision that balances accuracy and cost control. We represent this precision as a 90.00% confidence interval denoted "Δ mean % CI": there is a 90.00% chance that the true value of "Δ mean %" is in that interval.

We decide whether a change in performance is a "regression" -- a change worth investigating further -- if both of the following two criteria are true:

  1. The estimated |Δ mean %| ≥ 5.00%. This criterion intends to answer the question "Does the estimated change in mean optimization goal performance have a meaningful impact on your customers?". We assume that when |Δ mean %| < 5.00%, the impact on your customers is not meaningful. We also assume that a performance change in optimization goal is worth investigating whether it is an increase or decrease, so long as the magnitude of the change is sufficiently large.

  2. Zero is not in the 90.00% confidence interval "Δ mean % CI" about "Δ mean %". This statement is equivalent to saying that there is at least a 90.00% chance that the mean difference in optimization goal is not zero. This criterion intends to answer the question, "Is there a statistically significant difference in mean optimization goal performance?". It also means there is no more than a 10.00% chance this criterion reports a statistically significant difference when the true difference in mean optimization goal is zero -- a "false positive". We assume you are willing to accept a 10.00% chance of inaccurately detecting a change in performance when no true difference exists.

The table below, if present, lists those experiments that have experienced a statistically significant change in mean optimization goal performance between baseline and comparison SHAs with 90.00% confidence OR have been detected as newly erratic. Negative values of "Δ mean %" mean that baseline is faster, whereas positive values of "Δ mean %" mean that comparison is faster. Results that do not exhibit more than a ±5.00% change in their mean optimization goal are discarded. An experiment is erratic if its coefficient of variation is greater than 0.1. The abbreviated table will be omitted if no interesting change is observed.

No interesting changes in experiment optimization goals with confidence ≥ 90.00% and |Δ mean %| ≥ 5.00%.

Fine details of change detection per experiment.
experiment goal Δ mean % Δ mean % CI confidence
tcp_syslog_to_blackhole ingress throughput +1.03 [+0.89, +1.17] 100.00%
file_tree egress throughput +0.44 [-1.39, +2.26] 30.64%
idle egress throughput +0.14 [-2.24, +2.51] 7.46%
file_to_blackhole egress throughput +0.04 [-0.97, +1.06] 5.44%
trace_agent_json ingress throughput +0.03 [-0.10, +0.16] 31.75%
dogstatsd_string_interner_8MiB_50k ingress throughput +0.02 [-0.02, +0.07] 62.29%
uds_dogstatsd_to_api ingress throughput +0.02 [-0.15, +0.20] 18.41%
tcp_dd_logs_filter_exclude ingress throughput +0.01 [-0.13, +0.15] 8.35%
dogstatsd_string_interner_128MiB_1k ingress throughput +0.00 [-0.14, +0.15] 2.22%
dogstatsd_string_interner_64MiB_1k ingress throughput +0.00 [-0.13, +0.13] 0.17%
dogstatsd_string_interner_8MiB_100k ingress throughput +0.00 [-0.04, +0.04] 0.00%
dogstatsd_string_interner_128MiB_100 ingress throughput -0.00 [-0.14, +0.14] 0.21%
dogstatsd_string_interner_64MiB_100 ingress throughput -0.00 [-0.14, +0.14] 0.92%
dogstatsd_string_interner_8MiB_100 ingress throughput -0.00 [-0.13, +0.12] 1.67%
trace_agent_msgpack ingress throughput -0.00 [-0.13, +0.13] 1.98%
dogstatsd_string_interner_8MiB_1k ingress throughput -0.01 [-0.11, +0.09] 14.52%
dogstatsd_string_interner_8MiB_10k ingress throughput -0.01 [-0.04, +0.01] 66.10%
otel_to_otel_logs ingress throughput -0.45 [-2.02, +1.12] 36.10%

…nt as Go has improved escape analysis to avoid the byte to string copy.
@wiyu wiyu force-pushed the william.yu/process_linux-allocation-optimization branch from cb227fd to 334852a Compare November 29, 2023 14:01
@wiyu wiyu added team/processes [deprecated] qa/skip-qa - use other qa/ labels [DEPRECATED] Please use qa/done or qa/no-code-change to skip creating a QA card labels Dec 1, 2023
@wiyu wiyu added this to the 7.51.0 milestone Dec 1, 2023
@wiyu wiyu changed the title William.yu/process linux allocation optimization [process-agent] Remove the unsafe conversion and rely on Go's escape analysis to avoid allocation Dec 1, 2023
@wiyu wiyu marked this pull request as ready for review December 1, 2023 14:33
@wiyu wiyu requested a review from a team as a code owner December 1, 2023 14:33
Copy link
Contributor

@blt blt left a comment

Choose a reason for hiding this comment

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

Neat.

@wiyu wiyu merged commit 28ebfe5 into main Dec 4, 2023
161 checks passed
@wiyu wiyu deleted the william.yu/process_linux-allocation-optimization branch December 4, 2023 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog [deprecated] qa/skip-qa - use other qa/ labels [DEPRECATED] Please use qa/done or qa/no-code-change to skip creating a QA card team/processes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants