Debian Codegraph @

src/dr/inference/markovchain/Profiler.java

raw symbols SCIP
1 /*
2 * Profiler.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Andrew Rambaut
34 *
35 * @version $Id: Profiler.java,v 1.3 2004/12/16 10:25:01 alexei Exp $
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 //private static String currentProfile = null;
43 private static long startTime;
44
45 public static boolean startProfile() { //String name) {
46 if (profilerAvailable) {
47 //@todo unfortunately I can't get my Ant Build to work with asserts yet. Help!
48 //assert currentProfile == null : "Profile " + currentProfile + " is already recording";
49 //currentProfile = name;
50 startTime = getCurrentThreadCpuTime();
51 }
52 return true;
53 }
54
55 public static boolean stopProfile(String name) {
56 if (profilerAvailable) {
57 long stopTime = getCurrentThreadCpuTime();
58 //@todo unfortunately I can't get my Ant Build to work with asserts yet. Help!
59 //assert name.equals(currentProfile) : "Profile " + name + " is not recording";
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 //currentProfile = null;
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 }