<commit_before>// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network/<NAME> <<EMAIL>>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include <google/heap-profiler.h>
#include <google/malloc_extension.h>
#include "heap_profiler.h"
#include "common/LogClient.h"

bool ceph_using_tcmalloc()
{
  return true;
}

void ceph_heap_profiler_init()
{
  char profile_name[PATH_MAX];
  sprintf(profile_name, "%s/%s", g_conf.log_dir, g_conf.name);
  char *val = new char[sizeof(int)*8+1];
  sprintf(val, "%i", g_conf.profiler_allocation_interval);
  setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
  sprintf(val, "%i", g_conf.profiler_highwater_interval);
  setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
  if (g_conf.tcmalloc_profiler_run) {
    generic_dout(0) << "turning on heap profiler with prefix " << profile_name << dendl;
    HeapProfilerStart(profile_name);
  }
}

void ceph_heap_profiler_stats(char *buf, int length)
{
  MallocExtension::instance()->GetStats(buf, length);
}

void ceph_heap_release_free_memory()
{
  MallocExtension::instance()->ReleaseFreeMemory();
}

bool ceph_heap_profiler_running()
{
  return IsHeapProfilerRunning();
}

void ceph_heap_profiler_start()
{
  char profile_name[PATH_MAX];
  sprintf(profile_name, "%s/%s", g_conf.log_dir, g_conf.name);
  char *val = new char[sizeof(int)*8+1];
  sprintf(val, "%i", g_conf.profiler_allocation_interval);
  setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
  sprintf(val, "%i", g_conf.profiler_highwater_interval);
  setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
  generic_dout(0) << "turning on heap profiler with prefix " << profile_name << dendl;
  HeapProfilerStart(profile_name);
}

void ceph_heap_profiler_stop()
{
  HeapProfilerStop();
}

void ceph_heap_profiler_dump(const char *reason)
{
  HeapProfilerDump(reason);
}

void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
                                       LogClient &clog)
{
  if (cmd.size() == 2 && cmd[1] == "dump") {
    char *heap_stats = new char[1024];
    ceph_heap_profiler_stats(heap_stats, 1024);
    clog.info() << g_conf.name << "dumping heap profile now.\n"
        << heap_stats << std::endl;
    ceph_heap_profiler_dump("admin request");
  } else if (cmd.size() == 2 && cmd[1] == "export_config") {
    char val[sizeof(int)*8+1];
    snprintf(val, sizeof(val), "%i", g_conf.profiler_allocation_interval);
    setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
    snprintf(val, sizeof(val), "%i", g_conf.profiler_highwater_interval);
    setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
    clog.info() << g_conf.name << " set heap variables from current config";
  } else if (cmd.size() == 2 && cmd[1] == "start_profiler") {
    ceph_heap_profiler_start();
    clog.info() << g_conf.name << " started profiler \n";
  } else if (cmd.size() == 2 && cmd[1] == "stop_profiler") {
    ceph_heap_profiler_stop();
    clog.info() << g_conf.name << " stopped profiler\n";
  } else if (cmd.size() == 2 && cmd[1] == "release") {<commit_msg>tcmalloc: warn if you try and dump without the profiler running.<commit_after>// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 New Dream Network/<NAME> <<EMAIL>>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include <google/heap-profiler.h>
#include <google/malloc_extension.h>
#include "heap_profiler.h"
#include "common/LogClient.h"

bool ceph_using_tcmalloc()
{
  return true;
}

void ceph_heap_profiler_init()
{
  char profile_name[PATH_MAX];
  sprintf(profile_name, "%s/%s", g_conf.log_dir, g_conf.name);
  char *val = new char[sizeof(int)*8+1];
  sprintf(val, "%i", g_conf.profiler_allocation_interval);
  setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
  sprintf(val, "%i", g_conf.profiler_highwater_interval);
  setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
  if (g_conf.tcmalloc_profiler_run) {
    generic_dout(0) << "turning on heap profiler with prefix " << profile_name << dendl;
    HeapProfilerStart(profile_name);
  }
}

void ceph_heap_profiler_stats(char *buf, int length)
{
  MallocExtension::instance()->GetStats(buf, length);
}

void ceph_heap_release_free_memory()
{
  MallocExtension::instance()->ReleaseFreeMemory();
}

bool ceph_heap_profiler_running()
{
  return IsHeapProfilerRunning();
}

void ceph_heap_profiler_start()
{
  char profile_name[PATH_MAX];
  sprintf(profile_name, "%s/%s", g_conf.log_dir, g_conf.name);
  char *val = new char[sizeof(int)*8+1];
  sprintf(val, "%i", g_conf.profiler_allocation_interval);
  setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
  sprintf(val, "%i", g_conf.profiler_highwater_interval);
  setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
  generic_dout(0) << "turning on heap profiler with prefix " << profile_name << dendl;
  HeapProfilerStart(profile_name);
}

void ceph_heap_profiler_stop()
{
  HeapProfilerStop();
}

void ceph_heap_profiler_dump(const char *reason)
{
  HeapProfilerDump(reason);
}

void ceph_heap_profiler_handle_command(const std::vector<std::string>& cmd,
                                       LogClient &clog)
{
  if (cmd.size() == 2 && cmd[1] == "dump") {
    if (!ceph_heap_profiler_running())
      clog.info() << "heap profiler not running; can't dump!\n";
    else {
      char *heap_stats = new char[1024];
      ceph_heap_profiler_stats(heap_stats, 1024);
      clog.info() << g_conf.name << "dumping heap profile now.\n"
          << heap_stats << std::endl;
      ceph_heap_profiler_dump("admin request");
    }
  } else if (cmd.size() == 2 && cmd[1] == "export_config") {
    char val[sizeof(int)*8+1];
    snprintf(val, sizeof(val), "%i", g_conf.profiler_allocation_interval);
    setenv("HEAP_PROFILE_ALLOCATION_INTERVAL", val, g_conf.profiler_allocation_interval);
    snprintf(val, sizeof(val), "%i", g_conf.profiler_highwater_interval);
    setenv("HEAP_PROFILE_INUSE_INTERVAL", val, g_conf.profiler_highwater_interval);
    clog.info() << g_conf.name << " set heap variables from current config";
  } else if (cmd.size() == 2 && cmd[1] == "start_profiler") {
    ceph_heap_profiler_start();
    clog.info() << g_conf.name << " started profiler \n";
  } else if (cmd.size() == 2 && cmd[1] == "stop_profiler") {
    ceph_heap_profiler_stop();
    clog.info() << g_conf.name << " stopped profiler\n";
  } else if (cmd.size() == 2 && cmd[1] == "release") {
{'fraction_non_alphanumeric': 0.14453991130820398, 'fraction_numerical': 0.009977827050997782, 'mean_word_length': 4.8914285714285715, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5874655', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2659, 'n_words': 508}