Skip to main content

[jpcm~jpcm-commit:b7c1f6c7] Add support for resetting the state of the PMU

  • From:
  • To:
  • Subject: [jpcm~jpcm-commit:b7c1f6c7] Add support for resetting the state of the PMU
  • Date: Sun, 1 Apr 2012 17:33:08 +0000

Project:    jpcm
Repository: jpcm-commit
Revision:   b7c1f6c73d262dc180b9e6867609b039c99cd5cd
Author:     mikeb2701
Date:       2012-04-01 17:32:41 UTC
Link:       

Log Message:
------------
Fixed bug.  Use correct name to construct exception from within darwin native 
code.
Add support for resetting the state of the PMU


Revisions:
----------
0ed346bc69b8a14f792e0fc319cdf3e116d7ed9c
b7c1f6c73d262dc180b9e6867609b039c99cd5cd


Modified Paths:
---------------
jpcm-os-services-darwin/src/main/native/DarwinMsrJni.cpp
jpcm/src/main/java/net/jpcm/Cpu.java
jpcm/src/main/java/net/jpcm/Machine.java
jpcm/src/main/java/net/jpcm/Main.java
jpcm/src/main/java/net/jpcm/OnCoreCounter.java


Diffs:
------
diff --git a/jpcm-os-services-darwin/src/main/native/DarwinMsrJni.cpp 
b/jpcm-os-services-darwin/src/main/native/DarwinMsrJni.cpp
index f6a5d6a..9a794f0 100644
--- a/jpcm-os-services-darwin/src/main/native/DarwinMsrJni.cpp
+++ b/jpcm-os-services-darwin/src/main/native/DarwinMsrJni.cpp
@@ -8,6 +8,8 @@
 #include <fcntl.h>
 #include <IOKit/IOKitlib.h>
 
+const char* MSR_EXCEPTION = "net/jpcm/msr/DarwinMsrException";
+
 typedef struct {
        uint32_t core;
        uint32_t index;
@@ -52,7 +54,7 @@ JNIEXPORT jlong JNICALL 
Java_net_jpcm_msr_DarwinMsrJni_connect
 
        if (!iokit_uc)
        {
-               JNU_ThrowByName(env, "net.jpcm.DarwinMsrException", 
"DirectHW.kext not loaded");
+               JNU_ThrowByName(env, MSR_EXCEPTION, "DirectHW.kext not 
loaded");
                return -1;
        }
 
@@ -62,7 +64,7 @@ JNIEXPORT jlong JNICALL 
Java_net_jpcm_msr_DarwinMsrJni_connect
        /* Should not go further if error with service open */
        if (err != KERN_SUCCESS)
        {
-               JNU_ThrowByName(env, "net.jpcm.DarwinMsrException", "Failed 
to open DirectHW service");
+               JNU_ThrowByName(env, MSR_EXCEPTION, "Failed to open DirectHW 
service");
                return -1;
        }
 
@@ -91,7 +93,7 @@ JNIEXPORT jlong JNICALL Java_net_jpcm_msr_DarwinMsrJni_read
 
        if (err != KERN_SUCCESS)
        {
-               JNU_ThrowByName(env, "net.jpcm.DarwinMsrException", "Failed 
to read MSR");
+               JNU_ThrowByName(env, MSR_EXCEPTION, "Failed to read MSR");
                return -1;
        }
 
@@ -125,7 +127,7 @@ JNIEXPORT void JNICALL 
Java_net_jpcm_msr_DarwinMsrJni_write
 
        if (KERN_SUCCESS != err)
        {
-               JNU_ThrowByName(env, "net.jpcm.DarwinMsrException", "Failed 
to write MSR");
+               JNU_ThrowByName(env, MSR_EXCEPTION, "Failed to write MSR");
        }
 
        return;
diff --git a/jpcm/src/main/java/net/jpcm/Cpu.java 
b/jpcm/src/main/java/net/jpcm/Cpu.java
index 34b6965..dd034a7 100644
--- a/jpcm/src/main/java/net/jpcm/Cpu.java
+++ b/jpcm/src/main/java/net/jpcm/Cpu.java
@@ -49,7 +49,16 @@ public final class Cpu {
     public int getPerfMonVersion() {
         return perfMonVersion;
     }
-    
+
+    public void resetPmu() {
+        disableCounters();
+        Ia32PerfEventSelector field = new Ia32PerfEventSelector();
+        setCounter(field, Register.IA32_PERFEVTSEL0, Register.IA32_PMC0, 
OnCoreCounter.ZERO);
+        setCounter(field, Register.IA32_PERFEVTSEL1, Register.IA32_PMC1, 
OnCoreCounter.ZERO);
+        setCounter(field, Register.IA32_PERFEVTSEL2, Register.IA32_PMC2, 
OnCoreCounter.ZERO);
+        setCounter(field, Register.IA32_PERFEVTSEL3, Register.IA32_PMC3, 
OnCoreCounter.ZERO);
+    }
+
     public void readCounters(long[] counters) {
         if (counters.length != 8) {
             throw new IllegalArgumentException("Should provide an array of 
size 8, supplied: " + counters.length);
diff --git a/jpcm/src/main/java/net/jpcm/Machine.java 
b/jpcm/src/main/java/net/jpcm/Machine.java
index 4d38896..54fb74a 100644
--- a/jpcm/src/main/java/net/jpcm/Machine.java
+++ b/jpcm/src/main/java/net/jpcm/Machine.java
@@ -65,6 +65,12 @@ public final class Machine {
             cpu.setCounters(counter0, counter1, counter2, counter3);
         }
     }
+    
+    public void resetPmu() {
+        for (Cpu cpu : cpus) {
+            cpu.resetPmu();
+        }
+    }
 
     public Socket getSocket(int index) {
         
diff --git a/jpcm/src/main/java/net/jpcm/Main.java 
b/jpcm/src/main/java/net/jpcm/Main.java
index f7a16ec..206f20d 100644
--- a/jpcm/src/main/java/net/jpcm/Main.java
+++ b/jpcm/src/main/java/net/jpcm/Main.java
@@ -5,29 +5,61 @@ import net.jpcm.profile.L2L3CacheProfile;
 
 public class Main {
 
+    private static final int VAL = 200000000;
+
     public static void main(String[] args) {
         
-        Machine machine = Machine.create(ServiceFactory.newInstance());
-        
-        L2L3CacheProfile.enable(machine);
-        
-        L2L3CacheCounters before = L2L3CacheProfile.createCounters(machine);
-        L2L3CacheCounters after = L2L3CacheProfile.createCounters(machine);
-        
-        int[] nums = new int[10000000];
-        
-        before.load();
-        
-        long sum = 0;
-        for (int i = 0; i < 10000000; i++) {
-            sum += nums[i];
+        int[] nums = new int[VAL];        
+        {
+            long t0 = System.nanoTime();
+            long sum = 0;
+            for (int i = 0; i < VAL; i++) {
+                sum += nums[i];
+            }
+            long t1 = System.nanoTime();
+            
+            System.out.printf("Nums: %d%n", sum);
+            System.out.println("Taken: " + (t1-t0));
+        }
+                
+        {
+            Machine machine = Machine.create(ServiceFactory.newInstance());
+                        
+            L2L3CacheProfile.enable(machine);
+            
+            L2L3CacheCounters before = 
L2L3CacheProfile.createCounters(machine);
+            L2L3CacheCounters after = 
L2L3CacheProfile.createCounters(machine);
+            
+            before.load();
+            
+            long t0 = System.nanoTime();
+            long sum = 0;
+            for (int i = 0; i < VAL; i++) {
+                sum += nums[i];
+            }
+            long t1 = System.nanoTime();
+            
+            after.load();
+            
+            System.out.printf("Nums: %d%n", sum);
+            System.out.printf("L2 Hit Ratio: %f%n", 
L2L3CacheCounters.getL2HitRatio(machine.getSocket(0), before, after));
+            System.out.printf("L3 Hit Ratio: %f%n", 
L2L3CacheCounters.getL3HitRatio(machine.getSocket(0), before, after));
+            
+            System.out.println("Taken: " + (t1-t0));
+            
+            machine.resetPmu();
         }
         
-        after.load();
-        
-        System.out.printf("Nums: %d%n", sum);
-        System.out.printf("L2 Hit Ratio: %f%n", 
L2L3CacheCounters.getL2HitRatio(machine.getSocket(0), before, after));
-        System.out.printf("L3 Hit Ratio: %f%n", 
L2L3CacheCounters.getL3HitRatio(machine.getSocket(0), before, after));
-        
+        {
+            long t0 = System.nanoTime();
+            long sum = 0;
+            for (int i = 0; i < VAL; i++) {
+                sum += nums[i];
+            }
+            long t1 = System.nanoTime();
+            
+            System.out.printf("Nums: %d%n", sum);
+            System.out.println("Taken: " + (t1-t0));
+        }
     }
 }
diff --git a/jpcm/src/main/java/net/jpcm/OnCoreCounter.java 
b/jpcm/src/main/java/net/jpcm/OnCoreCounter.java
index 272dda3..5f59527 100644
--- a/jpcm/src/main/java/net/jpcm/OnCoreCounter.java
+++ b/jpcm/src/main/java/net/jpcm/OnCoreCounter.java
@@ -1,6 +1,7 @@
 package net.jpcm;
 
 public enum OnCoreCounter {
+    ZERO(0, 0),
     // Architectural on-core events
     ARCH_LLC_MISS(0x2E, 0x41),
     ARCH_LLC_REFERENCE(0x2E, 0x4F),




[jpcm~jpcm-commit:b7c1f6c7] Add support for resetting the state of the PMU

mikeb2701 04/01/2012
 
 
Close
loading
Please Confirm
Close