Skip to content

Commit 35ecd45

Browse files
authored
Merge branch 'master' into fix_before_copy_git
2 parents 0c0ed1d + 9752baa commit 35ecd45

File tree

4 files changed

+102
-18
lines changed

4 files changed

+102
-18
lines changed

src/nimble.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3006,5 +3006,6 @@ when isMainModule:
30063006
exitCode = QuitFailure
30073007
displayError(&"Couldn't save \"{nimbleDataFileName}\".")
30083008
displayDetails(error)
3009-
3009+
when defined(instrument):
3010+
printInstrumentationReport()
30103011
quit(exitCode)

src/nimblepkg/options.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ const noHookActions* = {actionCheck}
296296
const vNextSupportedActions* = { actionInstall, actionBuild,
297297
actionSetup, actionRun, actionLock, actionCustom, actionSync,
298298
actionShellEnv, actionShell, actionUpgrade, actionDoc, actionCompile,
299-
actionDeps
299+
actionDeps, actionAdd
300300
}
301301
proc writeHelp*(quit=true) =
302302
echo(help)

src/nimblepkg/tools.nim

Lines changed: 97 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
#
44
# Various miscellaneous utility functions reside here.
55
import osproc, pegs, strutils, os, uri, sets, json, parseutils, strformat,
6-
sequtils, macros, times, terminal
7-
when defined(instrument):
8-
import std/genasts
6+
sequtils, macros, times
97

108
from net import SslCVerifyMode, newContext, SslContext
119

@@ -225,22 +223,107 @@ proc newSSLContext*(disabled: bool): SslContext =
225223
sslVerifyMode = CVerifyNone
226224
return newContext(verifyMode = sslVerifyMode)
227225

228-
template measureTime*(name: static string, traceStack: bool, body: untyped) =
226+
when defined(instrument):
227+
import std/[tables, terminal, genasts]
228+
229+
type
230+
CallRecord = object
231+
name: string
232+
totalTime: Duration
233+
callCount: int
234+
children: seq[int] # Indices of child records
235+
parent: int # Index of parent record (-1 for root)
236+
237+
var gCallRecords: seq[CallRecord]
238+
var gCallStack: seq[int] # Stack of record indices
239+
var gNameToIndex: Table[string, int] # Map name+parent to record index
240+
var gInstrumentDepth: int
241+
242+
proc getOrCreateRecord(name: string, parentIdx: int): int {.inline.} =
243+
## Gets or creates a call record for the given function and parent
229244
when defined(instrument):
230-
let starts = times.now()
231-
body
232-
let ends = (times.now() - starts)
233-
let dur = ends.inSeconds
245+
let key = name & ":" & $parentIdx
246+
if key in gNameToIndex:
247+
return gNameToIndex[key]
248+
249+
result = gCallRecords.len
250+
gCallRecords.add(CallRecord(
251+
name: name,
252+
totalTime: initDuration(),
253+
callCount: 0,
254+
children: @[],
255+
parent: parentIdx
256+
))
257+
gNameToIndex[key] = result
258+
259+
# Add this record as a child of the parent
260+
if parentIdx >= 0 and parentIdx < gCallRecords.len:
261+
gCallRecords[parentIdx].children.add(result)
262+
263+
proc printCallTree*(idx: int, depth: int = 0) =
264+
## Recursively prints the call tree
265+
when defined(instrument):
266+
if idx < 0 or idx >= gCallRecords.len:
267+
return
268+
269+
let record = gCallRecords[idx]
270+
let indent = " ".repeat(depth)
271+
let dur = record.totalTime.inSeconds
234272
let color =
235273
if dur < 1: fgGreen
236274
elif dur < 2: fgYellow
237275
else: fgRed
238-
stdout.styledWrite(fgDefault, "[")
239-
stdout.styledWrite(color, $(name))
240-
stdout.styledWriteLine(fgDefault, "] took " & $(ends))
241-
if traceStack:
242-
for entry in getStackTraceEntries()[1..^2]: #Skips instrument and self
243-
echo " ", entry.filename, ":", entry.line, " ", entry.procname
276+
277+
stdout.styledWrite(fgDefault, indent & "[")
278+
stdout.styledWrite(color, record.name)
279+
stdout.styledWrite(fgDefault, "] ")
280+
stdout.styledWrite(color, $record.totalTime)
281+
if record.callCount > 1:
282+
stdout.styledWrite(fgMagenta, "" & $record.callCount & ")")
283+
stdout.writeLine("")
284+
285+
# Print children
286+
for childIdx in record.children:
287+
printCallTree(childIdx, depth + 1)
288+
289+
proc printInstrumentationReport*() =
290+
## Prints the accumulated instrumentation data as a hierarchical tree
291+
when defined(instrument):
292+
if gCallRecords.len == 0:
293+
return
294+
295+
echo ""
296+
stdout.styledWriteLine(fgCyan, "=== Instrumentation Report ===")
297+
298+
# Print all root-level calls
299+
for idx, record in gCallRecords:
300+
if record.parent == -1:
301+
printCallTree(idx)
302+
303+
stdout.styledWriteLine(fgCyan, "==============================")
304+
echo ""
305+
306+
template measureTime*(name: static string, traceStack: bool, body: untyped) =
307+
when defined(instrument):
308+
# Get or create the record for this call
309+
let parentIdx = if gCallStack.len > 0: gCallStack[^1] else: -1
310+
let recordIdx = getOrCreateRecord(name, parentIdx)
311+
312+
# Push this call onto the stack
313+
gCallStack.add(recordIdx)
314+
inc gInstrumentDepth
315+
316+
let starts = times.now()
317+
body
318+
let ends = times.now() - starts
319+
320+
# Update the record
321+
gCallRecords[recordIdx].totalTime = gCallRecords[recordIdx].totalTime + ends
322+
gCallRecords[recordIdx].callCount += 1
323+
324+
# Pop from stack
325+
dec gInstrumentDepth
326+
discard gCallStack.pop()
244327
else:
245328
body
246329

src/nimblepkg/vnext.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ proc packageExists(pkgInfo: PackageInfo, options: Options):
621621
return some(oldPkgInfo)
622622

623623

624-
proc installFromDirDownloadInfo(downloadDir: string, url: string, pv: PkgTuple, options: Options): PackageInfo =
624+
proc installFromDirDownloadInfo(downloadDir: string, url: string, pv: PkgTuple, options: Options): PackageInfo {.instrument.} =
625625

626626
let dir = downloadDir
627627
var pkgInfo = getPkgInfo(dir, options)
@@ -922,7 +922,7 @@ proc solutionToFullInfo*(satResult: SATResult, options: var Options) {.instrumen
922922
proc isRoot(pkgInfo: PackageInfo, satResult: SATResult): bool =
923923
pkgInfo.basicInfo.name == satResult.rootPackage.basicInfo.name and pkgInfo.basicInfo.version == satResult.rootPackage.basicInfo.version
924924

925-
proc buildPkg*(pkgToBuild: PackageInfo, isRootInRootDir: bool, options: Options) =
925+
proc buildPkg*(pkgToBuild: PackageInfo, isRootInRootDir: bool, options: Options) {.instrument.} =
926926
# let paths = getPathsToBuildFor(options.satResult, pkgToBuild, recursive = true, options)
927927
let paths = getPathsAllPkgs(options)
928928
# echo "Paths ", paths

0 commit comments

Comments
 (0)