Files
swift-nio/dev/stackdiff/Sources/stackdiff/Stackdiff+Dump.swift
T
George Barnett 4d7097406f Add stackdiff dump (#3289)
Motivation:

Sometimes it's helpful to view the alloc stacks from a program.

Modifications:

- Add a 'dump' subcommand to stackdiff allowing you to dump the stack
diff from heaptrack/dtrace/bpftrace
- You can also filter the stacks so that only certain stacks are
included

Result:

Can dump filtered alloc stacks to stdout
2025-07-08 13:37:25 +00:00

47 lines
1.3 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import ArgumentParser
struct Dump: ParsableCommand {
@Argument
var file: String
@OptionGroup
var options: StackdiffOptions
func run() throws {
let aggregate = try self.options.load(file: self.file)
let allocs = aggregate.netAllocations
// Sort by allocations.
let sortedStacks = aggregate.sorted {
$0.value.netAllocations > $1.value.netAllocations
}
for (stack, weightedSubstacks) in sortedStacks {
for line in StackFormatter.lines(
forStack: stack,
weightedSubstacks: weightedSubstacks,
substackPrefix: " "
) {
print(line)
}
print("")
}
print("TOTAL ALLOCATIONS: \(allocs)")
}
}