summaryrefslogtreecommitdiff
path: root/internal/tools/profile/analyze.go
blob: 59503b24356516731d244bf62618e46ee5195f2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package profile

import (
	"bufio"
	"flag"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"sort"
	"strings"

	"github.com/mimecast/dtail/internal/tools/common"
)

// Info holds information about a profile file.
type Info struct {
	Path    string
	Tool    string
	Type    string // cpu, mem, alloc
	ModTime string
	Size    int64
}

func runAnalyze(cfg *Config) error {
	args := flag.Args()
	if len(args) == 0 {
		return fmt.Errorf("no profile file specified")
	}

	profilePath := args[0]
	if !common.FileExists(profilePath) {
		return fmt.Errorf("profile file not found: %s", profilePath)
	}

	// Determine if web mode requested
	for _, arg := range args[1:] {
		if arg == "-web" || arg == "--web" {
			return openWebProfile(profilePath)
		}
	}

	// Default to text analysis
	return analyzeProfile(profilePath, args[1:]...)
}

func listProfiles(cfg *Config) error {
	common.PrintSection("Available Profiles")

	profiles, err := findProfiles(cfg.ProfileDir)
	if err != nil {
		return err
	}

	if len(profiles) == 0 {
		fmt.Printf("No profiles found in %s\n", cfg.ProfileDir)
		return nil
	}

	// Group by tool
	byTool := make(map[string][]Info)
	for _, p := range profiles {
		byTool[p.Tool] = append(byTool[p.Tool], p)
	}

	// Sort tools
	var tools []string
	for tool := range byTool {
		tools = append(tools, tool)
	}
	sort.Strings(tools)

	// Display profiles
	for _, tool := range tools {
		fmt.Printf("\n%s profiles:\n", tool)
		toolProfiles := byTool[tool]

		// Sort by modification time (newest first)
		sort.Slice(toolProfiles, func(i, j int) bool {
			return toolProfiles[i].ModTime > toolProfiles[j].ModTime
		})

		for _, p := range toolProfiles {
			fmt.Printf("  %-8s %s  %8s  %s\n",
				p.Type, p.ModTime, common.FormatSize(p.Size), filepath.Base(p.Path))
		}
	}

	fmt.Printf("\nTotal: %d profiles\n", len(profiles))
	fmt.Printf("\nUsage: dtail-tools profile -mode analyze <profile_file>\n")

	return nil
}

func findProfiles(dir string) ([]Info, error) {
	var profiles []Info

	pattern := filepath.Join(dir, "*.prof")
	matches, err := filepath.Glob(pattern)
	if err != nil {
		return nil, err
	}

	for _, path := range matches {
		info, err := os.Stat(path)
		if err != nil {
			continue
		}

		// Parse filename to extract tool and type
		base := filepath.Base(path)
		parts := strings.Split(base, "_")
		if len(parts) < 3 {
			continue
		}

		tool := parts[0]
		profType := parts[1]

		profiles = append(profiles, Info{
			Path:    path,
			Tool:    tool,
			Type:    profType,
			ModTime: info.ModTime().Format("2006-01-02 15:04:05"),
			Size:    info.Size(),
		})
	}

	return profiles, nil
}

func analyzeProfile(profilePath string, args ...string) error {
	// Detect profile type
	isMemProfile := strings.Contains(profilePath, "_mem_") || strings.Contains(profilePath, "_alloc_")

	fmt.Printf("Analyzing %s\n", profilePath)
	fmt.Println(strings.Repeat("-", 60))

	// Default analysis
	if err := showTopFunctions(profilePath, 10, isMemProfile); err != nil {
		return err
	}

	// Show tips
	fmt.Println("\nAnalysis tips:")
	if isMemProfile {
		fmt.Println("  - Use -alloc_space to see total allocations")
		fmt.Println("  - Use -alloc_objects to see allocation counts")
		fmt.Println("  - Use -inuse_space to see current memory usage")
	} else {
		fmt.Println("  - Use -cum to sort by cumulative time")
		fmt.Println("  - Use -list <function> to see source code")
		fmt.Println("  - Use -web to open interactive flame graph")
	}

	return nil
}

func showTopFunctions(profilePath string, count int, isMemProfile bool) error {
	args := []string{"tool", "pprof", "-top", fmt.Sprintf("-nodecount=%d", count)}

	if isMemProfile {
		args = append(args, "-alloc_space")
	}

	args = append(args, profilePath)

	cmd := exec.Command("go", args...)
	output, err := cmd.Output()
	if err != nil {
		return fmt.Errorf("pprof failed: %w", err)
	}

	// Parse and display output
	scanner := bufio.NewScanner(strings.NewReader(string(output)))
	lineCount := 0
	inTop := false

	fmt.Printf("Top %d functions (sorted by flat):\n", count)
	fmt.Println("================================================================")

	for scanner.Scan() {
		line := scanner.Text()

		// Skip header lines
		if strings.HasPrefix(line, "File:") || strings.HasPrefix(line, "Type:") ||
			strings.HasPrefix(line, "Time:") || strings.HasPrefix(line, "Duration:") {
			continue
		}

		// Start printing from the table header
		if strings.Contains(line, "flat") && strings.Contains(line, "cum") {
			inTop = true
			fmt.Println("# Command: go " + strings.Join(args[1:], " "))
		}

		if inTop {
			fmt.Println(line)
			if line != "" {
				lineCount++
			}
			if lineCount > count+2 { // +2 for header and separator
				break
			}
		}
	}

	return nil
}

func openWebProfile(profilePath string) error {
	fmt.Printf("Starting pprof web server for %s...\n", profilePath)
	fmt.Println("Opening http://localhost:8080 in your browser")
	fmt.Println("Press Ctrl+C to stop")

	cmd := exec.Command("go", "tool", "pprof", "-http=:8080", profilePath)
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	return cmd.Run()
}