runtime/cpp/src/builtins/ProfilingBuiltins.cpp
// gather statistics for the allocated size over time
// No need for jemalloc profiling (slow)
gather statistics for the allocated size over time
No need for jemalloc profiling (slow)
gather statistics for the allocated size over time No need for jemalloc profiling ( slow )
unit_t ProfilingBuiltins::jemallocTotalSizeStart(unit_t) {
#ifdef K3_JEMALLOC_HEAP_SIZE
    std::cout << "Starting JEMALLOC total memory size tracking. Period: " << period_ << "\n";
    mallctlnametomib("stats.allocated", mib_, &miblen_);
    auto init = []() {
      std::string name = "heap_size.txt";
      std::ofstream file;
      file.open(name, std::ios::trunc);
      file.close();
      return name;
    };
    auto body = [this](std::string& name, int) {
      size_t epoch = 1;
      size_t epoch_sz = sizeof(epoch);
      mallctl("epoch", &epoch, &epoch_sz, &epoch, epoch_sz);
      const int threshold = 100; 
      size_t alloc_sz = 0;
      size_t alloc_sz_len = sizeof(alloc_sz);
      uint64_t time = time_milli();
      mallctl("stats.allocated", &alloc_sz, &alloc_sz_len, NULL, 0);
      stats_.push_back(std::make_pair(time, (uint64_t)alloc_sz));
      if (stats_.size() > threshold) {
        dumpStatsToFile(name);
      }
    };
    auto shutdown = [this](std::string& name) {
      if (!stats_.empty()) {
        dumpStatsToFile(name);
      }
    };
    heap_series_start(init, body, shutdown);
#endif 
    return unit_t();
  }
unit_t ProfilingBuiltins :: jemallocTotalSizeStart ( unit_t ) { #ifdef K3_JEMALLOC_HEAP_SIZE std :: cout << " " << period_ << " \n " ; mallctlnametomib ( " " , mib_ , & miblen_ ) ; auto init = [ ] ( ) { std :: string name = " " ; std :: ofstream file ; file . open ( name , std :: ios :: trunc ) ; file . close ( ) ; return name ; } ; auto body = [ this ] ( std :: string & name , int ) { size_t epoch = 1 ; size_t epoch_sz = sizeof ( epoch ) ; mallctl ( " " , & epoch , & epoch_sz , & epoch , epoch_sz ) ; const int threshold = 100 ; size_t alloc_sz = 0 ; size_t alloc_sz_len = sizeof ( alloc_sz ) ; uint64_t time = time_milli ( ) ; mallctl ( " " , & alloc_sz , & alloc_sz_len , NULL , 0 ) ; stats_ . push_back ( std :: make_pair ( time , ( uint64_t ) alloc_sz ) ) ; if ( stats_ . size ( ) > threshold ) { dumpStatsToFile ( name ) ; } } ; auto shutdown = [ this ] ( std :: string & name ) { if ( ! stats_ . empty ( ) ) { dumpStatsToFile ( name ) ; } } ; heap_series_start ( init , body , shutdown ) ; #endif return unit_t ( ) ; }
gather statistics for the allocated size over time
No need for jemalloc profiling (slow)
gather statistics for the allocated size over time No need for jemalloc profiling ( slow )
// clear the existing file // Get time & allocated size and save them // Needed to refresh mallctl, otherwise we have stale data // how often to save (how many samples) // check if we need to dump // Make sure we clear any remaining stats // K3_JEMALLOC_HEAP_SIZE