Fixes for multi-threaded client test
authorJustin Sobota <jsobota@ti.com>
Tue, 2 Dec 2014 16:58:59 +0000 (11:58 -0500)
committerJustin Sobota <jsobota@ti.com>
Tue, 2 Dec 2014 16:58:59 +0000 (11:58 -0500)
test/armv7/linux/rm_linux_mt_client_test.c

index 44099f7315a81abf071715f9989da3a0b1351c7a..7eb65c368f5cf64df5475313ce5e0f344bf95656 100644 (file)
@@ -106,36 +106,39 @@ typedef struct trans_map_entry_s {
 /* Alloc and free OSAL variables */
 extern uint32_t rmMallocCounter;
 extern uint32_t rmFreeCounter;
+extern int32_t rmByteAlloc;
+extern int32_t rmByteFree;
+
 
 /**********************************************************************
  ********************** Global Variables ******************************
  **********************************************************************/
 
 /* Number of errors that occurred during the test */
-uint16_t            test_errors;
-
-/* Non-Serial client instance name */
-char                client_name[RM_NAME_MAX_CHARS] = "RM_Client";
+uint16_t           test_errors;
 
-/* Non-Serial client instance handle */
-Rm_Handle           client_handle = NULL;
+/* RM Client Variables */
+char               client_name[RM_NAME_MAX_CHARS] = "RM_Client";
+Rm_Handle          client_handle = NULL;
+Rm_ServiceHandle  *service_handle = NULL;
 
 /* pthread mutex lock needed for testing */
-pthread_mutex_t     client_mutex_lock;
+pthread_mutex_t    client_mutex_lock;
 
 /* Client socket name */
-char                client_sock_name[] = "/tmp/var/run/rm/rm_client";
+char               client_sock_name[] = "/tmp/var/run/rm/rm_client";
 
 /* Client socket handles */
-sock_h              client_sock;
+sock_h             client_sock;
 
 /* Transport map stores the RM transport handle to IPC MessageQ queue mapping */
-trans_map_entry_t   rm_trans_map[MAX_MAPPING_ENTRIES];
+trans_map_entry_t  rm_trans_map[MAX_MAPPING_ENTRIES];
 
 /* RM resource names (must match resource node names in GRL and policies */
-char                res_name_link_ram[RM_NAME_MAX_CHARS] = "link-ram";
+char               res_name_link_ram[RM_NAME_MAX_CHARS] = "link-ram";
 
-uint32_t            allocs[REQUEST_ITERATIONS];
+/* Used to track allocations in each thread */
+uint32_t           allocs[REQUEST_ITERATIONS];
 
 /**********************************************************************
  *************************** Test Functions ***************************
@@ -324,7 +327,6 @@ int32_t transportSendRcv(Rm_AppTransportHandle appTransport, Rm_PacketHandle pkt
 void *allocate_test (void *args)
 {
     uint32_t           *thread_num = (uint32_t *)args;
-    Rm_ServiceHandle   *service_handle = NULL;
     int32_t             result;
     Rm_ServiceReqInfo   request;
     Rm_ServiceRespInfo  response;
@@ -333,9 +335,6 @@ void *allocate_test (void *args)
     
     tim.tv_sec = 0;
     tim.tv_nsec = 1000;
-    
-    service_handle = Rm_serviceOpenHandle(client_handle, &result);
-    ERROR_CHECK(RM_OK, result, client_name, "Service handle open failed"); 
 
     if (service_handle) {
         /* Init request - request one resource at a time as unspecified.  Server
@@ -360,7 +359,7 @@ void *allocate_test (void *args)
                 }
             }
             else {
-                printf("Error Thread %d : Allocate request was not approved for resource %d - Exiting...\n",
+                printf("Error Thread %d : Allocate request was not approved for resource %d\n",
                        *thread_num, request.resourceBase);
                 test_errors++;
                 goto error_exit;
@@ -369,28 +368,21 @@ void *allocate_test (void *args)
     }
 
 error_exit:
-    if (service_handle) {
-        Rm_serviceCloseHandle(service_handle);
-    }
-    
+    printf("Thread %d : Exiting...\n", *thread_num);
     pthread_exit((void*) 0);
     return NULL;
 }
 
 void resource_cleanup(void)
 {
-    Rm_ServiceHandle   *service_handle = NULL;
-    int32_t             result;
-    Rm_ServiceReqInfo   request;
-    Rm_ServiceRespInfo  response;
-    uint32_t            i;
-
-    service_handle = Rm_serviceOpenHandle(client_handle, &result);
-    ERROR_CHECK(RM_OK, result, client_name, "Service handle open failed"); 
+    int32_t            result;
+    Rm_ServiceReqInfo  request;
+    Rm_ServiceRespInfo response;
+    uint32_t           i;
 
     if (service_handle) {
         printf("Freeing all %d resources...\n", REQUEST_ITERATIONS);
-        for (i = 0; i < (REQUEST_ITERATIONS); i++) {
+        for (i = 0; i < REQUEST_ITERATIONS; i++) {
             while (allocs[i]) {
                 memset(&request, 0, sizeof(request));
                 request.type = Rm_service_RESOURCE_FREE;
@@ -410,8 +402,6 @@ void resource_cleanup(void)
                 allocs[i]--;
             }
         }
-        
-        Rm_serviceCloseHandle(service_handle);
     }
 }
 
@@ -489,6 +479,12 @@ int32_t client_init(void)
         goto error_exit;
     }  
 
+    service_handle = Rm_serviceOpenHandle(client_handle, &result);
+    ERROR_CHECK(RM_OK, result, client_name, "Service handle open failed"); 
+    if (result != RM_OK) {
+        goto error_exit;
+    }
+
     return (RM_TEST_TRUE);
 error_exit:
     return (RM_TEST_FALSE);
@@ -501,6 +497,11 @@ void client_cleanup(void)
     int     pthread_result;
 
     printf("Deleting RM Client\n");
+
+    if (service_handle) {
+        Rm_serviceCloseHandle(service_handle);
+        ERROR_CHECK(RM_OK, result, client_name, "Service handle close failed");
+    }
     
     if (client_handle) {
         result = Rm_delete(client_handle, RM_TEST_TRUE);
@@ -516,6 +517,7 @@ int main(int argc, char *argv[])
 {
     int32_t     result;
     int32_t     malloc_free_diff;
+    int32_t     byte_free_diff;
     task_handle first_th;
     task_handle second_th;
     int         status;
@@ -548,6 +550,7 @@ int main(int argc, char *argv[])
         task_wait(&second_th);
 
         if (!test_errors) {
+            printf("Threads complete - Checking for allocation errors\n");
             for (i = 0; i < REQUEST_ITERATIONS; i++) {
                 if (allocs[i] != 1) {
                     printf ("FAILED: Resource %d not allocated exactly once\n", i);
@@ -568,17 +571,27 @@ cleanup_test:
     
     printf ("---------------------------------------------------------\n");
     printf ("------------------ Memory Leak Check --------------------\n");
-    printf ("-                       : malloc count   |   free count -\n");
-    printf ("- Example Completion    :  %6d        |  %6d      -\n", rmMallocCounter, rmFreeCounter);
-    malloc_free_diff = rmMallocCounter - rmFreeCounter; 
+    printf ("-                     :  malloc count  |   free count   -\n");
+    printf ("- Example Completion  :         %6d |         %6d -\n", rmMallocCounter, rmFreeCounter);
+    printf ("-            (bytes)  :       %8d |       %8d -\n", rmByteAlloc, rmByteFree);
+    malloc_free_diff = rmMallocCounter - rmFreeCounter;
+    byte_free_diff = rmByteAlloc - rmByteFree;
     if (malloc_free_diff > 0) {
         printf ("- FAILED - %6d unfreed mallocs                       -\n", malloc_free_diff);
         test_errors++;
     }
+    else if (byte_free_diff > 0) {
+        printf ("- FAILED - %6d unfreed bytes                         -\n", byte_free_diff);
+        test_errors++;
+    }    
     else if (malloc_free_diff < 0) {
         printf ("- FAILED - %6d more frees than mallocs               -\n", -malloc_free_diff);
         test_errors++;
     }
+    else if (byte_free_diff < 0) {
+        printf ("- FAILED - %6d more bytes freed than malloc'd        -\n", -byte_free_diff);
+        test_errors++;
+    }    
     else {
         printf ("- PASSED                                                -\n");
     }