Speed up linux ocserver to fix apparent instabilities.
authorMandeep Shetty <mandeep.shetty@intel.com>
Thu, 1 Oct 2015 00:55:50 +0000 (17:55 -0700)
committerPatrick Lankswert <patrick.lankswert@intel.com>
Tue, 6 Oct 2015 12:57:04 +0000 (12:57 +0000)
The linux ocserver had three threads running uncluding the main thread.
All three had sleeps in it and there was a delay of 2 seconds between
every call to OCProcess ().
This caused the ocserver to be slow and the server appeared to be
unstable specially when there were many clients sending in requests to
one server.

- Changed ocserver to start the observation thread only when observation
is requested similar to the C++ sample simpleserver.
- Removed the sleep between calls to OCProcess ()
- Moved stopping presence logic to the presence thread from the
  observation thread.

This may be cherrypicked to 1.0.0-dev.

Change-Id: Ib1c9fd732d0973435b556adb8a42610447f1ae7b
Signed-off-by: Mandeep Shetty <mandeep.shetty@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/3533
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Patrick Lankswert <patrick.lankswert@intel.com>
resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp

index 65d7075..42ba141 100644 (file)
@@ -50,8 +50,12 @@ static LightResource gLightInstance[SAMPLE_MAX_NUM_POST_INSTANCE];
 
 Observers interestedObservers[SAMPLE_MAX_NUM_OBSERVATIONS];
 
+pthread_t threadId_observe;
+pthread_t threadId_presence;
+
+static bool observeThreadStarted = false;
+
 #ifdef WITH_PRESENCE
-static int stopPresenceCount = 10;
 #define numPresenceResources (2)
 #endif
 
@@ -378,6 +382,12 @@ void ProcessObserveRegister (OCEntityHandlerRequest *ehRequest)
 {
     OC_LOG_V (INFO, TAG, "Received observation registration request with observation Id %d",
             ehRequest->obsInfo.obsId);
+
+    if (!observeThreadStarted)
+    {
+        pthread_create (&threadId_observe, NULL, ChangeLightRepresentation, (void *)NULL);
+        observeThreadStarted = 1;
+    }
     for (uint8_t i = 0; i < SAMPLE_MAX_NUM_OBSERVATIONS; i++)
     {
         if (interestedObservers[i].valid == false)
@@ -696,17 +706,6 @@ void *ChangeLightRepresentation (void *param)
                 OC_LOG (ERROR, TAG, "Incorrect notification type selected");
             }
         }
-#ifdef WITH_PRESENCE
-        if(stopPresenceCount > 0)
-        {
-            OC_LOG_V(INFO, TAG, "================  Counting down to stop presence %d", stopPresenceCount);
-        }
-        if(!stopPresenceCount--)
-        {
-            OC_LOG(INFO, TAG, "================ stopping presence");
-            OCStopPresence();
-        }
-#endif
     }
     return NULL;
 }
@@ -714,7 +713,9 @@ void *ChangeLightRepresentation (void *param)
 #ifdef WITH_PRESENCE
 void *presenceNotificationGenerator(void *param)
 {
-    sleep(10);
+    uint8_t secondsBeforePresence = 10;
+    OC_LOG_V(INFO, TAG, "Will send out presence in %u seconds", secondsBeforePresence);
+    sleep(secondsBeforePresence);
     (void)param;
     OCDoHandle presenceNotificationHandles[numPresenceResources];
     OCStackResult res = OC_STACK_OK;
@@ -765,6 +766,10 @@ void *presenceNotificationGenerator(void *param)
         OC_LOG_V(INFO, TAG, PCF("Deleted %s for presence notification"),
                                 presenceNotificationUris[i].c_str());
     }
+
+    OC_LOG(INFO, TAG, "================ stopping presence");
+    OCStopPresence();
+
     return NULL;
 }
 #endif
@@ -930,8 +935,6 @@ static void PrintUsage()
 
 int main(int argc, char* argv[])
 {
-    pthread_t threadId;
-    pthread_t threadId_presence;
     int opt;
 
     while ((opt = getopt(argc, argv, "o:")) != -1)
@@ -1028,10 +1031,6 @@ int main(int argc, char* argv[])
         interestedObservers[i].valid = false;
     }
 
-    /*
-     * Create a thread for changing the representation of the Light
-     */
-    pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)NULL);
 
     /*
      * Create a thread for generating changes that cause presence notifications
@@ -1057,16 +1056,14 @@ int main(int argc, char* argv[])
             OC_LOG(ERROR, TAG, "OCStack process error");
             return 0;
         }
-#ifndef ROUTING_GATEWAY
-        sleep(2);
-#endif
     }
 
-    /*
-     * Cancel the Light thread and wait for it to terminate
-     */
-    pthread_cancel(threadId);
-    pthread_join(threadId, NULL);
+    if (observeThreadStarted)
+    {
+        pthread_cancel(threadId_observe);
+        pthread_join(threadId_observe, NULL);
+    }
+
     pthread_cancel(threadId_presence);
     pthread_join(threadId_presence, NULL);