Skip to content

Commit

Permalink
Added wakeups interrupt, idle, timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Jan 14, 2024
1 parent 2dc282a commit 1553fe7
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Sources/DLog/Mach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func threadsInfo() -> (cpuUsage: Int32, threadsCount: UInt32) {

defer {
let size = MemoryLayout<thread_t>.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
Expand Down
63 changes: 34 additions & 29 deletions Sources/DLog/TraceProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
}
6 changes: 5 additions & 1 deletion Sources/NetConsole/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -176,3 +178,5 @@ let _ = Service(name: name, debug: debug, autoClear: autoClear)
print(overview)

RunLoop.main.run()

#endif
7 changes: 4 additions & 3 deletions Tests/DLogTests/DLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ final class DLogTests: XCTestCase {
}

// MARK: - Net
#if !os(watchOS)
func test_net() {
let logger = DLog(.net)
XCTAssertNotNil(logger.debug("oslog"))
Expand All @@ -298,6 +298,7 @@ final class DLogTests: XCTestCase {
let log2 = DLog(.net("MyName"))
XCTAssertNotNil(log2.debug("oslog"))
}
#endif

// MARK: - Filter

Expand Down Expand Up @@ -427,7 +428,7 @@ final class DLogTests: XCTestCase {
=> .file("dlog.txt")
=> .oslog
=> .filter { $0.type == .debug }
=> .net)
)

let netLogger = logger["NET"]
netLogger.log("log")
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 3 additions & 1 deletion Tests/DLogTestsObjC/DLogTestsObjC.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 &&
Expand Down

0 comments on commit 1553fe7

Please sign in to comment.