mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
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
47 lines
1.3 KiB
Swift
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)")
|
|
}
|
|
}
|