From 1553fe7c26089a1700ecce5afa4cb64f0012d882 Mon Sep 17 00:00:00 2001 From: Iurii Khvorost Date: Sun, 14 Jan 2024 16:12:25 +0200 Subject: [PATCH] Added wakeups interrupt, idle, timer --- Sources/DLog/Mach.swift | 2 +- Sources/DLog/TraceProcess.swift | 63 ++++++++++++++++------------- Sources/NetConsole/main.swift | 6 ++- Tests/DLogTests/DLogTests.swift | 7 ++-- Tests/DLogTestsObjC/DLogTestsObjC.m | 4 +- 5 files changed, 47 insertions(+), 35 deletions(-) diff --git a/Sources/DLog/Mach.swift b/Sources/DLog/Mach.swift index 0649623..a3db92b 100644 --- a/Sources/DLog/Mach.swift +++ b/Sources/DLog/Mach.swift @@ -71,7 +71,7 @@ func threadsInfo() -> (cpuUsage: Int32, threadsCount: UInt32) { defer { let size = MemoryLayout.size * Int(thread_count) - vm_deallocate(mach_task_self_, vm_address_t(bitPattern: threads), vm_size_t(size)) + vm_deallocate(mach_task_self_, vm_address_t(threads.pointee), vm_size_t(size)) } var cpu_usage: Int32 = 0 diff --git a/Sources/DLog/TraceProcess.swift b/Sources/DLog/TraceProcess.swift index 4626a76..8012f20 100644 --- a/Sources/DLog/TraceProcess.swift +++ b/Sources/DLog/TraceProcess.swift @@ -54,46 +54,51 @@ public struct ProcessOptions: OptionSet { /// Threads count. public static let threads = Self(5) - /// Wakeups per second (WPS). - public static let wps = Self(6) + /// Wakeups info + public static let wakeups = Self(6) /// Compact: `.cpu`, `.memory`, `.pid` and `.threads` public static let compact: Self = [.cpu, .memory, .pid, .threads] } +/// Contains configuration values regarding to a wakeups info. +public struct WakeupsOptions: OptionSet { + /// The corresponding value of the raw type. + public let rawValue: Int + + /// Creates a new option set from the given raw value. + public init(rawValue: Int) { + self.rawValue = rawValue + } + + /// Total number of wakeups generated by the process. + public static let interrupt = Self(0) + + /// The number of times the processor was taken out of its low-power idle state. + public static let idle = Self(1) + + /// Timer wakeups + public static let timer = Self(2) +} + /// Contains configuration values regarding to a process info. public struct ProcessConfig { /// Set which info from the current process should be used. Default value is `ProcessOptions.compact`. public var options: ProcessOptions = .compact + + /// Set which info about wakeups should be used. Default value is `ProcessOptions.all`. + public var wakeupsOptions: WakeupsOptions = .all } -fileprivate class Power { - - static let shared = Power() - - private var timer: Timer? - - private(set) var wakeupsTotalCount: UInt64 = 0 - private(set) var wakeupsPerSecond: UInt64 = 0 - - // mWh - private(set) var energyTotal = 0.0 - private(set) var energyPerSecond = 0.0 - - private init() { - timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { timer in - let wakeups = TaskInfo.power.task_interrupt_wakeups - self.wakeupsPerSecond = wakeups - self.wakeupsTotalCount - self.wakeupsTotalCount = wakeups - - // nJ / 3600 = nWh - let mWh = Double(TaskInfo.power_v2.task_energy) / (3600 * 1000000) - self.energyPerSecond = mWh - self.energyTotal - self.energyTotal = mWh - } - timer?.fire() - } +func wakeupsMetadata(options: WakeupsOptions) -> Metadata { + let power = TaskInfo.power + let items: [(WakeupsOptions, String, () -> Any)] = [ + (.interrupt, "interrupt", { power.task_interrupt_wakeups }), + (.idle, "idle", { power.task_platform_idle_wakeups }), + (.timer, "timer", { power.task_timer_wakeups_bin_1 + power.task_timer_wakeups_bin_2 }) + ] + return Metadata.metadata(from: items, options: options) } func processMetadata(processInfo: ProcessInfo, config: ProcessConfig) -> Metadata { @@ -104,7 +109,7 @@ func processMetadata(processInfo: ProcessInfo, config: ProcessConfig) -> Metadat (.name, "name", { processInfo.processName }), (.pid, "pid", { processInfo.processIdentifier }), (.threads, "threads", { threadsInfo().threadsCount }), - (.wps, "wps", { Power.shared.wakeupsPerSecond }), + (.wakeups, "wakeups", { wakeupsMetadata(options: config.wakeupsOptions) }), ] return Metadata.metadata(from: items, options: config.options) } diff --git a/Sources/NetConsole/main.swift b/Sources/NetConsole/main.swift index bf74179..25f8803 100644 --- a/Sources/NetConsole/main.swift +++ b/Sources/NetConsole/main.swift @@ -27,8 +27,10 @@ import Foundation +#if !os(watchOS) + class Service : NSObject { - + enum ANSIEscapeCode: String { case reset = "\u{001b}[0m" case clear = "\u{001b}c" @@ -176,3 +178,5 @@ let _ = Service(name: name, debug: debug, autoClear: autoClear) print(overview) RunLoop.main.run() + +#endif diff --git a/Tests/DLogTests/DLogTests.swift b/Tests/DLogTests/DLogTests.swift index ceb3841..8a599a4 100644 --- a/Tests/DLogTests/DLogTests.swift +++ b/Tests/DLogTests/DLogTests.swift @@ -274,7 +274,7 @@ final class DLogTests: XCTestCase { } // MARK: - Net - +#if !os(watchOS) func test_net() { let logger = DLog(.net) XCTAssertNotNil(logger.debug("oslog")) @@ -298,6 +298,7 @@ final class DLogTests: XCTestCase { let log2 = DLog(.net("MyName")) XCTAssertNotNil(log2.debug("oslog")) } +#endif // MARK: - Filter @@ -427,7 +428,7 @@ final class DLogTests: XCTestCase { => .file("dlog.txt") => .oslog => .filter { $0.type == .debug } - => .net) + ) let netLogger = logger["NET"] netLogger.log("log") @@ -1224,7 +1225,7 @@ final class TraceTests: XCTestCase { config.traceConfig.processConfig.options = .all let logger = DLog(config: config) - XCTAssert(logger.trace()?.match(#"\{process:\{cpu:\d+%,guid:[^,]+,memory:\d+MB,name:[^,]+,pid:\d+,threads:\d+,wps:\d+\}\}"#) == true) + XCTAssert(logger.trace()?.match(#"\{process:\{cpu:\d+%,guid:[^,]+,memory:\d+MB,name:[^,]+,pid:\d+,threads:\d+,wakeups:\{idle:\d+,interrupt:\d+,timer:\d+\}\}\}"#) == true) } func test_trace_func_only() { diff --git a/Tests/DLogTestsObjC/DLogTestsObjC.m b/Tests/DLogTestsObjC/DLogTestsObjC.m index d5da589..084b50b 100644 --- a/Tests/DLogTestsObjC/DLogTestsObjC.m +++ b/Tests/DLogTestsObjC/DLogTestsObjC.m @@ -221,8 +221,10 @@ - (void)test_AllOutputs { return logItem.type == LogTypeDebug; }], [LogOutput file:@"dlog.txt" append:NO], +#if !TARGET_OS_WATCH [LogOutput net], [LogOutput net:@"dlog"], +#endif ]; for (LogOutput* output in outputs) { @@ -236,7 +238,7 @@ - (void)test_AllOutputs { - (void)test_filter { let filterItem = [LogOutput filterWithItem:^BOOL(LogItem* logItem) { return - [logItem.time compare:NSDate.now] == NSOrderedAscending && + [logItem.time compare:NSDate.date] == NSOrderedAscending && [logItem.category isEqualToString:@"DLOG"] && [logItem.scope.name isEqualToString:@"Scope"] && logItem.type == LogTypeDebug &&