-
Notifications
You must be signed in to change notification settings - Fork 49
runtime: add memprofile attribution support #1905
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
910a6b4
runtime: add memprofile attribution support
cpunion 87049cc
runtime: keep memprofile Go call stacks
cpunion b71d843
runtime: cover memprofile instrumentation paths
cpunion 3d2ec88
runtime: stabilize memprofile heap profiling
cpunion 6bba3b7
test: accept host tiny allocation profiling
cpunion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| //go:build !llgo | ||
| // +build !llgo | ||
|
|
||
| /* | ||
| * Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package cl | ||
|
|
||
| import ( | ||
| "go/ast" | ||
| "go/parser" | ||
| "go/token" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestPackageUsesRuntimeMemProfile(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| src string | ||
| want bool | ||
| }{ | ||
| { | ||
| name: "no runtime import", | ||
| src: `package foo | ||
| func f() {} | ||
| `, | ||
| }, | ||
| { | ||
| name: "runtime mem profile call", | ||
| src: `package foo | ||
| import "runtime" | ||
| func f() { | ||
| runtime.MemProfile(nil, false) | ||
| } | ||
| `, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "runtime mem profile rate", | ||
| src: `package foo | ||
| import "runtime" | ||
| var _ = runtime.MemProfileRate | ||
| `, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "renamed runtime import", | ||
| src: `package foo | ||
| import rt "runtime" | ||
| var _ = rt.MemProfileRate | ||
| `, | ||
| want: true, | ||
| }, | ||
| { | ||
| name: "blank runtime import", | ||
| src: `package foo | ||
| import _ "runtime" | ||
| func f() {} | ||
| `, | ||
| }, | ||
| { | ||
| name: "dot runtime import", | ||
| src: `package foo | ||
| import . "runtime" | ||
| var _ = MemProfileRate | ||
| `, | ||
| }, | ||
| { | ||
| name: "other runtime selector", | ||
| src: `package foo | ||
| import "runtime" | ||
| var _ = runtime.GOOS | ||
| `, | ||
| }, | ||
| { | ||
| name: "selector on non runtime value", | ||
| src: `package foo | ||
| import "runtime" | ||
| var _ = runtime.GOOS | ||
| type profiler struct{} | ||
| var p profiler | ||
| var _ = p.MemProfile | ||
| `, | ||
| }, | ||
| { | ||
| name: "selector base is not identifier", | ||
| src: `package foo | ||
| import "runtime" | ||
| type profiler struct{} | ||
| var p profiler | ||
| var _ = (p).MemProfile | ||
| `, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fset := token.NewFileSet() | ||
| file, err := parser.ParseFile(fset, "foo.go", tt.src, parser.ParseComments) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got := packageUsesRuntimeMemProfile([]*ast.File{file}); got != tt.want { | ||
| t.Fatalf("packageUsesRuntimeMemProfile() = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| badImport := &ast.File{ | ||
| Imports: []*ast.ImportSpec{{ | ||
| Path: &ast.BasicLit{Kind: token.STRING, Value: "runtime"}, | ||
| }}, | ||
| } | ||
| if packageUsesRuntimeMemProfile([]*ast.File{badImport}) { | ||
| t.Fatal("bad import literal should not enable memprofile instrumentation") | ||
| } | ||
| } | ||
|
|
||
| func TestMemProfileFunctionName(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| want string | ||
| }{ | ||
| {name: "command-line-arguments.main", want: "main.main"}, | ||
| {name: "command-line-arguments.profiledAlloc", want: "main.profiledAlloc"}, | ||
| {name: "example.com/mod.profiledAlloc", want: "example.com/mod.profiledAlloc"}, | ||
| } | ||
| for _, tt := range tests { | ||
| if got := memProfileFunctionName(tt.name); got != tt.want { | ||
| t.Fatalf("memProfileFunctionName(%q) = %q, want %q", tt.name, got, tt.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestCompileRuntimeMemProfileInstrumentation(t *testing.T) { | ||
| _, m := mustCompileLLPkgFromSrc(t, `package foo | ||
| import "runtime" | ||
|
|
||
| var _ = runtime.MemProfileRate | ||
|
|
||
| func sample() { | ||
| } | ||
| `) | ||
| fn := mustNamedFunction(t, m, "foo.sample") | ||
| fnIR := fn.String() | ||
| for _, want := range []string{"MemProfileEnter", "MemProfileExit"} { | ||
| if !strings.Contains(fnIR, want) { | ||
| t.Fatalf("compiled function missing %s instrumentation:\n%s", want, fnIR) | ||
| } | ||
| } | ||
| ir := m.String() | ||
| for _, want := range []string{"noinline", "disable-tail-calls"} { | ||
| if !strings.Contains(ir, want) { | ||
| t.Fatalf("compiled module missing %s attribute:\n%s", want, ir) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个noinline的决策似乎只影响直接使用了MemProfile的包,间接的堆栈可能还是不准确?inline函数的栈和行号追踪是不是可以考虑像go一样自己建立一个表,在同一个函数内区分出来不同的部分。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zhouguangyuan0718 是的,这里是有意的范围控制。
这个 PR 只在直接使用
runtime.MemProfile/MemProfileRate的包里禁用 inline/tail-call 并加轻量MemProfileEnter/Exit,目的是稳定当前 heapsampling/MemProfile 的直接归因,同时避免给所有包引入全局 instrumentation 开销。间接包里的分配目前仍依赖 native stack fallback;如果符号或 inline 信息不足,确实不能保证像 Go 那样还原完整 inline call stack,也不能在同一个函数内区分所有 inlined call site。要做到这一点需要单独的 inline frame / line table 机制,应该放到后续 debug/lineinfo 方向处理,不放进这个 PR 扩大 scope。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok 可以理解