f25f42
commit 3a6c9a855195e6f6f44ad6dffe2cd4046426ab53
f25f42
Author: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
f25f42
Date:   Wed Nov 25 21:46:26 2020 +0900
f25f42
f25f42
    fix for performance improvement of _mx_init_component() function
f25f42
f25f42
diff --git a/src/components/mx/linux-mx.c b/src/components/mx/linux-mx.c
f25f42
index 2da406d14..34e6f02c2 100644
f25f42
--- a/src/components/mx/linux-mx.c
f25f42
+++ b/src/components/mx/linux-mx.c
f25f42
@@ -224,26 +224,35 @@ _mx_init_component( int cidx )
f25f42
 {
f25f42
 
f25f42
 	FILE *fff;
f25f42
-	char test_string[BUFSIZ];
f25f42
+	char *path;
f25f42
+	int len, pathlen;
f25f42
 
f25f42
 	/* detect if MX available */
f25f42
 
f25f42
-	strncpy(mx_counters_exe,"mx_counters 2> /dev/null",BUFSIZ);
f25f42
-	fff=popen(mx_counters_exe,"r");
f25f42
-	/* popen only returns NULL if "sh" fails, not the actual command */
f25f42
-	if (fgets(test_string,BUFSIZ,fff)==NULL) {
f25f42
-	   pclose(fff);
f25f42
-	   strncpy(mx_counters_exe,"./components/mx/utils/fake_mx_counters 2> /dev/null",BUFSIZ);
f25f42
-	   fff=popen(mx_counters_exe,"r");
f25f42
-	   if (fgets(test_string,BUFSIZ,fff)==NULL) {
f25f42
-	      pclose(fff);
f25f42
-	      /* neither real nor fake found */
f25f42
-	      strncpy(_mx_vector.cmp_info.disabled_reason,
f25f42
-		      "No MX utilities found",PAPI_MAX_STR_LEN);
f25f42
-	      return PAPI_ECMP;
f25f42
+	path = getenv("PATH");
f25f42
+	pathlen = strlen(path);
f25f42
+	while(pathlen > 0) {
f25f42
+	   len = strcspn(path, ":");
f25f42
+	   strncpy(mx_counters_exe, path, len);
f25f42
+	   mx_counters_exe[len] = '\0';
f25f42
+	   strcat(mx_counters_exe, "/mx_counters");
f25f42
+	   fff = fopen(mx_counters_exe, "r");
f25f42
+	   if (fff != NULL) {
f25f42
+	      strcat(mx_counters_exe, " 2> /dev/null");
f25f42
+	      break;
f25f42
 	   }
f25f42
+	   pathlen = pathlen - len - 1;
f25f42
+	   if (pathlen > 0) {
f25f42
+	      path = path + len + 1;
f25f42
+	   }
f25f42
+	}
f25f42
+	if (fff == NULL) {
f25f42
+	   /* neither real nor fake found */
f25f42
+	   strncpy(_mx_vector.cmp_info.disabled_reason,
f25f42
+		   "No MX utilities found",PAPI_MAX_STR_LEN);
f25f42
+	   return PAPI_ECMP;
f25f42
 	}
f25f42
-	pclose(fff);
f25f42
+	fclose(fff);
f25f42
 
f25f42
 	num_events=MX_MAX_COUNTERS;
f25f42
 	_mx_vector.cmp_info.num_native_events=num_events;
f25f42
commit 3a2560a86be44f4b15d96a45eda8e7f387b9166c
f25f42
Author: Masahiko, Yamada <yamada.masahiko@fujitsu.com>
f25f42
Date:   Tue Jan 26 16:30:40 2021 +0900
f25f42
f25f42
    Add string length check before strncpy() and strcat() calls in _mx_init_component()
f25f42
    
f25f42
    Myrinet Express-related component MX modules are initialized with the _mx_init_component() function,
f25f42
    which is called from the PAPI_library_init() function.
f25f42
    The popen(3) call runs a loadable module called "mx_counters",
f25f42
    and if the loadable module does not exist,
f25f42
    it attempts to run a loadable module called "./components/mx/utils/fake_mx_counters".
f25f42
    In an environment where there are no "mx_counters" and "./components/mx/utils/fake_mx_counters" loadable modules,
f25f42
    popen(3) will be called twice uselessly.
f25f42
    popen(3) internally calls pipe(2) once, fork(2) twice and exec(2) once.
f25f42
    
f25f42
    The size of the user space of the application calling the PAPI_library_init() function affects the performance of fork(2),
f25f42
    which is called as an extension of popen(3).
f25f42
    As a result, the performance of the PAPI_library_init() function is affected by the amount of user space in the application
f25f42
    that called the PAPI_library_init() function.
f25f42
    
f25f42
    In the _mx_init_component() function,
f25f42
    the MX module only needs to be able to verify that a load module named "mx_counters" exists.
f25f42
    We improved the _mx_init_component() function to call fopen(3) instead of popen(3).
f25f42
    We add string length check before strncpy() and strcat() calls in _mx_init_component() function.
f25f42
f25f42
diff --git a/src/components/mx/linux-mx.c b/src/components/mx/linux-mx.c
f25f42
index 34e6f02c2..c2920d65b 100644
f25f42
--- a/src/components/mx/linux-mx.c
f25f42
+++ b/src/components/mx/linux-mx.c
f25f42
@@ -225,7 +225,7 @@ _mx_init_component( int cidx )
f25f42
 
f25f42
 	FILE *fff;
f25f42
 	char *path;
f25f42
-	int len, pathlen;
f25f42
+	int checklen, len, pathlen;
f25f42
 
f25f42
 	/* detect if MX available */
f25f42
 
f25f42
@@ -233,13 +233,31 @@ _mx_init_component( int cidx )
f25f42
 	pathlen = strlen(path);
f25f42
 	while(pathlen > 0) {
f25f42
 	   len = strcspn(path, ":");
f25f42
-	   strncpy(mx_counters_exe, path, len);
f25f42
+	   if (len < BUFSIZ) {
f25f42
+	      strncpy(mx_counters_exe, path, len);
f25f42
+	   } else {
f25f42
+	      fff = NULL;
f25f42
+	      break;
f25f42
+	   }
f25f42
 	   mx_counters_exe[len] = '\0';
f25f42
-	   strcat(mx_counters_exe, "/mx_counters");
f25f42
+	   checklen = len + strlen("/mx_counters");
f25f42
+	   if (checklen < BUFSIZ) {
f25f42
+	      strcat(mx_counters_exe, "/mx_counters");
f25f42
+	   } else {
f25f42
+	      fff = NULL;
f25f42
+	      break;
f25f42
+	   }
f25f42
 	   fff = fopen(mx_counters_exe, "r");
f25f42
 	   if (fff != NULL) {
f25f42
-	      strcat(mx_counters_exe, " 2> /dev/null");
f25f42
-	      break;
f25f42
+	      checklen = checklen + strlen(" 2> /dev/null");
f25f42
+	      if (checklen < BUFSIZ) {
f25f42
+	         strcat(mx_counters_exe, " 2> /dev/null");
f25f42
+	         break;
f25f42
+	      } else {
f25f42
+	         fclose(fff);
f25f42
+	         fff = NULL;
f25f42
+	         break;
f25f42
+	      }
f25f42
 	   }
f25f42
 	   pathlen = pathlen - len - 1;
f25f42
 	   if (pathlen > 0) {
f25f42
@@ -247,7 +265,7 @@ _mx_init_component( int cidx )
f25f42
 	   }
f25f42
 	}
f25f42
 	if (fff == NULL) {
f25f42
-	   /* neither real nor fake found */
f25f42
+	   /* mx_counters not found */
f25f42
 	   strncpy(_mx_vector.cmp_info.disabled_reason,
f25f42
 		   "No MX utilities found",PAPI_MAX_STR_LEN);
f25f42
 	   return PAPI_ECMP;