/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package ciosim; import ciosim.util.getopts.*; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.List; import java.util.Random; /** * * @author hinmanm */ public class Ciosim implements Runnable { public List filelist = new ArrayList(); /** * @param args the command line arguments */ public static void main(String[] args) { Ciosim csim = new Ciosim(); Options opt = new Options(Options.Prefix.Dash, 1); opt.addOption("n", false, true); opt.addOption("min", false, true); opt.addOption("max", false, true); opt.addOption("readstop", false, false); opt.addOption("recurse", false, false); opt.addOption("v", false, false); opt.addOption("h", false, false); ParsedOptions popts; try { popts = opt.parse(args); } catch (OptionParseException ex) { System.out.println("usage: blah blah blah" + ex.toString()); return; } if (popts.isSet("h")) { System.out.println("Usage:"); return; } int threads = 10; int minTime = 10; int maxTime = 20; boolean readstop = popts.isSet("readstop"); boolean verbose = popts.isSet("v"); boolean recurse = popts.isSet("recurse"); if (popts.getData("n") != null) { threads = Integer.parseInt(popts.getData("n")); } if (popts.getData("min") != null) { minTime = Integer.parseInt(popts.getData("min")); } if (popts.getData("max") != null) { maxTime = Integer.parseInt(popts.getData("max")); } System.out.println("Dir: " + popts.getArguments()[0]); System.out.println("Threads: " + threads); System.out.println("Recurse: " + recurse); csim.filelist = getDirContents(popts.getArguments()[0]); List thrlist = new ArrayList(); // Start number of threads for (int k = 0; k < threads; k++) { thrlist.add(k, (new Thread(csim))); thrlist.get(k).start(); System.out.println("spawned tid [" + Long.toString(thrlist.get(k).getId()) + "]"); } int j = 0; while (true) { try { long tid = thrlist.get(j).getId(); System.out.println("reaping tid [" + Long.toString(tid) + "]..."); thrlist.get(j).join(); thrlist.set(j, (new Thread(csim))); thrlist.get(j).start(); System.out.println("spawned tid [" + Long.toString(thrlist.get(j).getId()) + "]"); } catch (Exception e) { System.out.println("Exception: " + e); } j++; if (j >= threads) { j = 0; } } } public static List getDirContents(String dir) { List list = new ArrayList(); File fdir = new File(dir); if (!fdir.isDirectory()) { System.out.println(dir + " is not a directory, exiting"); System.exit(1); } String files[] = fdir.list(); for (String file : files) { File f = new File(dir + "/" + file); if (f.isFile()) { list.add(dir + "/" + file); } else if (f.isDirectory()) { System.out.println("\\-> " + dir + "/" + file); list.addAll(getDirContents(dir + "/" + file)); } } return list; } public String getRandomFilename() { Random gen = new Random(); int i = gen.nextInt(this.filelist.size()); return this.filelist.get(i); } public void run() { long bytes = 0; String filename = this.getRandomFilename(); long tid = Thread.currentThread().getId(); System.out.println("tid[" + Long.toString(tid) + "] reading " + filename + "..."); // Read the file here try { FileReader fin = new FileReader(filename); while (fin.read() > -1) { // File is being read bytes++; } fin.close(); } catch (Exception e) { System.out.println("Exception: " + e); } System.out.println("tid[" + Long.toString(tid) + "] read " + bytes + " bytes from " + filename); return; } }