// Профилировщик: счётчики профилирования GC (PROFGC) и ioarena
// Раздел учебника: Том IV, глава 26, «Бенчмарки и измерения», §26.3 «PROFGC».
// Что демонстрирует: чтение gc_prof-полей из commit_latency (wloops,
//   coalescences, flushes, kicks, max_reader_lag, max_retained_pages и др.).
//   Поля заполняются только в сборках с MDBX_ENABLE_PROFGC (иначе нули).
// CLI для бенчмарков: ioarena (github.com/erthink/ioarena), команда вида
//   `ioarena -e db -E mdbx://<path> --tasks=...`.
// Сборка: g++ -std=c++17 -I<libmdbx-include> -I../common 33-profiler.c++ -lmdbx
// Запуск: ./33-profiler
// Ожидаемый вывод:
//   gc_prof: wloops=<N> coalescences=<M> flushes=<K> kicks=<J> max_reader_lag=<L> max_retained_pages=<P>
//   ok: PROFGC counters read
// При сбое — сообщение в stderr и exit 1.
//
// SPDX-License-Identifier: Apache-2.0

#include <iostream>
#include <string>

#include "common.h++"
#include <mdbx.h++>

int main() {
  try {
    const std::string path = std::string(example::tmpdir()) + "/mdbx-33-profiler.mdbx";
    mdbx::env::remove(path);
    mdbx::env_managed env = example::env_open(path);

    // Нагрузка, порождающая работу GC: вставки и удаления.
    for (int round = 0; round < 5; ++round) {
      auto txn = env.start_write();
      auto table = txn.open_map(nullptr);
      for (int i = 0; i < 500; ++i) {
        const auto key = "k" + std::to_string(round * 1000 + i);
        txn.insert(table, mdbx::slice(key), mdbx::slice("payload"));
      }
      txn.commit();
      auto del = env.start_write();
      auto dtable = del.open_map(nullptr);
      for (int i = 0; i < 500; ++i)
        del.erase(dtable, mdbx::slice("k" + std::to_string(round * 1000 + i)));
      del.commit();
    }

    auto txn = env.start_write();
    auto table = txn.open_map(nullptr);
    txn.insert(table, mdbx::slice("last"), mdbx::slice("v"));
    const auto lat = txn.commit_get_latency();

    const auto &gc = lat.gc_prof;
    std::cout << "gc_prof: wloops=" << gc.wloops << " coalescences=" << gc.coalescences << " flushes=" << gc.flushes
              << " kicks=" << gc.kicks << " max_reader_lag=" << gc.max_reader_lag
              << " max_retained_pages=" << gc.max_retained_pages << "\n";

    std::cout << "ok: PROFGC counters read\n";
    return EXIT_SUCCESS;
  } catch (const std::exception &ex) {
    std::cerr << "error: " << ex.what() << "\n";
    return EXIT_FAILURE;
  }
}