diff --git a/.gitignore b/.gitignore index da49946..4424084 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ storage/* *.yaml *.exe .vscode -*.code-workspace \ No newline at end of file +*.code-workspace +prof \ No newline at end of file diff --git a/Makefile b/Makefile index d135fe2..31be18c 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,8 @@ +.PHONY: gen init start dev bu + +# 程序参数写在 `--` 之后,避免 -c 等被当成 go 的选项(等同 go run main.go wire_gen.go)。 +RUN_PROF = go run ./cmd/prof -- + gen: go mod tidy go install github.com/google/wire/cmd/wire@latest @@ -9,7 +14,11 @@ init: ./prof -c env.yaml start: - ./prof -c env.yaml + $(RUN_PROF) -c env.yaml + +# Cursor MCP(stdio):终端 1 先 `make start`,终端 2 再 `make dev`。 +dev: + $(RUN_PROF) -c env.yaml mcp bu: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o logs/prof.exe cmd/prof/main.go cmd/prof/wire_gen.go diff --git a/cmd/prof/main.go b/cmd/prof/main.go index b6d774c..a50b206 100644 --- a/cmd/prof/main.go +++ b/cmd/prof/main.go @@ -3,10 +3,13 @@ package main import ( "flag" "fmt" + "github.com/go-kratos/kratos/v2/log" + + "github.com/matteo-gz/prof/internal/appx" "github.com/matteo-gz/prof/internal/conf" + "github.com/matteo-gz/prof/internal/mcpprof" "github.com/matteo-gz/prof/internal/server" - "github.com/matteo-gz/prof/internal/appx" "github.com/matteo-gz/prof/pkg/logx" ) @@ -30,6 +33,15 @@ func init() { } func main() { flag.Parse() + if args := flag.Args(); len(args) > 0 && args[0] == "mcp" { + base, err := mcpprof.ResolveAPIBase(flagConf, flagPort, flagEnv, flagDir, flagPprofPort, flagLog) + if err != nil { + panic(fmt.Errorf("mcp: resolve API base (use -port, -c env.yaml, or PROF_API_BASE): %w", err)) + } + mcpprof.RunStdio(base) + return + } + bs, err := conf.Load(flagConf, flagPort, flagEnv, flagDir, flagPprofPort, flagLog) if err != nil { panic(err) diff --git a/docs/mcp.md b/docs/mcp.md new file mode 100644 index 0000000..a308201 --- /dev/null +++ b/docs/mcp.md @@ -0,0 +1,78 @@ +# Prof MCP (stdio) + +Prof exposes the [Model Context Protocol](https://modelcontextprotocol.io/) over **stdio** using the official [Go SDK](https://github.com/modelcontextprotocol/go-sdk). The **`mcp` subcommand** runs in a **separate process** from the main HTTP server (tcp1 / tcp2); MCP tools proxy the existing JSON HTTP API on the main port. + +On startup, `prof mcp` probes **`GET {API base}/api/`** once (2s timeout) and logs a **warning to stderr** if the web server is not reachable (stdio is unchanged). Each tool call returns a **clear error** (e.g. connection refused / timeout) if the backend is still down, so the MCP client can surface it to the model. + +Chinese documentation: `pprof-ai/skills` repo → `docs/references/prof-mcp.md`. + +## stdio (Cursor and CLI agents) + +Start the normal Prof HTTP server, then run the **`mcp` subcommand** in a second process. + +**Recommended:** reuse the same YAML as the server so the MCP client picks `Server.Port` (and optional `Mcp.APIBase`) automatically: + +```bash +go build -o prof ./cmd/prof ./cmd/prof/wire_gen.go +# terminal A +./prof -c env.yaml + +# terminal B — same repo, same config file +./prof -c env.yaml mcp +``` + +YAML (see `env.yaml.example`): + +```yaml +Server: + Port: 8201 +Mcp: + APIBase: "" # optional; if set, overrides default http://127.0.0.1:{Server.Port} +``` + +Resolution order for the HTTP base MCP tools call: + +1. Environment variable **`PROF_API_BASE`** (if set) +2. **`Mcp.APIBase`** from `-c` YAML (if non-empty) +3. **`http://127.0.0.1:{Server.Port}`** from YAML/`conf.Load` (falls back to `-port` flag when `-c` is omitted) + +Without `-c`, **`./prof mcp` does not read any YAML**; the API base is **`http://127.0.0.1:{-port}`** (flag `-port`, default `8201`). Use `-c` when the main server’s port or `Mcp.APIBase` is only defined in a file. + +### Cursor `mcpServers` example + +Using the same config file as the server (adjust paths): + +```json +{ + "mcpServers": { + "prof": { + "command": "/ABS/PATH/TO/prof", + "args": ["-c", "/ABS/PATH/TO/env.yaml", "mcp"] + } + } +} +``` + +Only set `env.PROF_API_BASE` if you want to override YAML (e.g. Prof behind a reverse proxy). + +## Tools + +| Tool | Proxied HTTP | +|------|----------------| +| `prof_api_index` | `GET /api/` | +| `prof_api_plugins` | `GET /api/plugins` | +| `prof_pprof_top` | `GET /api/pprof/:dir/top` | +| `prof_pprof_source` | `GET /api/pprof/:dir/source` | +| `prof_pprof_peek` | `GET /api/pprof/:dir/peek` | +| `prof_pprof_flame` | `GET /api/pprof/:dir/flame` | +| `prof_pprof_flame_layout` | `GET /api/pprof/:dir/flame/layout` | + +Tool names use underscores only (Cursor requires alphanumeric + `_`, no `.`). + +The `dir` argument is the same **base64-encoded relative path** as in `/pprof/:dir/`. + +Optional numeric tool arguments use **JSON omitempty**: omit the field to use the server default; include `"si": 0` when you need sample index `0` explicitly. + +## Responses + +Successful tool calls return the same **JSON object** as the underlying HTTP API. Errors from non-2xx HTTP or invalid JSON are returned as MCP tool errors. diff --git a/env.yaml.example b/env.yaml.example index c4522b3..fd5662b 100644 --- a/env.yaml.example +++ b/env.yaml.example @@ -1,6 +1,9 @@ Server: Port: 8201 Port2: 8202 +# Optional: MCP stdio (`prof … mcp`). With `-c` this file: if APIBase empty, uses http://127.0.0.1:{Server.Port}. Without `-c`, MCP ignores YAML and uses flag -port (default 8201) or env PROF_API_BASE. +Mcp: + APIBase: "" App: # env: prod|local Env: prod diff --git a/go.mod b/go.mod index 6375006..bed5502 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,15 @@ module github.com/matteo-gz/prof -go 1.22 +go 1.25.0 require ( github.com/gabriel-vasile/mimetype v1.4.1 github.com/gin-contrib/pprof v1.3.0 github.com/gin-gonic/gin v1.7.7 github.com/go-kratos/kratos/v2 v2.4.0 + github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 github.com/google/wire v0.5.0 + github.com/modelcontextprotocol/go-sdk v1.5.0 github.com/natefinch/lumberjack v2.0.0+incompatible go.uber.org/zap v1.21.0 golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 @@ -20,18 +22,23 @@ require ( github.com/go-playground/universal-translator v0.17.0 // indirect github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/jsonschema-go v0.4.2 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kr/pretty v0.2.0 // indirect github.com/leodido/go-urn v1.2.0 // indirect github.com/mattn/go-isatty v0.0.14 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/segmentio/asm v1.1.3 // indirect + github.com/segmentio/encoding v0.5.4 // indirect github.com/ugorji/go/codec v1.1.7 // indirect + github.com/yosida95/uritemplate/v3 v3.0.2 // indirect go.uber.org/atomic v1.7.0 // indirect go.uber.org/multierr v1.6.0 // indirect golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect - golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect + golang.org/x/oauth2 v0.35.0 // indirect + golang.org/x/sys v0.41.0 // indirect google.golang.org/protobuf v1.28.0 // indirect gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect diff --git a/go.sum b/go.sum index 007637b..512e7e9 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GO github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE= github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= github.com/golang-jwt/jwt/v4 v4.4.1/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0= +github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY= +github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= @@ -76,9 +78,14 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8= +github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE= +github.com/google/pprof v0.0.0-20260402051712-545e8a4df936 h1:EwtI+Al+DeppwYX2oXJCETMO23COyaKGP6fHVpkpWpg= +github.com/google/pprof v0.0.0-20260402051712-545e8a4df936/go.mod h1:MxpfABSjhmINe3F1It9d+8exIHFvUqtLIRCdOGNXqiI= github.com/google/subcommands v1.0.1/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= @@ -101,6 +108,8 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/modelcontextprotocol/go-sdk v1.5.0 h1:CHU0FIX9kpueNkxuYtfYQn1Z0slhFzBZuq+x6IiblIU= +github.com/modelcontextprotocol/go-sdk v1.5.0/go.mod h1:gggDIhoemhWs3BGkGwd1umzEXCEMMvAnhTrnbXJKKKA= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -115,6 +124,10 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= +github.com/segmentio/asm v1.1.3 h1:WM03sfUOENvvKexOLp+pCqgb/WDjsi7EK8gIsICtzhc= +github.com/segmentio/asm v1.1.3/go.mod h1:Ld3L4ZXGNcSLRg4JBsZ3//1+f/TjYl0Mzen/DQy1EJg= +github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0= +github.com/segmentio/encoding v0.5.4/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0= github.com/shirou/gopsutil/v3 v3.21.8/go.mod h1:YWp/H8Qs5fVmf17v7JNZzA0mPJ+mS2e9JdiUF9LlKzQ= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= @@ -128,6 +141,8 @@ github.com/tklauser/numcpus v0.3.0/go.mod h1:yFGUr7TUHQRAhyqBcEg0Ge34zDBAsIvJJcy github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= github.com/ugorji/go/codec v1.1.7 h1:2SvQaVZ1ouYrrKKwoSk2pzd4A9evlKJb9oTL+OaLUSs= github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4= +github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opentelemetry.io/otel v1.7.0/go.mod h1:5BdUoMIz5WEs0vt0CUEMtSSaTSHBBVwrhnz7+nrD5xk= go.opentelemetry.io/otel/sdk v1.7.0/go.mod h1:uTEOTwaqIVuTGiJN7ii13Ibp75wJmYUDe374q6cZwUU= @@ -166,6 +181,8 @@ golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e h1:TsQ7F31D3bUCLeqPT0u+yjp1g golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= +golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ= +golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -189,8 +206,9 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.41.0 h1:Ivj+2Cp/ylzLiEU89QhWblYnOE9zerudt9Ftecq2C6k= +golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -206,10 +224,11 @@ golang.org/x/tools v0.0.0-20190422233926-fe54fb35175b/go.mod h1:LCzVGOaR6xXOjkQ3 golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= +golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k= +golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= diff --git a/internal/biz/analyze.go b/internal/biz/analyze.go new file mode 100644 index 0000000..69dbd1f --- /dev/null +++ b/internal/biz/analyze.go @@ -0,0 +1,232 @@ +package biz + +import ( + "fmt" + "math" + "os" + "sort" + "strings" + + "github.com/google/pprof/profile" +) + +type SampleTypeInfo struct { + Type string `json:"type"` + Unit string `json:"unit"` +} + +type TopItem struct { + Rank int `json:"rank"` + Name string `json:"name"` + Flat int64 `json:"flat"` + FlatPct float64 `json:"flat_pct"` + SumPct float64 `json:"sum_pct"` + Cum int64 `json:"cum"` + CumPct float64 `json:"cum_pct"` +} + +type TopResult struct { + Path string `json:"path"` + Type string `json:"type"` + SampleType SampleTypeInfo `json:"sample_type"` + SampleTypes []SampleTypeInfo `json:"sample_types"` + Total int64 `json:"total"` + Unit string `json:"unit"` + Top []TopItem `json:"top"` +} + +type funcStat struct { + FuncID uint64 + Name string + Flat int64 + Cum int64 +} + +func AnalyzeTop(absPath string, n int, sampleIdx int, cumSort bool) (*TopResult, error) { + f, err := os.Open(absPath) + if err != nil { + return nil, fmt.Errorf("open profile: %w", err) + } + defer f.Close() + + p, err := profile.Parse(f) + if err != nil { + return nil, fmt.Errorf("parse profile: %w", err) + } + + if len(p.SampleType) == 0 { + return nil, fmt.Errorf("profile has no sample types") + } + + idx := resolveSampleIndex(p, sampleIdx) + if idx < 0 || idx >= len(p.SampleType) { + return nil, fmt.Errorf("sample index %d out of range [0, %d)", sampleIdx, len(p.SampleType)) + } + + stats, total := aggregateFlatCum(p, idx) + + if cumSort { + sort.Slice(stats, func(i, j int) bool { + if stats[i].Cum != stats[j].Cum { + return stats[i].Cum > stats[j].Cum + } + return stats[i].Name < stats[j].Name + }) + } else { + sort.Slice(stats, func(i, j int) bool { + if stats[i].Flat != stats[j].Flat { + return stats[i].Flat > stats[j].Flat + } + return stats[i].Name < stats[j].Name + }) + } + + if n <= 0 { + n = 20 + } + if n > len(stats) { + n = len(stats) + } + + items := make([]TopItem, n) + var sumPct float64 + for i := 0; i < n; i++ { + s := stats[i] + flatPct := pct(s.Flat, total) + sumPct += flatPct + items[i] = TopItem{ + Rank: i + 1, + Name: s.Name, + Flat: s.Flat, + FlatPct: roundPct(flatPct), + SumPct: roundPct(sumPct), + Cum: s.Cum, + CumPct: roundPct(pct(s.Cum, total)), + } + } + + sampleTypes := make([]SampleTypeInfo, len(p.SampleType)) + for i, st := range p.SampleType { + sampleTypes[i] = SampleTypeInfo{Type: st.Type, Unit: st.Unit} + } + + return &TopResult{ + Path: absPath, + Type: inferType(absPath), + SampleType: SampleTypeInfo{Type: p.SampleType[idx].Type, Unit: p.SampleType[idx].Unit}, + SampleTypes: sampleTypes, + Total: total, + Unit: p.SampleType[idx].Unit, + Top: items, + }, nil +} + +func resolveSampleIndex(p *profile.Profile, si int) int { + if si >= 0 && si < len(p.SampleType) { + return si + } + return len(p.SampleType) - 1 +} + +func aggregateFlatCum(p *profile.Profile, idx int) ([]funcStat, int64) { + flatMap := make(map[uint64]int64) + cumMap := make(map[uint64]int64) + nameMap := make(map[uint64]string) + var total int64 + + for _, s := range p.Sample { + if idx >= len(s.Value) { + continue + } + v := s.Value[idx] + total += v + + // flat: leaf function only (Location[0], innermost Line) + if len(s.Location) > 0 { + if fn := leafFunction(s.Location[0]); fn != nil { + flatMap[fn.ID] += v + nameMap[fn.ID] = fn.Name + } + } + + // cum: every unique function on the stack + seen := make(map[uint64]bool) + for _, loc := range s.Location { + for _, line := range loc.Line { + if line.Function == nil { + continue + } + fid := line.Function.ID + if seen[fid] { + continue + } + seen[fid] = true + cumMap[fid] += v + nameMap[fid] = line.Function.Name + } + } + } + + allIDs := make(map[uint64]bool) + for id := range flatMap { + allIDs[id] = true + } + for id := range cumMap { + allIDs[id] = true + } + + stats := make([]funcStat, 0, len(allIDs)) + for id := range allIDs { + flat, cum := flatMap[id], cumMap[id] + if flat == 0 && cum == 0 { + continue + } + stats = append(stats, funcStat{ + FuncID: id, + Name: nameMap[id], + Flat: flat, + Cum: cum, + }) + } + return stats, total +} + +func leafFunction(loc *profile.Location) *profile.Function { + if len(loc.Line) == 0 { + return nil + } + return loc.Line[0].Function +} + +func pct(value, total int64) float64 { + if total == 0 { + return 0 + } + return float64(value) / float64(total) * 100 +} + +func roundPct(v float64) float64 { + return math.Round(v*100) / 100 +} + +func inferType(path string) string { + lower := strings.ToLower(path) + switch { + case strings.Contains(lower, "cpu"): + return "cpu" + case strings.Contains(lower, "heap"): + return "heap" + case strings.Contains(lower, "alloc"): + return "allocs" + case strings.Contains(lower, "goroutine"): + return "goroutine" + case strings.Contains(lower, "block"): + return "block" + case strings.Contains(lower, "mutex"): + return "mutex" + case strings.Contains(lower, "threadcreate"): + return "threadcreate" + default: + return "unknown" + } +} diff --git a/internal/biz/batch.go b/internal/biz/batch.go index eb79e9f..c0b62e3 100644 --- a/internal/biz/batch.go +++ b/internal/biz/batch.go @@ -163,13 +163,25 @@ func (b *batch) run(ctx context.Context, task urlTask, ch chan Cse, wg *sync.Wai func (uc *Usecase) DealRun(ctx context.Context, p BatchParams) (res []Cse, err error) { b := newBatchFromParams(p, uc.log, uc.denyPrivateIP, uc.samplingSeconds, uc.deltaSeconds, uc.traceSeconds) - return b.Start(ctx, uc.repo.CreateFile) + dir, err := uc.repo.PreAllocDir() + if err != nil { + return + } + return b.Start(ctx, func(uri, contentType string, data []byte) (string, error) { + return uc.repo.CreateFileInDir(dir, uri, contentType, data) + }) } // DealRunLegacy handles the old URL-based batch request for backward compatibility. func (uc *Usecase) DealRunLegacy(ctx context.Context, uri string) (res []Cse, err error) { b := newBatchLegacy(uri, uc.log, uc.denyPrivateIP, uc.samplingSeconds, uc.deltaSeconds, uc.traceSeconds) - return b.Start(ctx, uc.repo.CreateFile) + dir, err := uc.repo.PreAllocDir() + if err != nil { + return + } + return b.Start(ctx, func(u, contentType string, data []byte) (string, error) { + return uc.repo.CreateFileInDir(dir, u, contentType, data) + }) } func (b *batch) setUrl() (err error) { diff --git a/internal/biz/flame.go b/internal/biz/flame.go new file mode 100644 index 0000000..5b6ecc7 --- /dev/null +++ b/internal/biz/flame.go @@ -0,0 +1,262 @@ +package biz + +import ( + "crypto/sha256" + "encoding/binary" + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + + "github.com/google/pprof/profile" +) + +// FlameStack is one sampled stack (same semantics as google/pprof/internal/report.Stack). +type FlameStack struct { + Value int64 `json:"value"` + Sources []int `json:"sources"` +} + +// FlamePlace references one slot in FlameResult.Stacks (aligned with pprof StackSlot). +type FlamePlace struct { + Stack int `json:"stack"` + Pos int `json:"pos"` +} + +// FlameSource is one frame identity in the flame graph (aligned with pprof StackSource). +type FlameSource struct { + FullName string `json:"full_name"` + FileName string `json:"file_name"` + UniqueName string `json:"unique_name"` + Inlined bool `json:"inlined"` + Display []string `json:"display"` + Places []FlamePlace `json:"places"` + Self int64 `json:"self"` + Color int `json:"color"` +} + +// FlameResult is the JSON payload for the flame graph view (pprof Web /flamegraph data model). +type FlameResult struct { + Path string `json:"path"` + Type string `json:"type"` + SampleType SampleTypeInfo `json:"sample_type"` + SampleTypes []SampleTypeInfo `json:"sample_types"` + Total int64 `json:"total"` + Unit string `json:"unit"` + Scale float64 `json:"scale"` + Stacks []FlameStack `json:"stacks"` + Sources []FlameSource `json:"sources"` +} + +var flameSepRE = regexp.MustCompile(`::|\.`) +var flameFileSepRE = regexp.MustCompile(`/`) + +// AnalyzeFlame builds stack list + source table like pprof's report.StackSet (see google-pprof/internal/report/stacks.go). +func AnalyzeFlame(absPath string, sampleIdx int, trimPathOpt, sourcePath string) (*FlameResult, error) { + f, err := os.Open(absPath) + if err != nil { + return nil, fmt.Errorf("open profile: %w", err) + } + defer f.Close() + + p, err := profile.Parse(f) + if err != nil { + return nil, fmt.Errorf("parse profile: %w", err) + } + + if len(p.SampleType) == 0 { + return nil, fmt.Errorf("profile has no sample types") + } + + idx := resolveSampleIndex(p, sampleIdx) + if idx < 0 || idx >= len(p.SampleType) { + return nil, fmt.Errorf("sample index %d out of range [0, %d)", sampleIdx, len(p.SampleType)) + } + + sampleTypes := make([]SampleTypeInfo, len(p.SampleType)) + for i, st := range p.SampleType { + sampleTypes[i] = SampleTypeInfo{Type: st.Type, Unit: st.Unit} + } + + type srcKey struct { + funcName string + fileName string + line int64 + column int64 + inlined bool + } + srcIndex := map[srcKey]int{} + seenFunctions := map[string]bool{} + unknownIndex := 1 + + out := &FlameResult{ + Path: absPath, + Type: inferType(absPath), + SampleType: SampleTypeInfo{Type: p.SampleType[idx].Type, Unit: p.SampleType[idx].Unit}, + SampleTypes: sampleTypes, + Unit: p.SampleType[idx].Unit, + Scale: 1, + Stacks: []FlameStack{}, + Sources: []FlameSource{{ + FullName: "root", + Display: []string{"root"}, + Places: []FlamePlace{}, + }}, + } + + getSrc := func(line profile.Line, inlined bool) int { + fn := line.Function + if fn == nil { + fn = &profile.Function{Name: fmt.Sprintf("?%d?", unknownIndex)} + unknownIndex++ + } + + k := srcKey{fn.Name, fn.Filename, line.Line, line.Column, inlined} + if i, ok := srcIndex[k]; ok { + return i + } + + fileName := trimPathForFlame(fn.Filename, trimPathOpt, sourcePath) + fs := FlameSource{ + FileName: fileName, + Inlined: inlined, + Places: []FlamePlace{}, + } + if fn.Name != "" { + fs.FullName = addLineInfoFlame(fn.Name, line) + fs.Display = shortNameListFlame(fs.FullName) + fs.Color = pickColorFlame(packageNameFlame(fn.Name)) + } else { + fs.FullName = addLineInfoFlame(fileName, line) + fs.Display = fileNameSuffixesFlame(fs.FullName) + fs.Color = pickColorFlame(filepath.Dir(fileName)) + } + + if !seenFunctions[fs.FullName] { + fs.UniqueName = fs.FullName + seenFunctions[fs.FullName] = true + } else { + fs.UniqueName = fmt.Sprint(fs.FullName, "#", fn.ID) + } + + out.Sources = append(out.Sources, fs) + i := len(out.Sources) - 1 + srcIndex[k] = i + return i + } + + var total int64 + for _, sample := range p.Sample { + if idx >= len(sample.Value) { + continue + } + v := sample.Value[idx] + total += v + + st := FlameStack{Value: v, Sources: []int{0}} + for i := len(sample.Location) - 1; i >= 0; i-- { + loc := sample.Location[i] + for j := len(loc.Line) - 1; j >= 0; j-- { + line := loc.Line[j] + inlined := j != len(loc.Line)-1 + st.Sources = append(st.Sources, getSrc(line, inlined)) + } + } + + leaf := st.Sources[len(st.Sources)-1] + out.Sources[leaf].Self += v + out.Stacks = append(out.Stacks, st) + } + + fillPlacesFlame(out) + + out.Total = total + return out, nil +} + +func fillPlacesFlame(s *FlameResult) { + for si, stack := range s.Stacks { + seen := map[int]bool{} + for pos, src := range stack.Sources { + if seen[src] { + continue + } + seen[src] = true + s.Sources[src].Places = append(s.Sources[src].Places, FlamePlace{Stack: si, Pos: pos}) + } + } +} + +func addLineInfoFlame(str string, line profile.Line) string { + if line.Column != 0 { + return fmt.Sprint(str, ":", line.Line, ":", line.Column) + } + if line.Line != 0 { + return fmt.Sprint(str, ":", line.Line) + } + return str +} + +func allSuffixesFlame(name string, re *regexp.Regexp) []string { + seps := re.FindAllStringIndex(name, -1) + result := make([]string, 0, len(seps)+1) + result = append(result, name) + for _, sep := range seps { + if sep[1] < len(name) { + result = append(result, name[sep[1]:]) + } + } + return result +} + +func shortNameListFlame(name string) []string { + return allSuffixesFlame(name, flameSepRE) +} + +func fileNameSuffixesFlame(name string) []string { + if name == "" { + return []string{""} + } + return allSuffixesFlame(filepath.ToSlash(filepath.Clean(name)), flameFileSepRE) +} + +var flamePkgRE = regexp.MustCompile(`^((.*/)?[\w\d_]+)(\.|::)([^/]*)$`) + +func packageNameFlame(name string) string { + m := flamePkgRE.FindStringSubmatch(name) + if m == nil { + return "" + } + return m[1] +} + +func pickColorFlame(key string) int { + const numColors = 1048576 + h := sha256.Sum256([]byte(key)) + index := binary.LittleEndian.Uint32(h[:]) + return int(index % numColors) +} + +// trimPathForFlame mirrors google/pprof/internal/report.trimPath (simplified import path). +func trimPathForFlame(path, trimPath, searchPath string) string { + sPath := filepath.ToSlash(path) + if trimPath == "" { + for _, dir := range filepath.SplitList(searchPath) { + want := "/" + filepath.Base(dir) + "/" + if found := strings.Index(sPath, want); found != -1 { + return path[found+len(want):] + } + } + } + trimPaths := append(filepath.SplitList(filepath.ToSlash(trimPath)), "/proc/self/cwd/./", "/proc/self/cwd/") + for _, tp := range trimPaths { + if !strings.HasSuffix(tp, "/") { + tp += "/" + } + if strings.HasPrefix(sPath, tp) { + return path[len(tp):] + } + } + return path +} diff --git a/internal/biz/flame_layout.go b/internal/biz/flame_layout.go new file mode 100644 index 0000000..cc2aa1c --- /dev/null +++ b/internal/biz/flame_layout.go @@ -0,0 +1,215 @@ +package biz + +import ( + "fmt" + "math" + "sort" + "strconv" + "strings" +) + +// FlameLayoutCell is one rectangle in the merged flame tree: row depth, column among siblings (weight-desc). +type FlameLayoutCell struct { + Name string `json:"name"` + Row int `json:"row"` // 1-based: root row = 1 + Col int `json:"col"` // 1-based among siblings under the same parent (sorted by value desc) + Pct float64 `json:"pct"` // share of profile total (0–100) + Value int64 `json:"value"` // raw aggregated value through this node + Size string `json:"size"` // human-readable amount (e.g. "2.73") + SizeUnit string `json:"size_unit"` // e.g. "MB", "KB", "ns" + SrcIndex int `json:"src_index"` // index into raw /flame sources + FullName string `json:"full_name,omitempty"` +} + +// FlameLayoutResult merges per-sample stacks into one tree and assigns row/col like the Web flame view +// (root top; under each parent, wider/heavier callees to the left → col 1,2,3…). +type FlameLayoutResult struct { + Path string `json:"path"` + Type string `json:"type"` + SampleType SampleTypeInfo `json:"sample_type"` + SampleTypes []SampleTypeInfo `json:"sample_types"` + Total int64 `json:"total"` + Unit string `json:"unit"` + Cells []FlameLayoutCell `json:"cells"` + Rows [][]FlameLayoutCell `json:"rows"` // rows[i] = row (i+1); each slice sorted by col +} + +type flameTrieNode struct { + srcIdx int + val int64 + kids map[int]*flameTrieNode +} + +func newFlameTrie() *flameTrieNode { + return &flameTrieNode{srcIdx: 0, kids: make(map[int]*flameTrieNode)} +} + +func mergeStackIntoTrie(root *flameTrieNode, sources []int, v int64) { + if len(sources) == 0 || v == 0 { + return + } + cur := root + cur.val += v + for i := 1; i < len(sources); i++ { + sid := sources[i] + next, ok := cur.kids[sid] + if !ok { + next = &flameTrieNode{srcIdx: sid, kids: make(map[int]*flameTrieNode)} + cur.kids[sid] = next + } + next.val += v + cur = next + } +} + +func sortedTrieChildren(n *flameTrieNode) []*flameTrieNode { + if len(n.kids) == 0 { + return nil + } + list := make([]*flameTrieNode, 0, len(n.kids)) + for _, ch := range n.kids { + list = append(list, ch) + } + sort.Slice(list, func(i, j int) bool { + if list[i].val != list[j].val { + return list[i].val > list[j].val + } + return list[i].srcIdx < list[j].srcIdx + }) + return list +} + +func layoutTrieDFS(n *flameTrieNode, depth, col int, raw *FlameResult, total int64, out *[]FlameLayoutCell) { + row := depth + 1 + name, full := flameDisplayNames(raw, n.srcIdx) + sz, su := formatFlameSize(n.val, raw.Unit) + p := pctFlame(n.val, total) + *out = append(*out, FlameLayoutCell{ + Name: name, Row: row, Col: col, Pct: p, Value: n.val, + Size: sz, SizeUnit: su, SrcIndex: n.srcIdx, FullName: full, + }) + for i, ch := range sortedTrieChildren(n) { + layoutTrieDFS(ch, depth+1, i+1, raw, total, out) + } +} + +func flameDisplayNames(raw *FlameResult, srcIdx int) (shortName, fullName string) { + if raw == nil || srcIdx < 0 || srcIdx >= len(raw.Sources) { + return "?", "" + } + s := raw.Sources[srcIdx] + fullName = s.FullName + if len(s.Display) > 0 { + shortName = s.Display[len(s.Display)-1] + } else { + shortName = s.FullName + } + if shortName == "" { + shortName = "?" + } + return shortName, fullName +} + +func pctFlame(v, total int64) float64 { + if total == 0 { + return 0 + } + return math.Round(float64(v)/float64(total)*10000) / 100 +} + +func formatFlameSize(v int64, unit string) (amount string, sizeUnit string) { + u := strings.ToLower(strings.TrimSpace(unit)) + switch u { + case "bytes": + const kb int64 = 1024 + const mb = kb * 1024 + const gb = mb * 1024 + switch { + case v >= gb: + return fmt.Sprintf("%.2f", float64(v)/float64(gb)), "GB" + case v >= mb: + return fmt.Sprintf("%.2f", float64(v)/float64(mb)), "MB" + case v >= kb: + return fmt.Sprintf("%.2f", float64(v)/float64(kb)), "KB" + default: + return strconv.FormatInt(v, 10), "B" + } + case "nanoseconds": + switch { + case v >= 1e9: + return fmt.Sprintf("%.2f", float64(v)/1e9), "s" + case v >= 1e6: + return fmt.Sprintf("%.2f", float64(v)/1e6), "ms" + case v >= 1e3: + return fmt.Sprintf("%.2f", float64(v)/1e3), "µs" + default: + return strconv.FormatInt(v, 10), "ns" + } + default: + return strconv.FormatInt(v, 10), unit + } +} + +func groupCellsByRow(cells []FlameLayoutCell) [][]FlameLayoutCell { + maxRow := 0 + for _, c := range cells { + if c.Row > maxRow { + maxRow = c.Row + } + } + if maxRow == 0 { + return nil + } + rows := make([][]FlameLayoutCell, maxRow) + for _, c := range cells { + i := c.Row - 1 + rows[i] = append(rows[i], c) + } + for i := range rows { + sort.Slice(rows[i], func(a, b int) bool { + if rows[i][a].Col != rows[i][b].Col { + return rows[i][a].Col < rows[i][b].Col + } + return rows[i][a].Name < rows[i][b].Name + }) + } + return rows +} + +// AnalyzeFlameLayout parses the profile once, merges stacks into a trie, emits cells with row/col + formatted size. +func AnalyzeFlameLayout(absPath string, sampleIdx int, trimPathOpt, sourcePath string) (*FlameLayoutResult, error) { + raw, err := AnalyzeFlame(absPath, sampleIdx, trimPathOpt, sourcePath) + if err != nil { + return nil, err + } + + root := newFlameTrie() + for _, st := range raw.Stacks { + mergeStackIntoTrie(root, st.Sources, st.Value) + } + + var cells []FlameLayoutCell + layoutTrieDFS(root, 0, 1, raw, raw.Total, &cells) + + sort.Slice(cells, func(i, j int) bool { + if cells[i].Row != cells[j].Row { + return cells[i].Row < cells[j].Row + } + if cells[i].Col != cells[j].Col { + return cells[i].Col < cells[j].Col + } + return cells[i].Name < cells[j].Name + }) + + out := &FlameLayoutResult{ + Path: raw.Path, + Type: raw.Type, + SampleType: raw.SampleType, + SampleTypes: raw.SampleTypes, + Total: raw.Total, + Unit: raw.Unit, + Cells: cells, + Rows: groupCellsByRow(cells), + } + return out, nil +} diff --git a/internal/biz/peek.go b/internal/biz/peek.go new file mode 100644 index 0000000..947237b --- /dev/null +++ b/internal/biz/peek.go @@ -0,0 +1,310 @@ +package biz + +import ( + "fmt" + "os" + "regexp" + "sort" + "strings" + + "github.com/google/pprof/profile" +) + +// PeekEdge is one caller or callee edge (aligned with pprof peek: weight / pct / name + file:line + optional inline). +type PeekEdge struct { + Name string `json:"name"` + File string `json:"file,omitempty"` + Line int64 `json:"line,omitempty"` + Inline bool `json:"inline"` + Weight int64 `json:"weight"` + PctOfCum float64 `json:"pct_of_cum"` +} + +type PeekMatch struct { + Name string `json:"name"` + File string `json:"file,omitempty"` + Line int64 `json:"line,omitempty"` + Flat int64 `json:"flat"` + Cum int64 `json:"cum"` + FlatPct float64 `json:"flat_pct"` + CumPct float64 `json:"cum_pct"` + Callers []PeekEdge `json:"callers"` + Callees []PeekEdge `json:"callees"` +} + +type PeekResult struct { + Path string `json:"path"` + Type string `json:"type"` + SampleType SampleTypeInfo `json:"sample_type"` + SampleTypes []SampleTypeInfo `json:"sample_types"` + Total int64 `json:"total"` + Unit string `json:"unit"` + Filter string `json:"filter"` + Matches []PeekMatch `json:"matches"` +} + +// nodeKey identifies one graph node at line granularity (same idea as graph.NodeInfo for lines mode). +type nodeKey struct { + FuncID uint64 + File string + Line int64 +} + +type edgeKE struct { + From, To nodeKey +} + +type frameNode struct { + Key nodeKey + Name string + LocID uint64 + Ni int // index in Location.Line + NLines int // len(Line) +} + +func AnalyzePeek(absPath, filterPattern string, sampleIdx int) (*PeekResult, error) { + re, err := regexp.Compile(filterPattern) + if err != nil { + return nil, fmt.Errorf("invalid filter regexp: %w", err) + } + + f, err := os.Open(absPath) + if err != nil { + return nil, fmt.Errorf("open profile: %w", err) + } + defer f.Close() + + p, err := profile.Parse(f) + if err != nil { + return nil, fmt.Errorf("parse profile: %w", err) + } + + if len(p.SampleType) == 0 { + return nil, fmt.Errorf("profile has no sample types") + } + + idx := resolveSampleIndex(p, sampleIdx) + if idx < 0 || idx >= len(p.SampleType) { + return nil, fmt.Errorf("sample index %d out of range [0, %d)", sampleIdx, len(p.SampleType)) + } + + flat := make(map[nodeKey]int64) + cum := make(map[nodeKey]int64) + meta := make(map[nodeKey]string) // short function name + edges := make(map[edgeKE]struct { + W int64 + Inline bool + }) + var total int64 + + for _, s := range p.Sample { + if idx >= len(s.Value) { + continue + } + v := s.Value[idx] + total += v + + chain := buildFrameChain(s) + if len(chain) == 0 { + continue + } + + seenN := make(map[nodeKey]bool) + for _, fr := range chain { + if seenN[fr.Key] { + continue + } + seenN[fr.Key] = true + cum[fr.Key] += v + meta[fr.Key] = fr.Name + } + + leaf := chain[len(chain)-1].Key + flat[leaf] += v + + seenE := make(map[edgeKE]bool) + for j := 0; j < len(chain)-1; j++ { + a, b := chain[j], chain[j+1] + ek := edgeKE{From: a.Key, To: b.Key} + if seenE[ek] { + continue + } + seenE[ek] = true + // Mirror graph.newGraph AddToEdgeDiv(..., ni != len(locNodes)-1): callee is b. + inline := b.NLines > 1 && b.Ni != b.NLines-1 + e := edges[ek] + e.W += v + e.Inline = e.Inline || inline + edges[ek] = e + } + } + + allK := make(map[nodeKey]bool) + for k := range flat { + allK[k] = true + } + for k := range cum { + allK[k] = true + } + for k := range allK { + if flat[k] == 0 && cum[k] == 0 { + delete(flat, k) + delete(cum, k) + delete(meta, k) + } + } + + var keys []nodeKey + for k := range meta { + pn := nodePrintableName(k, meta[k]) + if !re.MatchString(pn) && !re.MatchString(meta[k]) { + continue + } + if cum[k] == 0 && flat[k] == 0 { + continue + } + keys = append(keys, k) + } + sort.Slice(keys, func(i, j int) bool { + a := nodePrintableName(keys[i], meta[keys[i]]) + b := nodePrintableName(keys[j], meta[keys[j]]) + return a < b + }) + + if len(keys) == 0 { + return nil, fmt.Errorf("no functions match filter: %s", filterPattern) + } + + sampleTypes := make([]SampleTypeInfo, len(p.SampleType)) + for i, st := range p.SampleType { + sampleTypes[i] = SampleTypeInfo{Type: st.Type, Unit: st.Unit} + } + + out := &PeekResult{ + Path: absPath, + Type: inferType(absPath), + SampleType: SampleTypeInfo{Type: p.SampleType[idx].Type, Unit: p.SampleType[idx].Unit}, + SampleTypes: sampleTypes, + Total: total, + Unit: p.SampleType[idx].Unit, + Filter: filterPattern, + Matches: nil, + } + + for _, nk := range keys { + nodeCum := cum[nk] + var callers []PeekEdge + for ek, ew := range edges { + if ek.To != nk || ew.W == 0 { + continue + } + callers = append(callers, peekEdgeFrom(ek.From, meta, ew.W, nodeCum, ew.Inline)) + } + sort.Slice(callers, func(i, j int) bool { + if callers[i].Weight != callers[j].Weight { + return callers[i].Weight > callers[j].Weight + } + return peekEdgeLess(callers[i], callers[j]) + }) + + var callees []PeekEdge + for ek, ew := range edges { + if ek.From != nk || ew.W == 0 { + continue + } + callees = append(callees, peekEdgeFrom(ek.To, meta, ew.W, nodeCum, ew.Inline)) + } + sort.Slice(callees, func(i, j int) bool { + if callees[i].Weight != callees[j].Weight { + return callees[i].Weight > callees[j].Weight + } + return peekEdgeLess(callees[i], callees[j]) + }) + + fl, cm := flat[nk], cum[nk] + nm := meta[nk] + out.Matches = append(out.Matches, PeekMatch{ + Name: nm, + File: nk.File, + Line: nk.Line, + Flat: fl, + Cum: cm, + FlatPct: roundPct(pct(fl, total)), + CumPct: roundPct(pct(cm, total)), + Callers: callers, + Callees: callees, + }) + } + + return out, nil +} + +func peekEdgeFrom(k nodeKey, meta map[nodeKey]string, w, nodeCum int64, inline bool) PeekEdge { + nm := meta[k] + return PeekEdge{ + Name: nm, + File: k.File, + Line: k.Line, + Inline: inline, + Weight: w, + PctOfCum: roundPct(pct(w, nodeCum)), + } +} + +func peekEdgeLess(a, b PeekEdge) bool { + if a.Name != b.Name { + return a.Name < b.Name + } + if a.File != b.File { + return a.File < b.File + } + return a.Line < b.Line +} + +// nodePrintableName follows google/pprof/internal/graph.NodeInfo.NameComponents for line granularity (no PC hex). +func nodePrintableName(k nodeKey, funcName string) string { + var parts []string + if funcName != "" { + parts = append(parts, funcName) + } + switch { + case k.Line != 0 && k.File != "": + s := fmt.Sprintf("%s:%d", k.File, k.Line) + parts = append(parts, s) + case k.File != "": + parts = append(parts, k.File) + case funcName != "": + default: + parts = append(parts, "") + } + return strings.Join(parts, " ") +} + +// buildFrameChain mirrors google/pprof/internal/graph.newGraph stack expansion order. +func buildFrameChain(s *profile.Sample) []frameNode { + var chain []frameNode + for i := len(s.Location) - 1; i >= 0; i-- { + loc := s.Location[i] + lines := loc.Line + if len(lines) == 0 { + continue + } + nLines := len(lines) + for ni := len(lines) - 1; ni >= 0; ni-- { + ln := lines[ni] + if ln.Function == nil { + continue + } + fn := ln.Function + key := nodeKey{FuncID: fn.ID, File: fn.Filename, Line: ln.Line} + chain = append(chain, frameNode{ + Key: key, + Name: fn.Name, + LocID: loc.ID, + Ni: ni, + NLines: nLines, + }) + } + } + return chain +} diff --git a/internal/biz/repo.go b/internal/biz/repo.go index da5cf8a..6339574 100644 --- a/internal/biz/repo.go +++ b/internal/biz/repo.go @@ -2,10 +2,11 @@ package biz import "github.com/go-kratos/kratos/v2/log" -// FileInfo holds name and size for a file in the storage directory. +// FileInfo holds name, size and detected type for a file in the storage directory. type FileInfo struct { Name string Size int64 + Type string // "pprof" | "trace" | "txt" | "unknown" } // BatchGroup holds files from one collection batch under a date directory. @@ -22,7 +23,9 @@ type Repo interface { GetFileList(date string) (list []string, files []FileInfo, err error) GetBatchGroups(date string) ([]BatchGroup, error) GetFileType(dir string) string + PreAllocDir() (absDir string, err error) CreateFile(url string, contentType string, data []byte) (relativePath string, err error) + CreateFileInDir(absDir, url, contentType string, data []byte) (relativePath string, err error) CreateFileByUpload(fileName string, data []byte) (relativePath string, err error) } diff --git a/internal/biz/source.go b/internal/biz/source.go new file mode 100644 index 0000000..b48131b --- /dev/null +++ b/internal/biz/source.go @@ -0,0 +1,365 @@ +package biz + +import ( + "bufio" + "fmt" + "os" + "path/filepath" + "regexp" + "sort" + "strings" + + "github.com/google/pprof/profile" +) + +// lineStatKey identifies one profiling node at source line granularity (similar to graph.Node). +type lineStatKey struct { + File string + Line int64 + Name string +} + +type SourceLine struct { + Line int64 `json:"line"` + Text string `json:"text"` + Flat int64 `json:"flat"` + Cum int64 `json:"cum"` + FlatPct float64 `json:"flat_pct"` + CumPct float64 `json:"cum_pct"` +} + +type SourceFuncBlock struct { + Name string `json:"name"` + FlatSum int64 `json:"flat_sum"` + CumSum int64 `json:"cum_sum"` + Lines []SourceLine `json:"lines"` +} + +type SourceFileBlock struct { + Path string `json:"path"` + Error string `json:"error,omitempty"` + Functions []SourceFuncBlock `json:"functions"` +} + +type SourceResult struct { + Path string `json:"path"` + Type string `json:"type"` + SampleType SampleTypeInfo `json:"sample_type"` + SampleTypes []SampleTypeInfo `json:"sample_types"` + Total int64 `json:"total"` + Unit string `json:"unit"` + Filter string `json:"filter"` + Truncated bool `json:"truncated,omitempty"` + MaxFiles int `json:"max_files,omitempty"` + Files []SourceFileBlock `json:"files"` +} + +// DefaultSourceMaxFiles aligns with google/pprof internal/driver maxEntries for weblist (50). +const DefaultSourceMaxFiles = 50 + +func AnalyzeSource(absPath, filterPattern string, sampleIdx, margin int, extraSearchPath string, maxFiles int) (*SourceResult, error) { + re, err := regexp.Compile(filterPattern) + if err != nil { + return nil, fmt.Errorf("invalid filter regexp: %w", err) + } + + f, err := os.Open(absPath) + if err != nil { + return nil, fmt.Errorf("open profile: %w", err) + } + defer f.Close() + + p, err := profile.Parse(f) + if err != nil { + return nil, fmt.Errorf("parse profile: %w", err) + } + + if len(p.SampleType) == 0 { + return nil, fmt.Errorf("profile has no sample types") + } + + idx := resolveSampleIndex(p, sampleIdx) + if idx < 0 || idx >= len(p.SampleType) { + return nil, fmt.Errorf("sample index %d out of range [0, %d)", sampleIdx, len(p.SampleType)) + } + + if margin <= 0 { + margin = 5 + } + + searchDirs := sourceSearchDirs(extraSearchPath) + + flatMap, cumMap, total := aggregateLineSamples(p, idx) + + matched := make(map[lineStatKey]bool) + for k := range flatMap { + if re.MatchString(k.Name) { + matched[k] = true + } + } + for k := range cumMap { + if re.MatchString(k.Name) { + matched[k] = true + } + } + if len(matched) == 0 { + return nil, fmt.Errorf("no functions match filter: %s", filterPattern) + } + + byFileThenName := make(map[string]map[string][]lineStatKey) + for k := range matched { + fm := k.File + if fm == "" { + continue + } + if byFileThenName[fm] == nil { + byFileThenName[fm] = make(map[string][]lineStatKey) + } + byFileThenName[fm][k.Name] = append(byFileThenName[fm][k.Name], k) + } + + if len(byFileThenName) == 0 { + return nil, fmt.Errorf("matched samples have no source file paths in profile") + } + + if maxFiles <= 0 { + maxFiles = DefaultSourceMaxFiles + } + + type fileScore struct { + path string + score int64 + } + scores := make([]fileScore, 0, len(byFileThenName)) + for fp, funcs := range byFileThenName { + var s int64 + for _, keys := range funcs { + for _, k := range keys { + s += flatMap[k] + cumMap[k] + } + } + scores = append(scores, fileScore{path: fp, score: s}) + } + sort.Slice(scores, func(i, j int) bool { + if scores[i].score != scores[j].score { + return scores[i].score > scores[j].score + } + return scores[i].path < scores[j].path + }) + truncated := len(scores) > maxFiles + if truncated { + scores = scores[:maxFiles] + } + + filePaths := make([]string, len(scores)) + for i := range scores { + filePaths[i] = scores[i].path + } + + sampleTypes := make([]SampleTypeInfo, len(p.SampleType)) + for i, st := range p.SampleType { + sampleTypes[i] = SampleTypeInfo{Type: st.Type, Unit: st.Unit} + } + + out := SourceResult{ + Path: absPath, + Type: inferType(absPath), + SampleType: SampleTypeInfo{Type: p.SampleType[idx].Type, Unit: p.SampleType[idx].Unit}, + SampleTypes: sampleTypes, + Total: total, + Unit: p.SampleType[idx].Unit, + Filter: filterPattern, + Truncated: truncated, + MaxFiles: maxFiles, + Files: nil, + } + + for _, logicalPath := range filePaths { + funcs := byFileThenName[logicalPath] + names := make([]string, 0, len(funcs)) + for n := range funcs { + names = append(names, n) + } + sort.Strings(names) + + absFile, openErr := openSourceFile(logicalPath, searchDirs) + fileBlock := SourceFileBlock{Path: logicalPath, Functions: nil} + if openErr != nil { + fileBlock.Error = openErr.Error() + out.Files = append(out.Files, fileBlock) + continue + } + + allLines, readErr := readAllLines(absFile) + _ = absFile.Close() + if readErr != nil { + fileBlock.Error = readErr.Error() + out.Files = append(out.Files, fileBlock) + continue + } + + for _, fnName := range names { + keys := funcs[fnName] + var minL, maxL int64 + first := true + for _, k := range keys { + line := k.Line + if line <= 0 { + continue + } + if first { + minL, maxL = line, line + first = false + } else { + if line < minL { + minL = line + } + if line > maxL { + maxL = line + } + } + } + if first { + continue + } + start := minL - int64(margin) + if start < 1 { + start = 1 + } + end := maxL + int64(margin) + + var flatSum, cumSum int64 + var linesOut []SourceLine + for ln := start; ln <= end; ln++ { + k := lineStatKey{File: logicalPath, Line: ln, Name: fnName} + fl := flatMap[k] + cm := cumMap[k] + flatSum += fl + cumSum += cm + txt := "" + if int(ln) < len(allLines) { + txt = allLines[ln] + } + linesOut = append(linesOut, SourceLine{ + Line: ln, + Text: txt, + Flat: fl, + Cum: cm, + FlatPct: roundPct(pct(fl, total)), + CumPct: roundPct(pct(cm, total)), + }) + } + if len(linesOut) == 0 { + continue + } + + fileBlock.Functions = append(fileBlock.Functions, SourceFuncBlock{ + Name: fnName, + FlatSum: flatSum, + CumSum: cumSum, + Lines: linesOut, + }) + } + + if fileBlock.Error != "" || len(fileBlock.Functions) > 0 { + out.Files = append(out.Files, fileBlock) + } + } + + return &out, nil +} + +func aggregateLineSamples(p *profile.Profile, idx int) (flat, cum map[lineStatKey]int64, total int64) { + flat = make(map[lineStatKey]int64) + cum = make(map[lineStatKey]int64) + + for _, s := range p.Sample { + if idx >= len(s.Value) { + continue + } + v := s.Value[idx] + total += v + + if len(s.Location) > 0 && len(s.Location[0].Line) > 0 { + ln := s.Location[0].Line[0] + if ln.Function != nil { + k := lineStatKey{File: ln.Function.Filename, Line: ln.Line, Name: ln.Function.Name} + flat[k] += v + } + } + + seen := make(map[lineStatKey]bool) + for _, loc := range s.Location { + for _, ln := range loc.Line { + if ln.Function == nil { + continue + } + k := lineStatKey{File: ln.Function.Filename, Line: ln.Line, Name: ln.Function.Name} + if seen[k] { + continue + } + seen[k] = true + cum[k] += v + } + } + } + + allK := make(map[lineStatKey]bool) + for k := range flat { + allK[k] = true + } + for k := range cum { + allK[k] = true + } + for k := range allK { + if flat[k] == 0 && cum[k] == 0 { + delete(flat, k) + delete(cum, k) + } + } + return flat, cum, total +} + +func sourceSearchDirs(extra string) []string { + wd, err := os.Getwd() + if err != nil { + wd = "." + } + var dirs []string + if extra != "" { + for _, p := range filepath.SplitList(extra) { + p = strings.TrimSpace(p) + if p != "" { + dirs = append(dirs, p) + } + } + } + dirs = append(dirs, wd) + return dirs +} + +func openSourceFile(logicalPath string, searchDirs []string) (*os.File, error) { + if logicalPath == "" { + return nil, fmt.Errorf("empty source path in profile") + } + if filepath.IsAbs(logicalPath) { + return os.Open(logicalPath) + } + for _, dir := range searchDirs { + full := filepath.Join(dir, logicalPath) + if f, err := os.Open(full); err == nil { + return f, nil + } + } + return nil, fmt.Errorf("source file not found (try source_path=): %s", logicalPath) +} + +func readAllLines(f *os.File) ([]string, error) { + var lines []string + lines = append(lines, "") + sc := bufio.NewScanner(f) + for sc.Scan() { + lines = append(lines, sc.Text()) + } + return lines, sc.Err() +} diff --git a/internal/biz/usecase.go b/internal/biz/usecase.go index bf52fa0..bb73bbb 100644 --- a/internal/biz/usecase.go +++ b/internal/biz/usecase.go @@ -34,3 +34,73 @@ func (uc *Usecase) GetBatchGroups(date string) ([]BatchGroup, error) { func (uc *Usecase) GetFileType(dir string) string { return uc.repo.GetFileType(dir) } + +func (uc *Usecase) AnalyzeTop(enBase64Dir string, n int, sampleIdx int, cumSort bool) (*TopResult, error) { + dir, err := base64.StdEncoding.DecodeString(enBase64Dir) + if err != nil { + return nil, err + } + absPath := uc.repo.GetAbsDir(string(dir)) + result, err := AnalyzeTop(absPath, n, sampleIdx, cumSort) + if err != nil { + return nil, err + } + result.Path = string(dir) + return result, nil +} + +func (uc *Usecase) AnalyzeSource(enBase64Dir, filter string, sampleIdx, margin int, sourcePath string, maxFiles int) (*SourceResult, error) { + dir, err := base64.StdEncoding.DecodeString(enBase64Dir) + if err != nil { + return nil, err + } + absPath := uc.repo.GetAbsDir(string(dir)) + result, err := AnalyzeSource(absPath, filter, sampleIdx, margin, sourcePath, maxFiles) + if err != nil { + return nil, err + } + result.Path = string(dir) + return result, nil +} + +func (uc *Usecase) AnalyzePeek(enBase64Dir, filter string, sampleIdx int) (*PeekResult, error) { + dir, err := base64.StdEncoding.DecodeString(enBase64Dir) + if err != nil { + return nil, err + } + absPath := uc.repo.GetAbsDir(string(dir)) + result, err := AnalyzePeek(absPath, filter, sampleIdx) + if err != nil { + return nil, err + } + result.Path = string(dir) + return result, nil +} + +func (uc *Usecase) AnalyzeFlame(enBase64Dir string, sampleIdx int, trimPath, sourcePath string) (*FlameResult, error) { + dir, err := base64.StdEncoding.DecodeString(enBase64Dir) + if err != nil { + return nil, err + } + absPath := uc.repo.GetAbsDir(string(dir)) + result, err := AnalyzeFlame(absPath, sampleIdx, trimPath, sourcePath) + if err != nil { + return nil, err + } + result.Path = string(dir) + return result, nil +} + +func (uc *Usecase) AnalyzeFlameLayout(enBase64Dir string, sampleIdx int, trimPath, sourcePath string) (*FlameLayoutResult, error) { + dir, err := base64.StdEncoding.DecodeString(enBase64Dir) + if err != nil { + return nil, err + } + absPath := uc.repo.GetAbsDir(string(dir)) + result, err := AnalyzeFlameLayout(absPath, sampleIdx, trimPath, sourcePath) + if err != nil { + return nil, err + } + result.Path = string(dir) + return result, nil +} diff --git a/internal/conf/type.go b/internal/conf/type.go index cecd16d..a8b9b35 100644 --- a/internal/conf/type.go +++ b/internal/conf/type.go @@ -10,12 +10,12 @@ type Server struct { Port2 string `yaml:"Port2"` } type App struct { - Env string `yaml:"Env"` - Log string `yaml:"Log"` - DenyPrivateIP bool `yaml:"DenyPrivateIP"` - SamplingSeconds int `yaml:"SamplingSeconds"` - DeltaSeconds int `yaml:"DeltaSeconds"` - TraceSeconds int `yaml:"TraceSeconds"` + Env string `yaml:"Env"` + Log string `yaml:"Log"` + DenyPrivateIP bool `yaml:"DenyPrivateIP"` + SamplingSeconds int `yaml:"SamplingSeconds"` + DeltaSeconds int `yaml:"DeltaSeconds"` + TraceSeconds int `yaml:"TraceSeconds"` } type PluginsConfig struct { I18nZh bool `yaml:"i18n_zh"` @@ -24,9 +24,17 @@ type PluginsConfig struct { GraphExplain bool `yaml:"graph_explain"` } +// Mcp configures the stdio MCP subprocess (`prof … mcp`). +type Mcp struct { + // APIBase is the full Prof HTTP root used by MCP tools (no trailing slash). + // If empty, defaults to http://127.0.0.1:{Server.Port}. + APIBase string `yaml:"APIBase"` +} + type Bs struct { Server *Server `yaml:"Server"` Data *Data `yaml:"Data"` App *App `yaml:"App"` Plugins *PluginsConfig `yaml:"Plugins"` + Mcp *Mcp `yaml:"Mcp"` } diff --git a/internal/data/file.go b/internal/data/file.go index 7662763..44bc397 100644 --- a/internal/data/file.go +++ b/internal/data/file.go @@ -3,7 +3,6 @@ package data import ( "fmt" "os" - "path" "sort" "strings" @@ -38,19 +37,11 @@ func (f *file) getAbsDir(relPath string) string { return f.dir + relPath } func getFileType(dir string) (fileType string) { - ext := path.Ext(dir) - if ext != "" { - ext = ext[1:] - } - if pproftype.ExtTrace == ext { - return pproftype.ExtTrace - } else if pproftype.ExtTxt == ext { - return pproftype.ExtTxt - } else if pproftype.ExtPprof == ext { - return pproftype.ExtPprof - } else { + sniffed, err := checkMimeByDir(dir) + if err != nil { return pproftype.ExtUnknown } + return sniffed } func (f *file) getFileList(date string) (list []string, files []biz.FileInfo, err error) { final := f.getAbsDir(date) @@ -67,7 +58,12 @@ func (f *file) getFileList(date string) (list []string, files []biz.FileInfo, er if fi.IsDir() { list = append(list, fi.Name()) } else { - files = append(files, biz.FileInfo{Name: fi.Name(), Size: fi.Size()}) + absFile := final + string(os.PathSeparator) + fi.Name() + files = append(files, biz.FileInfo{ + Name: fi.Name(), + Size: fi.Size(), + Type: getFileType(absFile), + }) } } sort.Sort(sort.Reverse(sort.StringSlice(list))) diff --git a/internal/data/repo.go b/internal/data/repo.go index e59db79..5d975fd 100644 --- a/internal/data/repo.go +++ b/internal/data/repo.go @@ -66,15 +66,29 @@ func (rp *repo) GetFileList(date string) (list []string, files []biz.FileInfo, e return rp.data.file.getFileList(date) } func (rp *repo) GetFileType(dir string) string { - return getFileType(dir) + return getFileType(rp.data.file.getAbsDir(dir)) +} + +func (rp *repo) PreAllocDir() (string, error) { + return rp.data.file.createDir() +} + +func (rp *repo) CreateFileInDir(absDir, uri, contentType string, data []byte) (RelativePath string, err error) { + if _, err = checkMimeByData(data, contentType); err != nil { + return + } + filename, err := rp.data.file.getFileName(uri) + if err != nil { + return + } + return rp.data.file.createFile(filename, absDir, data) } func (rp *repo) CreateFile(uri, contentType string, data []byte) (RelativePath string, err error) { - var ext string - if ext, err = checkMimeByData(data, contentType); err != nil { + if _, err = checkMimeByData(data, contentType); err != nil { return } - filename, err := rp.data.file.getFileName(uri, ext) + filename, err := rp.data.file.getFileName(uri) if err != nil { return } @@ -85,17 +99,12 @@ func (rp *repo) CreateFile(uri, contentType string, data []byte) (RelativePath s return rp.data.file.createFile(filename, dir, data) } func (rp *repo) CreateFileByUpload(fileName string, data []byte) (relativePath string, err error) { - var ext string - if ext, err = checkMimeByData(data, ""); err != nil { - return - } - filename, err := rp.data.file.getFileName2(fileName, ext) - if err != nil { + if _, err = checkMimeByData(data, ""); err != nil { return } dir, err := rp.data.file.createDir() if err != nil { return } - return rp.data.file.createFile(filename, dir, data) + return rp.data.file.createFile(fileName, dir, data) } diff --git a/internal/data/storage.go b/internal/data/storage.go index 1bc08e9..b45605f 100644 --- a/internal/data/storage.go +++ b/internal/data/storage.go @@ -3,15 +3,17 @@ package data import ( "errors" "fmt" - "github.com/gabriel-vasile/mimetype" - "github.com/matteo-gz/prof/pkg/filex" - "github.com/matteo-gz/prof/pkg/pproftype" "net/url" "os" "path" "path/filepath" + "sort" "strings" "time" + + "github.com/gabriel-vasile/mimetype" + "github.com/matteo-gz/prof/pkg/filex" + "github.com/matteo-gz/prof/pkg/pproftype" ) const ( @@ -20,17 +22,25 @@ const ( mimeTxt = "text/plain" ) -func (f *file) getFileName(uri, ext string) (filename string, err error) { +func (f *file) getFileName(uri string) (filename string, err error) { u2, err := url.Parse(uri) if err != nil { return } filename = strings.ReplaceAll(path.Base(u2.Path), "/", "_") - filename += "." + ext - return -} -func (f *file) getFileName2(name, ext string) (filename string, err error) { - filename = name + "." + ext + q := u2.Query() + keys := make([]string, 0, len(q)) + for k := range q { + keys = append(keys, k) + } + sort.Strings(keys) + for _, k := range keys { + v := q.Get(k) + if v == "" || v == "0" { + continue + } + filename += "_" + k + "_" + v + } return } @@ -89,7 +99,7 @@ func checkMimeByDir(dir string) (string, error) { func (f *file) dirName() string { t := time.Now() dateStr := t.Format("20060102") - id := fmt.Sprintf("%d_%d_%d", t.Hour(), t.Minute(), t.Second()) + id := fmt.Sprintf("%02d_%02d_%02d", t.Hour(), t.Minute(), t.Second()) dir := f.getAbsDir(fmt.Sprintf("/%s/%s", dateStr, id)) dir = filepath.Join(filepath.Split(dir)) return dir diff --git a/internal/mcpprof/config.go b/internal/mcpprof/config.go new file mode 100644 index 0000000..080a528 --- /dev/null +++ b/internal/mcpprof/config.go @@ -0,0 +1,42 @@ +package mcpprof + +import ( + "fmt" + "os" + "strings" + + "github.com/matteo-gz/prof/internal/conf" +) + +// ResolveAPIBase returns the Prof HTTP root for MCP tool HTTP calls. +// Order: PROF_API_BASE env → (if -c set) yaml Mcp.APIBase → (if -c set) yaml Server.Port or flagPort → else flagPort only (no yaml read). +func ResolveAPIBase(flagConf, flagPort, flagEnv, flagDir, flagPprofPort, flagLog string) (string, error) { + if v := strings.TrimSpace(os.Getenv("PROF_API_BASE")); v != "" { + return strings.TrimRight(v, "/"), nil + } + if strings.TrimSpace(flagConf) == "" { + port := strings.TrimSpace(flagPort) + if port == "" { + port = "8201" + } + return fmt.Sprintf("http://127.0.0.1:%s", port), nil + } + bs, err := conf.Load(flagConf, flagPort, flagEnv, flagDir, flagPprofPort, flagLog) + if err != nil { + return "", err + } + if bs.Mcp != nil && strings.TrimSpace(bs.Mcp.APIBase) != "" { + return strings.TrimRight(strings.TrimSpace(bs.Mcp.APIBase), "/"), nil + } + port := "" + if bs.Server != nil { + port = strings.TrimSpace(bs.Server.Port) + } + if port == "" { + port = strings.TrimSpace(flagPort) + } + if port == "" { + port = "8201" + } + return fmt.Sprintf("http://127.0.0.1:%s", port), nil +} diff --git a/internal/mcpprof/server.go b/internal/mcpprof/server.go new file mode 100644 index 0000000..dea5b0b --- /dev/null +++ b/internal/mcpprof/server.go @@ -0,0 +1,274 @@ +// Package mcpprof registers Prof JSON APIs as MCP tools (stdio only). +package mcpprof + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + "log" + "net" + "net/http" + "net/url" + "strings" + "syscall" + "time" + + "github.com/modelcontextprotocol/go-sdk/mcp" +) + +var httpClient = &http.Client{Timeout: 3 * time.Minute} + +// RunStdio runs MCP over stdin/stdout against the given Prof HTTP root (no trailing slash). +func RunStdio(apiBase string) { + apiBase = strings.TrimRight(strings.TrimSpace(apiBase), "/") + if apiBase == "" { + apiBase = "http://127.0.0.1:8201" + } + warnIfProfAPIUnreachable(apiBase) + srv := mcp.NewServer(&mcp.Implementation{Name: "prof", Version: "1.0.0"}, nil) + RegisterTools(srv, apiBase) + if err := srv.Run(context.Background(), &mcp.StdioTransport{}); err != nil { + log.Fatal(err) + } +} + +// warnIfProfAPIUnreachable logs to stderr (never stdout) so stdio MCP is not corrupted. +func warnIfProfAPIUnreachable(apiBase string) { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + u := apiBase + "/api/" + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u, nil) + if err != nil { + return + } + req.Header.Set("Accept", "application/json") + c := &http.Client{Timeout: 2 * time.Second} + resp, err := c.Do(req) + if err != nil { + log.Printf("mcp: [警告] 当前无法连上 Prof Web/API(%v)。Cursor 调用工具将失败,请先启动主服务;API 基址: %s", err, apiBase) + return + } + defer resp.Body.Close() + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + log.Printf("mcp: [警告] Prof GET /api/ 返回 HTTP %d,请检查主服务是否正常;API 基址: %s", resp.StatusCode, apiBase) + } +} + +// RegisterTools attaches all Prof proxy tools to the MCP server. +func RegisterTools(s *mcp.Server, base string) { + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_api_index", + Description: "GET /api/ — list all JSON endpoints and parameter docs from the running Prof server", + }, func(ctx context.Context, _ *mcp.CallToolRequest, _ struct{}) (*mcp.CallToolResult, map[string]any, error) { + return profGET(ctx, base, base+"/api/") + }) + + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_api_plugins", + Description: "GET /api/plugins — list enabled JS plugins", + }, func(ctx context.Context, _ *mcp.CallToolRequest, _ struct{}) (*mcp.CallToolResult, map[string]any, error) { + return profGET(ctx, base, base+"/api/plugins") + }) + + type topIn struct { + Dir string `json:"dir" jsonschema:"base64-encoded profile relative path (same as /pprof/:dir/ URL segment)"` + N *int `json:"n,omitempty" jsonschema:"top N functions (server default 20 if omitted)"` + Si *int `json:"si,omitempty" jsonschema:"sample type index (server default -1 = last if omitted)"` + Cum bool `json:"cum,omitempty" jsonschema:"if true, sort by cum instead of flat"` + } + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_pprof_top", + Description: "GET /api/pprof/:dir/top — top hot functions (flat/cum)", + }, func(ctx context.Context, _ *mcp.CallToolRequest, in topIn) (*mcp.CallToolResult, map[string]any, error) { + if in.Dir == "" { + return nil, nil, fmt.Errorf("dir is required (base64 profile path)") + } + q := url.Values{} + if in.N != nil { + q.Set("n", fmt.Sprintf("%d", *in.N)) + } + if in.Si != nil { + q.Set("si", fmt.Sprintf("%d", *in.Si)) + } + if in.Cum { + q.Set("cum", "true") + } + u := fmt.Sprintf("%s/api/pprof/%s/top", base, url.PathEscape(in.Dir)) + if enc := q.Encode(); enc != "" { + u += "?" + enc + } + return profGET(ctx, base, u) + }) + + type sourceIn struct { + Dir string `json:"dir" jsonschema:"base64-encoded profile relative path"` + F string `json:"f,omitempty" jsonschema:"function name regexp; empty matches all"` + Si *int `json:"si,omitempty" jsonschema:"sample type index (server default -1 if omitted)"` + Margin *int `json:"margin,omitempty" jsonschema:"context lines (server default 5 if omitted)"` + MaxFiles *int `json:"max_files,omitempty" jsonschema:"max files (server default 50 if omitted)"` + SourcePath string `json:"source_path,omitempty" jsonschema:"local source search path (PATH separator)"` + } + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_pprof_source", + Description: "GET /api/pprof/:dir/source — annotated source lines with flat/cum", + }, func(ctx context.Context, _ *mcp.CallToolRequest, in sourceIn) (*mcp.CallToolResult, map[string]any, error) { + if in.Dir == "" { + return nil, nil, fmt.Errorf("dir is required") + } + q := url.Values{} + if in.F != "" { + q.Set("f", in.F) + } + if in.Si != nil { + q.Set("si", fmt.Sprintf("%d", *in.Si)) + } + if in.Margin != nil { + q.Set("margin", fmt.Sprintf("%d", *in.Margin)) + } + if in.MaxFiles != nil { + q.Set("max_files", fmt.Sprintf("%d", *in.MaxFiles)) + } + if in.SourcePath != "" { + q.Set("source_path", in.SourcePath) + } + u := fmt.Sprintf("%s/api/pprof/%s/source", base, url.PathEscape(in.Dir)) + if enc := q.Encode(); enc != "" { + u += "?" + enc + } + return profGET(ctx, base, u) + }) + + type peekIn struct { + Dir string `json:"dir" jsonschema:"base64-encoded profile relative path"` + F string `json:"f,omitempty" jsonschema:"function name regexp; empty matches all"` + Si *int `json:"si,omitempty" jsonschema:"sample type index; omit for server default (-1)"` + } + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_pprof_peek", + Description: "GET /api/pprof/:dir/peek — callers/callees per matched node (line granularity)", + }, func(ctx context.Context, _ *mcp.CallToolRequest, in peekIn) (*mcp.CallToolResult, map[string]any, error) { + if in.Dir == "" { + return nil, nil, fmt.Errorf("dir is required") + } + q := url.Values{} + if in.F != "" { + q.Set("f", in.F) + } + if in.Si != nil { + q.Set("si", fmt.Sprintf("%d", *in.Si)) + } + u := fmt.Sprintf("%s/api/pprof/%s/peek", base, url.PathEscape(in.Dir)) + if enc := q.Encode(); enc != "" { + u += "?" + enc + } + return profGET(ctx, base, u) + }) + + type flameIn struct { + Dir string `json:"dir" jsonschema:"base64-encoded profile relative path"` + Si *int `json:"si,omitempty" jsonschema:"sample type index; omit for server default (-1)"` + TrimPath string `json:"trim_path,omitempty" jsonschema:"trim path prefix (pprof -trim_path style)"` + SourcePath string `json:"source_path,omitempty" jsonschema:"source search path for heuristic trim"` + } + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_pprof_flame", + Description: "GET /api/pprof/:dir/flame — raw stacks+sources flamegraph model (one stack per sample)", + }, func(ctx context.Context, _ *mcp.CallToolRequest, in flameIn) (*mcp.CallToolResult, map[string]any, error) { + if in.Dir == "" { + return nil, nil, fmt.Errorf("dir is required") + } + q := url.Values{} + if in.Si != nil { + q.Set("si", fmt.Sprintf("%d", *in.Si)) + } + if in.TrimPath != "" { + q.Set("trim_path", in.TrimPath) + } + if in.SourcePath != "" { + q.Set("source_path", in.SourcePath) + } + u := fmt.Sprintf("%s/api/pprof/%s/flame", base, url.PathEscape(in.Dir)) + if enc := q.Encode(); enc != "" { + u += "?" + enc + } + return profGET(ctx, base, u) + }) + + mcp.AddTool(s, &mcp.Tool{ + Name: "prof_pprof_flame_layout", + Description: "GET /api/pprof/:dir/flame/layout — merged flame tree with row/col/pct/size", + }, func(ctx context.Context, _ *mcp.CallToolRequest, in flameIn) (*mcp.CallToolResult, map[string]any, error) { + if in.Dir == "" { + return nil, nil, fmt.Errorf("dir is required") + } + q := url.Values{} + if in.Si != nil { + q.Set("si", fmt.Sprintf("%d", *in.Si)) + } + if in.TrimPath != "" { + q.Set("trim_path", in.TrimPath) + } + if in.SourcePath != "" { + q.Set("source_path", in.SourcePath) + } + u := fmt.Sprintf("%s/api/pprof/%s/flame/layout", base, url.PathEscape(in.Dir)) + if enc := q.Encode(); enc != "" { + u += "?" + enc + } + return profGET(ctx, base, u) + }) +} + +func profGET(ctx context.Context, apiBase, rawURL string) (*mcp.CallToolResult, map[string]any, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, rawURL, nil) + if err != nil { + return nil, nil, err + } + req.Header.Set("Accept", "application/json") + + resp, err := httpClient.Do(req) + if err != nil { + return nil, nil, explainProfBackendErr(apiBase, rawURL, err) + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, nil, err + } + if resp.StatusCode < 200 || resp.StatusCode >= 300 { + return nil, nil, fmt.Errorf("HTTP %d from %s: %s", resp.StatusCode, rawURL, truncateBody(body, 2048)) + } + var out map[string]any + if err := json.Unmarshal(body, &out); err != nil { + return nil, nil, fmt.Errorf("invalid JSON from %s: %w (body: %s)", rawURL, err, truncateBody(body, 512)) + } + return nil, out, nil +} + +func truncateBody(b []byte, max int) string { + s := string(b) + if len(s) > max { + return s[:max] + "…" + } + return s +} + +// explainProfBackendErr turns low-level dial errors into actionable text for the MCP client / model. +func explainProfBackendErr(apiBase, rawURL string, err error) error { + var netErr net.Error + if errors.As(err, &netErr) && netErr.Timeout() { + return fmt.Errorf("依赖的 Prof Web/API 无响应(超时)。请求: %s。请先在另一终端启动主服务,例如 `./prof -c env.yaml` 或 `./prof -port <端口>`;当前 MCP API 基址: %q", rawURL, apiBase) + } + var opErr *net.OpError + if errors.As(err, &opErr) { + if errors.Is(opErr.Err, syscall.ECONNREFUSED) { + return fmt.Errorf("依赖的 Prof Web/API 未在该端口监听(connection refused)。请求: %s。请先启动 prof 主进程再调用 MCP;API 基址应为 %q(与主服务 HTTP 端口一致)", rawURL, apiBase) + } + } + if strings.Contains(strings.ToLower(err.Error()), "connection refused") { + return fmt.Errorf("依赖的 Prof Web/API 未启动(connection refused)。请求: %s。请先启动主服务;MCP 当前 API 基址: %q", rawURL, apiBase) + } + return fmt.Errorf("无法访问 Prof HTTP API(%w)。请求: %s。请确认主服务已启动且端口与 MCP 一致(API 基址 %q)", err, rawURL, apiBase) +} diff --git a/internal/server/router.go b/internal/server/router.go index f7a6906..6837605 100644 --- a/internal/server/router.go +++ b/internal/server/router.go @@ -57,7 +57,14 @@ func (h *HTTPServerX) router() (r *gin.Engine, err error) { r.POST("/opt/run1", h.srv.Run1) api := r.Group("/api") { + api.GET("/", h.srv.APIIndex) + api.GET("/mcp-hint", h.srv.APIMcpHint) api.GET("/plugins", h.srv.APIPlugins) + api.GET("/pprof/:dir/peek", h.srv.APIPprofPeek) + api.GET("/pprof/:dir/source", h.srv.APIPprofSource) + api.GET("/pprof/:dir/top", h.srv.APIPprofTop) + api.GET("/pprof/:dir/flame", h.srv.APIPprofFlame) + api.GET("/pprof/:dir/flame/layout", h.srv.APIPprofFlameLayout) } return } diff --git a/internal/service/handler.go b/internal/service/handler.go index d79bc09..e0635af 100644 --- a/internal/service/handler.go +++ b/internal/service/handler.go @@ -7,6 +7,7 @@ import ( "net/http" "os" "path/filepath" + "strconv" "strings" "time" @@ -33,6 +34,7 @@ func (s *Service) Index(c *gin.Context) { "SamplingSeconds": s.samplingSeconds, "DeltaSeconds": s.deltaSeconds, "TraceSeconds": s.traceSeconds, + "ProfBin": s.profBin, } if err := s.tmpl.ExecuteTemplate(&buf, "index.html", data); err != nil { c.String(http.StatusInternalServerError, err.Error()) @@ -317,20 +319,315 @@ func (s *Service) APIPlugins(c *gin.Context) { Enabled: s.pluginsConfig.SourceFold, Description: "Source 视图函数折叠", }, - { - Name: "peek-fold", - Enabled: s.pluginsConfig.PeekFold, - Description: "Peek 视图分组折叠", + { + Name: "peek-fold", + Enabled: s.pluginsConfig.PeekFold, + Description: "Peek 视图分组折叠", + }, + { + Name: "graph-explain", + Enabled: s.pluginsConfig.GraphExplain, + Description: "Graph 节点点击解释", + }, }, - { - Name: "graph-explain", - Enabled: s.pluginsConfig.GraphExplain, - Description: "Graph 节点点击解释", + }) +} + +type apiEndpoint struct { + Method string `json:"method"` + Path string `json:"path"` + Desc string `json:"description"` + Params []apiParam `json:"params,omitempty"` + ResponseSummary string `json:"response_summary,omitempty"` + ResponseSchema []apiField `json:"response_schema,omitempty"` + ResponseExample map[string]any `json:"response_example,omitempty"` +} + +type apiParam struct { + Name string `json:"name"` + In string `json:"in"` + Required bool `json:"required"` + Default string `json:"default,omitempty"` + Desc string `json:"description"` +} + +type apiField struct { + Name string `json:"name"` + Type string `json:"type"` + Desc string `json:"description"` +} + +func (s *Service) APIIndex(c *gin.Context) { + c.JSON(http.StatusOK, gin.H{ + "name": "Prof API", + "version": "1.0", + "docs": "https://github.com/matteo-gz/prof", + "endpoints": []apiEndpoint{ + { + Method: "GET", + Path: "/api/", + Desc: "API 索引,返回所有可用接口列表", + ResponseSummary: "返回 API 元信息与 endpoint 列表", + ResponseSchema: []apiField{ + {Name: "name", Type: "string", Desc: "API 名称"}, + {Name: "version", Type: "string", Desc: "API 版本"}, + {Name: "docs", Type: "string", Desc: "项目文档地址"}, + {Name: "endpoints", Type: "array", Desc: "接口列表(每项含 method/path/description/params/response_*)"}, + }, + ResponseExample: map[string]any{ + "name": "Prof API", + "version": "1.0", + "docs": "https://github.com/matteo-gz/prof", + "endpoints": []map[string]any{ + {"method": "GET", "path": "/api/plugins"}, + }, + }, + }, + { + Method: "GET", + Path: "/api/plugins", + Desc: "获取已注册的 JS 插件列表及启用状态", + ResponseSummary: "返回插件名、开关状态与用途说明", + ResponseSchema: []apiField{ + {Name: "plugins", Type: "array", Desc: "插件数组"}, + {Name: "plugins[].name", Type: "string", Desc: "插件名"}, + {Name: "plugins[].enabled", Type: "bool", Desc: "是否启用"}, + {Name: "plugins[].description", Type: "string", Desc: "插件说明"}, + }, + ResponseExample: map[string]any{ + "plugins": []map[string]any{ + {"name": "i18n-zh", "enabled": true, "description": "界面中文汉化"}, + }, + }, + }, + { + Method: "GET", + Path: "/api/pprof/:dir/top", + Desc: "解析 .pprof 文件,返回 Top N 热点函数(flat/cum)", + Params: []apiParam{ + {Name: "dir", In: "path", Required: true, Desc: "profile 相对路径的 base64 编码(与 /pprof/:dir/ 相同)"}, + {Name: "n", In: "query", Default: "20", Desc: "返回前 N 个函数"}, + {Name: "si", In: "query", Default: "-1", Desc: "SampleType 索引,-1 = 最后一个(与 go tool pprof 一致)"}, + {Name: "cum", In: "query", Default: "false", Desc: "true 时按 cum 排序,否则按 flat 排序"}, + }, + ResponseSummary: "返回当前采样类型下的 top 函数列表与占比", + ResponseSchema: []apiField{ + {Name: "path", Type: "string", Desc: "profile 相对路径(base64 解码后)"}, + {Name: "type", Type: "string", Desc: "profile 类型推断值"}, + {Name: "sample_type", Type: "object", Desc: "当前采样类型(type/unit)"}, + {Name: "sample_types", Type: "array", Desc: "全部采样类型"}, + {Name: "total", Type: "int64", Desc: "当前采样类型总值"}, + {Name: "unit", Type: "string", Desc: "单位(如 bytes/count/nanoseconds)"}, + {Name: "top", Type: "array", Desc: "热点函数列表"}, + {Name: "top[].rank/name/flat/cum/flat_pct/sum_pct/cum_pct", Type: "mixed", Desc: "排名、函数名、flat/cum 及百分比"}, + }, + ResponseExample: map[string]any{ + "path": "/20260420/23_34_52/heap", + "sample_type": map[string]any{"type": "inuse_space", "unit": "bytes"}, + "total": 6043872, + "unit": "bytes", + "top": []map[string]any{ + {"rank": 1, "name": "runtime.mallocgc", "flat": 2100281, "flat_pct": 34.75, "sum_pct": 34.75, "cum": 2100281, "cum_pct": 34.75}, + }, + }, + }, + { + Method: "GET", + Path: "/api/pprof/:dir/source", + Desc: "解析 .pprof,按源码行聚合 flat/cum;需 f 正则过滤函数名", + Params: []apiParam{ + {Name: "dir", In: "path", Required: true, Desc: "profile 相对路径 base64"}, + {Name: "f", In: "query", Default: "(空)", Desc: "函数名正则;空字符串与 go tool pprof Web 一致,等价匹配全部符号"}, + {Name: "si", In: "query", Default: "-1", Desc: "SampleType 索引"}, + {Name: "margin", In: "query", Default: "5", Desc: "采样行前后附加展示行数"}, + {Name: "max_files", In: "query", Default: "50", Desc: "最多返回多少个源文件(按采样权重排序),与 pprof weblist 上限一致"}, + {Name: "source_path", In: "query", Default: "", Desc: "源码搜索路径(可多路径,与 PATH 分隔符一致),找不到文件时必填"}, + }, + ResponseSummary: "返回按源码文件/函数/行聚合后的 flat/cum 与源码片段", + ResponseSchema: []apiField{ + {Name: "path/type/sample_type/sample_types/total/unit/filter", Type: "mixed", Desc: "通用元信息与过滤条件"}, + {Name: "files", Type: "array", Desc: "源文件列表,按权重排序"}, + {Name: "files[].path", Type: "string", Desc: "源码文件路径"}, + {Name: "files[].functions", Type: "array", Desc: "文件内函数聚合"}, + {Name: "files[].functions[].name/flat/cum", Type: "mixed", Desc: "函数及统计值"}, + {Name: "files[].functions[].lines", Type: "array", Desc: "命中行及上下文文本"}, + {Name: "truncated", Type: "bool", Desc: "是否因 max_files 截断"}, + }, + ResponseExample: map[string]any{ + "path": "/20260420/23_34_52/heap", + "filter": "mallocgc", + "truncated": false, + "files": []map[string]any{ + {"path": "runtime/malloc.go", "functions": []map[string]any{{"name": "runtime.mallocgc", "flat": 2100281, "cum": 3200000}}}, + }, + }, + }, + { + Method: "GET", + Path: "/api/pprof/:dir/peek", + Desc: "调用关系:对每个匹配正则的函数列出 callers / callees 边权重(相对该节点 cum 的占比)", + Params: []apiParam{ + {Name: "dir", In: "path", Required: true, Desc: "profile 相对路径 base64"}, + {Name: "f", In: "query", Default: "(空)", Desc: "函数名正则;空串匹配全部(结果可能很多)"}, + {Name: "si", In: "query", Default: "-1", Desc: "SampleType 索引"}, + }, + ResponseSummary: "返回匹配节点的 callers/callees 关系与边权重", + ResponseSchema: []apiField{ + {Name: "path/type/sample_type/sample_types/total/unit/filter", Type: "mixed", Desc: "通用元信息与过滤条件"}, + {Name: "matches", Type: "array", Desc: "命中节点列表(行级)"}, + {Name: "matches[].name/file/line", Type: "mixed", Desc: "节点标识"}, + {Name: "matches[].flat/cum/flat_pct/cum_pct", Type: "mixed", Desc: "节点统计"}, + {Name: "matches[].callers|callees", Type: "array", Desc: "入边/出边"}, + {Name: "edge.name/file/line/inline/weight/pct_of_cum", Type: "mixed", Desc: "边目标及占比(分母为该节点 cum)"}, + }, + ResponseExample: map[string]any{ + "filter": "mallocgc", + "matches": []map[string]any{ + { + "name": "runtime.mallocgc", "file": "runtime/malloc.go", "line": 1195, + "flat": 2100281, "cum": 3200000, "flat_pct": 34.75, "cum_pct": 52.94, + "callers": []map[string]any{{"name": "foo.alloc", "weight": 1800000, "pct_of_cum": 56.25}}, + "callees": []map[string]any{{"name": "runtime.nextFreeFast", "weight": 700000, "pct_of_cum": 21.88}}, + }, + }, + }, + }, + { + Method: "GET", + Path: "/api/pprof/:dir/flame", + Desc: "火焰图原始数据:与 pprof Web /flamegraph 相同的 stacks + sources 模型(每样本一条栈,信息最全但体积通常更大)", + Params: []apiParam{ + {Name: "dir", In: "path", Required: true, Desc: "profile 相对路径 base64"}, + {Name: "si", In: "query", Default: "-1", Desc: "SampleType 索引"}, + {Name: "trim_path", In: "query", Default: "", Desc: "裁剪路径前缀(与 go tool pprof -trim_path 一致,多路径用 PATH 分隔符)"}, + {Name: "source_path", In: "query", Default: "", Desc: "源码搜索路径(未设置 trim_path 时用于启发式缩短路径)"}, + }, + ResponseSummary: "返回原始 flamegraph 模型(stacks + sources),每个 sample 一条栈", + ResponseSchema: []apiField{ + {Name: "path/type/sample_type/sample_types/total/unit/scale", Type: "mixed", Desc: "通用元信息"}, + {Name: "stacks", Type: "array", Desc: "样本栈数组(条数约等于 sample 条数)"}, + {Name: "stacks[].value", Type: "int64", Desc: "该样本值"}, + {Name: "stacks[].sources", Type: "array", Desc: "帧索引序列(指向 sources)"}, + {Name: "sources", Type: "array", Desc: "帧字典"}, + {Name: "sources[].full_name/file_name/unique_name/inlined/display/self/color/places", Type: "mixed", Desc: "帧信息、自身值与出现位置"}, + }, + ResponseExample: map[string]any{ + "total": 6043872, + "unit": "bytes", + "stacks": []map[string]any{ + {"value": 524344, "sources": []int{0, 12, 23, 31}}, + }, + "sources": []map[string]any{ + {"full_name": "root", "display": []string{"root"}, "self": 0}, + {"full_name": "http.(*conn).serve:2047", "file_name": "net/http/server.go", "inlined": false}, + }, + }, + }, + { + Method: "GET", + Path: "/api/pprof/:dir/flame/layout", + Desc: "火焰图聚合布局:基于 /flame 合并调用前缀并给出 row/col/pct/size(更适合直接画图,但不保留每条原始 sample 栈)", + Params: []apiParam{ + {Name: "dir", In: "path", Required: true, Desc: "profile 相对路径 base64"}, + {Name: "si", In: "query", Default: "-1", Desc: "SampleType 索引"}, + {Name: "trim_path", In: "query", Default: "", Desc: "同 /flame"}, + {Name: "source_path", In: "query", Default: "", Desc: "同 /flame"}, + }, + ResponseSummary: "返回聚合布局结果(row/col/pct/size),便于直接绘制 flame 视图", + ResponseSchema: []apiField{ + {Name: "path/type/sample_type/sample_types/total/unit", Type: "mixed", Desc: "通用元信息"}, + {Name: "cells", Type: "array", Desc: "扁平节点列表(已排序)"}, + {Name: "cells[].name/row/col/pct/value/size/size_unit/src_index/full_name", Type: "mixed", Desc: "节点展示名、位置、占比、值、可读大小与来源索引"}, + {Name: "rows", Type: "array>", Desc: "按行分组的 cells,rows[0] 对应 row=1(root)"}, + }, + ResponseExample: map[string]any{ + "total": 6043872, + "unit": "bytes", + "cells": []map[string]any{ + {"name": "root", "row": 1, "col": 1, "pct": 100, "value": 6043872, "size": "5.77", "size_unit": "MB", "src_index": 0}, + {"name": "http.(*conn).serve", "row": 2, "col": 1, "pct": 47.40, "value": 2864800, "size": "2.73", "size_unit": "MB", "src_index": 42}, + }, + "rows": []any{ + []map[string]any{{"name": "root", "row": 1, "col": 1}}, + []map[string]any{{"name": "http.(*conn).serve", "row": 2, "col": 1}}, + }, + }, + }, }, - }, }) } +func (s *Service) APIPprofTop(c *gin.Context) { + dir := c.Param("dir") + n, _ := strconv.Atoi(c.DefaultQuery("n", "20")) + si, _ := strconv.Atoi(c.DefaultQuery("si", "-1")) + cumSort := c.Query("cum") == "true" + + result, err := s.uc.AnalyzeTop(dir, n, si, cumSort) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, result) +} + +func (s *Service) APIPprofSource(c *gin.Context) { + f := c.Query("f") + dir := c.Param("dir") + si, _ := strconv.Atoi(c.DefaultQuery("si", "-1")) + margin, _ := strconv.Atoi(c.DefaultQuery("margin", "5")) + maxFiles, _ := strconv.Atoi(c.DefaultQuery("max_files", "50")) + sourcePath := c.Query("source_path") + + result, err := s.uc.AnalyzeSource(dir, f, si, margin, sourcePath, maxFiles) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, result) +} + +func (s *Service) APIPprofPeek(c *gin.Context) { + dir := c.Param("dir") + f := c.Query("f") + si, _ := strconv.Atoi(c.DefaultQuery("si", "-1")) + + result, err := s.uc.AnalyzePeek(dir, f, si) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, result) +} + +func (s *Service) APIPprofFlame(c *gin.Context) { + dir := c.Param("dir") + si, _ := strconv.Atoi(c.DefaultQuery("si", "-1")) + trimPath := c.Query("trim_path") + sourcePath := c.Query("source_path") + + result, err := s.uc.AnalyzeFlame(dir, si, trimPath, sourcePath) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, result) +} + +func (s *Service) APIPprofFlameLayout(c *gin.Context) { + dir := c.Param("dir") + si, _ := strconv.Atoi(c.DefaultQuery("si", "-1")) + trimPath := c.Query("trim_path") + sourcePath := c.Query("source_path") + + result, err := s.uc.AnalyzeFlameLayout(dir, si, trimPath, sourcePath) + if err != nil { + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + c.JSON(http.StatusOK, result) +} + func (s *Service) Run1(c *gin.Context) { uri := c.PostForm("url") relativePath, size, err := s.uc.DealRun1(c.Request.Context(), uri) diff --git a/internal/service/mcp_hint.go b/internal/service/mcp_hint.go new file mode 100644 index 0000000..7627600 --- /dev/null +++ b/internal/service/mcp_hint.go @@ -0,0 +1,54 @@ +package service + +import ( + "os" + "path/filepath" + "strings" + + "github.com/gin-gonic/gin" +) + +func resolveProfExecutable() string { + ex, err := os.Executable() + if err != nil { + return "" + } + if abs, err := filepath.EvalSymlinks(ex); err == nil { + ex = abs + } + return ex +} + +func requestAPIBase(c *gin.Context) string { + scheme := "http" + if c.Request.TLS != nil { + scheme = "https" + } + host := c.Request.Host + if host == "" { + host = "127.0.0.1" + } + return scheme + "://" + strings.TrimSuffix(host, "/") +} + +func listenPortFromRequest(c *gin.Context) string { + host := c.Request.Host + if i := strings.LastIndex(host, ":"); i >= 0 { + return host[i+1:] + } + if c.Request.TLS != nil { + return "443" + } + return "80" +} + +// APIMcpHint returns MCP stdio client hints (running binary path, API base, suggested args). +func (s *Service) APIMcpHint(c *gin.Context) { + port := listenPortFromRequest(c) + c.JSON(200, gin.H{ + "command": s.profBin, + "api_base": requestAPIBase(c), + "port": port, + "args": []string{"-port", port, "mcp"}, + }) +} diff --git a/internal/service/service.go b/internal/service/service.go index 767ed9e..2bd3376 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -21,6 +21,7 @@ type Service struct { traceSeconds int tmpl *template.Template pluginsConfig conf.PluginsConfig + profBin string // os.Executable() at process start, for MCP Cursor config UI } func NewService(c *conf.Bs, uc *biz.Usecase, logger log.Logger) *Service { @@ -55,6 +56,7 @@ func NewService(c *conf.Bs, uc *biz.Usecase, logger log.Logger) *Service { log: log.NewHelper(logger), tmpl: tmpl, pluginsConfig: pluginsCfg, + profBin: resolveProfExecutable(), } } diff --git a/readme.md b/readme.md index f2d0177..b7c0fc9 100644 --- a/readme.md +++ b/readme.md @@ -51,3 +51,9 @@ func main() { ``` ![](./doc/proxy.jpg) + +### MCP (Cursor, local, stdio) + +**With YAML:** `./prof -c env.yaml` then `./prof -c env.yaml mcp` (same file picks `Server.Port` / optional `Mcp.APIBase`). **Without YAML:** `./prof -port 8201` then `./prof mcp` (MCP does not read any config file; same default port). Override base URL with **`PROF_API_BASE`** if needed. See **[docs/mcp.md](./docs/mcp.md)**. + +Chinese notes: `pprof-ai/skills` repo → `docs/references/prof-mcp.md`. diff --git a/web/static/i18n/en.json b/web/static/i18n/en.json index d0befad..7b77f95 100644 --- a/web/static/i18n/en.json +++ b/web/static/i18n/en.json @@ -2,6 +2,7 @@ "nav.home": "Home", "nav.files": "Files", "nav.history": "History", + "nav.useMcp": "Use MCP", "card.fetchOne": "Fetch One", "card.fetchUrl": "Fetch URL", @@ -38,6 +39,22 @@ "modal.curlScript": "Curl Script", + "mcp.title": "Use MCP (Cursor)", + "mcp.intro": "Cursor runs prof mcp as a subprocess to call the same API as this page. Keep this web server running.", + "mcp.apiStatus": "API status", + "mcp.apiOk": "OK", + "mcp.apiDown": "Unreachable", + "mcp.profPath": "prof binary path", + "mcp.profPathPlaceholder": "/path/to/prof", + "mcp.pathHint": "Auto-detected from the running prof binary (os.Executable); you may edit it. Do not paste the JSON below.", + "mcp.pathInvalid": "Not a valid path (looks like JSON was pasted). Clear the field and enter the prof binary path.", + "mcp.configLabel": "~/.cursor/mcp.json snippet", + "mcp.step1": "Cursor → Settings → MCP → Edit in JSON", + "mcp.step2": "Paste the config below (or merge mcpServers.prof)", + "mcp.step3": "Save and reload MCP; ask the model to use prof tools in chat", + "mcp.copyConfig": "Copy config", + "mcp.copyArgs": "Copy args only", + "err.enterUrl": "Please enter a URL", "fetching": "Fetching... ", diff --git a/web/static/i18n/zh.json b/web/static/i18n/zh.json index 2df9a7e..bcb7d1e 100644 --- a/web/static/i18n/zh.json +++ b/web/static/i18n/zh.json @@ -2,6 +2,7 @@ "nav.home": "主页", "nav.files": "文件", "nav.history": "历史", + "nav.useMcp": "Use MCP", "card.fetchOne": "单次采集", "card.fetchUrl": "手动 URL", @@ -38,6 +39,22 @@ "modal.curlScript": "Curl 脚本", + "mcp.title": "Use MCP(Cursor)", + "mcp.intro": "Cursor 通过子进程 prof mcp 调用本页同一 API。请保持本 Web 服务运行。", + "mcp.apiStatus": "API 状态", + "mcp.apiOk": "可达", + "mcp.apiDown": "不可达", + "mcp.profPath": "prof 可执行文件路径", + "mcp.profPathPlaceholder": "/path/to/prof", + "mcp.pathHint": "启动 Web 时会自动检测当前 prof 可执行文件路径(os.Executable);也可手动修改。不要粘贴下方 JSON。", + "mcp.pathInvalid": "当前内容不是有效路径(可能误贴了 JSON)。请清空后只填 prof 可执行文件路径。", + "mcp.configLabel": "~/.cursor/mcp.json 片段", + "mcp.step1": "Cursor → Settings → MCP → Edit in JSON", + "mcp.step2": "粘贴下方配置(或合并 mcpServers.prof)", + "mcp.step3": "保存并重载 MCP;在对话中让模型使用 prof 工具", + "mcp.copyConfig": "复制配置", + "mcp.copyArgs": "仅复制 args", + "err.enterUrl": "请输入 URL", "fetching": "采集中... ", diff --git a/web/static/prof-mcp.js b/web/static/prof-mcp.js new file mode 100644 index 0000000..6878b21 --- /dev/null +++ b/web/static/prof-mcp.js @@ -0,0 +1,308 @@ +(function () { + 'use strict'; + + var BIN_KEY = 'prof:mcp:binary'; + + function t(key) { + return typeof window.t === 'function' ? window.t(key) : key; + } + + function listenPort() { + var p = window.location.port; + if (p) return p; + return window.location.protocol === 'https:' ? '443' : '80'; + } + + function apiBase() { + return window.location.origin; + } + + function isProfPath(s) { + if (!s) return false; + s = s.trim(); + if (s.charAt(0) === '{' || s.charAt(0) === '[') return false; + if (s.indexOf('mcpServers') >= 0 || s.indexOf('"command"') >= 0) return false; + return true; + } + + function serverProfBin() { + var body = document.body && document.body.dataset.profBin; + if (isProfPath(body)) return body.trim(); + return ''; + } + + function profBin() { + var el = document.getElementById('mcpProfBin'); + var v = el && el.value ? el.value.trim() : ''; + if (isProfPath(v)) return v; + var detected = serverProfBin(); + if (detected) return detected; + try { + var saved = localStorage.getItem(BIN_KEY); + if (isProfPath(saved)) return saved; + } catch (e) {} + return '/ABS/PATH/TO/prof'; + } + + function applyMcpHint(data) { + if (!data) return; + var bin = document.getElementById('mcpProfBin'); + if (bin && data.command && isProfPath(data.command)) { + if (!bin.value.trim() || !isProfPath(bin.value.trim())) { + bin.value = data.command; + } + } + } + + function fetchMcpHint(done) { + fetch(apiBase() + '/api/mcp-hint', { headers: { Accept: 'application/json' } }) + .then(function (r) { + if (!r.ok) throw new Error('HTTP ' + r.status); + return r.json(); + }) + .then(function (data) { + applyMcpHint(data); + if (done) done(); + }) + .catch(function () { + if (done) done(); + }); + } + + function setPathInvalid(invalid) { + var bin = document.getElementById('mcpProfBin'); + var hint = document.getElementById('mcpPathHint'); + if (bin) bin.classList.toggle('is-invalid', invalid); + if (hint) hint.style.display = invalid ? 'block' : 'none'; + } + + function buildMcpJSON() { + return JSON.stringify( + { + mcpServers: { + prof: { + command: profBin(), + args: ['-port', listenPort(), 'mcp'], + }, + }, + }, + null, + 2 + ); + } + + function buildArgsJSON() { + return JSON.stringify(['-port', listenPort(), 'mcp'], null, 2); + } + + function applyI18n(root) { + if (!root) return; + root.querySelectorAll('[data-i18n]').forEach(function (el) { + el.textContent = t(el.getAttribute('data-i18n')); + }); + root.querySelectorAll('[data-i18n-placeholder]').forEach(function (el) { + el.placeholder = t(el.getAttribute('data-i18n-placeholder')); + }); + } + + function injectNav() { + var nav = document.querySelector('.navbar-nav'); + if (!nav || document.getElementById('navUseMcp')) return; + var li = document.createElement('li'); + li.className = 'nav-item'; + var a = document.createElement('a'); + a.className = 'nav-link'; + a.href = '#'; + a.id = 'navUseMcp'; + a.setAttribute('data-i18n', 'nav.useMcp'); + a.textContent = t('nav.useMcp'); + li.appendChild(a); + nav.appendChild(li); + } + + function injectModal() { + if (document.getElementById('mcpModal')) return; + var h = []; + h.push(''); + document.body.insertAdjacentHTML('beforeend', h.join('')); + applyI18n(document.getElementById('mcpModal')); + } + + function refreshMcpConfig() { + var bin = document.getElementById('mcpProfBin'); + if (bin) { + var v = bin.value.trim(); + if (v && !isProfPath(v)) { + setPathInvalid(true); + try { + localStorage.removeItem(BIN_KEY); + } catch (e) {} + } else { + setPathInvalid(false); + if (v) { + try { + localStorage.setItem(BIN_KEY, v); + } catch (e) {} + } + } + } + var pre = document.getElementById('mcpConfigContent'); + if (pre) pre.textContent = buildMcpJSON(); + var baseEl = document.getElementById('mcpApiBase'); + if (baseEl) baseEl.textContent = apiBase(); + } + + function checkAPI() { + var badge = document.getElementById('mcpApiBadge'); + if (!badge) return; + badge.className = 'badge bg-secondary'; + badge.textContent = '…'; + fetch(apiBase() + '/api/', { headers: { Accept: 'application/json' } }) + .then(function (r) { + if (r.ok) { + badge.className = 'badge bg-success'; + badge.textContent = t('mcp.apiOk'); + } else { + badge.className = 'badge bg-warning text-dark'; + badge.textContent = 'HTTP ' + r.status; + } + }) + .catch(function () { + badge.className = 'badge bg-danger'; + badge.textContent = t('mcp.apiDown'); + }); + } + + function showMcpModal() { + var modal = document.getElementById('mcpModal'); + if (!modal) return; + modal.style.display = 'block'; + modal.classList.add('show'); + document.body.classList.add('modal-open'); + var backdrop = document.createElement('div'); + backdrop.className = 'modal-backdrop fade show'; + backdrop.id = 'mcpModalBackdrop'; + document.body.appendChild(backdrop); + } + + function hideMcpModal() { + var modal = document.getElementById('mcpModal'); + if (!modal) return; + modal.style.display = 'none'; + modal.classList.remove('show'); + document.body.classList.remove('modal-open'); + var backdrop = document.getElementById('mcpModalBackdrop'); + if (backdrop) backdrop.remove(); + } + + function copyText(text, btn, labelKey) { + var orig = t(labelKey); + var done = function () { + btn.textContent = '✓'; + setTimeout(function () { + btn.textContent = orig; + }, 2000); + }; + if (navigator.clipboard && navigator.clipboard.writeText) { + navigator.clipboard.writeText(text).then(done).catch(function () { + fallbackCopy(text); + done(); + }); + } else { + fallbackCopy(text); + done(); + } + } + + function fallbackCopy(text) { + var ta = document.createElement('textarea'); + ta.value = text; + ta.style.position = 'fixed'; + ta.style.left = '-9999px'; + document.body.appendChild(ta); + ta.select(); + try { + document.execCommand('copy'); + } catch (e) {} + document.body.removeChild(ta); + } + + function openMcpModal() { + fetchMcpHint(function () { + refreshMcpConfig(); + checkAPI(); + showMcpModal(); + }); + } + + function bind() { + injectNav(); + injectModal(); + var nav = document.getElementById('navUseMcp'); + if (nav && !nav.dataset.mcpBound) { + nav.dataset.mcpBound = '1'; + nav.addEventListener('click', function (e) { + e.preventDefault(); + openMcpModal(); + }); + } + var bin = document.getElementById('mcpProfBin'); + if (bin) { + if (!bin.value.trim() && serverProfBin()) bin.value = serverProfBin(); + try { + var saved = localStorage.getItem(BIN_KEY); + if (isProfPath(saved) && !bin.value.trim()) bin.value = saved; + else if (!isProfPath(saved)) localStorage.removeItem(BIN_KEY); + } catch (e) {} + bin.addEventListener('input', refreshMcpConfig); + } + var closeBtn = document.getElementById('mcpModalClose'); + var dismissBtn = document.getElementById('mcpModalDismiss'); + var modal = document.getElementById('mcpModal'); + if (closeBtn) closeBtn.addEventListener('click', hideMcpModal); + if (dismissBtn) dismissBtn.addEventListener('click', hideMcpModal); + if (modal) { + modal.addEventListener('click', function (e) { + if (e.target === modal) hideMcpModal(); + }); + } + var copyCfg = document.getElementById('mcpCopyConfigBtn'); + var copyArgs = document.getElementById('mcpCopyArgsBtn'); + if (copyCfg) { + copyCfg.addEventListener('click', function () { + copyText(buildMcpJSON(), copyCfg, 'mcp.copyConfig'); + }); + } + if (copyArgs) { + copyArgs.addEventListener('click', function () { + copyText(buildArgsJSON(), copyArgs, 'mcp.copyArgs'); + }); + } + } + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', bind); + } else { + bind(); + } +})(); diff --git a/web/template/index.html b/web/template/index.html index 247b8b3..ab3cff0 100644 --- a/web/template/index.html +++ b/web/template/index.html @@ -14,7 +14,7 @@ .result-area { min-height: 40px; margin-top: 12px; } - + @@ -217,6 +218,44 @@ + + +