<commit_before>//-----------------------------------
// Copyright <NAME> 2013-2017
//-----------------------------------

#include <iostream>
#include <fstream>

#include "LinuxUtils.h"
#include "Utils.h"
#include "PrintVar.h"
#include "OrbitModule.h"
#include "Path.h"
#include "ScopeTimer.h"
#include "Capture.h"
#include "Callstack.h"
#include "SamplingProfiler.h"
#include "EventBuffer.h"
#include "Capture.h"
#include "ScopeTimer.h"
#include "OrbitProcess.h"
#include "ConnectionManager.h"
#include "TcpClient.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <string.h>
#include <iomanip>
#include <signal.h>
#include <sys/wait.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <memory>
#include <cxxabi.h>

namespace LinuxUtils {

//-----------------------------------------------------------------------------
std::vector<std::string> ListModules( uint32_t a_PID )
{
    std::vector<std::string> modules;
    std::string result = ExecuteCommand( Format("cat /proc/%u/maps", a_PID).c_str() );

    std::stringstream ss(result);
    std::string line;
    while(std::getline(ss,line,'\n'))
    {
        modules.push_back(line);
    }

    return modules;
}

//-----------------------------------------------------------------------------
std::string ExecuteCommand( const char* a_Cmd ) 
{
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(a_Cmd, "r"), pclose);
    if (!pipe) {
        std::cout << "Could not open pipe" << std::endl;
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}

//-----------------------------------------------------------------------------
void ListModules( uint32_t a_PID, std::map< uint64_t, std::shared_ptr<Module> > & o_ModuleMap )
{
    std::map<std::string, std::shared_ptr<Module>> modules;
    std::vector<std::string> result = ListModules( a_PID );
    for( const std::string& line : result )
    {
        std::vector<std::string> tokens = Tokenize( line ); 
        if( tokens.size() == 6 )
        {
            std::string& moduleName = tokens[5];
            
            auto addresses = Tokenize(tokens[0], "-");
            if( addresses.size() == 2 )
            {
                auto iter = modules.find(moduleName);
                std::shared_ptr<Module> module = 
                    (iter == modules.end()) ? std::make_shared<Module>() : iter->second;

                uint64_t start = std::stoull(addresses[0], nullptr, 16);
                uint64_t end = std::stoull(addresses[1], nullptr, 16);
                
                if( module->m_AddressStart == 0 || start < module->m_AddressStart )
                    module->m_AddressStart = start;
                if( end > module->m_AddressEnd )
                    module->m_AddressEnd = end;
                
                module->m_FullName = moduleName;
                module->m_Name = ws2s(Path::GetFileName( s2ws(module->m_FullName) ));
                module->m_Directory = ws2s(Path::GetDirectory( s2ws(module->m_FullName) ));
                auto prettyName = module->GetPrettyName();
                modules[moduleName] = module;
            }
        }
    }

    for( auto& iter : modules )
    {
        auto module = iter.second;
        o_ModuleMap[module->m_AddressStart] = module;
    }
}

//-----------------------------------------------------------------------------
uint32_t GetPID(const char* a_Name)
{
    std::string command = Format("ps -A | grep %s", a_Name);
    std::string result = ExecuteCommand( command.c_str() );<commit_msg>Also display file size on Linux.<commit_after>//-----------------------------------
// Copyright <NAME> 2013-2017
//-----------------------------------

#include <iostream>
#include <fstream>

#include "LinuxUtils.h"
#include "Utils.h"
#include "PrintVar.h"
#include "OrbitModule.h"
#include "Path.h"
#include "ScopeTimer.h"
#include "Capture.h"
#include "Callstack.h"
#include "SamplingProfiler.h"
#include "EventBuffer.h"
#include "Capture.h"
#include "ScopeTimer.h"
#include "OrbitProcess.h"
#include "ConnectionManager.h"
#include "TcpClient.h"

#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <linux/types.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <string.h>
#include <iomanip>
#include <signal.h>
#include <sys/wait.h>
#include <sstream>
#include <string>
#include <cstdlib>
#include <memory>
#include <cxxabi.h>

namespace LinuxUtils {

//-----------------------------------------------------------------------------
std::vector<std::string> ListModules( uint32_t a_PID )
{
    std::vector<std::string> modules;
    std::string result = ExecuteCommand( Format("cat /proc/%u/maps", a_PID).c_str() );

    std::stringstream ss(result);
    std::string line;
    while(std::getline(ss,line,'\n'))
    {
        modules.push_back(line);
    }

    return modules;
}

//-----------------------------------------------------------------------------
std::string ExecuteCommand( const char* a_Cmd ) 
{
    std::array<char, 128> buffer;
    std::string result;
    std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(a_Cmd, "r"), pclose);
    if (!pipe) {
        std::cout << "Could not open pipe" << std::endl;
    }
    while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
        result += buffer.data();
    }
    return result;
}

//-----------------------------------------------------------------------------
void ListModules( uint32_t a_PID, std::map< uint64_t, std::shared_ptr<Module> > & o_ModuleMap )
{
    std::map<std::string, std::shared_ptr<Module>> modules;
    std::vector<std::string> result = ListModules( a_PID );
    for( const std::string& line : result )
    {
        std::vector<std::string> tokens = Tokenize( line ); 
        if( tokens.size() == 6 )
        {
            std::string& moduleName = tokens[5];
            
            auto addresses = Tokenize(tokens[0], "-");
            if( addresses.size() == 2 )
            {
                auto iter = modules.find(moduleName);
                std::shared_ptr<Module> module = 
                    (iter == modules.end()) ? std::make_shared<Module>() : iter->second;

                uint64_t start = std::stoull(addresses[0], nullptr, 16);
                uint64_t end = std::stoull(addresses[1], nullptr, 16);
                
                if( module->m_AddressStart == 0 || start < module->m_AddressStart )
                    module->m_AddressStart = start;
                if( end > module->m_AddressEnd )
                    module->m_AddressEnd = end;
                
                module->m_FullName = moduleName;
                module->m_Name = ws2s(Path::GetFileName( s2ws(module->m_FullName) ));
                module->m_Directory = ws2s(Path::GetDirectory( s2ws(module->m_FullName) ));
                module->m_PdbSize = Path::FileSize( moduleName );
                auto prettyName = module->GetPrettyName();
                modules[moduleName] = module;
            }
        }
    }

    for( auto& iter : modules )
    {
        auto module = iter.second;
        o_ModuleMap[module->m_AddressStart] = module;
    }
}

//-----------------------------------------------------------------------------
uint32_t GetPID(const char* a_Name)
{
    std::string command = Format("ps -A | grep %s", a_Name);
    std::string result = ExecuteCommand( command.c_str() );
{'fraction_non_alphanumeric': 0.23979790128254955, 'fraction_numerical': 0.009845834952714082, 'mean_word_length': 1.9187145557655954, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 98, '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': '3591336', 'n_tokens_mistral': 2424, 'n_tokens_neox': 2319, 'n_words': 472}