Files @ 07ee87c60d2d
Branch filter:

Location: libtransport.git/libtransport/MemoryUsage.cpp

Andrey Utkin
Define 'foreach' in backends/libcommuni/session.cpp

Fixes compilation issue

[ 64%] Building CXX object backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o
cd /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni && /usr/bin/x86_64-pc-linux-gnu-g++ -DBOOST_FILESYSTEM_VERSION=3 -DDEBUG -DIRC_SHARED -DPURPLE_RUNTIME=0 -DQT_CORE_LIB -DQT_NETWORK_LIB -DQT_NO_KEYWORDS -DSPECTRUM_VERSION=\"2.0.5-git-b15fcedf\" -DSUPPORT_LEGACY_CAPS -DWITH_LIBEVENT -DWITH_LOG4CXX -DWITH_PQXX -DWITH_PROTOBUF -isystem /usr/include/qt4 -isystem /usr/include/qt4/QtCore -I/usr/include/libpurple -I/usr/include/glib-2.0 -I/usr/lib64/glib-2.0/include -isystem /usr/include/qt4/QtNetwork -I/usr/include/qt4/Communi/IrcCore -I/usr/include/qt4/Communi/IrcUtil -I/usr/include/qt4/Communi/IrcModel -I/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include -isystem /usr/share/qt4/mkspecs/default -DBOOST_SIGNALS_NO_DEPRECATION_WARNING -O2 -pipe -O0 -ggdb -o CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o -c /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_socketError(QAbstractSocket::SocketError)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:22: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:23: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:23: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:55: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:208:55: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_namesMessageReceived(IrcMessage*)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:19: error: expected primary-expression before ‘*’ token
foreach (IrcUser *user, userModel->users()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:20: error: ‘user’ was not declared in this scope
foreach (IrcUser *user, userModel->users()) {
^~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:44: error: ‘foreach’ was not declared in this scope
foreach (IrcUser *user, userModel->users()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:348:44: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::sendMessageToFrontend(const string&, const string&, const string&)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:24: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:24: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:56: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:387:56: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::on_numericMessageReceived(IrcMessage*)’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:24: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:24: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:56: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:465:56: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:490:23: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp: In member function ‘void MyIrcSession::awayTimeout()’:
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:21: error: expected primary-expression before ‘*’ token
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:22: error: ‘buffer’ was not declared in this scope
foreach (IrcBuffer *buffer, m_bufferModel->buffers()) {
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:22: note: suggested alternative:
In file included from /usr/include/boost/asio/detail/reactive_socket_service.hpp:22:0,
from /usr/include/boost/asio/socket_acceptor_service.hpp:29,
from /usr/include/boost/asio/basic_socket_acceptor.hpp:25,
from /usr/include/boost/asio/ip/tcp.hpp:19,
from /usr/include/Swiften/Network/HostAddressPort.h:9,
from /usr/include/Swiften/Elements/S5BProxyRequest.h:22,
from /usr/include/Swiften/Parser/PayloadParsers/S5BProxyRequestParser.h:18,
from /usr/include/Swiften/Swiften.h:112,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.h:30,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:21:
/usr/include/boost/asio/buffer.hpp:1231:24: note: ‘boost::asio::buffer’
inline const_buffers_1 buffer(
^~~~~~
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:54: error: ‘foreach’ was not declared in this scope
ch (IrcBuffer *buffer, m_bufferModel->buffers()) {
^
/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:523:54: note: suggested alternative:
In file included from /usr/include/boost/foreach.hpp:86:0,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/include/transport/Config.h:24,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/ircnetworkplugin.h:27,
from /var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999/backends/libcommuni/session.cpp:30:
/usr/include/boost/foreach_fwd.hpp:26:11: note: ‘boost::foreach’
namespace foreach
^~~~~~~
make[2]: *** [backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/build.make:126: backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/session.cpp.o] Error 1
make[2]: Leaving directory '/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999'
make[1]: *** [CMakeFiles/Makefile2:646: backends/libcommuni/CMakeFiles/spectrum2_libcommuni_backend.dir/all] Error 2
make[1]: Leaving directory '/var/tmp/portage/net-im/spectrum-9999/work/spectrum-9999'
make: *** [Makefile:152: all] Error 2
/**
 * 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>
#else 
#include <windows.h>
#include <psapi.h>
#endif
#ifdef __MACH__
#include <mach/mach.h>
#elif 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 __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
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 */
#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;
	}
#endif /* WIN32 */

}