Savarese Software Research Corporation
memusage.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <ssrc/wispers/utility/memusage.h>
00018 
00019 // TODO: Implement for FreeBSD.
00020 // TODO: exceptions?  errors?  scale all values to bytes?
00021 #if defined(WISPERS_HAVE_GET_MEMUSAGE)
00022 
00023 #include <sstream>
00024 
00025 #  if defined(linux) || defined(__linux) || defined(__linux__)
00026 
00027 #  include <fstream>
00028 
00029 __BEGIN_NS_SSRC_WSPR_UTILITY
00030 
00031 bool get_memusage(const unsigned int pid, memusage & usage) {
00032   std::ostringstream buf;
00033 
00034   buf << "/proc/" << pid << "/statm";
00035 
00036   std::ifstream in(buf.str().c_str());
00037 
00038   if(in) {
00039     in >> usage.size >> usage.resident >> usage.share
00040        >> usage.text >> usage.lib >> usage.data >> usage.dt;
00041 
00042     usage.page_size = ::sysconf(_SC_PAGE_SIZE);
00043 
00044     return true;
00045   }
00046 
00047   return false;
00048 }
00049 
00050 __END_NS_SSRC_WSPR_UTILITY
00051 
00052 #  elif defined(sun) || defined(__sun) || defined(__sun__)
00053 
00054 #  include <fcntl.h>
00055 #  include <procfs.h>
00056 
00057 __BEGIN_NS_SSRC_WSPR_UTILITY
00058 
00059 bool get_memusage(const unsigned int pid, memusage & usage) {
00060   ::psinfo_t psinfo;
00061   ::pstatus_t pstatus;
00062   std::ostringstream buf;
00063 
00064   buf << "/proc/" << pid << "/status";
00065 
00066   int fd = ::open(buf.str().c_str(), O_RDONLY);
00067 
00068   if(fd < 0)
00069     return false;
00070 
00071   if(::read(fd, &pstatus, sizeof(pstatus_t)) != sizeof(pstatus_t))
00072     return false;
00073 
00074   ::close(fd);
00075 
00076   buf.str("");
00077   buf << "/proc/" << pid << "/psinfo";
00078 
00079   fd = ::open(buf.str().c_str(), O_RDONLY);
00080 
00081   if(fd < 0)
00082     return false;
00083 
00084   if(::read(fd, &psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t))
00085     return false;
00086 
00087   ::close(fd);
00088 
00089   usage.size     = psinfo.pr_size;
00090   usage.resident = psinfo.pr_rssize;
00091   usage.heap     = pstatus.pr_brksize;
00092   usage.stack    = pstatus.pr_stksize;
00093 
00094   return true;
00095 }
00096 
00097 __END_NS_SSRC_WSPR_UTILITY
00098 
00099 #  endif
00100 
00101 #else
00102 
00103 __BEGIN_NS_SSRC_WSPR_UTILITY
00104 
00105 // Dummy function to allow module to compile.
00106 bool get_memusage(const unsigned int pid) { return false; }
00107 
00108 __END_NS_SSRC_WSPR_UTILITY
00109 
00110 #endif

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.