1 /* 2 * Copyright (c) 2001 by Matt Welsh and The Regents of the University of 3 * California. All rights reserved. 4 * 5 * Permission to use, copy, modify, and distribute this software and its 6 * documentation for any purpose, without fee, and without written agreement is 7 * hereby granted, provided that the above copyright notice and the following 8 * two paragraphs appear in all copies of this software. 9 * 10 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR 11 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 12 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF 13 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 14 * 15 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 16 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 17 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 18 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO 19 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 20 * 21 * Author: Matt Welsh <mdw@cs.berkeley.edu> 22 * 23 */ 24 25 26 package seda.sandStorm.internal; 27 28 import seda.sandStorm.api.*; 29 import seda.sandStorm.main.*; 30 import java.io.*; 31 import java.util.*; 32 33 /** 34 * sandStormProfiler is an implementation of the ProfilerIF interface 35 * for Sandstorm. It is implemented using a thread that periodically 36 * samples the set of ProfilableIF's registered with it, and outputs 37 * the profile to a file. 38 * 39 * @author Matt Welsh 40 * @see ProfilerIF 41 * @see ProfilableIF 42 */ 43 class sandStormProfiler extends Thread implements sandStormConst, ProfilerIF { 44 45 private int delay; 46 private PrintWriter pw; 47 private Vector profilables; 48 private boolean started = false; 49 private StageGraph graphProfiler; 50 sandStormProfiler(ManagerIF mgr)51 sandStormProfiler(ManagerIF mgr) throws IOException { 52 graphProfiler = new StageGraph(mgr); 53 SandstormConfig config = mgr.getConfig(); 54 delay = config.getInt("global.profile.delay"); 55 String filename = config.getString("global.profile.filename"); 56 if (config.getBoolean("global.profile.enable")) { 57 pw = new PrintWriter(new FileWriter(filename, true)); 58 } 59 profilables = new Vector(1); 60 } 61 62 /** 63 * Returns true if the profiler is enabled. 64 */ enabled()65 public boolean enabled() { 66 return started; 67 } 68 69 /** 70 * Add a class to this profiler. 71 */ add(String name, ProfilableIF pr)72 public void add(String name, ProfilableIF pr) { 73 if (pr == null) return; 74 if (pw == null) return; 75 synchronized (profilables) { 76 pw.println("# Registered "+profilables.size()+" "+name); 77 profilables.addElement(new profile(name, pr)); 78 } 79 } 80 run()81 public void run() { 82 83 if (pw == null) return; 84 started = true; 85 pw.println("##### Profile started at "+(new Date()).toString()); 86 pw.println("##### Sample delay "+delay+" msec"); 87 Runtime r = Runtime.getRuntime(); 88 89 while (true) { 90 91 long totalmem = r.totalMemory()/1024; 92 long freemem = r.freeMemory()/1024; 93 94 pw.print("totalmem(kb) "+totalmem+" freemem(kb) "+freemem+" "); 95 96 synchronized(profilables) { 97 if (profilables.size() > 0) { 98 for (int i = 0; i < profilables.size(); i++) { 99 profile p = (profile)profilables.elementAt(i); 100 pw.print("pr"+i+" "+p.pr.profileSize()+" "); 101 } 102 } 103 } 104 pw.println(""); 105 pw.flush(); 106 107 try { 108 Thread.currentThread().sleep(delay); 109 } catch (InterruptedException ie) { 110 } 111 } 112 } 113 getGraphProfiler()114 public StageGraph getGraphProfiler() { 115 return graphProfiler; 116 } 117 118 class profile { 119 String name; 120 ProfilableIF pr; 121 profile(String name, ProfilableIF pr)122 profile(String name, ProfilableIF pr) { 123 this.name = name; 124 this.pr = pr; 125 } 126 } 127 128 } 129