private void increment(String eventType) {
   ProcessStats eventStats = stats.get(eventType);
   if (eventStats == null) {
     stats.putIfAbsent(eventType, new ProcessStats(executor, eventClient, eventType));
     eventStats = stats.get(eventType);
     eventStats.start();
   }
   eventStats.processed(1);
 }
  @Override
  public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject(Fields.COUNT);
    counts.toXContent(builder, params);
    builder.endObject();

    builder.startArray(Fields.VERSIONS);
    for (Version v : versions) {
      builder.value(v.toString());
    }
    builder.endArray();

    builder.startObject(Fields.OS);
    os.toXContent(builder, params);
    builder.endObject();

    builder.startObject(Fields.PROCESS);
    process.toXContent(builder, params);
    builder.endObject();

    builder.startObject(Fields.JVM);
    jvm.toXContent(builder, params);
    builder.endObject();

    builder.field(Fields.FS);
    fs.toXContent(builder, params);

    builder.startArray(Fields.PLUGINS);
    for (PluginInfo pluginInfo : plugins) {
      pluginInfo.toXContent(builder, params);
    }
    builder.endArray();
    return builder;
  }
 @Override
 public void writeTo(StreamOutput out) throws IOException {
   counts.writeTo(out);
   out.writeVInt(versions.size());
   for (Version v : versions) Version.writeVersion(v, out);
   os.writeTo(out);
   process.writeTo(out);
   jvm.writeTo(out);
   fs.writeTo(out);
   out.writeVInt(plugins.size());
   for (PluginInfo p : plugins) {
     p.writeTo(out);
   }
 }
  @Override
  public void readFrom(StreamInput in) throws IOException {
    counts = Counts.readCounts(in);

    int size = in.readVInt();
    versions = new HashSet<Version>(size);
    for (; size > 0; size--) {
      versions.add(Version.readVersion(in));
    }

    os = OsStats.readOsStats(in);
    process = ProcessStats.readStats(in);
    jvm = JvmStats.readJvmStats(in);
    fs = FsStats.Info.readInfoFrom(in);

    size = in.readVInt();
    plugins = new HashSet<PluginInfo>(size);
    for (; size > 0; size--) {
      plugins.add(PluginInfo.readPluginInfo(in));
    }
  }
  public ClusterStatsNodes(ClusterStatsNodeResponse[] nodeResponses) {
    this.counts = new Counts();
    this.versions = new HashSet<Version>();
    this.os = new OsStats();
    this.jvm = new JvmStats();
    this.fs = new FsStats.Info();
    this.plugins = new HashSet<PluginInfo>();
    this.process = new ProcessStats();

    Set<InetAddress> seenAddresses = new HashSet<InetAddress>(nodeResponses.length);

    for (ClusterStatsNodeResponse nodeResponse : nodeResponses) {

      counts.addNodeInfo(nodeResponse.nodeInfo());
      versions.add(nodeResponse.nodeInfo().getVersion());
      process.addNodeStats(nodeResponse.nodeStats());
      jvm.addNodeInfoStats(nodeResponse.nodeInfo(), nodeResponse.nodeStats());
      plugins.addAll(nodeResponse.nodeInfo().getPlugins().getInfos());

      // now do the stats that should be deduped by hardware (implemented by ip deduping)
      TransportAddress publishAddress =
          nodeResponse.nodeInfo().getTransport().address().publishAddress();
      InetAddress inetAddress = null;
      if (publishAddress.uniqueAddressTypeId() == 1) {
        inetAddress = ((InetSocketTransportAddress) publishAddress).address().getAddress();
      }

      if (!seenAddresses.add(inetAddress)) {
        continue;
      }

      os.addNodeInfo(nodeResponse.nodeInfo());
      if (nodeResponse.nodeStats().getFs() != null) {
        fs.add(nodeResponse.nodeStats().getFs().total());
      }
    }
  }
 public static ProcessStats readStats(StreamInput in) throws IOException {
   ProcessStats cpu = new ProcessStats();
   cpu.readFrom(in);
   return cpu;
 }