# Copyright (c) 2016, Diana Andreea Popescu
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
#
# * Neither the name of the project, the copyright holder nor the names of its
#  contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


#Run client-server test using memaslap as client and memcached as server
OUTPUT_FILE = out.txt
PIN_CPU = 2
RUN_RT_C = 0
RUN_RT_S = 1
SLEEP_DELAY = 10
IP_SRC = 192.168.101.2
IP_DST = 192.168.101.1
SAMPLES = 1010000
USE_DAG = 0
DAG_OUTPUT = capture/dag.dat
SKIP=10000
RECORDS=$$(( $(SAMPLES) - $(SKIP) ))
RUNS=$$(($(TOTAL_SAMPLES) / $(SAMPLES)))
IF_NAME=p2p2

all:
	for i in `seq 1 10`; do\
                ${MAKE} start_dag;\
                ${MAKE} test_client I=$$i BYPASS=0;\
                ${MAKE} stop_dag;\
                ${MAKE} analyse_dag I=$$i;\
        done
	./parse.sh $(DAG_OUTPUT) $(OUTPUT_FILE)

message:  
	@echo " #################################################"
	@echo " This is not clean! must add pinning etc!"
	@echo " #################################################"

test_bypass:
	for i in `seq 1 10`; do\
		${MAKE} start_dag;\
		${MAKE} test_client I=$$i BYPASS=1;\
		${MAKE} stop_dag;\
		${MAKE} analyse_dag I=$$i;\
	done
	./parse.sh $(DAG_OUTPUT) $(OUTPUT_FILE)


test_client:
	@echo " #################################################"
	@echo " Starting client-server test                      "
	@echo " #################################################"
	sleep $(SLEEP_DELAY)
	 ifconfig $(IF_NAME) $(IP_SRC) up
	#$(PIN_PATH)/pininit
ifeq ($(BYPASS),0)
	#$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT_C) "
	#memaslap -s $(IP_DST):11211 -S 60s -T 1 -c 1 -x $(SAMPLES) > $(I)_$(OUTPUT_FILE)
	memaslap -s $(IP_DST):11211 -S 60s -T 1 -c 1 -x $(SAMPLES) > $(I)_$(OUTPUT_FILE)
else
	#$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT_C) "
	exasock memaslap -s $(IP_DST):11211 -S 60s -T 1 -c 1 -x $(SAMPLES) > $(I)_$(OUTPUT_FILE)
endif
	#$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished client-server test                      "
	@echo " #################################################"


server:
	@echo " #################################################"
	@echo " Starting client-server test                      "
	@echo " #################################################"
	sleep $(SLEEP_DELAY)
	ifconfig $(IF_NAME) $(IP_DST) up
	sudo /etc/init.d/memcached stop
	#$(PIN_PATH)/pininit
	#$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT_S) "
	memcached -u memcache -l $(IP_DST) #"
	#$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished client-server test                      "
	@echo " #################################################"

server_bypass:
	@echo " #################################################"
	@echo " Starting client-server test                      "
	@echo " #################################################"
	sleep $(SLEEP_DELAY)
	ifconfig $(IF_NAME) $(IP_DST) up
	#sudo /etc/init.d/memcached stop
	#$(PIN_PATH)/pininit
	#$(PIN_PATH)/pin $(PIN_CPU) $(RUN_RT_S) "
	chown memcache /dev/exasock
	chown memcache /dev/exanic0
	exasock memcached -u memcache -l $(IP_DST) -U 0 -p 1337  
	chown root /dev/exasock
	chown root /dev/exanic0
	#"
	#$(PIN_PATH)/pinreset
	@echo " #################################################"
	@echo " Finished client-server test			"                   
	@echo " #################################################"



start_dag:
	@echo "Starting DAG Capture..."
ifeq ($(USE_DAG),1)
	dagload ;\
	$(CAMIO_PATH)/dag_capture/bin/dag_capture & echo $$! > capture.PID
endif

stop_dag:
	@echo "Stopping DAG Capture..."
ifeq ($(USE_DAG), 1)
	sleep 1
	kill `cat capture.PID`
	rm capture.PID
	sleep 5
endif

analyse_dag:
ifeq ($(USE_DAG),1)
	cp /tmp/dag_cap_A $(DAG_OUTPUT)$(I)_cap_A;
	cp /tmp/dag_cap_B $(DAG_OUTPUT)$(I)_cap_B;
	$(CAMIO_PATH)/dag_analyse_join/bin/dag_analyse -i /tmp/dag_cap_B -I /tmp/dag_cap_A -o $(DAG_OUTPUT)$(I) -f $(SKIP) -F $(SKIP) -l $(RECORDS)  -c
endif


clean:
	rm -f *$(OUTPUT_FILE)
