Skip to content
Snippets Groups Projects
MemWatch.cpp 1.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • Lars Bilke's avatar
    Lars Bilke committed
    /**
    
    Lars Bilke's avatar
    Lars Bilke committed
     * \file
     * \author Thomas Fischer
     * \date   2012-05-07
     * \brief  Implementation of the MemWatch class.
     *
     * \copyright
    
     * Copyright (c) 2012-2021, OpenGeoSys Community (http://www.opengeosys.org)
    
    Lars Bilke's avatar
    Lars Bilke committed
     *            Distributed under a Modified BSD License.
     *              See accompanying file LICENSE.txt or
    
    Lars Bilke's avatar
    Lars Bilke committed
     *              http://www.opengeosys.org/project/license
    
    Lars Bilke's avatar
    Lars Bilke committed
     *
    
    Tom Fischer's avatar
    Tom Fischer committed
     */
    
    #include "MemWatch.h"
    
    
    Karsten Rink's avatar
    Karsten Rink committed
    #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__MINGW32__)
    
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <sys/types.h>
    #include <unistd.h>
    #endif
    
    Tom Fischer's avatar
    Tom Fischer committed
    
    namespace BaseLib {
    
    MemWatch::MemWatch ()
    {
            updateMemUsage ();
    }
    
    unsigned MemWatch::updateMemUsage ()
    {
    
    Karsten Rink's avatar
    Karsten Rink committed
    #if !defined(_WIN32) && !defined(__APPLE__) && !defined(__MINGW32__)
    
    Tom Fischer's avatar
    Tom Fischer committed
            std::string fname ("/proc/");
            std::stringstream str_pid;
    
            str_pid << static_cast<unsigned> (getpid());
    
    Tom Fischer's avatar
    Tom Fischer committed
            fname += str_pid.str();
            fname += "/statm";
            unsigned pages;
    
            std::ifstream in (fname.c_str(), std::ios::in);
    
    Lars Bilke's avatar
    Lars Bilke committed
            if (!in.is_open())
            {
    
    Tom Fischer's avatar
    Tom Fischer committed
                perror( "open" );
                return 1;
            }
    
            in >> pages;
    
            vmem_size_ = static_cast<unsigned long>(pages) *
    
                         static_cast<unsigned long>(getpagesize());
    
    Tom Fischer's avatar
    Tom Fischer committed
            in.close ();
    
    Tom Fischer's avatar
    Tom Fischer committed
            return 0;
    }
    
    unsigned long MemWatch::getVirtMemUsage ()
    {
            updateMemUsage ();
    
    Tom Fischer's avatar
    Tom Fischer committed
    }
    
    } // end namespace BaseLib