UProcessInfo.cpp
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #include "rtabmap/utilite/UProcessInfo.h"
00021 
00022 #ifdef _WIN32
00023 #include "Windows.h"
00024 #include "Psapi.h"
00025 #elif __APPLE__
00026 #include <sys/resource.h>
00027 #else
00028 #include <fstream>
00029 #include <stdlib.h>
00030 #include "rtabmap/utilite/UStl.h"
00031 #endif
00032 
00033 UProcessInfo::UProcessInfo() {}
00034 
00035 UProcessInfo::~UProcessInfo() {}
00036 
00037 // return in bytes
00038 long int UProcessInfo::getMemoryUsage()
00039 {
00040         long int memoryUsage = -1;
00041 
00042 #ifdef _WIN32
00043                 HANDLE hProc = GetCurrentProcess();
00044                 PROCESS_MEMORY_COUNTERS info;
00045                 BOOL okay = GetProcessMemoryInfo(hProc, &info, sizeof(info));
00046                 if(okay)
00047                 {
00048                         memoryUsage = info.WorkingSetSize;
00049                 }
00050 #elif __APPLE__
00051                 rusage u;
00052                 if(getrusage(RUSAGE_SELF, &u) == 0)
00053                 {
00054                         memoryUsage = u.ru_maxrss;
00055                 }
00056 #else
00057                 std::fstream file("/proc/self/status", std::fstream::in);
00058                 if(file.is_open())
00059                 {
00060                         std::string bytes;
00061                         while(std::getline(file, bytes))
00062                         {
00063                                 if(bytes.find("VmRSS") != bytes.npos)
00064                                 {
00065                                         std::list<std::string> strs = uSplit(bytes, ' ');
00066                                         if(strs.size()>1)
00067                                         {
00068                                                 memoryUsage = atol(uValueAt(strs,1).c_str()) * 1024;
00069                                         }
00070                                         break;
00071                                 }
00072                         }
00073                         file.close();
00074                 }
00075 #endif
00076 
00077         return memoryUsage;
00078 }


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42