From c48489575cd00448be825a361856985fd01811af Mon Sep 17 00:00:00 2001
From: Danny Auble <da@schedmd.com>
Date: Wed, 5 Jun 2013 10:36:56 -0700
Subject: [PATCH] acct_gather - bring the freq parsing out as a separate
 function.

---
 src/common/slurm_acct_gather.c         | 61 ++++++++++++++++++++++-
 src/common/slurm_acct_gather.h         |  1 +
 src/common/slurm_acct_gather_profile.c | 68 ++++++--------------------
 3 files changed, 74 insertions(+), 56 deletions(-)

diff --git a/src/common/slurm_acct_gather.c b/src/common/slurm_acct_gather.c
index 01fb76bd3e0..bb69461f115 100644
--- a/src/common/slurm_acct_gather.c
+++ b/src/common/slurm_acct_gather.c
@@ -36,12 +36,30 @@
 \*****************************************************************************/
 
 #include <sys/stat.h>
+#include <stdlib.h>
 
-#include "slurm_acct_gather.h"
-#include "xstring.h"
+#include "src/common/slurm_acct_gather.h"
+#include "src/common/slurm_strcasestr.h"
+#include "src/common/xstring.h"
 
 static bool inited = 0;
 
+static int _get_int(const char *my_str)
+{
+	char *end = NULL;
+	int value;
+
+	if (!my_str)
+		return -1;
+	value = strtol(my_str, &end, 10);
+	//info("from %s I get %d and %s: %m", my_str, value, end);
+	/* means no numbers */
+	if (my_str == end)
+		return -1;
+
+	return value;
+}
+
 extern int acct_gather_conf_init(void)
 {
 	s_p_hashtbl_t *tbl = NULL;
@@ -109,3 +127,42 @@ extern int acct_gather_conf_destroy(void)
 
 	return SLURM_SUCCESS;
 }
+
+extern int acct_gather_parse_freq(int type, char *freq)
+{
+	int freq_int = -1;
+	char *sub_str = NULL;
+
+	if (!freq)
+		return freq_int;
+
+	switch (type) {
+	case PROFILE_ENERGY:
+		if ((sub_str = slurm_strcasestr(freq, "energy=")))
+			freq_int = _get_int(sub_str + 7);
+		break;
+	case PROFILE_TASK:
+		/* backwards compatibility for when the freq was only
+		   for task.
+		*/
+		freq_int = _get_int(freq);
+		if ((freq_int == -1)
+		    && (sub_str = slurm_strcasestr(freq, "task=")))
+			freq_int = _get_int(sub_str + 5);
+		break;
+	case PROFILE_FILESYSTEM:
+		if ((sub_str = slurm_strcasestr(freq, "filesystem=")))
+			freq_int = _get_int(sub_str + 11);
+		break;
+	case PROFILE_NETWORK:
+		if ((sub_str = slurm_strcasestr(freq, "network=")))
+			freq_int = _get_int(sub_str + 8);
+		break;
+	default:
+		fatal("Unhandled profile option %d please update "
+		      "slurm_acct_gather.c "
+		      "(acct_gather_parse_freq)", type);
+	}
+
+	return freq_int;
+}
diff --git a/src/common/slurm_acct_gather.h b/src/common/slurm_acct_gather.h
index 54e40165e76..b99617d009c 100644
--- a/src/common/slurm_acct_gather.h
+++ b/src/common/slurm_acct_gather.h
@@ -59,5 +59,6 @@
 
 extern int acct_gather_conf_init(void);
 extern int acct_gather_conf_destroy(void);
+extern int acct_gather_parse_freq(int type, char *freq);
 
 #endif
diff --git a/src/common/slurm_acct_gather_profile.c b/src/common/slurm_acct_gather_profile.c
index 1513061fc30..2932a385299 100644
--- a/src/common/slurm_acct_gather_profile.c
+++ b/src/common/slurm_acct_gather_profile.c
@@ -95,20 +95,13 @@ static pthread_mutex_t profile_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_t timer_thread_id = 0;
 static bool init_run = false;
 
-static int _get_int(const char *my_str)
+static void _set_freq(int type, char *freq, char *freq_def)
 {
-	char *end = NULL;
-	int value;
-
-	if (!my_str)
-		return -1;
-	value = strtol(my_str, &end, 10);
-	//info("from %s I get %d and %s: %m", my_str, value, end);
-	/* means no numbers */
-	if (my_str == end)
-		return -1;
-
-	return value;
+	if ((acct_gather_profile_timer[type].freq =
+	     acct_gather_parse_freq(type, freq)) == -1)
+		if ((acct_gather_profile_timer[type].freq =
+		     acct_gather_parse_freq(type, freq_def)) == -1)
+			acct_gather_profile_timer[type].freq = 0;
 }
 
 static void *_timer_thread(void *args)
@@ -308,8 +301,6 @@ extern int acct_gather_profile_startpoll(char *freq, char *freq_def)
 	xassert(profile != ACCT_GATHER_PROFILE_NOT_SET);
 
 	for (i=0; i < PROFILE_CNT; i++) {
-		char *type = NULL;
-
 		memset(&acct_gather_profile_timer[i], 0,
 		       sizeof(acct_gather_profile_timer_t));
 		pthread_cond_init(&acct_gather_profile_timer[i].notify, NULL);
@@ -319,13 +310,8 @@ extern int acct_gather_profile_startpoll(char *freq, char *freq_def)
 		case PROFILE_ENERGY:
 			if (!(profile & ACCT_GATHER_PROFILE_ENERGY))
 				break;
-			if (freq && (type = slurm_strcasestr(freq, "energy=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+7);
-			else if (freq_def && (type = slurm_strcasestr(
-						      freq_def, "energy=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+7);
+			_set_freq(i, freq, freq_def);
+
 			acct_gather_energy_startpoll(
 				acct_gather_profile_timer[i].freq);
 			break;
@@ -335,23 +321,8 @@ extern int acct_gather_profile_startpoll(char *freq, char *freq_def)
 			   consumption and such.  It will check
 			   profile inside it's plugin.
 			*/
-			acct_gather_profile_timer[i].freq = _get_int(freq);
-			if (acct_gather_profile_timer[i].freq == -1) {
-				if (freq && (type = slurm_strcasestr(
-						     freq, "task=")))
-					acct_gather_profile_timer[i].freq =
-						_get_int(type+5);
-				else if (freq_def) {
-					acct_gather_profile_timer[i].freq =
-						_get_int(freq_def);
-					if ((acct_gather_profile_timer[i].freq
-					     == -1)
-					    && (type = slurm_strcasestr(
-							freq_def, "task=")))
-						acct_gather_profile_timer[i].
-							freq = _get_int(type+5);
-				}
-			}
+			_set_freq(i, freq, freq_def);
+
 			jobacct_gather_startpoll(
 				acct_gather_profile_timer[i].freq);
 
@@ -359,27 +330,16 @@ extern int acct_gather_profile_startpoll(char *freq, char *freq_def)
 		case PROFILE_FILESYSTEM:
 			if (!(profile & ACCT_GATHER_PROFILE_LUSTRE))
 				break;
-			if (freq && (type = slurm_strcasestr(
-					     freq, "filesystem=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+11);
-			else if (freq_def && (type = slurm_strcasestr(
-						      freq_def, "filesystem=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+11);
+			_set_freq(i, freq, freq_def);
+
 			acct_gather_filesystem_startpoll(
 				acct_gather_profile_timer[i].freq);
 			break;
 		case PROFILE_NETWORK:
 			if (!(profile & ACCT_GATHER_PROFILE_NETWORK))
 				break;
-			if (freq && (type = slurm_strcasestr(freq, "network=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+8);
-			else if (freq_def && (type = slurm_strcasestr(
-						      freq_def, "network=")))
-				acct_gather_profile_timer[i].freq =
-					_get_int(type+8);
+			_set_freq(i, freq, freq_def);
+
 			acct_gather_infiniband_startpoll(
 				acct_gather_profile_timer[i].freq);
 			break;
-- 
GitLab