diff --git a/backends/skype/main.cpp b/backends/skype/main.cpp index 984d9f60..a56b985d 100644 --- a/backends/skype/main.cpp +++ b/backends/skype/main.cpp @@ -4,6 +4,7 @@ #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" @@ -97,6 +98,10 @@ class Skype { bool createDBusProxy(); bool loadSkypeBuddies(); + int getPid() { + return (int) m_pid; + } + private: std::string m_username; std::string m_password; @@ -134,6 +139,19 @@ class SpectrumNetworkPlugin : public NetworkPlugin { skype->login(); } + void handleMemoryUsage(double &res, double &shared) { + res = 0; + shared = 0; + for(std::map::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) { @@ -834,17 +852,7 @@ int main(int argc, char **argv) { 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(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(getpid())); log4cxx::PropertyConfigurator::configure(p); } diff --git a/include/transport/memoryusage.h b/include/transport/memoryusage.h index d2080e21..d38917d9 100644 --- a/include/transport/memoryusage.h +++ b/include/transport/memoryusage.h @@ -22,10 +22,14 @@ #include +#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 diff --git a/src/memoryusage.cpp b/src/memoryusage.cpp index ded5130e..d620569b 100644 --- a/src/memoryusage.cpp +++ b/src/memoryusage.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #ifndef WIN32 #include #endif @@ -41,13 +42,18 @@ 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); @@ -75,7 +81,7 @@ void process_mem_usage(double& vm_usage, double& resident_set) { 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; @@ -84,8 +90,11 @@ void process_mem_usage(double& shared, double& resident_set) { 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(pid) + "/statm"; + } + ifstream stat_stream(f.c_str(), ios_base::in); if (!stat_stream.is_open()) { shared = 0; resident_set = 0; @@ -94,7 +103,7 @@ void process_mem_usage(double& shared, double& resident_set) { // 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;