#ifndef MEMORY_UTIL_H
#define MEMORY_UTIL_H

#include <unistd.h>
#include <ios>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

//////////////////////////////////////////////////////////////////////////////
//
// process_mem_usage(double &, double &) - takes two doubles by reference,
// attempts to read the system-dependent data for a process' virtual memory
// size and resident set size, and return the results in KB.
//
// On failure, returns 0.0, 0.0

void process_mem_usage(double& vm_usage, double& resident_set)
{
    using std::ios_base;
    using std::ifstream;
    using std::string;
    
    vm_usage     = 0.0;
    resident_set = 0.0;
    
    // 'file' stat seems to give the most reliable results
    //
    ifstream stat_stream("/proc/self/stat",ios_base::in);
    
    // dummy vars for leading entries in stat that we don't care about
    //
    string pid, comm, state, ppid, pgrp, session, tty_nr;
    string tpgid, flags, minflt, cminflt, majflt, cmajflt;
    string utime, stime, cutime, cstime, priority, nice;
    string O, itrealvalue, starttime;
    
    // the two fields we want
    //
    unsigned long vsize;
    long rss;
    
    stat_stream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr
    >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt
    >> utime >> stime >> cutime >> cstime >> priority >> nice
    >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest
    
    stat_stream.close();
    
    long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
    vm_usage     = vsize / 1024.0;
    resident_set = rss * page_size_kb;
}

void print_CPU_Info()
{
    double vm,rss;
    process_mem_usage(vm, rss);
    std::cout <<"VM: " << vm <<" RSS: " << rss <<"\n";
}

// for string

std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
    std::stringstream ss(s);
    std::string item;
    while (std::getline(ss, item, delim)) {
        elems.push_back(item);
    }
    return elems;
}


std::vector<std::string> split(const std::string &s, char delim) {
    std::vector<std::string> elems;
    split(s, delim, elems);
    return elems;
}


class Timer {
    
    std::unordered_map<std::string,std::chrono::time_point<std::chrono::system_clock>> starts;
    std::unordered_map<std::string,std::chrono::duration<double>> durations;

public:
  
    void start(std::string key){
        this->starts[key] = std::chrono::system_clock::now();
    }
    
    
    void end(std::string key){
        std::chrono::duration<double> dur = std::chrono::duration<double>::zero();
        dur = std::chrono::system_clock::now() - this->starts[key];
        if (durations.count(key) == 0){
            durations[key] = dur;
        } else {
            durations[key] += dur;
        }
    }
    
    void report(std::string key){
        std::chrono::duration<double> dur = durations[key];
        std::cout<<key<<" :"<<dur.count()<<" sec\n";
    }
    
    void report(){
        for (auto const & i : durations ){
            std::string key = i.first;
            report(key);
        }
    }
    
    void clear(){
        this->starts.clear();
        this->durations.clear();
    }
    
    
    
};


#endif
