You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 07:37:49 UTC

svn commit: r1131825 - /incubator/mesos/trunk/src/memhog_executor.cpp

Author: benh
Date: Sun Jun  5 05:37:49 2011
New Revision: 1131825

URL: http://svn.apache.org/viewvc?rev=1131825&view=rev
Log:
Made memhog access memory in a more random pattern, so that it is more
stressful on isolation.

Modified:
    incubator/mesos/trunk/src/memhog_executor.cpp

Modified: incubator/mesos/trunk/src/memhog_executor.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/memhog_executor.cpp?rev=1131825&r1=1131824&r2=1131825&view=diff
==============================================================================
--- incubator/mesos/trunk/src/memhog_executor.cpp (original)
+++ incubator/mesos/trunk/src/memhog_executor.cpp Sun Jun  5 05:37:49 2011
@@ -21,11 +21,11 @@ class MemHogExecutor;
 struct ThreadArg
 {
   MemHogExecutor *executor;
-  TaskID tid;
-  bool primary;
+  TaskID taskId;
+  int threadId;
 
-  ThreadArg(MemHogExecutor *e, TaskID t, bool p)
-    : executor(e), tid(t), primary(p) {}
+  ThreadArg(MemHogExecutor *exec, TaskID task, int thread)
+    : executor(exec), taskId(task), threadId(thread) {}
 };
 
 
@@ -54,7 +54,7 @@ public:
   virtual void launchTask(ExecutorDriver*, const TaskDescription& task) {
     cout << "Executor starting task " << task.taskId << endl;
     for (int i = 0; i < threadsPerTask; i++) {
-      ThreadArg *arg = new ThreadArg(this, task.taskId, i == 0);
+      ThreadArg *arg = new ThreadArg(this, task.taskId, i);
       pthread_t thread;
       pthread_create(&thread, 0, runTask, arg);
       pthread_detach(thread);
@@ -63,6 +63,18 @@ public:
 };
 
 
+// A simple linear congruential generator, used to access memory in a random
+// pattern without relying on a possibly synchronized stdlib rand().
+// Constants from http://en.wikipedia.org/wiki/Linear_congruential_generator.
+uint32_t nextRand(uint32_t x) {
+  const int64_t A = 1664525;
+  const int64_t B = 1013904223;
+  int64_t longX = x;
+  return (uint32_t) ((A * longX + B) & 0xFFFFFFFF);
+}
+
+
+// Function executed by each worker thread.
 void *runTask(void *arg)
 {
   ThreadArg *threadArg = (ThreadArg *) arg;
@@ -73,22 +85,23 @@ void *runTask(void *arg)
   char *data = new char[memToHog];
   int32_t count = 0;
   time_t start = time(0);
+  uint32_t pos = threadArg->threadId;
   while (true) {
-    for (int64_t i = 0; i < memToHog; i++) {
-      data[i] = i;
-      count++;
-      if (count == 10000) {
-        count = 0;
-        time_t now = time(0);
-        if (difftime(now, start) > taskLen) {
-          delete[] data;
-          if (threadArg->primary) {
-            usleep(100000); // sleep 0.1 seconds
-            TaskStatus status(threadArg->tid, TASK_FINISHED, "");
-            executor->driver->sendStatusUpdate(status);
-          }
-          return 0;
+    pos = nextRand(pos);
+    data[pos % memToHog] = pos;
+    count++;
+    if (count == 5000) {
+      // Check whether enough time has elapsed to end the task
+      count = 0;
+      time_t now = time(0);
+      if (difftime(now, start) > taskLen) {
+        delete[] data;
+        if (threadArg->threadId == 0) {
+          usleep(100000); // sleep 0.1 seconds for other threads to finish
+          TaskStatus status(threadArg->taskId, TASK_FINISHED, "");
+          executor->driver->sendStatusUpdate(status);
         }
+        return 0;
       }
     }
   }