コード例 #1
0
 @Override
 public int compare(TraceEntry e1, TraceEntry e2) {
   long d0 = e1.getTime() - e2.getTime();
   if (d0 < 0) return -1;
   else if (d0 > 0) return 1;
   return 0;
 }
コード例 #2
0
  private void outputEntry(TraceEntry te) {
    ThreadData td = te.getThread();
    if (td == null) return;

    // System.err.println("TRACE: " + te);

    BdynCallback cb = bdyn_factory.getCallback(te.getEntryLocation());
    if (cb == null) return;

    if (cb.getCallbackType() == CallbackType.CONSTRUCTOR) {
      OutputTask ot = td.getCurrentTransaction();
      if (ot == null) return;
      if (ot.isMainTask() && !BdynFactory.getOptions().useMainTask()) return;
      int i0 = te.getObject1();
      if (i0 != 0) {
        // System.err.println("ASSOC TASK " + i0 + " " + te.getObject2() + " " +
        // ot.getTaskRoot().getDisplayName());
        OutputTask ot1 = object_tasks.get(i0);
        if (ot1 == null) object_tasks.put(i0, ot);
        else if (ot1 != dummy_task && ot1 != ot) object_tasks.put(i0, dummy_task);
      }
      return;
    }

    td.beginTask(te);
    end_time = Math.max(end_time, te.getTime());
  }
コード例 #3
0
  public static void main(String args[]) throws IOException, Exception {
    PhysicalGameState pgs = MapGenerator.basesWorkers8x8Obstacle();
    GameState gs = new GameState(pgs, UnitTypeTable.utt);
    int MAXCYCLES = 5000;
    boolean gameover = false;

    AI ai1 = new RandomBiasedAI();
    AI ai2 = new WorkerRush(UnitTypeTable.utt, new BFSPathFinding());

    Trace trace = new Trace();
    TraceEntry te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
    trace.addEntry(te);

    do {
      PlayerAction pa1 = ai1.getAction(0, gs);
      PlayerAction pa2 = ai2.getAction(1, gs);
      gs.issueSafe(pa1);
      gs.issueSafe(pa2);

      if (!pa1.isEmpty() || !pa2.isEmpty()) {
        te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
        te.addPlayerAction(pa1);
        te.addPlayerAction(pa2);
        trace.addEntry(te);
      }

      // simulate:
      gameover = gs.cycle();
    } while (!gameover && gs.getTime() < MAXCYCLES);

    te = new TraceEntry(gs.getPhysicalGameState().clone(), gs.getTime());
    trace.addEntry(te);

    XMLWriter xml = new XMLWriter(new FileWriter("trace.xml"));
    trace.toxml(xml);
    xml.flush();
  }
コード例 #4
0
 OutputEntry createOutputEntry(TraceEntry te) {
   BdynCallback cb = bdyn_factory.getCallback(te.getEntryLocation());
   OutputEntry oe = new OutputEntry(te.getTime(), 0, this, current_transaction, cb);
   if (te.getUseCount() > 1) oe.setUse(te.getUseCount(), te.getFractionUsed());
   output_set.add(oe);
   if (trace_writer != null)
     trace_writer.println(
         "START " + cb.getId() + " " + nest_level + " " + te.getTime() + " " + oe.hashCode());
   return oe;
 }
コード例 #5
0
    void beginTask(TraceEntry te) {
      BdynCallback cb = bdyn_factory.getCallback(te.getEntryLocation());
      if (cb == null) return;

      if (trace_writer != null)
        trace_writer.println(
            "PROCESS " + te.getEntryLocation() + " " + te.isExit() + " " + te.getTime());

      if (current_transaction == null) {
        OutputTask ot = null;
        if (te.isExit()) return;
        int i0 = te.getObject1();
        int i1 = te.getObject2();
        if (i0 != 0) {
          ot = object_tasks.get(i0);
          if (ot == dummy_task) ot = null;
        }
        if (ot == null) {
          if (i1 != 0) {
            ot = object_tasks.get(i1);
            if (ot == dummy_task) ot = null;
          }
        }
        if (ot == null) {
          ot = new OutputTask(++task_counter, cb);
        }
        current_transaction = ot;
        nest_level = 0;
      }
      if (!te.isExit()) {
        OutputEntry oe = null;
        if (nest_level == 0) {
          oe = createOutputEntry(te);
        } else if (cb.getCallbackType() == CallbackType.KEY
            && BdynFactory.getOptions().useKeyCallback()) {
          for (int i = output_stack.size() - 1; i >= 0; --i) {
            OutputEntry poe = output_stack.get(i);
            if (poe != null) {
              poe.finishAt(te.getTime());
              if (trace_writer != null)
                trace_writer.println(
                    "FINISHINT "
                        + poe.getEntryTask().getId()
                        + " "
                        + nest_level
                        + " "
                        + poe.getTotalTime(0)
                        + " "
                        + poe.hashCode());
              break;
            }
          }
          oe = createOutputEntry(te);
        } else {
          if (trace_writer != null) trace_writer.println("IGNORE " + cb.getId() + " " + nest_level);
          // System.err.println("TRACE: Ignore " + cb.getDisplayName() + " " + nest_level);
        }
        output_stack.push(oe);
        active_threads.add(this);
        ++nest_level;
      } else {
        if (nest_level <= 0) {
          return;
        }
        --nest_level;
        OutputEntry oe = output_stack.pop();
        if (oe != null) {
          // TODO: max_delta doesn't take into account calls that haven't ended yet
          max_delta = Math.max(max_delta, te.getTime() - oe.getStartTime());
          oe.finishAt(te.getTime());
          if (trace_writer != null)
            trace_writer.println(
                "FINISH "
                    + oe.getEntryTask().getId()
                    + " "
                    + nest_level
                    + " "
                    + oe.getTotalTime(0)
                    + " "
                    + oe.hashCode());
          if (!oe.isRelevant()) {
            output_set.remove(oe);
            if (trace_writer != null)
              trace_writer.println("REMOVE KEY " + oe.getEntryTask().getId() + " " + nest_level);
            // System.err.println("REMOVE KEY " + oe.getEntryTask().getDisplayName());
            oe = null;
            for (int i = output_stack.size() - 1; i >= 0; --i) {
              OutputEntry poe = output_stack.get(i);
              if (poe != null) {
                poe.finishAt(0);
                if (trace_writer != null)
                  trace_writer.println(
                      "NO END "
                          + poe.getEntryTask().getId()
                          + " "
                          + nest_level
                          + " "
                          + poe.hashCode());
                break;
              }
            }
          }
        }

        if (last_entry != null
            && oe != null
            && last_entry.getEntryTask() == oe.getEntryTask()
            && (te.getTime() - last_entry.getStartTime()) < MERGE_TIME) {
          last_entry.mergeWith(oe);
          output_set.remove(oe);
          if (trace_writer != null)
            trace_writer.println("MERGE KEY " + oe.getEntryTask().getId() + " " + nest_level);
          // System.err.println("REMOVE " + oe.getEntryTask().getDisplayName());
        }

        if (oe != null) last_entry = oe;

        if (nest_level == 0) endTask();
        else if (oe != null) {
          for (int i = output_stack.size() - 1; i >= 0; --i) {
            OutputEntry poe = output_stack.get(i);
            if (poe != null) {
              if (!poe.isSignificant()) {
                output_set.remove(poe);
              }
              if (trace_writer != null)
                trace_writer.println(
                    "NEST KEY "
                        + poe.getEntryTask().getId()
                        + " "
                        + nest_level
                        + " "
                        + poe.isSignificant());
              oe = new OutputEntry(te.getTime(), 0, this, current_transaction, poe.getEntryTask());
              oe.setDeletable();
              output_set.add(oe);
              output_stack.set(i, oe);
              break;
            }
          }
        }
      }
    }
コード例 #6
0
  /** ***************************************************************************** */
  void addEntry(String s) {
    // System.err.println("TRACX: " + s);
    if (trace_writer != null) trace_writer.println(s);

    char s0 = s.charAt(0);
    if (s0 == 'T') { // THREAD
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      int id = Integer.parseInt(tok.nextToken());
      current_thread = thread_map.get(id);
      if (current_thread == null) {
        int tid = Integer.parseInt(tok.nextToken());
        String tnm = tok.nextToken("\n");
        current_thread = new ThreadData(++thread_counter, tid, tnm);
        thread_map.put(id, current_thread);
      }
      thread_entries = new ArrayList<TraceEntry>();
    } else if (s0 == 'D') { // DONE
      if (trace_writer != null) trace_writer.flush();
      if (thread_entries != null) {
        for (TraceEntry te : thread_entries) pending_entries.add(te);
        thread_entries = null;
      }
      StringTokenizer tok = new StringTokenizer(s);
      tok.nextToken();
      long time = Long.parseLong(tok.nextToken());
      if (next_time != 0) {
        int ct = 0;
        // System.err.println("TRACE: Pending size = " + pending_entries.size());
        while (!pending_entries.isEmpty() && pending_entries.peek().getTime() < next_time) {
          TraceEntry te = pending_entries.remove();
          ++ct;
          outputEntry(te);
        }
        if (ct > 0) {
          for (BdynEventUpdater eu : update_listeners) {
            eu.eventsAdded();
          }
        }
        end_time = Math.max(next_time, end_time);
      }
      next_time = time;
    } else if (s0 == 'S') {
      if (cpu_time == null) {
        StringTokenizer tok = new StringTokenizer(s);
        tok.nextToken();
        cpu_time = Boolean.parseBoolean(tok.nextToken());
      }
    } else if (cpu_time != null) {
      TraceEntry te = new TraceEntry(s, current_thread, cpu_time);
      if (thread_entries == null) pending_entries.add(te);
      else {
        thread_entries.add(te);
        if (te.isExit()) {
          int sz = thread_entries.size();
          int loc = te.getEntryLocation();
          if (sz >= 4) {
            TraceEntry t3 = thread_entries.get(sz - 2);
            if (!t3.isExit() && t3.getEntryLocation() == loc) {
              TraceEntry t2 = thread_entries.get(sz - 3);
              if (t2.isExit() && t2.getEntryLocation() == loc) {
                TraceEntry t1 = thread_entries.get(sz - 4);
                if (!t1.isExit()
                    && t1.getEntryLocation() == loc
                    && (te.getTime() - t1.getTime()) < MERGE_TIME) {
                  t1.merge(t2.getTime(), t3.getTime(), te.getTime());
                  thread_entries.remove(sz - 2);
                  thread_entries.remove(sz - 3);
                }
              }
            }
          }
        }
      }
    }
  }