Changeset - 4c9f82cb3591
[Not reviewed]
0 3 0
HanzZ - 13 years ago 2012-03-09 08:25:03
hanzz.k@gmail.com
Report also skype process memory usage
3 files changed with 39 insertions and 18 deletions:
0 comments (0 inline, 0 general)
backends/skype/main.cpp
Show inline comments
 
#include "glib.h"
 
#include <iostream>
 

	
 
#include "transport/config.h"
 
#include "transport/transport.h"
 
#include "transport/usermanager.h"
 
#include "transport/memoryusage.h"
 
#include "transport/logger.h"
 
#include "transport/sqlite3backend.h"
 
#include "transport/userregistration.h"
 
#include "transport/user.h"
 
#include "transport/storagebackend.h"
 
#include "transport/rostermanager.h"
 
@@ -94,12 +95,16 @@ class Skype {
 
			return m_username;
 
		}
 

	
 
		bool createDBusProxy();
 
		bool loadSkypeBuddies();
 

	
 
		int getPid() {
 
			return (int) m_pid;
 
		}
 

	
 
	private:
 
		std::string m_username;
 
		std::string m_password;
 
		GPid m_pid;
 
		DBusGConnection *m_connection;
 
		DBusGProxy *m_proxy;
 
@@ -131,12 +136,25 @@ class SpectrumNetworkPlugin : public NetworkPlugin {
 
			m_sessions[user] = skype;
 
			m_accounts[skype] = user;
 

	
 
			skype->login();
 
		}
 

	
 
		void handleMemoryUsage(double &res, double &shared) {
 
			res = 0;
 
			shared = 0;
 
			for(std::map<std::string, Skype *>::const_iterator it = m_sessions.begin(); it != m_sessions.end(); it++) {
 
				Skype *skype = it->second;
 
				double r;
 
				double s;
 
				process_mem_usage(s, r, skype->getPid());
 
				res += r;
 
				shared += s;
 
			}
 
		}
 

	
 
		void handleLogoutRequest(const std::string &user, const std::string &legacyName) {
 
			Skype *skype = m_sessions[user];
 
			if (skype) {
 
				skype->logout();
 
				exit(1);
 
			}
 
@@ -831,23 +849,13 @@ int main(int argc, char **argv) {
 
		}
 
		else {
 
			log4cxx::helpers::Properties p;
 
			log4cxx::helpers::FileInputStream *istream = new log4cxx::helpers::FileInputStream(CONFIG_STRING(&config, "logging.backend_config"));
 

	
 
			p.load(istream);
 

	
 
			LogString pid, jid;
 
			log4cxx::helpers::Transcoder::decode(boost::lexical_cast<std::string>(getpid()), pid);
 
			log4cxx::helpers::Transcoder::decode(CONFIG_STRING(&config, "service.jid"), jid);
 
#ifdef _MSC_VER
 
			p.setProperty(L"pid", pid);
 
			p.setProperty(L"jid", jid);
 
#else
 
			p.setProperty("pid", pid);
 
			p.setProperty("jid", jid);
 
#endif
 
			p.setProperty("pid", boost::lexical_cast<std::string>(getpid()));
 
			log4cxx::PropertyConfigurator::configure(p);
 
		}
 

	
 
// 		initPurple(config);
 

	
 
		g_type_init();
include/transport/memoryusage.h
Show inline comments
 
@@ -19,13 +19,17 @@
 
 */
 

	
 
#pragma once
 

	
 
#include <vector>
 

	
 
#ifndef WIN32
 
#include "signal.h"
 
#endif
 

	
 
namespace Transport {
 

	
 
#ifndef WIN32
 
	void process_mem_usage(double& shared, double& resident_set);
 
	void process_mem_usage(double& shared, double& resident_set, pid_t pid = 0);
 
#endif
 

	
 
}
 
\ No newline at end of file
src/memoryusage.cpp
Show inline comments
 
@@ -22,12 +22,13 @@
 

	
 
#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>
 
@@ -38,19 +39,24 @@
 
#endif
 

	
 
namespace Transport {
 

	
 
#ifndef WIN32
 
#ifdef BSD
 
void process_mem_usage(double& vm_usage, double& resident_set) {
 
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;
 
	mib[3] = getpid();
 
	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;
 
@@ -72,32 +78,35 @@ void process_mem_usage(double& vm_usage, double& resident_set) {
 
	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) {
 
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
 
	//
 
	ifstream stat_stream("/proc/self/statm",ios_base::in);
 
	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 pid, comm, state, ppid, pgrp, session, tty_nr;
 
	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
 
	//
0 comments (0 inline, 0 general)