| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.markovchain; |
| 27 |
|
| 28 |
import java.util.HashMap; |
| 29 |
import java.util.Iterator; |
| 30 |
import java.util.Map; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class Profiler { |
| 38 |
|
| 39 |
private static boolean profilerAvailable = true; |
| 40 |
|
| 41 |
private static Map<String, Profile> profiles = new HashMap<String, Profile>(); |
| 42 |
|
| 43 |
private static long startTime; |
| 44 |
|
| 45 |
public static boolean startProfile() { |
| 46 |
if (profilerAvailable) { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
startTime = getCurrentThreadCpuTime(); |
| 51 |
} |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
public static boolean stopProfile(String name) { |
| 56 |
if (profilerAvailable) { |
| 57 |
long stopTime = getCurrentThreadCpuTime(); |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
long count = 1; |
| 62 |
long totalTime = stopTime - startTime; |
| 63 |
Profile profile = profiles.get(name); |
| 64 |
if (profile != null) { |
| 65 |
totalTime += profile.time; |
| 66 |
count += profile.count; |
| 67 |
} |
| 68 |
|
| 69 |
profiles.put(name, new Profile(count, totalTime)); |
| 70 |
|
| 71 |
} |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
public static void report() { |
| 76 |
if (profilerAvailable) { |
| 77 |
Iterator<String> iter = profiles.keySet().iterator(); |
| 78 |
while (iter.hasNext()) { |
| 79 |
String name = iter.next(); |
| 80 |
Profile profile = profiles.get(name); |
| 81 |
long average = profile.time / profile.count; |
| 82 |
System.err.println("PROFILE: " + name + " [" + profile.time + " ms, " + profile.count + " calls, " + average + " ms / call]"); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private static class Profile { |
| 88 |
long time; |
| 89 |
long count; |
| 90 |
|
| 91 |
Profile(long count, long time) { |
| 92 |
this.count = count; |
| 93 |
this.time = time; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
public static native long getCurrentThreadCpuTime(); |
| 98 |
|
| 99 |
static { |
| 100 |
try { |
| 101 |
System.loadLibrary("mcmcprof"); |
| 102 |
} catch (UnsatisfiedLinkError ule) { |
| 103 |
profilerAvailable = false; |
| 104 |
} |
| 105 |
} |
| 106 |
} |