PLearn 0.1
procinfo.cc
Go to the documentation of this file.
00001 #include <plearn/base/RemoteDeclareMethod.h>  
00002 #include "procinfo.h"
00003 #include <plearn/base/plerror.h>
00004 #include <plearn/base/stringutils.h>
00005 #include <plearn/base/tostring.h>  
00006 
00007 #include <sys/types.h>
00008 #include <cstdio>
00009 #include <cstdlib>
00010 #include <iostream>
00011 #include <nspr/prenv.h>
00012 
00013 #if defined(WIN32) && defined(_MSC_VER)
00014 // unistd.h is not available under Microsoft Visual Studio, and some function
00015 // names are not the same.
00016 #define popen _popen
00017 #define pclose _pclose
00018 #define getpid _getpid
00019 #define pid_t int
00020 #include <process.h>
00021 #else
00022 #include <unistd.h>                          // for getpid
00023 #endif
00024 
00025 namespace PLearn {
00026 
00027 size_t getSystemTotalMemory()
00028 {
00029     unsigned int memory_size_uint = 0;
00030     char units[1000];
00031     FILE* p=popen("grep MemTotal /proc/meminfo","r");
00032     fscanf(p,"%*s %u %s", &memory_size_uint, units);
00033     size_t memory_size = size_t(memory_size_uint);
00034     if (strcmp(units,"kB")==0)
00035         memory_size*=1024;
00036     else 
00037         PLERROR("getSystemTotalMemory: unknown memory units %s",units);
00038     pclose(p);
00039     return memory_size;
00040 }
00041 
00042 size_t getProcessDataMemory()
00043 {
00044     pid_t pid = getpid();
00045     size_t memory_size=0;
00046     string file = "/proc/"+tostring(pid)+"/status";
00047     ifstream ifs(file.c_str());
00048     while (ifs) {
00049         string line = pgetline(ifs);
00050         if (line.substr(0,7) == "VmData:") {
00051             vector<string> elements = split(line);
00052             memory_size = size_t(toint(elements[1]));
00053             if (elements[2] == "kB")
00054                 memory_size *= 1024;
00055             else
00056                 PLERROR("getProcessDataMemory: unknown memory units '%s'",
00057                         elements[2].c_str());
00058             break;
00059         }
00060     }
00061     return memory_size;
00062 }
00063 
00065 // getPid //
00067 int getPid()
00068 {
00069 #if _POSIX_VERSION >= 200112L
00070 #include <unistd.h>
00071     return getpid();
00072 #else
00073     return -999;
00074 #endif
00075 }
00076 
00078 // getUser //
00080 string getUser()
00081 {
00082     const char* h = PR_GetEnv("USER");
00083     if (!h)
00084         h = PR_GetEnv("LOGNAME");
00085     if (!h)
00086         return "USERNAME_NOT_FOUND";
00087     return tostring(h);
00088 }
00089 
00090 BEGIN_DECLARE_REMOTE_FUNCTIONS
00091 
00092     declareFunction("getSystemTotalMemory", &getSystemTotalMemory,
00093                     (BodyDoc("Return the total memory installed in the system in bytes."),
00094                      RetDoc ("Memory size")));
00095 
00096     declareFunction("getProcessDataMemory", &getProcessDataMemory,
00097                     (BodyDoc("Return the total data memory used by the current process in bytes."),
00098                      RetDoc ("Used memory size")));
00099 
00100 END_DECLARE_REMOTE_FUNCTIONS
00101 
00102 } // end of namespace PLearn
00103 
00104 
00105 /*
00106   Local Variables:
00107   mode:c++
00108   c-basic-offset:4
00109   c-file-style:"stroustrup"
00110   c-file-offsets:((innamespace . 0)(inline-open . 0))
00111   indent-tabs-mode:nil
00112   fill-column:79
00113   End:
00114 */
00115 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines