Browse Source

* partially done checking memory limit for AIs

mateuszb 14 years ago
parent
commit
696f85a2ed
6 changed files with 78 additions and 7 deletions
  1. 1 1
      AI/Makefile.am
  2. 1 1
      AI/Makefile.in
  3. 1 2
      configure
  4. 1 1
      configure.ac
  5. 55 0
      server/CVCMIServer.cpp
  6. 19 2
      vcmiinstall.sh

+ 1 - 1
AI/Makefile.am

@@ -1 +1 @@
-SUBDIRS = StupidAI EmptyAI GeniusAI MadAI
+SUBDIRS = StupidAI EmptyAI GeniusAI

+ 1 - 1
AI/Makefile.in

@@ -233,7 +233,7 @@ target_alias = @target_alias@
 top_build_prefix = @top_build_prefix@
 top_builddir = @top_builddir@
 top_srcdir = @top_srcdir@
-SUBDIRS = StupidAI EmptyAI GeniusAI MadAI
+SUBDIRS = StupidAI EmptyAI GeniusAI
 all: all-recursive
 
 .SUFFIXES:

+ 1 - 2
configure

@@ -16468,7 +16468,7 @@ VCMI_AI_LIBS_DIR="$libdir/vcmi/AI"
 VCMI_SCRIPTING_LIBS_DIR="$libdir/vcmi/Scripting"
 
 
-ac_config_files="$ac_config_files Makefile lib/Makefile client/Makefile server/Makefile AI/Makefile AI/StupidAI/Makefile AI/GeniusAI/Makefile AI/EmptyAI/Makefile AI/MadAI/Makefile Scripting/ERM/Makefile Odpalarka/Makefile VCMI_BattleAiHost/Makefile"
+ac_config_files="$ac_config_files Makefile lib/Makefile client/Makefile server/Makefile AI/Makefile AI/StupidAI/Makefile AI/GeniusAI/Makefile AI/EmptyAI/Makefile Scripting/ERM/Makefile Odpalarka/Makefile VCMI_BattleAiHost/Makefile"
 
 cat >confcache <<\_ACEOF
 # This file is a shell script that caches the results of configure
@@ -17570,7 +17570,6 @@ do
     "AI/StupidAI/Makefile") CONFIG_FILES="$CONFIG_FILES AI/StupidAI/Makefile" ;;
     "AI/GeniusAI/Makefile") CONFIG_FILES="$CONFIG_FILES AI/GeniusAI/Makefile" ;;
     "AI/EmptyAI/Makefile") CONFIG_FILES="$CONFIG_FILES AI/EmptyAI/Makefile" ;;
-    "AI/MadAI/Makefile") CONFIG_FILES="$CONFIG_FILES AI/MadAI/Makefile" ;;
     "Scripting/ERM/Makefile") CONFIG_FILES="$CONFIG_FILES Scripting/ERM/Makefile" ;;
     "Odpalarka/Makefile") CONFIG_FILES="$CONFIG_FILES Odpalarka/Makefile" ;;
     "VCMI_BattleAiHost/Makefile") CONFIG_FILES="$CONFIG_FILES VCMI_BattleAiHost/Makefile" ;;

+ 1 - 1
configure.ac

@@ -122,4 +122,4 @@ AC_SUBST(VCMI_AI_LIBS_DIR)
 VCMI_SCRIPTING_LIBS_DIR="$libdir/vcmi/Scripting"
 AC_SUBST(VCMI_SCRIPTING_LIBS_DIR)
 
-AC_OUTPUT(Makefile lib/Makefile client/Makefile server/Makefile AI/Makefile AI/StupidAI/Makefile AI/GeniusAI/Makefile AI/EmptyAI/Makefile AI/MadAI/Makefile Scripting/ERM/Makefile Odpalarka/Makefile VCMI_BattleAiHost/Makefile)
+AC_OUTPUT(Makefile lib/Makefile client/Makefile server/Makefile AI/Makefile AI/StupidAI/Makefile AI/GeniusAI/Makefile AI/EmptyAI/Makefile Scripting/ERM/Makefile Odpalarka/Makefile VCMI_BattleAiHost/Makefile)

+ 55 - 0
server/CVCMIServer.cpp

@@ -23,6 +23,7 @@
 #include "../lib/VCMIDirs.h"
 #include "CGameHandler.h"
 #include "../lib/CMapInfo.h"
+#include "../lib/CondSh.h"
 
 std::string NAME_AFFIX = "server";
 std::string NAME = NAME_VER + std::string(" (") + NAME_AFFIX + ')'; //application name
@@ -498,6 +499,52 @@ void CVCMIServer::loadGame()
 	gh.run(true);
 }
 
+//memory monitoring
+CondSh<bool> testMem;
+
+volatile int monitringRes; //0 -- left AI violated mem limit; 1 -- right AI violated mem limit; 2 -- no mem limit violation;
+
+bool memViolated(const int pid, const int refpid, const int limit) {
+	char call[1000], c2[1000];
+	//sprintf(call, "if (( `cat /proc/%d/statm | cut -d' ' -f1` > `cat /proc/%d/statm | cut -d' ' -f1` + %d )); then cat /kle; else echo b; fi;", pid, refpid, limit);
+	sprintf(call, "/proc/%d/statm", pid);
+	sprintf(c2, "/proc/%d/statm", refpid);
+	std::ifstream proc(call),
+		ref(c2);
+	int procUsage, refUsage;
+	proc >> procUsage;
+	ref >> refUsage;
+	
+	return procUsage > refUsage + limit;
+	//return 0 != ::system(call);
+}
+
+void memoryMonitor(int lAIpid, int rAIpid, int refPid) {
+	const int MAX_MEM = 20000; //in blocks (of, I hope, 4096 B)
+	monitringRes = 2;
+	tlog0 << "Monitor is activated\n";
+	try {
+		while(testMem.get()) {
+			tlog0 << "Monitor is active 12\n";
+			if(memViolated(lAIpid, refPid, MAX_MEM)) {
+				monitringRes = 0;
+				break;
+			}
+			if(memViolated(rAIpid, refPid, MAX_MEM)) {
+				monitringRes = 1;
+				break;
+			}
+			sleep(3);
+			tlog0 << "Monitor is active 34\n";
+		}
+	}
+	catch(...) {
+		tlog0 << "Monitor is throwing something...\n";
+	}
+	tlog0 << "Monitor is closing\n";
+}
+////
+
 void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI, const std::string &rightAI, int howManyClients)
 {
 	std::map<CConnection *, si32> pidsFromConns;
@@ -588,6 +635,8 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI
 	}
 
 	//TODO monitor memory of PIDs
+	testMem.set(true);
+	boost::thread* memMon = new boost::thread(boost::bind(memoryMonitor, PIDs[0], PIDs[1], PIDs[2]));
 
 	std::string logFName = "duel_log.vdat";
 	tlog0 << "Logging battle activities (for replay possibility) in " << logFName << std::endl;
@@ -599,11 +648,17 @@ void CVCMIServer::startDuel(const std::string &battle, const std::string &leftAI
 	gh->runBattle();
 	tlog0 << "Battle over!\n";
 	tlog0 << "Waiting for connections to close\n";
+	
+	testMem.set(false);
+	memMon->join();
+	delNull(memMon);
+	tlog0 << "Memory violation checking result: " << monitringRes << std::endl;
 	BOOST_FOREACH(boost::thread *t, threads)
 	{
 		t->join();
 		delNull(t);
 	}
+	
 	tlog0 << "Removing gh\n";
 	delNull(gh);
 	tlog0 << "Removed gh!\n";

+ 19 - 2
vcmiinstall.sh

@@ -14,14 +14,30 @@ function incusage(){
 	exit
 }
 
-if [ "$1" = "--install" ]; then
+if [ "$1" == "reconf" ]; then
+	cd vcmi
+	autoreconf -i
+	cd ..
+	vcmi/configure --datadir=`pwd` --bindir=`pwd`vcmi --libdir=`pwd`
+	exit
+fi
+
+if [ "$1" == "--install" ]; then
 	if [ $# -lt 2 ]; then
 		incusage
 	fi
+	if [ "$2" == "micro" ]; then
+	    unzip vcmipack.zip -d YourAI
+	    MAINVCMI_ROOT="/home/mateusz/vcmi_retl"
+	    ln -s "$MAINVCMI_ROOT/odpalarka"
+	    exit
+	fi
+	#only lean and full modes
 	svn co https://vcmi.svn.sourceforge.net/svnroot/vcmi/branches/programmingChallenge/ vcmi
 	errorcheck "fetching sources"
 	cd vcmi
-	if [ "$2" = "lean" ]; then
+		
+	elif [ "$2" = "lean" ]; then
 		mv "Makefile without client.am" Makefile.am
 		mv "configure without client.ac" configure.ac
 		rm client/Makefile.am
@@ -64,6 +80,7 @@ elif [ "$1" = "--help" ]; then
 	echo "--help			displays this info."
 	echo "--install full	downloads and compiles full VCMI with graphical client; requires ffmpeg."
 	echo "--install lean	downloads and compiles most of VCMI (without graphical client)."
+	echo "--install micro	unpacks vcmipack.zip and makes appropriate symbolic links."
 else
 	incusage
 fi