]> rtime.felk.cvut.cz Git - can-benchmark.git/commitdiff
Adds a library with pair of functions to put load on CPU.
authorMartin Hořeňovský <Martin.Horenovsky@gmail.com>
Wed, 21 Aug 2013 18:21:43 +0000 (20:21 +0200)
committerMartin Hořeňovský <Martin.Horenovsky@gmail.com>
Wed, 21 Aug 2013 18:21:43 +0000 (20:21 +0200)
The implementation is, with the exception of error handling, feature complete, but not well commented.

rtems/gw/libs/Makefile [new file with mode: 0644]
rtems/gw/libs/Makefile.omk [new file with mode: 0644]
rtems/gw/libs/load.c [new file with mode: 0644]
rtems/gw/libs/load.h [new file with mode: 0644]

diff --git a/rtems/gw/libs/Makefile b/rtems/gw/libs/Makefile
new file mode 100644 (file)
index 0000000..76b56fd
--- /dev/null
@@ -0,0 +1,14 @@
+# Generic directory or leaf node makefile for OCERA make framework
+
+ifndef MAKERULES_DIR
+MAKERULES_DIR := $(shell ( old_pwd="" ;  while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd`  ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) )
+endif
+
+ifeq ($(MAKERULES_DIR),)
+all : default
+.DEFAULT::
+       @echo -e "\nThe Makefile.rules has not been found in this or parent directory\n"
+else
+include $(MAKERULES_DIR)/Makefile.rules
+endif
+
diff --git a/rtems/gw/libs/Makefile.omk b/rtems/gw/libs/Makefile.omk
new file mode 100644 (file)
index 0000000..dd4d803
--- /dev/null
@@ -0,0 +1,5 @@
+lib_LIBRARIES = load
+
+include_HEADERS = load.h
+
+load_SOURCES = load.c
diff --git a/rtems/gw/libs/load.c b/rtems/gw/libs/load.c
new file mode 100644 (file)
index 0000000..ba7fac4
--- /dev/null
@@ -0,0 +1,116 @@
+/*\r
+* Implements simple producer/consumer thread pair to cause load on the CPU.\r
+* \r
+* Used in benchmarking the CAN gateway.\r
+*\r
+* Co-opted from http://support.dce.felk.cvut.cz/pos/cv3/src/semaphore.html. \r
+*/\r
+\r
+#include <stdio.h>\r
+\r
+#include <pthread.h>\r
+#include <semaphore.h>\r
+\r
+#include "load.h"\r
+\r
+\r
+static void* produce(void* arg);\r
+static void* consume(void* arg);\r
+\r
+/* semaphores for consumer/producer pair */\r
+static sem_t produced, consumed;\r
+/* pthread handles for the loader */\r
+static pthread_t consumer, producer;\r
+static int n = 0;\r
+static char running = 0;\r
+\r
+static void* produce(void* arg){\r
+    while (1) {\r
+            sem_wait(&consumed);\r
+            n++;  /* increment n by 1 */\r
+            sem_post(&produced);\r
+            pthread_testcancel();\r
+    }\r
+    return NULL;\r
+}       \r
+\r
+static void* consume(void* arg){\r
+    while (1) {\r
+            sem_wait(&produced);\r
+            n--; /* aaand decrement */\r
+            sem_post(&consumed);\r
+            pthread_testcancel();\r
+    }\r
+    return NULL;\r
+}\r
+\r
+int start_thread_load(){\r
+    if (running == 0){\r
+        printf("Attempting to start load.\n");\r
+        int res;\r
+        running = 1;\r
+        res = sem_init(&consumed, 0, 0);\r
+        if (res < 0){\r
+            printf("Couldn't initialize consumed semaphore.\n");\r
+            return 1;\r
+        }\r
+        res = sem_init(&produced, 0, 1);\r
+        if (res < 0){\r
+            printf("Couldn't initialize produced semaphore.\n");\r
+            return 1;\r
+        }\r
+        \r
+        res = pthread_create(&producer, NULL, produce, NULL);\r
+        if (res < 0){\r
+            printf("Couldn't create producer thread.\n");\r
+            return 1;\r
+        }\r
+        \r
+        res = pthread_create(&consumer, NULL, consume, NULL);\r
+        if (res < 0){\r
+            printf("Couldn't create consumer thread.\n");\r
+            return 1;\r
+        }\r
+    \r
+        pthread_detach(producer);\r
+        pthread_detach(consumer);\r
+        printf("Load started succesfully.\n");\r
+        return 0;\r
+    } else {\r
+        printf("Load is already running.\n");\r
+        return 0;\r
+    }\r
+}\r
+\r
+/*\r
+* This function stops threads loading the CPU and destroys associated semaphores. \r
+* \r
+* No error handling currently, only tries to report errors.\r
+* \r
+*/\r
+int end_thread_load(){\r
+    if (running == 1){\r
+        int res;\r
+        printf("Attempting to cancel producer thread.\n");\r
+        res = pthread_cancel(producer);\r
+        if (res != 0){\r
+            printf("Failed.\n");\r
+            /* Not sure what to do with error here, will have to figure out later. */\r
+        }\r
+        printf("Attempting to cancel consumer thread.\n");\r
+        res = pthread_cancel(consumer);\r
+        if (res != 0){\r
+            printf("Failed.\n");\r
+        }\r
+        printf("Preparing to destroy semaphores.\n");\r
+        sleep(1);\r
+        sem_destroy(&produced);\r
+        sem_destroy(&consumed);\r
+        running = 0;\r
+        printf("Finished.\n");\r
+        return 0;\r
+    } else {\r
+        printf("Load is not running.\n");\r
+        return 0;\r
+    }\r
+}
\ No newline at end of file
diff --git a/rtems/gw/libs/load.h b/rtems/gw/libs/load.h
new file mode 100644 (file)
index 0000000..b355c24
--- /dev/null
@@ -0,0 +1,7 @@
+#ifndef __CPU_LOAD_H_\r
+#define __CPU_LOAD_H_\r
+\r
+int start_thread_load();\r
+int end_thread_load();\r
+\r
+#endif
\ No newline at end of file