*** empty log message ***
[IRC.git] / Robust / Transactions / dstm2 / src / dstm2 / benchmark / CustomThread.java
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package dstm2.benchmark;
6
7 import TransactionalIO.Utilities.Range;
8 import TransactionalIO.interfaces.IOOperations;
9 import TransactionalIO.core.MyDefaults;
10 import TransactionalIO.core.ExtendedTransaction;
11 import TransactionalIO.core.TransactionLocalFileAttributes;
12 import TransactionalIO.core.TransactionalFile;
13 import TransactionalIO.core.Wrapper;
14 import TransactionalIO.core.WriteOperations;
15 import TransactionalIO.exceptions.GracefulException;
16 import dstm2.SpecialTransactionalFile;
17 import java.io.FileNotFoundException;
18 import java.io.IOException;
19 import java.util.Iterator;
20 import java.util.HashMap;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23 import dstm2.Thread;
24 import dstm2.atomic;
25 import dstm2.AtomicArray;
26 import dstm2.Defaults;
27 import dstm2.factory.Factory;
28
29
30 import java.io.RandomAccessFile;
31
32 import java.util.Vector;
33
34 import java.util.concurrent.Callable;
35
36 /**
37  *
38  * @author navid
39  */
40 public class CustomThread implements Runnable {
41
42
43     private Thread thread;
44     private String benchmark;
45     int thresh; 
46     boolean inevitable = Defaults.INEVITABLE;
47     int size;
48     long threadoffset;
49     static int version = 0;
50     static int occurence = 0;
51
52
53     ///////////////////PureIO data structure//////////////////////
54     
55     static TransactionalFile[] pureiofiles;
56     static SpecialTransactionalFile[] inevitablepureiofiles;
57
58     ///////////////////financialbenchmark data structure//////////////////
59     String[] stocks;
60     HashMap financialbm1;
61     HashMap financialbm2;
62     String st = new String();
63     byte[] financedata = new byte[11];
64     char[] balance = new char[20];      
65     byte[] financedata2 = new byte[41];
66     TransactionalFile accountbalance=null; 
67     SpecialTransactionalFile inevitableaccountbalance=null; 
68     static AtomicArray<FTrHolder> financialTransactionKeeper;
69     private static Factory<FinancialTransactionDS> factory = Thread.makeFactory(FinancialTransactionDS.class);
70     private static Factory<RootHolder> factory2 = Thread.makeFactory(RootHolder.class);
71     private static Factory<FTrHolder> factory3 = Thread.makeFactory(FTrHolder.class);
72     //////////////////////////////////////////////////////////////////////
73
74
75     ///////////////////counter data structure//////////////////////////////
76     static TransactionalFile counterfile;
77     static SpecialTransactionalFile inevitablecounterfile;
78     static Factory<CountKeeper> counterfactory = Thread.makeFactory(CountKeeper.class);
79     private static CountKeeper word1_occurence;
80     private static CountKeeper word2_occurence;
81     private static CountKeeper word3_occurence;
82     private static CountKeeper word4_occurence;
83     private static CountKeeper word5_occurence;
84     public static RandomAccessFile globallocktestfile;
85     public static SpecialTransactionalFile inevitabletestfile;
86     public static TransactionalFile outoftransactiontestfile;
87     public static Object lock = new Object();
88     ///////////////////////////////////////////////////////////////////////
89
90     public CustomThread(String benchmark, int versionwanted) {
91         
92         this.benchmark = benchmark;
93         version = versionwanted;
94    
95         if (benchmark.equals("dstm2.benchmark.ParralelGrep")) {
96                 initParralelGrep();
97         } else if (benchmark.equals("dstm2.benchmark.FinancialTransaction")) {
98                 intiFinance();
99         } else if (benchmark.equals("dstm2.benchmark.ParralelSort")) {
100                 initPureIO();
101         }
102         /*}  else if (benchmark.equals("dstm2.benchmark.PureIOInevitablev2")) {
103                 initPureIO();
104         } else if (benchmark.equals("dstm2.benchmark.PureIOTransactionalv2")) {
105                 initPureIO();
106         }*/
107
108         thread = new Thread(this);
109    }
110    
111
112
113     public int dummy(int j){
114         int result = 0 ;
115         for (int i=0; i<10000;i++){
116                 result+=i*i;
117                 for (int k=0; k<10;k++){
118                         result+=k*i+j;
119                 }
120         }
121         return result;
122     }
123    
124     public int lock(RandomAccessFile g, byte[] res){
125
126                                         try{
127                                                 g.read(res);    
128                                                 int dum = 0;
129                                                 for(int j=0;j<5;j++){
130                                                         dum += dummy(dum+j); 
131                                                 }
132                                                 g.read(res);    
133                                                 return dum;
134                                         }catch(IOException e){
135                                                 e.printStackTrace();
136                                                 return -1;
137                                         }
138
139     }
140     
141     public int lock(IOOperations g, byte[] res){
142
143                                         try{
144                                                 g.read(res);    
145                                                 int dum = 0;
146                                                 for(int j=0;j<5;j++){
147                                                         dum += dummy(dum+j); 
148                                                 }
149                                                 g.read(res);    
150                                                 return dum;
151                                         }catch(IOException e){
152                                                 e.printStackTrace();
153                                                 return -1;
154                                         }
155     }
156     
157     public void GlobalLockTest(){
158         byte[] res = new byte[1023];
159         for (int l=0; l<1023;l++)
160                 res[l]=(byte)'a';
161         try{
162                 RandomAccessFile g = new RandomAccessFile("/scratch/TransactionalIO/test","rw"); 
163                 long toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) *4000;
164                 for (int i=0; i<2000;i++){
165                         g.seek(toseek);
166                         synchronized(lock){
167                                 int resu = lock(g, res);
168                                 /*      try{
169                                                 g.write(res);   
170                                                 int dum = 0;
171                                                 for(int j=0;j<5;j++){
172                                                         dum += dummy(dum+j); 
173                                                 }
174                                                 g.write(res);   
175                                         }catch(IOException e){
176                                                 e.printStackTrace();
177                                         }*/
178                         }       
179                 }
180         }catch(IOException e){
181                 e.printStackTrace();
182         }
183     }
184
185     public void NonTest(){
186          final byte[] res = new byte[1023];
187         for (int l=0; l<1023;l++)
188                 res[l]=(byte)'a';
189         try{
190                 final RandomAccessFile g = new RandomAccessFile("/scratch/TransactionalIO/test","rw"); 
191                 long toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) *4000;
192                 for (int i=0; i<2000;i++){
193                                         g.seek(toseek);
194                 //      for (int j=0; j<300;j++){
195 //                              try{
196                                         int resu = lock(g, res);
197                                 //      for(int j=0;j<50;j++)
198 /*                                              g.write(res);   
199                                                 int dum = 0;
200                                                 for (int j=0; j<5;j++){
201                                                         dum += dummy(dum+j);
202                                                 }
203                                                 g.write(res);   */
204 //                              }catch(IOException e){
205 //                                      e.printStackTrace();
206 //                              }
207                 //      }
208                 }
209         }catch(IOException e){
210                 e.printStackTrace();
211         }
212     }
213
214     public void InevitableTest(){
215         final byte[] res = new byte[1023];
216         for (int l=0; l<1023;l++)
217                 res[l]=(byte)'a';
218         try{
219                 final SpecialTransactionalFile in = new SpecialTransactionalFile("/scratch/TransactionalIO/test","rw"); 
220                 final long toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) *4000;
221
222                 for (int i=0; i<2000;i++){
223                         in.seek(toseek);
224 //                      for (int j=0; j<300;j++){
225                                 Thread.doIt(new Callable<Boolean>(){
226                                         public Boolean call(){
227 /*                                              try{
228                                                         in.write(res);
229                                                         int dum = 0;
230                                                         for (int j=0; j<5;j++){
231                                                                 dum += dummy(dum+j);
232                                                         }
233                                                         in.write(res);
234                                                 }catch(IOException e){
235                                                         e.printStackTrace();
236                                                 }
237                                                 finally{
238                                                         return true;
239                                                 }*/
240                                                 int resu = lock(in, res);
241                                                 return true;
242                                         }
243                                 });
244 //                      }
245                 }
246         }catch(IOException e){
247                 e.printStackTrace();
248         }
249     }
250     
251     public void TransactionalTest(){
252         final byte[] res = new byte[1023];
253         for (int l=0; l<1023;l++)
254                 res[l]=(byte)'a';
255 //      try{
256                 final TransactionalFile in = new TransactionalFile("/scratch/TransactionalIO/test","rw"); 
257                 final long toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) *4000;
258                 try{
259                         for (int i=0; i<2000;i++){
260                                 in.seek(toseek);
261                         //for (int j=0; j<30;j++){
262                 //              long st = System.currentTimeMillis();
263                                 Thread.doIt(new Callable<Boolean>(){
264                                         public Boolean call(){
265                                                 /*try{
266                                                         in.write(res);
267                                                         int dum = 0;
268                                                         for (int j=0; j<5;j++){
269                                                                 dum += dummy(dum+j);
270                                                         }
271                                                         in.write(res);
272                                                 }
273                                                 catch(IOException e){
274                                                         e.printStackTrace();
275                                                 }
276                                                 finally{
277                                                         return true;
278                                                 }*/
279                                                 int resu = lock(in, res);
280                                                         return true;
281                                         }
282                                 });
283                 //              long sp = System.currentTimeMillis();
284                 //              System.out.println("aaaa  " + (sp-st));
285                 //      }
286                 }
287         //      }
288         }catch(IOException e){
289                 e.printStackTrace();
290         }
291     }
292
293
294
295
296     public void intiFinance()
297     {
298         
299                 try{
300                         if (version == 0){
301                                 if (inevitable) 
302                                         inevitableaccountbalance= new SpecialTransactionalFile("/scratch/TransactionalIO/FinancialTransactionFilesv0/accountbalance.txt","rw");
303                                 else 
304                                         accountbalance= new TransactionalFile("/scratch/TransactionalIO/FinancialTransactionFilesv0/accountbalance.txt","rw");
305                         }
306                         else if (version == 1){
307                                 if (inevitable) 
308                                         inevitableaccountbalance= new SpecialTransactionalFile("/scratch/TransactionalIO/FinancialTransactionFilesv1/accountbalance.txt","rw");
309                                 else 
310                                         accountbalance= new TransactionalFile("/scratch/TransactionalIO/FinancialTransactionFilesv1/accountbalance.txt","rw");
311                         }
312
313                 }catch(IOException ex){
314                         ex.printStackTrace();
315                 }
316         
317                 preparenamelist();      
318         if (occurence == 0){
319
320                 occurence++;
321                 RootHolder ck = factory2.create();
322                 ck.setFinancialTransactionKeeper(new AtomicArray<FTrHolder>(FTrHolder.class, 20));
323
324                 financialTransactionKeeper = ck.getFinancialTransactionKeeper();
325                 for (int i=0; i<20; i++){
326                         FTrHolder f1 = factory3.create();
327                         f1.setCounter(1);
328                         f1.setFinancialTransactionKeeper(new AtomicArray<FinancialTransactionDS>(FinancialTransactionDS.class, 5));
329                         for (int j=0; j<5; j++) {
330                                 FinancialTransactionDS ftk = factory.create();
331                                 ftk.setBuyer("");
332                                 ftk.setSeller("");
333                                 ftk.setSoldShare(0);
334                                 AtomicArray<FinancialTransactionDS> tmp = f1.getFinancialTransactionKeeper();
335                                 tmp.set(j, ftk);
336                         }
337         
338                         financialTransactionKeeper.set(i, f1);
339                 }
340         }
341     }
342    
343     public void initPureIO(){
344         if (occurence == 0){
345                 occurence++;
346                 try{
347                         if (inevitable){
348                                 inevitablepureiofiles = new SpecialTransactionalFile[26];
349                                 for (int i=0; i<26; i++){
350                                         int j= i+ 'a';
351                                         if (version == 0)
352                                                 inevitablepureiofiles[i] = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelSortFilesv0/"+String.valueOf((char) (97+i))+ ".text","rw");
353                                         else if (version == 1)
354                                                 inevitablepureiofiles[i] = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelSortFilesv1/"+String.valueOf((char) (97+i))+ ".text","rw");
355                                 }
356                         }
357                         else{
358                                 pureiofiles = new TransactionalFile[26];
359                                 for (int i=0; i<26; i++){
360                                         int j= i+ 'a';
361                                         if (version == 0)
362                                                 pureiofiles[i] = new TransactionalFile("/scratch/TransactionalIO/ParallelSortFilesv0/"+String.valueOf((char) (97+i))+ ".text","rw");
363                                         else if (version == 1)
364                                                 pureiofiles[i] = new TransactionalFile("/scratch/TransactionalIO/ParallelSortFilesv1/"+String.valueOf((char) (97+i))+ ".text","rw");
365                                 }       
366                         }
367        
368                 }catch(IOException ex){ 
369                         ex.printStackTrace();
370                 }
371         }
372     }
373
374
375
376
377     public void initParralelGrep(){
378         if (occurence == 0){
379                 occurence++;
380             int count = 0;
381
382             word1_occurence = counterfactory.create();
383             word1_occurence.setOccurence(0);
384               
385             word2_occurence = counterfactory.create();
386             word2_occurence.setOccurence(0);
387               
388             word3_occurence = counterfactory.create();
389             word3_occurence.setOccurence(0);
390               
391             word4_occurence = counterfactory.create();
392             word4_occurence.setOccurence(0);
393            
394             word5_occurence = counterfactory.create();
395             word5_occurence.setOccurence(0);
396
397             if (version ==0){
398                 if (inevitable){
399                     try {
400                             inevitablecounterfile = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv0/output.text", "rw");
401                    } catch (FileNotFoundException ex) {
402                             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
403                    }               
404                 }  
405                 else 
406                     counterfile = new TransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv0/output.text", "rw");
407            }
408           else  if (version ==1){
409             if (inevitable){
410                     try {
411                             inevitablecounterfile = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv1/output.text", "rw");
412                     } catch (FileNotFoundException ex) {
413                             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
414                     }
415                 }
416                     else 
417                         counterfile = new TransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv1/output.text", "rw");
418         
419          }
420         }
421   }
422
423
424
425
426     public void start() {
427         thread.start();
428     }
429
430     public void join() {
431         try {
432             thread.join();
433         } catch (InterruptedException ex) {
434             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
435         }
436     }
437
438     public void run() {
439
440         if (benchmark.equals("GlobalLockTest")) {
441             GlobalLockTest();
442         }
443         else if (benchmark.equals("NonTest")) {
444             NonTest();
445         }
446         else if (benchmark.equals("InevitableTest")) {
447             InevitableTest();
448         }
449         else if (benchmark.equals("TransactionalTest")) {
450             TransactionalTest();
451         }
452         else if (benchmark.equals("dstm2.benchmark.ParralelGrep")) {
453             parralelGrepBenchmark();
454         } else if (benchmark.equals("dstm2.benchmark.FinancialTransaction")) {
455             financialBenchmark();
456         } else if (benchmark.equals("dstm2.benchmark.ParralelSort")) {
457             pureIOBenchmark();
458         }
459         else
460                 System.out.println("No Such Benchmark");
461     /*    } else if (benchmark.equals("dstm2.benchmark.PureIOInevitablev2")) {
462             pureIOInevitablev2();
463         } else if (benchmark.equals("dstm2.benchmark.PureIOTransactionalv2")) {
464             pureIOTransactionalv2();
465         } */
466     }
467
468     public void pureIOBenchmark() {
469         try {
470             RandomAccessFile f1;
471             if (version == 0)
472                     f1 = new RandomAccessFile("/scratch/TransactionalIO/ParallelSortFilesv0/randomwords.text", "rw");
473             else 
474                     f1 = new RandomAccessFile("/scratch/TransactionalIO/ParallelSortFilesv1/randomwords.text", "rw");
475
476             long toseek;
477             long threadoffset;
478             threadoffset = 51121;
479             toseek =
480                     (Integer.valueOf(Thread.currentThread().getName().substring(7))) * threadoffset;
481
482             for (int i=0; i<10; i++){
483                     f1.seek(toseek);
484                     if (toseek != 0) {
485                         //////////////// skipt the first word since its been read already
486                         f1.readLine();
487                     }
488                    while (f1.getFilePointer() < toseek + threadoffset) {
489                         final String pureiodata = f1.readLine();
490                         if (pureiodata == null) {
491                             break;
492                         }
493                         boolean resultt = Thread.doIt(new Callable<Boolean>() {
494
495                             public Boolean call() {
496                                 pureioexecute(pureiodata);
497                                 return true;
498                             }
499                         });
500
501                     }
502            }
503         }catch (IOException ex) {
504                     Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
505         }
506
507     }
508
509
510
511     public void financialBenchmark() {
512       try {
513             RandomAccessFile f1;
514             if (version == 0)
515                 f1 = new RandomAccessFile("/scratch/TransactionalIO/FinancialTransactionFilesv0/financialtransaction.text", "rw");
516             else 
517                 f1 = new RandomAccessFile("/scratch/TransactionalIO/FinancialTransactionFilesv1/financialtransaction.text", "rw");
518
519             String data = new String();
520             int counter = 0;
521             long toseek;
522             long threadoffset = 360611*2;
523             toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) * threadoffset; 
524             f1.seek(toseek);
525             if (toseek != 0) {
526                 //////////////// skipt the first line since its been read already            
527                 data = f1.readLine();
528                 if (data == null)
529                         return;
530             }
531
532             while (f1.getFilePointer() < toseek + threadoffset) {
533
534                 counter = 0;
535                 data = f1.readLine();
536
537                 if (data == null) 
538                     break;
539
540                 while (data.charAt(counter) != ' ') {
541                     counter++;
542                 }
543
544                 final String oldowner = data.substring(0, counter);
545                 counter++;
546                 int counter2 = counter;
547                 while (data.charAt(counter) != ' ') {
548                     counter++;
549                 }
550
551
552                 final int exchange = Integer.parseInt(data.substring(counter2, counter ));
553
554                 counter++;
555                 counter2 = counter;
556
557                 while (data.charAt(counter) != ' ') {
558                     counter++;
559                 }
560
561                 final String newowner = data.substring(counter2, counter);
562
563                 counter++;
564                 counter2 = counter;
565                 for (int j=counter; j<data.length(); j++) {
566                     counter++;
567                 }
568
569                 final String company = data.substring(counter2, counter);
570                 boolean result = Thread.doIt(new Callable<Boolean>() {
571                     public Boolean call() {
572                         financialexecute(oldowner, exchange, newowner, company);
573                         return true;
574                     }
575                 });
576             }
577
578
579         } catch (IOException ex) {
580            Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
581         }
582
583     }
584
585
586     public void parralelGrepBenchmark() {
587         try {
588
589             final IOOperations f1;
590             if (!(inevitable)){
591                 if (version == 0)
592                         f1 = new TransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv0/iliad.text", "rw");
593                 else
594                         f1 = new TransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv1/iliad.text", "rw");
595             }
596             else{
597                 if (version == 0)
598                         f1 = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv0/iliad.text", "rw");
599                 else
600                         f1 = new SpecialTransactionalFile("/scratch/TransactionalIO/ParallelGrepFilesv1/iliad.text", "rw");
601             }
602             final long threadoffset;
603             final long toseek;
604             final byte[] data = new byte[1000];
605             //threadoffset = 52921;
606             threadoffset = 50000;
607             toseek = Integer.valueOf(Thread.currentThread().getName().substring(7)) * threadoffset;
608             for (int m=0; m<3000; m++){ 
609             f1.seek(toseek);
610             for (int l=0; l<2; l++){
611                     Thread.doIt (new Callable<Boolean>(){
612                                 public Boolean call(){  
613                                         try{
614                                                 for (int k=0; k<25; k++){       
615                                                         int end=0;
616                                                         int start = 0;
617                                                         long offset = 0;        
618                                                         int result = f1.read(data);
619                                                         if (result==-1)
620                                                                 return false;
621                                                         while (end<data.length){
622                                                                 start = end;
623                                                         while(!(Character.isLetter((char)data[end]))){
624                                                                         start++;
625                                                                         end++;
626                                                                 }
627                                                                 while(Character.isLetter((char)data[end])){
628                                                                         
629                                                                         end++;
630                                                                 }
631                                                                 int index_of_object = processInput(new String(data, start, end-start));
632                                                                 if (index_of_object == -1)
633                                                                         continue;
634
635                                                                if (inevitable){
636                                                                         String tmp = "\n\n Found word " + new String(data, start, end-start) + " At offset: " + offset +"\n\n";
637                                                                         inevitablecounterfile.write(tmp.getBytes());
638                                                                         inevitablecounterfile.write(data, 0, end);
639                                                                }
640                                                                else{
641                                                                         String tmp = "\n\n Found word " + new String(data, start, end-start) + " At offset: " + offset + "\n\n";
642                                                                         counterfile.write(tmp.getBytes());
643                                                                         counterfile.write(data, 0, end);
644                                                                }
645                                                                 switch(index_of_object){
646                                                                     case 0:
647                                                                             word1_occurence.setOccurence(word1_occurence.getOccurence() + 1);
648                                                                             break;
649                                                                     case 1:
650                                                                             word2_occurence.setOccurence(word2_occurence.getOccurence() + 1);
651                                                                             break;
652                                                                     case 2:
653                                                                                  word3_occurence.setOccurence(word3_occurence.getOccurence() + 1);
654                                                                                  break;
655                                                                     case 3:
656                                                                                 word4_occurence.setOccurence(word4_occurence.getOccurence() + 1);
657                                                                                 break;
658                                                                     case 4:
659                                                                                  word5_occurence.setOccurence(word5_occurence.getOccurence() + 1);
660                                                                                  break;
661                                                                 }
662                                                         }
663                                                 }
664                                         } catch (IOException ex) {
665                                                  Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
666                                         }
667                                         finally {
668                                                 return true;
669                                         }
670                                  }
671                         });
672                 }
673                 }
674
675         } catch (IOException ex) {
676             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
677         }
678
679     }
680
681
682
683     private long financialcomputeandupdate(boolean type, String oldowner, int stocktrade, String newowner, String nameofstock) {
684         try {
685
686             int counter = 0;
687             if (inevitable)
688                   inevitableaccountbalance.read(financedata);
689             else 
690                   accountbalance.read(financedata);
691             int adad;
692             for (adad = 0; adad < stocks.length; adad++) {
693                 if (stocks[adad].equalsIgnoreCase(nameofstock)) {
694                     break;
695                 }
696             }
697             if (inevitable){
698                     inevitableaccountbalance.skipBytes(adad * 41);
699                     inevitableaccountbalance.read(financedata2);
700             }
701             else{
702                     accountbalance.skipBytes(adad * 41);
703                     accountbalance.read(financedata2);
704             }
705
706             int i = 0;
707             while ((char) financedata2[i] != ':') {
708                 i++;
709             }
710
711             i++;
712             long offsettowrite;
713
714
715             if (inevitable)
716                  offsettowrite = inevitableaccountbalance.getFilePointer();
717             else
718                  offsettowrite = accountbalance.getFilePointer();
719                  
720             offsettowrite += i - 40;
721
722             do {
723                 i++;
724                 balance[counter] = (char) financedata2[i];
725                 counter++;
726             } while (Character.isDigit((char) financedata2[i]) || (char) financedata2[i] == '-');
727
728             int oldbalance = Integer.parseInt(String.valueOf(balance, 0, counter - 1));
729             int newnumber;
730             if (type) {
731                 newnumber = oldbalance - stocktrade;
732             } else {
733                 newnumber = oldbalance + stocktrade;
734
735             }
736             st = String.valueOf(newnumber);
737             if (st.length() < counter - 1) {
738
739                 for (int j = 0; j < counter - String.valueOf(newnumber).length(); j++) {
740                     st += new String(" ");
741                 }
742             }
743
744             return offsettowrite;
745
746
747         } catch (IOException ex) {
748             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
749             return -1;
750         }
751
752     }
753    
754
755     public void pureioexecute(String str){
756         try{
757         
758                 int i = str.toLowerCase().charAt(0) - 'a';
759                 if (inevitable)
760                         inevitablepureiofiles[i].write(str.getBytes());
761                 else
762                         pureiofiles[i].write(str.getBytes());
763
764         }catch(IOException e){
765                 e.printStackTrace();
766         }
767     }
768
769
770
771     static Integer  processInput(String str) {
772             if (str.equalsIgnoreCase("Polydamas")) {
773                 return 0;
774             }
775             else if (str.equalsIgnoreCase("Cebriones")) {
776                 return 1;
777             }
778             else if (str.equalsIgnoreCase("Eurybates")) {
779                 return 2;
780             }
781             else if (str.equalsIgnoreCase("Menoetius")) {
782                 return 3;
783             }
784             else if (str.equalsIgnoreCase("countless")) {
785                 return 4;
786             }
787         return -1;
788    }
789
790
791         
792 private void preparenamelist() {
793         try {
794             byte[] data = new byte[1];
795             char[] name = new char[20];
796             RandomAccessFile file = new RandomAccessFile("/scratch/TransactionalIO/FinancialTransactionFilesv0/namelist.text", "rw");
797             RandomAccessFile file3 = new RandomAccessFile("/scratch/TransactionalIO/FinancialTransactionFilesv0/accountbalance.text", "rw");
798
799             stocks = new String[20];
800             stocks[0] = "Yahoo";
801             stocks[1] = "Google";
802             stocks[2] = "Microsoft";
803             stocks[3] = "Broadcom";
804             stocks[4] = "Sun";
805             stocks[5] = "Qualcom";
806             stocks[6] = "Intel";
807             stocks[7] = "WaMU";
808             stocks[8] = "BoA";
809             stocks[9] = "IMU";
810             stocks[10] = "BMW";
811             stocks[11] = "Nokia";
812             stocks[12] = "Motorolla";
813             stocks[13] = "Samsung";
814             stocks[14] = "TMobile";
815             stocks[15] = "ATT";
816             stocks[16] = "PRops";
817             stocks[17] = "Asia";
818             stocks[18] = "LOLa";
819             stocks[19] = "Brita";
820
821             boolean flag = false;
822             boolean done = false;
823             int wordcounter = 0;
824             int counter = 0;
825             financialbm1 = new HashMap();
826             financialbm2 = new HashMap();
827
828             while (true) {
829                 if (flag) {
830                     break;
831                 }
832                 if (done) {
833                     financialbm1.put(Integer.valueOf(wordcounter), String.copyValueOf(name, 0, counter));
834                     financialbm2.put(String.copyValueOf(name, 0, counter), Integer.valueOf(wordcounter));
835                     wordcounter++;
836                     done = false;
837                 }
838                 counter = 0;
839                 data[0] = 'a';
840                 while (data[0] != '\n') {
841                     int res;
842                     res = file.read(data);
843                     if (res == -1) {
844                         flag = true;
845                         break;
846                     }
847                     if (!(Character.isLetter((char) data[0]))) {
848                         continue;
849                     }
850                     name[counter] = (char) data[0];
851                     done = true;
852                     counter++;
853                 }
854             }
855
856
857
858 ///preparing the accountbalance file/////////
859 /*
860             for (int i = 0; i < 50; i++) {
861                 String towrite = (String) financialbm1.get(Integer.valueOf(i));
862                 String tmpst = (String) financialbm2.get(Integer.valueOf(i));
863                 System.out.println("Ss " + tmpst);
864                 int tmp = tmpst.length();
865                 while (tmp < 10) {
866                     towrite += new String(" ");
867                     tmp++;
868                 }
869                 towrite += "\n";
870                 for (int j = 0; j < stocks.length; j++) {
871                     tmpst = stocks[j] + " Stock Balance: " + ((int) (Math.random() * 100 + 100));
872                     towrite += stocks[j] + " Stock Balance: " + ((int) (Math.random() * 100 + 100));
873                     tmp = tmpst.length();
874                     while (tmp < 40) {
875                         towrite += new String(" ");
876                         tmp++;
877                     }
878                     towrite += "\n";
879                 }
880
881                 file3.write(towrite.getBytes());
882                 while (file3.getFilePointer() < (i + 1) * MyDefaults.FILEFRAGMENTSIZE) {
883                     file3.write(new String(" ").getBytes());
884                 }
885             }
886
887 ////////////////////////////////////////////
888 */
889             file.close();
890             file3.close();
891         } catch (IOException ex) {
892             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
893         }
894     }
895
896   
897        
898     protected void financialexecute(String oldowner, int stocktrade, String newowner, String nameofstock) {
899        try {
900               Integer offset1 = (Integer) financialbm2.get(oldowner);
901               Integer offset2 = (Integer) financialbm2.get(newowner);
902
903               if (inevitable)   
904                       inevitableaccountbalance.seek(offset1 * MyDefaults.FILEFRAGMENTSIZE);
905               else
906                       accountbalance.seek(offset1 * MyDefaults.FILEFRAGMENTSIZE);
907
908               long offsettowrite = financialcomputeandupdate(true, oldowner, stocktrade, newowner, nameofstock);
909               if (inevitable){
910                       inevitableaccountbalance.seek(offsettowrite);
911                       inevitableaccountbalance.write(st.getBytes());
912               }
913               else {
914                       accountbalance.seek(offsettowrite);
915                       accountbalance.write(st.getBytes());
916               }
917
918
919               if (inevitable)
920                       inevitableaccountbalance.seek(offset2 * MyDefaults.FILEFRAGMENTSIZE);
921               else 
922                       accountbalance.seek(offset2 * MyDefaults.FILEFRAGMENTSIZE);
923
924               offsettowrite = financialcomputeandupdate(false, oldowner, stocktrade, newowner, nameofstock);
925                 
926             
927               if (inevitable){
928                       inevitableaccountbalance.seek(offsettowrite);
929                       inevitableaccountbalance.write(st.getBytes());
930               }
931               else {
932                       accountbalance.seek(offsettowrite);
933                       accountbalance.write(st.getBytes());
934               }
935
936             int i;
937             for (i = 0; i < stocks.length; i++) {
938                 if (stocks[i].equalsIgnoreCase(nameofstock)) {
939                     break;
940                 }
941             }
942             switch (financialTransactionKeeper.get(i).getCounter()) {
943                 case 1:
944                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setSeller(oldowner);
945                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setSoldShare(stocktrade);
946                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(0).setBuyer(newowner);
947                     financialTransactionKeeper.get(i).setCounter(2);
948                     break;
949                 case 2:
950                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setSeller(oldowner);
951                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setSoldShare(stocktrade);
952                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(1).setBuyer(newowner);
953                     financialTransactionKeeper.get(i).setCounter(3);
954                     break;
955                 case 3:
956                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setSeller(oldowner);
957                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setSoldShare(stocktrade);
958                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(2).setBuyer(newowner);
959                     financialTransactionKeeper.get(i).setCounter(4);
960                     break;
961                 case 4:
962                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setSeller(oldowner);
963                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setSoldShare(stocktrade);
964                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(3).setBuyer(newowner);
965                     financialTransactionKeeper.get(i).setCounter(5);
966                     break;
967                 case 5:
968                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setSeller(oldowner);
969                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setSoldShare(stocktrade);
970                     financialTransactionKeeper.get(i).getFinancialTransactionKeeper().get(4).setBuyer(newowner);
971                     financialTransactionKeeper.get(i).setCounter(1);
972                     break;
973             }
974
975         } catch (IOException ex) {
976             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
977         }
978     }
979
980
981     @atomic
982     public interface FinancialTransactionDS {
983         String getSeller();
984         void setSeller(String value);
985         int getSoldShare();
986         void setSoldShare(int value);
987         String getBuyer();
988         void setBuyer(String value);
989
990     }   
991
992     @atomic 
993     public interface FTrHolder {
994         AtomicArray<FinancialTransactionDS> getFinancialTransactionKeeper();
995         void setFinancialTransactionKeeper(AtomicArray<FinancialTransactionDS> arr);
996         int getCounter();
997         void setCounter(int value);
998     }   
999
1000     @atomic 
1001     public interface RootHolder {
1002         AtomicArray<FTrHolder> getFinancialTransactionKeeper(); 
1003         void setFinancialTransactionKeeper(AtomicArray<FTrHolder> arr);
1004
1005     }
1006
1007     @atomic
1008     public interface CountKeeper{
1009         int getOccurence();
1010         void setOccurence(int value);;
1011     }
1012 }
1013
1014 /*    public void pureIOInevitablev2() {
1015         try {
1016
1017             final SpecialTransactionalFile f1;
1018             f1 = new SpecialTransactionalFile("/scratch/TransactionalIO/PureIOBenchmarkFiles/randomwords.text", "rw");
1019             final long toseek;
1020             final long threadoffset = 51121;
1021             toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) * threadoffset;
1022             for (int l=0; l<10; l++){
1023                 f1.seek(toseek);        
1024                 if (toseek !=0)
1025                         f1.readLine();
1026                 while (f1.getFilePointer() < toseek + threadoffset) {
1027                         boolean resultt = Thread.doIt(new Callable<Boolean>() {
1028                                 public Boolean call() {
1029                                     try{
1030                                                 for (int k=0; k<1000; k++){     
1031                                                         if (f1.getFilePointer() < toseek + threadoffset){       
1032                                                                 
1033                                                                 String str = f1.readLine();
1034                                                                 if (str == null)
1035                                                                         return true;
1036                                                                 int i = Character.toLowerCase((char)str.charAt(0)) - 'a';
1037                                                                 inevitablepureiofiles[i].write(str.getBytes());
1038                                                         }
1039                                                         else
1040                                                                 return true;
1041                                                 
1042                                                 }
1043                                 
1044                                              return true;
1045
1046                                       } catch (IOException ex) {
1047                                             System.exit(0);
1048                                             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
1049                                     return false;
1050                                   }
1051                                }
1052                         });
1053                 }
1054         } 
1055       } catch (IOException ex) {
1056             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
1057       }
1058     }
1059
1060     public void pureIOTransactionalv2() {
1061         try {
1062             final TransactionalFile f1;
1063             f1 = new TransactionalFile("/scratch/TransactionalIO/PureIOBenchmarkFiles/randomwords.text", "rw");
1064             final long toseek;
1065             final long threadoffset = 51121;
1066             toseek = (Integer.valueOf(Thread.currentThread().getName().substring(7))) * threadoffset;
1067             for (int l=0; l<10; l++){
1068                    f1.seek(toseek);
1069                    if (toseek !=0)
1070                         f1.readLine();
1071                    while (f1.getFilePointer() < toseek + threadoffset) {
1072                         boolean resultt = Thread.doIt(new Callable<Boolean>() {
1073                                 public Boolean call() {
1074                                         try{
1075                                                         for (int k=0; k<1000; k++){     
1076                                                                 if (f1.getFilePointer() < toseek + threadoffset){       
1077                                                                         String str = f1.readLine();
1078                                                                         if (str == null)
1079                                                                                 return true;
1080                                                                         int i = Character.toLowerCase(str.charAt(0)) - 'a';
1081                                                                         pureiofiles[i].write(str.getBytes());
1082                                                                 }
1083                                                                 else {
1084                                                                         return true;
1085                                                                 }
1086                                                       }
1087                                               return true;
1088                                         } catch (IOException ex) {
1089                                             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
1090                                             return true;
1091                                         }
1092                                 }
1093                         });
1094                    }
1095
1096                          
1097             }
1098                     
1099         } catch (IOException ex) {
1100             Logger.getLogger(CustomThread.class.getName()).log(Level.SEVERE, null, ex);
1101         }
1102    }*/