2011-07-18 14:45:50 +02:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2015-11-18 14:05:57 +01:00
|
|
|
#include "transport/MemoryUsage.h"
|
2011-07-18 14:45:50 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstring>
|
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <algorithm>
|
2012-03-09 08:25:03 +01:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2011-09-07 14:03:27 +02:00
|
|
|
#ifndef WIN32
|
2011-07-18 14:45:50 +02:00
|
|
|
#include <sys/param.h>
|
2013-02-04 14:40:15 +04:00
|
|
|
#else
|
|
|
|
#include <windows.h>
|
|
|
|
#include <psapi.h>
|
2011-09-07 14:03:27 +02:00
|
|
|
#endif
|
2013-02-03 16:10:17 +04:00
|
|
|
#ifdef __MACH__
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#elif BSD
|
2011-07-18 14:45:50 +02:00
|
|
|
#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
|
2013-02-03 16:10:17 +04:00
|
|
|
#ifdef __MACH__
|
|
|
|
|
|
|
|
void process_mem_usage(double& vm_usage, double& resident_set, pid_t pid) {
|
|
|
|
|
|
|
|
struct task_basic_info t_info;
|
|
|
|
mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
|
|
|
|
|
|
|
|
if (KERN_SUCCESS != task_info(mach_task_self(),
|
|
|
|
TASK_BASIC_INFO, (task_info_t)&t_info,
|
|
|
|
&t_info_count)) {
|
|
|
|
vm_usage = 0;
|
|
|
|
resident_set = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
vm_usage = t_info.virtual_size;
|
|
|
|
resident_set = t_info.resident_size;
|
|
|
|
}
|
|
|
|
#elif BSD
|
2012-03-09 08:25:03 +01:00
|
|
|
void process_mem_usage(double& vm_usage, double& resident_set, pid_t pid) {
|
2011-07-18 14:45:50 +02:00
|
|
|
int mib[4];
|
|
|
|
size_t size;
|
|
|
|
mib[0] = CTL_KERN;
|
|
|
|
mib[1] = KERN_PROC;
|
|
|
|
mib[2] = KERN_PROC_PID;
|
2012-03-09 08:25:03 +01:00
|
|
|
if (pid == 0) {
|
|
|
|
mib[3] = getpid();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mib[3] = pid;
|
|
|
|
}
|
2011-07-18 14:45:50 +02:00
|
|
|
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 */
|
2012-03-09 08:25:03 +01:00
|
|
|
void process_mem_usage(double& shared, double& resident_set, pid_t pid) {
|
2011-07-18 14:45:50 +02:00
|
|
|
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
|
2012-03-09 08:25:03 +01:00
|
|
|
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);
|
2011-07-18 14:45:50 +02:00
|
|
|
if (!stat_stream.is_open()) {
|
|
|
|
shared = 0;
|
|
|
|
resident_set = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// dummy vars for leading entries in stat that we don't care about
|
|
|
|
//
|
2012-03-09 08:25:03 +01:00
|
|
|
string pid1, comm, state, ppid, pgrp, session, tty_nr;
|
2011-07-18 14:45:50 +02:00
|
|
|
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 */
|
2013-02-04 14:40:15 +04:00
|
|
|
#else
|
|
|
|
#define PSAPI_VERSION 1
|
|
|
|
#define pid_t void*
|
|
|
|
void process_mem_usage(double& shared, double& resident_set, pid_t pid) {
|
|
|
|
PROCESS_MEMORY_COUNTERS_EX pmc;
|
|
|
|
GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*)&pmc, sizeof(pmc));
|
|
|
|
shared = (double)pmc.PrivateUsage;
|
|
|
|
resident_set = (double)pmc.WorkingSetSize;
|
|
|
|
}
|
2011-07-18 14:45:50 +02:00
|
|
|
#endif /* WIN32 */
|
|
|
|
|
|
|
|
}
|