<commit_before>#ifndef SRC_ZLOG_BENCH_OP_HISTORY_H
#define SRC_ZLOG_BENCH_OP_HISTORY_H
#include <fstream>
#include <iostream>
#include <mutex>
#include <vector>

/*
 *
 */
class OpHistory {
 public:
  OpHistory() {}
  OpHistory(size_t capacity) {
    history_.reserve(capacity);
  }

  void add_latency(uint64_t start, uint64_t latency) {
    std::lock_guard<std::mutex> l(lock_);
    history_.emplace_back(op{start, latency});
  }

  void dump(std::ostream& os) {
    std::lock_guard<std::mutex> l(lock_);
    for (auto& e : history_) {
      os << e.start_ns << " " << e.latency_ns << std::endl;
    }
    os.flush();
  }

  void dump(std::string& output) {
    if (output == "")
      return;
    std::ofstream ofs;
    if (output != "-")
      ofs.open(output, std::ios::out|std::ios::trunc);
    std::ostream& os = output == "-" ? std::cout : ofs;
    dump(os);
  }

 private:
  struct op {
    uint64_t start_ns;
    uint64_t latency_ns;
  };

  std::mutex lock_;
  std::vector<op> history_;
};

#endif
<commit_msg>bench: create latency traces read-only<commit_after>#ifndef SRC_ZLOG_BENCH_OP_HISTORY_H
#define SRC_ZLOG_BENCH_OP_HISTORY_H
#include <fstream>
#include <iostream>
#include <mutex>
#include <vector>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

/*
 *
 */
class OpHistory {
 public:
  OpHistory() {}
  OpHistory(size_t capacity) {
    history_.reserve(capacity);
  }

  void add_latency(uint64_t start, uint64_t latency) {
    std::lock_guard<std::mutex> l(lock_);
    history_.emplace_back(op{start, latency});
  }

  void dump(int fd) {
    std::lock_guard<std::mutex> l(lock_);
    for (auto& e : history_) {
      dprintf(fd, "%llu %llu\n",
          (unsigned long long)e.start_ns,
          (unsigned long long)e.latency_ns);
    }
    fsync(fd);
  }

  void dump(std::string& output) {
    if (output == "")
      return;

    int fd;
    bool close_fd = false;
    if (output == "-")
      fd = fileno(stdout);
    else {
      fd = open(output.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0440);
      assert(fd != -1);
      close_fd = true;
    }

    dump(fd);

    if (close_fd)
      close(fd);
  }

 private:
  struct op {
    uint64_t start_ns;
    uint64_t latency_ns;
  };

  std::mutex lock_;
  std::vector<op> history_;
};

#endif

{'fraction_non_alphanumeric': 0.15620886353663888, 'fraction_numerical': 0.009214567792891619, 'mean_word_length': 3.2144177449168208, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3673809', 'n_tokens_mistral': 913, 'n_tokens_neox': 878, 'n_words': 169}