Give the threads names to facilitate thread performance analysis.
[debian/openrocket] / core / src / net / sf / openrocket / startup / ConcurrentLoadingThrustCurveMotorSetDatabase.java
1 package net.sf.openrocket.startup;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.util.List;
7 import java.util.concurrent.ExecutorService;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ThreadFactory;
10 import java.util.concurrent.TimeUnit;
11
12 import net.sf.openrocket.database.ThrustCurveMotorSet;
13 import net.sf.openrocket.database.ThrustCurveMotorSetDatabase;
14 import net.sf.openrocket.file.iterator.DirectoryIterator;
15 import net.sf.openrocket.file.iterator.FileIterator;
16 import net.sf.openrocket.file.motor.GeneralMotorLoader;
17 import net.sf.openrocket.file.motor.MotorLoaderHelper;
18 import net.sf.openrocket.gui.util.SimpleFileFilter;
19 import net.sf.openrocket.gui.util.SwingPreferences;
20 import net.sf.openrocket.logging.LogHelper;
21 import net.sf.openrocket.motor.Motor;
22 import net.sf.openrocket.motor.ThrustCurveMotor;
23 import net.sf.openrocket.util.BugException;
24 import net.sf.openrocket.util.Pair;
25
26 /**
27  * Load motors in parallel using a three stage pipeline.
28  * 
29  * Stage 1: single thread managed by the ThrustCurveMotorSetDatabase.  This thread generates
30  *          one object for each thrust curve motor file and puts it in the second stage.
31  *          
32  * Stage 2: multiple threads which process individual files.  Each process takes
33  *          a single motor file and parses out the list of motors it contains.
34  *          The list of motors is queued up for the third stage to process.
35  *          
36  * Stage 3: single thread which processes the list of motors generated in stage 2.
37  *          This thread puts all the motors from the list in the motor set database.
38  *          
39  * It is important that stage 3 be done with a single thread because ThrustCurveMotorSetDatabase
40  * is not thread safe.  Even if synchronization were to be done, it is unlikely that parallelizing
41  * this process would improve anything.
42  * 
43  *
44  */
45 public class ConcurrentLoadingThrustCurveMotorSetDatabase extends ThrustCurveMotorSetDatabase {
46
47         private static final LogHelper log = Application.getLogger();
48         private final String thrustCurveDirectory;
49
50
51         public ConcurrentLoadingThrustCurveMotorSetDatabase(String thrustCurveDirectory) {
52                 // configure ThrustCurveMotorSetDatabase as true so we get our own thread in
53                 // loadMotors.
54                 super(true);
55                 this.thrustCurveDirectory = thrustCurveDirectory;
56         }
57
58         @Override
59         protected void loadMotors() {
60
61                 BookKeeping keeper = new BookKeeping();
62                 keeper.start();
63
64                 try {
65                         keeper.waitForFinish();
66                 }
67                 catch ( InterruptedException iex ) {
68                         throw new BugException(iex);
69                 }
70
71                 keeper = null;
72
73         }
74
75         private void addAll( List<Motor> motors ) {
76                 for (Motor m : motors) {
77                         addMotor( (ThrustCurveMotor) m);
78                 }
79         }
80
81         /**
82          * A class which holds all the threading data.
83          * Implemented as an inner class so we can easily jettison the references when
84          * the processing is terminated.
85          *
86          */
87         private class BookKeeping {
88
89                 /*
90                  * Executor for Stage 3.
91                  */
92                 private final ExecutorService writerThread;
93
94                 /*
95                  * Executor for Stage 2.
96                  */
97                 private final ExecutorService loaderPool;
98
99                 /*
100                  * Runnable used for Stage 1.
101                  */
102                 private final WorkGenerator workGenerator;
103
104                 private long startTime;
105
106                 /*
107                  * Number of thrust curves loaded
108                  */
109                 private int thrustCurveCount = 0;
110
111                 /*
112                  * Number of files processed.
113                  */
114                 private int fileCount = 0;
115
116                 /*
117                  * We have to hold on to the zip file iterator which is used to load
118                  * the system motor files until all processing is done.  This is because
119                  * closing the iterator prematurely causes all the InputStreams opened
120                  * with it to close. 
121                  */
122                 private FileIterator iterator;
123
124                 private BookKeeping() {
125
126                         writerThread = Executors.newSingleThreadExecutor( new ThreadFactory() {
127                                 @Override
128                                 public Thread newThread(Runnable r) {
129                                         Thread t = new Thread(r,"MotorWriterThread");
130                                         return t;
131                                 }
132                         });
133
134                         loaderPool = Executors.newFixedThreadPool(25, new ThreadFactory() {
135                                 int threadCount = 0;
136                                 @Override
137                                 public Thread newThread(Runnable r) {
138                                         Thread t = new Thread(r,"MotorLoaderPool-" + threadCount++);
139                                         return t;
140                                 }
141                         });
142
143                         workGenerator = new WorkGenerator();
144
145                 }
146
147                 private void start() {
148
149                         startTime = System.currentTimeMillis();
150
151                         log.info("Starting motor loading from " + thrustCurveDirectory + " in background thread.");
152
153                         // Run the work generator - in this thread.
154                         workGenerator.run();
155
156                 }
157
158                 private void waitForFinish() throws InterruptedException {
159                         try {
160                                 loaderPool.shutdown();
161                                 loaderPool.awaitTermination(10, TimeUnit.SECONDS);
162                                 writerThread.shutdown();
163                                 writerThread.awaitTermination(10, TimeUnit.SECONDS);
164                         }
165                         finally {
166                                 iterator.close();
167                         }
168
169                         long endTime = System.currentTimeMillis();
170
171                         int distinctMotorCount = 0;
172                         int distinctThrustCurveCount = 0;
173                         distinctMotorCount = motorSets.size();
174                         for (ThrustCurveMotorSet set : motorSets) {
175                                 distinctThrustCurveCount += set.getMotorCount();
176                         }
177
178                         log.info("Motor loading done, took " + (endTime - startTime) + " ms to load " 
179                                         + fileCount + " files/directories containing " 
180                                         + thrustCurveCount + " thrust curves which contained "
181                                         + distinctMotorCount + " distinct motors with "
182                                         + distinctThrustCurveCount + " distinct thrust curves.");
183
184                 }
185
186
187                 private class WorkGenerator implements Runnable {
188
189                         @Override
190                         public void run() {
191                                 // Start loading
192                                 log.info("Loading motors from " + thrustCurveDirectory);
193
194                                 iterator = DirectoryIterator.findDirectory(thrustCurveDirectory,
195                                                 new SimpleFileFilter("", false, "eng", "rse"));
196
197                                 // Load the packaged thrust curves
198                                 if (iterator == null) {
199                                         throw new IllegalStateException("Thrust curve directory " + thrustCurveDirectory +
200                                                         "not found, distribution built wrong");
201                                 }
202
203                                 while( iterator.hasNext() ) {
204                                         Pair<String,InputStream> f = iterator.next();
205                                         MotorLoader loader = new MotorLoader( f.getV(), f.getU() );
206                                         loaderPool.execute(loader);
207                                         fileCount ++;
208                                 }
209
210                                 // Load the user-defined thrust curves
211                                 for (File file : ((SwingPreferences) Application.getPreferences()).getUserThrustCurveFiles()) {
212                                         log.info("Loading motors from " + file);
213                                         MotorLoader loader = new MotorLoader( file );
214                                         loaderPool.execute(loader);
215                                         fileCount++;
216                                 }
217                         }
218                 }
219
220                 private class MotorLoader implements Runnable {
221
222                         private final InputStream is;
223                         private final String fileName;
224
225                         private final File file;
226
227                         public MotorLoader( File file ) {
228                                 super();
229                                 this.file = file;
230                                 this.is = null;
231                                 this.fileName = null;
232                         }
233
234                         public MotorLoader(InputStream is, String fileName) {
235                                 super();
236                                 this.file = null;
237                                 this.is = is;
238                                 this.fileName = fileName;
239                         }
240
241                         @Override
242                         public void run() {
243                                 log.debug("Loading motor from " + fileName);
244
245                                 try {
246                                         List<Motor> motors;
247                                         if ( file == null ) {
248                                                 motors = MotorLoaderHelper.load(is, fileName);
249                                         } else {
250                                                 motors = MotorLoaderHelper.load(file);
251                                         }
252                                         writerThread.submit( new MotorInserter(motors));
253                                 }
254                                 finally {
255                                         if ( is != null ) {
256                                                 try {
257                                                         is.close();
258                                                 } catch ( IOException iex ) {
259                                                 }
260                                         }
261                                 }
262                         }
263                 }
264
265                 private class MotorInserter implements Runnable {
266
267                         private final List<Motor> motors;
268
269                         MotorInserter( List<Motor> motors ) {
270                                 this.motors = motors;
271                         }
272
273                         @Override
274                         public void run() {
275                                 thrustCurveCount += motors.size();
276                                 ConcurrentLoadingThrustCurveMotorSetDatabase.this.addAll(motors);
277                         }
278
279                 }
280         }
281
282 }