From 25704d56f0649a3b2d3865199bd809a5169bc417 Mon Sep 17 00:00:00 2001
From: "Ivan T. Ivanov" <iivanov@mm-sol.com>
Date: Mon, 27 Oct 2014 15:56:15 +0200
Subject: [PATCH] add maccess event

Simulation of memory intensive task

Signed-off-by: Ivan T. Ivanov <iivanov@mm-sol.com>
---
 doc/examples/maccess.json | 22 ++++++++++++++++++++++
 src/rt-app.c              | 34 ++++++++++++++++++++++++++++++++++
 src/rt-app_parse_config.c | 14 ++++++++++++++
 src/rt-app_types.h        |  1 +
 4 files changed, 71 insertions(+)
 create mode 100644 doc/examples/maccess.json

diff --git a/doc/examples/maccess.json b/doc/examples/maccess.json
new file mode 100644
index 0000000..ac4123b
--- /dev/null
+++ b/doc/examples/maccess.json
@@ -0,0 +1,22 @@
+{
+	/*
+	 * Simple use case with 2 threads that runs for 10 ms and wake up each
+	 * other until the use case is stopped with Ctrl+C
+	 */
+	"tasks" : {
+		"thread0" : {
+			"loop" : -1,
+			"maccess" : 16384,
+			"resume" : "thread1",
+			"run" :     10000,
+			"suspend" : "thread0",
+		},
+		"thread1" : {
+			"loop" : -1,
+			"maccess" : 4096,
+			"resume" : "thread0",
+			"run" :     10000,
+			"suspend" : "thread1",
+		},
+	},
+}
diff --git a/src/rt-app.c b/src/rt-app.c
index de91ff1..013a44f 100644
--- a/src/rt-app.c
+++ b/src/rt-app.c
@@ -21,6 +21,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 #define _GNU_SOURCE
 #include <fcntl.h>
+#include <stdlib.h>
 #include "rt-app.h"
 #include "rt-app_utils.h"
 #include <sched.h>
@@ -110,6 +111,33 @@ static inline loadwait(unsigned long exec)
 	return load_count;
 }
 
+void memory_access(size_t size)
+{
+	int *src_buf;
+	int *dst_buf;
+	int i,j;
+
+	src_buf = malloc(size);
+	dst_buf = malloc(size);
+	if(!src_buf || !dst_buf) {
+		log_error("maccess can't allocate %d\n", size);
+		return;
+	}
+
+	/* Initialize buffers and transfer */
+	memset(dst_buf, 0, size);
+	for (j = 0; j < (size/4); j++) {
+		src_buf[j] = rand();
+	}
+
+	memcpy(dst_buf, src_buf, size);
+	if(memcmp(dst_buf, src_buf, size))
+		log_error("maccess mismatch between source and destination buffers\n");
+
+	free(src_buf);
+	free(dst_buf);
+}
+
 static int run_event(event_data_t *event, int dry_run,
 		unsigned long *perf, unsigned long *duration, rtapp_resource_t *resources)
 {
@@ -168,6 +196,12 @@ static int run_event(event_data_t *event, int dry_run,
 			*duration += timespec_to_usec(&t_end);
 		}
 		break;
+	case rtapp_maccess:
+		{
+		log_debug("maccess %d ", event->duration);
+		memory_access(event->duration);
+		break;
+		}
 	case rtapp_timer:
 		{
 			struct timespec t_period, t_now;
diff --git a/src/rt-app_parse_config.c b/src/rt-app_parse_config.c
index 63699e8..10d2e71 100644
--- a/src/rt-app_parse_config.c
+++ b/src/rt-app_parse_config.c
@@ -320,6 +320,18 @@ parse_thread_event_data(char *name, struct json_object *obj,
 		return;
 	}
 
+	if (!strncmp(name, "maccess", strlen("maccess"))) {
+
+		if (!json_object_is_type(obj, json_type_int))
+			goto unknown_event;
+
+		data->duration = json_object_get_int(obj);
+		data->type = rtapp_maccess;
+
+		log_info(PIN2 "type %d size %d", data->type, data->duration);
+		return;
+	}
+
 	if (!strncmp(name, "lock", strlen("lock")) ||
 			!strncmp(name, "unlock", strlen("unlock"))) {
 
@@ -493,6 +505,8 @@ obj_is_event(char *name)
 			return rtapp_suspend;
 	if (!strncmp(name, "resume", strlen("resume")))
 			return rtapp_resume;
+	if (!strncmp(name, "maccess", strlen("maccess")))
+			return rtapp_maccess;
 
 	return 0;
 }
diff --git a/src/rt-app_types.h b/src/rt-app_types.h
index fd29a06..243d5ce 100644
--- a/src/rt-app_types.h
+++ b/src/rt-app_types.h
@@ -65,6 +65,7 @@ typedef enum resource_t
 	rtapp_timer,
 	rtapp_suspend,
 	rtapp_resume,
+	rtapp_maccess,
 } resource_t;
 
 struct _rtapp_mutex {
-- 
1.9.1

