totalMessageBytes = messageStatsService.getMessageStatistics(false).totalMessageBytes;
totalMessageBytes = messageStatsService.getMessageStatistics(false).totalSentMessageBytes;
  private void sampleStatistics() {    long currentTime = System.currentTimeMillis();    // MessageStatsService    double averageMessageQueueLength = 0.0;    long totalMessageBytes = 0l;    long totalMessageCount = 0l;    if (messageStatsService != null) {      averageMessageQueueLength = messageStatsService.getMessageStatistics(false).averageMessageQueueLength;      averageMessageQueueLength = (averageMessageQueueLength == -1 ? 0.0 : averageMessageQueueLength);      totalMessageBytes = messageStatsService.getMessageStatistics(false).totalMessageBytes;      totalMessageBytes = (totalMessageBytes == -1 ? 0 : totalMessageBytes);      totalMessageCount = messageStatsService.getMessageStatistics(false).totalMessageCount;      totalMessageCount = (totalMessageCount == -1 ? 0 : totalMessageCount);    }    // Blackboard Statistics    int assetCount = 0;    int planElementCount = 0;    int bbTaskCount = 0;    int bbObjCount = 0;    if (bbMetricsService != null) {      assetCount = bbMetricsService.getBlackboardCount(Asset.class);      planElementCount = bbMetricsService.getBlackboardCount(PlanElement.class);      bbTaskCount = bbMetricsService.getBlackboardCount(Task.class);      bbObjCount = bbMetricsService.getBlackboardCount();    }    // Prototype Registry Statistics    int cachedPrototypeCount = 0;    int propProvCount = 0;    int protoProvCount = 0;    if (protoRegistryService != null) {      cachedPrototypeCount = protoRegistryService.getCachedPrototypeCount();      propProvCount = protoRegistryService.getPropertyProviderCount();      protoProvCount = protoRegistryService.getPrototypeProviderCount();    }    // NodeMetricsService    long freeMemory = 0l;    long totalMemory = 0l;    int activeThreadCount = 0;    // FIXME! These duplicate the Node statistics!!    if (nodeMetricsService != null) {      freeMemory = nodeMetricsService.getFreeMemory();      totalMemory = nodeMetricsService.getTotalMemory();      activeThreadCount = nodeMetricsService.getActiveThreadCount();    } else {      freeMemory = Runtime.getRuntime().freeMemory();      totalMemory = Runtime.getRuntime().totalMemory();    }    // MessageTransport Stats    int directivesIn = 0;    int directivesOut = 0;    int notificationsIn = 0;    int notificationsOut = 0;    if (messageWatchService != null) {      directivesIn = _messageWatcher.getDirectivesIn();      directivesOut = _messageWatcher.getDirectivesOut();      notificationsIn = _messageWatcher.getNotificationsIn();      notificationsOut = _messageWatcher.getNotificationsOut();    }    long usedMemory = totalMemory - freeMemory;    long elapsedTime = currentTime - startTime;    long cpu = CpuClock.cpuTimeMillis();    long cpuTime = cpu - startCPU;    int deltaPlanElementCount = completedPlanElementCount - startPlanElementCount;    startTime += elapsedTime;    startCPU += cpuTime; // aka startCPU = cpu    startPlanElementCount += deltaPlanElementCount;    writer.print(elapsedTime/1000.0);    writer.print("\t");    writer.print(cpuTime/1000.0);    writer.print("\t");    writer.print(usedMemory/(1024.0*1024.0));    writer.print("\t");    writer.print(totalMemory/(1024.0*1024.0));    writer.print("\t");    if (wantMessStats) {      writer.print(averageMessageQueueLength + "\t");      writer.print(totalMessageBytes + "\t");      writer.print(totalMessageCount);    }    writer.print("\t");    writer.print(deltaPlanElementCount);    double timeDelta = (timeFirstMyTask == 0l ? 0l : (System.currentTimeMillis() - timeFirstMyTask) / 1000.0);    if (myTaskSearch != null) {      if (timeFirstMyTask == 0l) {	writer.print("\t-1\t");      } else {	writer.print("\t" + timeDelta + "\t");      }      writer.print(countMyTasksThisInterval + "\t");      writer.print(countMyTasks);    }    if (wantBBStats) {      writer.print("\t" + assetCount);      writer.print("\t" + planElementCount);      writer.print("\t" + bbTaskCount);      writer.print("\t" + bbObjCount);    } // end of if ()    if (wantProtoRegStats) {      writer.print("\t" + cachedPrototypeCount);      writer.print("\t" + propProvCount);      writer.print("\t" + protoProvCount);    } // end of if ()    if (wantNodeStats) {      writer.print("\t" + activeThreadCount);      writer.print("\t" + freeMemory);      writer.print("\t" + totalMemory);    } // end of if ()    if (wantMessWatchStats) {      writer.print("\t" + directivesIn);      writer.print("\t" + directivesOut);      writer.print("\t" + notificationsIn);      writer.print("\t" + notificationsOut);    } // end of if ()    writer.println();    if (log.isDebugEnabled()) {      log.debug("Duration     : " + timeFormat.format(elapsedTime/1000.0) + "\n" +	      "CPU          : " + timeFormat.format(cpuTime/1000.0) + "\n" +	      "Used Memory  : " + memoryFormat.format(usedMemory/(1024.0*1024.0)) + "\n" +	      "Total Memory : " + memoryFormat.format(totalMemory/(1024.0*1024.0)) + "\n");      if (wantMessStats) {	log.debug(  	      "Message Queue: " + averageMessageQueueLength + "\n" +  	      "Message Bytes: " + totalMessageBytes + "\n" +		"Message Count: " + totalMessageCount + "\n");      }      log.debug("Tasks Done   : " + deltaPlanElementCount);      if (myTaskSearch != null) {	if (timeFirstMyTask == 0l) {	  log.debug("Time (sec) since first task of Verb " + searchVerb + ": <Not seen yet>\n");	} else {	  log.debug("Time (sec) since first task of Verb " + searchVerb + ": " + timeDelta + "\n");	}	log.debug("Num Tasks this interval of Verb " + searchVerb + ": " + countMyTasksThisInterval + "\n");	log.debug("Total Tasks seen with Verb " + searchVerb + ": " + countMyTasks + "\n");      }      // Blackboard stats      if (wantBBStats) {	log.debug("Total Assets on BBoard: " + assetCount + "\n");	log.debug("Total PlanElements on BBoard: " + planElementCount + "\n");	log.debug("Total Tasks on BBoard: " + bbTaskCount + "\n");	log.debug("Total Objects on BBoard: " + bbObjCount + "\n");      }      // ProtypeRegistryStats      if (wantProtoRegStats) {	log.debug("Cached Prototypes: " + cachedPrototypeCount + "\n");	log.debug("Property Providers: " + propProvCount + "\n");	log.debug("Prototype Providers: " + protoProvCount + "\n");      }      // Node Statistics      if (wantNodeStats) {	log.debug("Active COUGAAR Threads: " + activeThreadCount + "\n");	log.debug("Free memory in Node's allocation (bytes): " + freeMemory + "\n");	log.debug("Total memory for Node (bytes): " + totalMemory + "\n");      }      // MessageWatcher Stats      if (wantMessWatchStats) {	log.debug("Directives received by Agent: " + directivesIn + "\n");	log.debug("Directives sent by Agent: " + directivesOut + "\n");	log.debug("Notifications received by Agent: " + notificationsIn + "\n");	log.debug("Notifications sent by Agent: " + notificationsOut + "\n");      }    }    // reset task count variable    countMyTasksThisInterval = 0;    // Flush the writer?    writer.flush();  } // end of sampleStatistics()