11extension Processor {
2+ #if PROCESSOR_MEASUREMENTS_ENABLED
23 struct ProcessorMetrics {
34 var instructionCounts : [ Instruction . OpCode : Int ] = [ : ]
45 var backtracks : Int = 0
56 var resets : Int = 0
7+ var cycleCount : Int = 0
8+
9+ var isTracingEnabled : Bool = false
10+ var shouldMeasureMetrics : Bool = false
11+
12+ init ( isTracingEnabled: Bool , shouldMeasureMetrics: Bool ) {
13+ self . isTracingEnabled = isTracingEnabled
14+ self . shouldMeasureMetrics = shouldMeasureMetrics
15+ }
616 }
7-
17+ #else
18+ struct ProcessorMetrics {
19+ var isTracingEnabled : Bool { false }
20+ var shouldMeasureMetrics : Bool { false }
21+ var cycleCount : Int { 0 }
22+
23+ init ( isTracingEnabled: Bool , shouldMeasureMetrics: Bool ) { }
24+ }
25+ #endif
26+ }
27+
28+ extension Processor {
29+
30+ mutating func startCycleMetrics( ) {
31+ #if PROCESSOR_MEASUREMENTS_ENABLED
32+ if metrics. cycleCount == 0 {
33+ trace ( )
34+ measureMetrics ( )
35+ }
36+ #endif
37+ }
38+
39+ mutating func endCycleMetrics( ) {
40+ #if PROCESSOR_MEASUREMENTS_ENABLED
41+ metrics. cycleCount += 1
42+ trace ( )
43+ measureMetrics ( )
44+ _checkInvariants ( )
45+ #endif
46+ }
47+ }
48+
49+ extension Processor . ProcessorMetrics {
50+
51+ mutating func addReset( ) {
52+ #if PROCESSOR_MEASUREMENTS_ENABLED
53+ self . resets += 1
54+ #endif
55+ }
56+
57+ mutating func addBacktrack( ) {
58+ #if PROCESSOR_MEASUREMENTS_ENABLED
59+ self . backtracks += 1
60+ #endif
61+ }
62+ }
63+
64+ extension Processor {
65+ #if PROCESSOR_MEASUREMENTS_ENABLED
866 func printMetrics( ) {
967 print ( " === " )
10- print ( " Total cycle count: \( cycleCount) " )
68+ print ( " Total cycle count: \( metrics . cycleCount) " )
1169 print ( " Backtracks: \( metrics. backtracks) " )
1270 print ( " Resets: \( metrics. resets) " )
1371 print ( " Instructions: " )
@@ -21,7 +79,7 @@ extension Processor {
2179 }
2280
2381 mutating func measure( ) {
24- let ( opcode, _) = fetch ( ) . destructure
82+ let ( opcode, _) = fetch ( )
2583 if metrics. instructionCounts. keys. contains ( opcode) {
2684 metrics. instructionCounts [ opcode] ! += 1
2785 } else {
@@ -30,8 +88,9 @@ extension Processor {
3088 }
3189
3290 mutating func measureMetrics( ) {
33- if shouldMeasureMetrics {
91+ if metrics . shouldMeasureMetrics {
3492 measure ( )
3593 }
3694 }
95+ #endif
3796}
0 commit comments