-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmemoryLogger.ts
221 lines (192 loc) · 6.26 KB
/
memoryLogger.ts
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
import * as fs from "fs/promises"
import * as os from "os"
import * as path from "path"
import * as vscode from "vscode"
/**
* A class for tracking memory usage and logging resource lifecycles
* to help identify memory leaks in the extension.
*/
export class MemoryLogger {
private outputChannel: vscode.OutputChannel
private logFile: string | undefined
private resourceCounts = new Map<string, number>()
private startTime: number = Date.now()
private logInterval: NodeJS.Timeout | undefined
private disposed: boolean = false
constructor() {
this.outputChannel = vscode.window.createOutputChannel("Coder Memory Logging")
this.outputChannel.show()
// Setup periodic logging of memory usage
this.startPeriodicLogging()
}
/**
* Start logging memory usage periodically
*/
private startPeriodicLogging(intervalMs = 60000) {
if (this.logInterval) {
clearInterval(this.logInterval)
}
this.logInterval = setInterval(() => {
if (this.disposed) {
return
}
this.logMemoryUsage("PERIODIC")
this.logResourceCounts()
}, intervalMs)
}
/**
* Initialize the log file for persistent logging
*/
public async initLogFile(globalStoragePath: string): Promise<void> {
try {
const logDir = path.join(globalStoragePath, "logs")
await fs.mkdir(logDir, { recursive: true })
this.logFile = path.join(logDir, `memory-log-${new Date().toISOString().replace(/[:.]/g, "-")}.txt`)
await this.writeToLogFile("Memory logging initialized")
this.info("Memory logging initialized to file: " + this.logFile)
// Log initial memory state
this.logMemoryUsage("INIT")
} catch (err) {
this.error(`Failed to initialize log file: ${err}`)
}
}
/**
* Log a new resource creation
*/
public trackResourceCreated(resourceType: string, id: string = ""): void {
const count = (this.resourceCounts.get(resourceType) || 0) + 1
this.resourceCounts.set(resourceType, count)
this.info(`RESOURCE_CREATED: ${resourceType}${id ? ":" + id : ""} (Total: ${count})`)
}
/**
* Log a resource disposal
*/
public trackResourceDisposed(resourceType: string, id: string = ""): void {
const count = Math.max(0, (this.resourceCounts.get(resourceType) || 1) - 1)
if (count === 0) {
this.resourceCounts.delete(resourceType)
} else {
this.resourceCounts.set(resourceType, count)
}
this.info(`RESOURCE_DISPOSED: ${resourceType}${id ? ":" + id : ""} (Remaining: ${count})`)
}
/**
* Log error with memory usage
*/
public error(message: string, error?: unknown): void {
const errorMsg = error ? `: ${error instanceof Error ? error.stack || error.message : String(error)}` : ""
const fullMessage = `[ERROR] ${message}${errorMsg}`
this.outputChannel.appendLine(fullMessage)
this.writeToLogFile(fullMessage)
this.logMemoryUsage("ERROR")
}
/**
* Log info with timestamp
*/
public info(message: string): void {
const fullMessage = `[INFO] ${message}`
this.outputChannel.appendLine(fullMessage)
this.writeToLogFile(fullMessage)
}
/**
* Log debug info (only to file)
*/
public debug(message: string): void {
const fullMessage = `[DEBUG] ${message}`
this.writeToLogFile(fullMessage)
}
/**
* Log current memory usage
*/
public logMemoryUsage(context: string): void {
try {
const memoryUsage = process.memoryUsage()
const nodeMemoryInfo = {
rss: `${(memoryUsage.rss / 1024 / 1024).toFixed(2)}MB`,
heapTotal: `${(memoryUsage.heapTotal / 1024 / 1024).toFixed(2)}MB`,
heapUsed: `${(memoryUsage.heapUsed / 1024 / 1024).toFixed(2)}MB`,
external: `${(memoryUsage.external / 1024 / 1024).toFixed(2)}MB`,
uptime: formatDuration(process.uptime() * 1000),
totalUptime: formatDuration(Date.now() - this.startTime),
}
const systemMemoryInfo = {
totalMem: `${(os.totalmem() / 1024 / 1024 / 1024).toFixed(2)}GB`,
freeMem: `${(os.freemem() / 1024 / 1024 / 1024).toFixed(2)}GB`,
loadAvg: os
.loadavg()
.map((load) => load.toFixed(2))
.join(", "),
}
const memoryLog = `[MEMORY:${context}] Node: ${JSON.stringify(nodeMemoryInfo)} | System: ${JSON.stringify(systemMemoryInfo)}`
this.outputChannel.appendLine(memoryLog)
this.writeToLogFile(memoryLog)
} catch (err) {
this.outputChannel.appendLine(`[ERROR] Failed to log memory usage: ${err}`)
}
}
/**
* Log the current counts of active resources
*/
private logResourceCounts(): void {
const counts = Array.from(this.resourceCounts.entries())
.map(([type, count]) => `${type}=${count}`)
.join(", ")
const message = `[RESOURCES] Active resources: ${counts || "none"}`
this.outputChannel.appendLine(message)
this.writeToLogFile(message)
}
/**
* Write to log file
*/
private async writeToLogFile(message: string): Promise<void> {
if (!this.logFile) {
return
}
try {
const timestamp = new Date().toISOString()
await fs.appendFile(this.logFile, `${timestamp} ${message}\n`)
} catch (err) {
// Don't recursively call this.error to avoid potential loops
this.outputChannel.appendLine(`[ERROR] Failed to write to log file: ${err}`)
}
}
/**
* Show the log in the output channel
*/
public show(): void {
this.outputChannel.show()
}
/**
* Dispose of the logger
*/
public dispose(): void {
this.disposed = true
if (this.logInterval) {
clearInterval(this.logInterval)
this.logInterval = undefined
}
this.logMemoryUsage("DISPOSE")
this.outputChannel.dispose()
}
}
/**
* Format duration in milliseconds to a human-readable string
*/
function formatDuration(ms: number): string {
const seconds = Math.floor((ms / 1000) % 60)
const minutes = Math.floor((ms / (1000 * 60)) % 60)
const hours = Math.floor((ms / (1000 * 60 * 60)) % 24)
const days = Math.floor(ms / (1000 * 60 * 60 * 24))
return `${days}d ${hours}h ${minutes}m ${seconds}s`
}
// Singleton instance
let instance: MemoryLogger | undefined
/**
* Get or initialize the memory logger instance
*/
export function getMemoryLogger(): MemoryLogger {
if (!instance) {
instance = new MemoryLogger()
}
return instance
}