int32_t TFunctionStatHandler::consolidateStats()

in fb303/TFunctionStatHandler.cpp [281:405]


int32_t TFunctionStatHandler::consolidateStats(
    time_t now,
    const std::string& fnName,
    TStatsPerThread& spt) {
  std::unique_lock lock(spt.mutex_);

  auto calls = spt.calls_;
  auto addAllValues = [&](const auto& prefix) {
    // update counts
    statMapSum_.addValue(prefix + ".num_calls", now, spt.calls_);
    statMapSum_.addValue(prefix + ".num_reads", now, spt.readData_.count);
    statMapSum_.addValue(prefix + ".num_writes", now, spt.writeData_.count);
    statMapSum_.addValue(prefix + ".num_processed", now, spt.processed_);
    // userExceptions is Thrift name for all exceptions escaped from the handler
    // counter is named differently to better represent what it actually means
    statMapSum_.addValue(
        prefix + ".num_all_exceptions", now, spt.userExceptions_);
    // this counter only includes exceptions not declared in the Thrift schema
    statMapSum_.addValue(prefix + ".num_exceptions", now, spt.exceptions_);
    statMapSum_.addValue(prefix + ".num_samples", now, spt.samples_);
    statMapSum_.addValue(prefix + ".bytes_read", now, spt.readData_.sum);
    statMapSum_.addValue(prefix + ".bytes_written", now, spt.writeData_.sum);

    if (spt.requestStatsMeasureRate_ > 1e-9) {
      statMapAvg_.addValue(
          prefix + ".request_stats_rate",
          now,
          1 / spt.requestStatsMeasureRate_);
    }
    if (spt.requestStatsLogRate_ > 1e-9) {
      statMapAvg_.addValue(
          prefix + ".request_stats_log_rate",
          now,
          1 / spt.requestStatsLogRate_);
    }

    // update averages
    statMapAvg_.addValueAggregated(
        prefix + ".bytes_read", now, spt.readData_.sum, spt.readData_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".bytes_written",
        now,
        spt.writeData_.sum,
        spt.writeData_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".time_read_us", now, spt.readTime_.sum, spt.readTime_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".time_write_us",
        now,
        spt.writeTime_.sum,
        spt.writeTime_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".time_process_us",
        now,
        spt.processTime_.sum,
        spt.processTime_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".total_cpu_us",
        now,
        spt.totalCpuTime_.sum,
        spt.totalCpuTime_.count);
    statMapAvg_.addValueAggregated(
        prefix + ".total_worked_us",
        now,
        spt.totalWorkedTime_.sum,
        spt.totalWorkedTime_.count);

    // update histogram
    if (spt.readTime_.hist.isEnabled()) {
      histogramMap_.addValues(
          prefix + ".time_read_us",
          now,
          *spt.readTime_.hist.getHistogram(),
          spt.readTime_.hist.getExportedHistogram(),
          spt.readTime_.hist.getPercentiles());
    }

    if (spt.writeTime_.hist.isEnabled()) {
      histogramMap_.addValues(
          prefix + ".time_write_us",
          now,
          *spt.writeTime_.hist.getHistogram(),
          spt.writeTime_.hist.getExportedHistogram(),
          spt.writeTime_.hist.getPercentiles());
    }

    if (spt.processTime_.hist.isEnabled()) {
      histogramMap_.addValues(
          prefix + ".time_process_us",
          now,
          *spt.processTime_.hist.getHistogram(),
          spt.processTime_.hist.getExportedHistogram(),
          spt.processTime_.hist.getPercentiles());
    }
    if (spt.readData_.hist.isEnabled()) {
      histogramMap_.addValues(
          prefix + ".bytes_read",
          now,
          *spt.readData_.hist.getHistogram(),
          spt.readData_.hist.getExportedHistogram(),
          spt.readData_.hist.getPercentiles());
    }
    if (spt.writeData_.hist.isEnabled()) {
      histogramMap_.addValues(
          prefix + ".bytes_written",
          now,
          *spt.writeData_.hist.getHistogram(),
          spt.writeData_.hist.getExportedHistogram(),
          spt.writeData_.hist.getPercentiles());
    }
  };

  addAllValues(counterNamePrefix_ + fnName);

  // update sample rate
  if (spt.calls_ > 0) {
    spt.setSampleRate(desiredSamplesPerPeriod_ / nThreads_ / spt.calls_);
  } else {
    spt.setSampleRate(1.0);
  }

  // zero out stats for new period
  spt.clear();
  return calls;
}