Files
@ 0f3b28fdedfa
Branch filter:
Location: libtransport.git/src/memoryusage.cpp - annotation
0f3b28fdedfa
3.1 KiB
text/x-c++hdr
Merge branch 'master' of git://github.com/hanzz/libtransport into twitter-backend
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 cbcf2f145298 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 7e8720849725 c0168cf082c3 7e8720849725 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 4c9f82cb3591 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 4c9f82cb3591 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 c0168cf082c3 | /**
* XMPP - libpurple transport
*
* Copyright (C) 2009, Jan Kaluza <hanzz@soc.pidgin.im>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "transport/memoryusage.h"
#include <iostream>
#include <cstring>
#include <sstream>
#include <fstream>
#include <algorithm>
#include <boost/lexical_cast.hpp>
#ifndef WIN32
#include <sys/param.h>
#endif
#ifdef BSD
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#endif
namespace Transport {
#ifndef WIN32
#ifdef BSD
void process_mem_usage(double& vm_usage, double& resident_set, pid_t pid) {
int mib[4];
size_t size;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PID;
if (pid == 0) {
mib[3] = getpid();
}
else {
mib[3] = pid;
}
struct kinfo_proc proc;
size = sizeof(struct kinfo_proc);
if (sysctl((int*)mib, 4, &proc, &size, NULL, 0) == -1) {
vm_usage = 0;
resident_set = 0;
return;
}
// sysctl stores 0 in the size if we can't find the process information.
// Set errno to ESRCH which will be translated in NoSuchProcess later on.
if (size == 0) {
vm_usage = 0;
resident_set = 0;
return;
}
int pagesize,cnt;
size = sizeof(pagesize);
sysctlbyname("hw.pagesize", &pagesize, &size, NULL, 0);
resident_set = (double) (proc.ki_rssize * pagesize / 1024);
vm_usage = (double) proc.ki_size;
}
#else /* BSD */
void process_mem_usage(double& shared, double& resident_set, pid_t pid) {
using std::ios_base;
using std::ifstream;
using std::string;
shared = 0.0;
resident_set = 0.0;
// 'file' stat seems to give the most reliable results
std::string f = "/proc/self/statm";
if (pid != 0) {
f = "/proc/" + boost::lexical_cast<std::string>(pid) + "/statm";
}
ifstream stat_stream(f.c_str(), ios_base::in);
if (!stat_stream.is_open()) {
shared = 0;
resident_set = 0;
return;
}
// dummy vars for leading entries in stat that we don't care about
//
string pid1, comm, state, ppid, pgrp, session, tty_nr;
string tpgid, flags, minflt, cminflt, majflt, cmajflt;
string utime, stime, cutime, cstime, priority, nice;
string O, itrealvalue, starttime;
// the two fields we want
//
unsigned long shared_;
long rss;
stat_stream >> O >> rss >> shared_; // don't care about the rest
long page_size_kb = sysconf(_SC_PAGE_SIZE) / 1024; // in case x86-64 is configured to use 2MB pages
shared = shared_ * page_size_kb;
resident_set = rss * page_size_kb;
}
#endif /* else BSD */
#endif /* WIN32 */
}
|