collect statistics
[IRC.git] / Robust / src / Benchmarks / SingleTM / LeeRouting / LeeThread.java
1 /*
2  * BSD License
3  *
4  * Copyright (c) 2007, The University of Manchester (UK)
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *     - Redistributions of source code must retain the above copyright
13  *       notice, this list of conditions and the following disclaimer.
14  *     - Redistributions in binary form must reproduce the above
15  *       copyright notice, this list of conditions and the following
16  *       disclaimer in the documentation and/or other materials provided
17  *       with the distribution.
18  *     - Neither the name of the University of Manchester nor the names
19  *       of its contributors may be used to endorse or promote products
20  *       derived from this software without specific prior written
21  *       permission.
22
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35
36 /********************************************************************
37  *  Ported for our STM implementation
38  *  This version copyright(c) University of California, Irvine 2009
39  *  @author:  Alokika Dash, adash@uci.edu
40  *  @date:    04/05/2009
41  ********************************************************************/
42 public class LeeThread extends Thread {
43   public boolean stop;
44   boolean finished;
45   public boolean sampleNow;
46   public boolean doneSample;
47   public long totalLaidTracks;
48   public long myLaidTracks;
49   LeeRouter lt;
50   WorkQueue t;
51   boolean done;
52   int[][][] tempg;
53
54   public LeeThread() {
55     stop = false;
56     finished = false;
57     sampleNow = false;
58     doneSample = true;
59     totalLaidTracks=0;
60     myLaidTracks=0;
61     done = true;
62   }
63
64   /*
65   protected static ThreadLocal<ThreadState> _threadState = new ThreadLocal<ThreadState>() {
66     protected synchronized ThreadState initialValue() {
67       return new ThreadState();
68     }
69   };
70   static ThreadLocal<Thread> _thread = new ThreadLocal<Thread>() {
71     protected synchronized Thread initialValue() {
72       return null;
73     }
74   };
75   */
76
77
78   LeeThread(LeeRouter lt) {
79     this.lt = lt;
80     tempg = new int[lt.GRID_SIZE][lt.GRID_SIZE][2]; // Lee 2D Grid copy
81   }
82
83   public void run() {
84     while (!finished && !stop) {
85       if(sampleNow) {
86         //collectMyStatistics();
87         doneSample = true;
88         sampleNow = false;
89       }
90       atomic {
91         if(done) {
92           t = lt.getNextTrack();
93           done = false;
94         }
95       }
96       if(t==null) {
97         finished = true;
98         System.out.println("Finished");
99         //collectMyStatistics();
100         //collectStatistics(_threadState.get());
101         break;
102       } else {
103         atomic {
104           //System.out.println("Laying track "+t.nn);
105           lt.layNextTrack(t, tempg);
106           done = true;
107         }
108         //updateStatistics();
109       }
110     }
111   }
112
113
114   /*
115   protected static void collectStatistics(ThreadState threadState) {
116     // collect statistics
117     //synchronized (lock){
118     totalLaidTracks+=threadState.myLaidTracks;
119     threadState.reset();  // set up for next iteration
120     //}
121   }
122
123   public void updateStatistics(){
124     _threadState.get().myLaidTracks++;
125   }
126
127   public void collectMyStatistics() {
128     myLaidTracks=_threadState.get().myLaidTracks-myLaidTracks;
129   }
130   */
131
132   public void resetMyStatistics() {
133     myLaidTracks=0;
134   }
135
136 }
137
138 /**
139  * Class that holds thread's actual state
140  */
141 public class ThreadState {
142   private long myLaidTracks;        // number of laid tracks
143
144   /**
145    * Creates new ThreadState
146    */
147   public ThreadState() {
148     myLaidTracks = 0; 
149   }
150
151   /**
152    * Resets any metering information (commits/aborts, etc).
153    */
154   public void reset() {
155     myLaidTracks = 0;            // total number of transactions
156   }
157
158   /**
159    * used for debugging
160    * @return string representation of thread state
161    */
162   public String toString() {
163     return
164       "Thread" + hashCode() + "["+
165       "total: " +  myLaidTracks + "," +
166       "]";
167   }
168
169 }
170