Skip to content

Add option to print number of instructions executed by swift-format #625

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ let package = Package(
// See the "Dependencies" section below.
],
targets: [
.target(
name: "_InstructionCounter"
),

.target(
name: "SwiftFormat",
dependencies: [
Expand Down Expand Up @@ -108,6 +112,7 @@ let package = Package(
.executableTarget(
name: "swift-format",
dependencies: [
"_InstructionCounter",
"SwiftFormat",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftSyntax", package: "swift-syntax"),
Expand Down
17 changes: 17 additions & 0 deletions Sources/_InstructionCounter/include/InstructionsExecuted.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include <stdint.h>

/// On macOS returns the number of instructions the process has executed since
/// it was launched, on all other platforms returns 0.
uint64_t getInstructionsExecuted();
38 changes: 38 additions & 0 deletions Sources/_InstructionCounter/src/InstructionsExecuted.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#if __APPLE__
#include <TargetConditionals.h>
#if TARGET_OS_MAC && !TARGET_OS_IPHONE
#define TARGET_IS_MACOS 1
#endif
#endif

#include "InstructionsExecuted.h"

#ifdef TARGET_IS_MACOS
#include <libproc.h>
#include <sys/resource.h>
#include <unistd.h>

uint64_t getInstructionsExecuted() {
struct rusage_info_v4 ru;
if (proc_pid_rusage(getpid(), RUSAGE_INFO_V4, (rusage_info_t *)&ru) == 0) {
return ru.ri_instructions;
}
return 0;
}
#else
uint64_t getInstructionsExecuted() {
return 0;
}
#endif
11 changes: 8 additions & 3 deletions Sources/swift-format/Subcommands/Format.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ extension SwiftFormatCommand {
@OptionGroup()
var formatOptions: LintFormatOptions

@OptionGroup(visibility: .hidden)
var performanceMeasurementOptions: PerformanceMeasurementsOptions

func validate() throws {
if inPlace && formatOptions.paths.isEmpty {
throw ValidationError("'--in-place' is only valid when formatting files")
}
}

func run() throws {
let frontend = FormatFrontend(lintFormatOptions: formatOptions, inPlace: inPlace)
frontend.run()
if frontend.diagnosticsEngine.hasErrors { throw ExitCode.failure }
try performanceMeasurementOptions.countingInstructionsIfRequested {
let frontend = FormatFrontend(lintFormatOptions: formatOptions, inPlace: inPlace)
frontend.run()
if frontend.diagnosticsEngine.hasErrors { throw ExitCode.failure }
}
}
}
}
15 changes: 10 additions & 5 deletions Sources/swift-format/Subcommands/Lint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ extension SwiftFormatCommand {
)
var strict: Bool = false

func run() throws {
let frontend = LintFrontend(lintFormatOptions: lintOptions)
frontend.run()
@OptionGroup(visibility: .hidden)
var performanceMeasurementOptions: PerformanceMeasurementsOptions

if frontend.diagnosticsEngine.hasErrors || strict && frontend.diagnosticsEngine.hasWarnings {
throw ExitCode.failure
func run() throws {
try performanceMeasurementOptions.countingInstructionsIfRequested {
let frontend = LintFrontend(lintFormatOptions: lintOptions)
frontend.run()

if frontend.diagnosticsEngine.hasErrors || strict && frontend.diagnosticsEngine.hasWarnings {
throw ExitCode.failure
}
}
}
}
Expand Down
33 changes: 33 additions & 0 deletions Sources/swift-format/Subcommands/PerformanceMeasurement.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import ArgumentParser
import _InstructionCounter

struct PerformanceMeasurementsOptions: ParsableArguments {
@Flag(help: "Measure number of instructions executed by swift-format")
var measureInstructions = false

/// If `measureInstructions` is set, execute `body` and print the number of instructions
/// executed by it. Otherwise, just execute `body`
func printingInstructionCountIfRequested<T>(_ body: () throws -> T) rethrows -> T {
if !measureInstructions {
return try body()
} else {
let startInstructions = getInstructionsExecuted()
defer {
print("Instructions executed: \(getInstructionsExecuted() - startInstructions)")
}
return try body()
}
}
}