@@ -7,17 +7,55 @@ import type {
77 MetaParams ,
88 MetaResult ,
99 Provider ,
10- ProviderSettings ,
1110} from '@openctx/provider'
1211import { parseGolang } from './parser.js'
13- import { type Node , findReportPath as findPprofSources , getPprof } from './pprof.js'
12+ import { type Node , type TopOptions , findReportPath as findPprofSources , getPprof } from './pprof.js'
13+
14+ interface Settings {
15+ /**
16+ * Glob pattern to match the profile report.
17+ *
18+ * Note, that forward slashes _do not need_ to be escaped in the patterns provided in `settings.json`
19+ *
20+ * @default "**\/*.pprof"
21+ * @example "**\/cmd\/*.pb.gz" (limit to asubdirectory)
22+ */
23+ reportGlob ?: string
24+
25+ /**
26+ * Glob pattern to match the Go binary from which the report was generated.
27+ *
28+ * By default `binaryGlob` not set. The provider will try to locate it by searching
29+ * for an executable file whose name matches that of its parent directory.
30+ * This is what a binary produces by `go build .` would be conventionally named.
31+ */
32+ binaryGlob ?: string
33+
34+ /**
35+ * The provider will not traverse the file tree past the directory containing `rootDirectoryMarkers`,
36+ * when searching for the profile report and the binary.
37+ *
38+ * @default [".git", "go.mod"]
39+ */
40+ rootDirectoryMarkers ?: string [ ]
41+
42+ /**
43+ * Options to control `pprof -top` output.
44+ *
45+ * @default top: { excludeInline: true, sort: 'cum' }
46+ * @example top: { excludeInline: false, sort: 'flat', nodeCount: 10 }
47+ */
48+ top ?: Pick < TopOptions , 'excludeInline' | 'nodeCount' | 'sort' >
49+ }
1450
1551/**
1652 * An [OpenCtx](https://openctx.org) provider that annotates every function declaration with
1753 * the CPU time and memory allocations associated with it.
54+ *
55+ * Only Go files are supported.
1856 */
19- const pprof : Provider = {
20- meta ( params : MetaParams , settings : ProviderSettings ) : MetaResult {
57+ const pprof : Provider < Settings > = {
58+ meta ( params : MetaParams , settings : Settings ) : MetaResult {
2159 return {
2260 name : 'pprof' ,
2361 annotations : {
@@ -26,7 +64,7 @@ const pprof: Provider = {
2664 }
2765 } ,
2866
29- annotations ( params : AnnotationsParams , settings : ProviderSettings ) : AnnotationsResult {
67+ annotations ( params : AnnotationsParams , settings : Settings ) : AnnotationsResult {
3068 // Test files do not need pprof annotations.
3169 if ( params . uri . endsWith ( '_test.go' ) ) {
3270 return [ ]
@@ -40,8 +78,9 @@ const pprof: Provider = {
4078
4179 const searchDir = dirname ( params . uri ) . replace ( / ^ f i l e : \/ { 2 } / , '' )
4280 const sources = findPprofSources ( searchDir , {
43- reportGlob : ( settings . reportGlob as string ) || '**/*.pb.gz' ,
44- rootDirectoryMarkers : settings . rootDirectoryMarkers as string [ ] ,
81+ reportGlob : settings . reportGlob || '**/*.pprof' ,
82+ rootDirectoryMarkers : settings . rootDirectoryMarkers || [ '.git' , 'go.mod' ] ,
83+ binaryGlob : settings . binaryGlob ,
4584 // TODO: pass workspaceRoot once it's made available
4685 // workspaceRoot: workspaceRoot,
4786 } )
@@ -55,7 +94,7 @@ const pprof: Provider = {
5594 return [ ]
5695 }
5796
58- const top = pprof . top ( { package : content . package } )
97+ const top = pprof . top ( { ... settings . top , package : content . package } )
5998 if ( top === null ) {
6099 return [ ]
61100 }
@@ -68,7 +107,9 @@ const pprof: Provider = {
68107 }
69108
70109 let item : Item = {
71- title : `pprof ${ top . type } : ${ node . cum } ${ top . unit } , ${ node . cumPerc } % (#${ i + 1 } , cum)` ,
110+ title : `pprof ${ top . type } : cum ${ node . cum } ${ top . unit } , ${ node . cumPerc } % (#${
111+ i + 1
112+ } , sort=${ settings . top ?. sort || 'cum' } )`,
72113 }
73114
74115 const list = pprof . list ( node . function )
0 commit comments