Browse Source

Merge remote-tracking branch 'bfg-pull/153/head' into bfgminer

Conflicts:
	miner.c
Luke Dashjr 13 years ago
parent
commit
f871403202
3 changed files with 229 additions and 114 deletions
  1. 14 58
      api.c
  2. 205 56
      miner.c
  3. 10 0
      miner.h

+ 14 - 58
api.c

@@ -2167,70 +2167,26 @@ static void enablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __
 
 
 static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 static void poolpriority(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 {
 {
-	char *ptr, *next;
-	int i, pr, prio = 0;
-
-	// TODO: all cgminer code needs a mutex added everywhere for change
-	//	access to total_pools and also parts of the pools[] array,
-	//	just copying total_pools here wont solve that
-
-	if (total_pools == 0) {
-		strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
-		return;
-	}
-
-	if (param == NULL || *param == '\0') {
-		strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
-		return;
-	}
-
-	bool pools_changed[total_pools];
-	int new_prio[total_pools];
-	for (i = 0; i < total_pools; ++i)
-		pools_changed[i] = false;
-
-	next = param;
-	while (next && *next) {
-		ptr = next;
-		next = strchr(ptr, ',');
-		if (next)
-			*(next++) = '\0';
+	int i;
 
 
-		i = atoi(ptr);
-		if (i < 0 || i >= total_pools) {
+	switch (prioritize_pools(param, &i)) {
+		case MSG_NOPOOL:
+			strcpy(io_buffer, message(MSG_NOPOOL, 0, NULL, isjson));
+			return;
+		case MSG_MISPID:
+			strcpy(io_buffer, message(MSG_MISPID, 0, NULL, isjson));
+			return;
+		case MSG_INVPID:
 			strcpy(io_buffer, message(MSG_INVPID, i, NULL, isjson));
 			strcpy(io_buffer, message(MSG_INVPID, i, NULL, isjson));
 			return;
 			return;
-		}
-
-		if (pools_changed[i]) {
+		case MSG_DUPPID:
 			strcpy(io_buffer, message(MSG_DUPPID, i, NULL, isjson));
 			strcpy(io_buffer, message(MSG_DUPPID, i, NULL, isjson));
 			return;
 			return;
-		}
-
-		pools_changed[i] = true;
-		new_prio[i] = prio++;
-	}
-
-	// Only change them if no errors
-	for (i = 0; i < total_pools; i++) {
-		if (pools_changed[i])
-			pools[i]->prio = new_prio[i];
+		case MSG_POOLPRIO:
+		default:
+			strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
+			return;
 	}
 	}
-
-	// In priority order, cycle through the unchanged pools and append them
-	for (pr = 0; pr < total_pools; pr++)
-		for (i = 0; i < total_pools; i++) {
-			if (!pools_changed[i] && pools[i]->prio == pr) {
-				pools[i]->prio = prio++;
-				pools_changed[i] = true;
-				break;
-			}
-		}
-
-	if (current_pool()->prio)
-		switch_pools(NULL);
-
-	strcpy(io_buffer, message(MSG_POOLPRIO, 0, NULL, isjson));
 }
 }
 
 
 static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
 static void disablepool(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)

+ 205 - 56
miner.c

@@ -789,6 +789,19 @@ static char *set_userpass(const char *arg)
 	return NULL;
 	return NULL;
 }
 }
 
 
+static char *set_pool_priority(const char *arg)
+{
+	struct pool *pool;
+
+	if (!total_pools)
+		return "Usage of --pool-priority before pools are defined does not make sense";
+
+	pool = pools[total_pools - 1];
+	opt_set_intval(arg, &pool->prio);
+
+	return NULL;
+}
+
 static char *set_pool_proxy(const char *arg)
 static char *set_pool_proxy(const char *arg)
 {
 {
 	struct pool *pool;
 	struct pool *pool;
@@ -1224,6 +1237,9 @@ static struct opt_table opt_config_table[] = {
 	OPT_WITHOUT_ARG("--per-device-stats",
 	OPT_WITHOUT_ARG("--per-device-stats",
 			opt_set_bool, &want_per_device_stats,
 			opt_set_bool, &want_per_device_stats,
 			"Force verbose mode and output per-device statistics"),
 			"Force verbose mode and output per-device statistics"),
+	OPT_WITH_ARG("--pool-priority",
+			 set_pool_priority, NULL, NULL,
+			 "Priority for just the previous-defined pool"),
 	OPT_WITH_ARG("--pool-proxy|-x",
 	OPT_WITH_ARG("--pool-proxy|-x",
 		     set_pool_proxy, NULL, NULL,
 		     set_pool_proxy, NULL, NULL,
 		     "Proxy URI to use for connecting to just the previous-defined pool"),
 		     "Proxy URI to use for connecting to just the previous-defined pool"),
@@ -3716,6 +3732,101 @@ static struct pool *priority_pool(int choice)
 	return ret;
 	return ret;
 }
 }
 
 
+int prioritize_pools(char *param, int *pid)
+{
+	char *ptr, *next;
+	int i, pr, prio = 0;
+
+	if (total_pools == 0) {
+		return MSG_NOPOOL;
+	}
+
+	if (param == NULL || *param == '\0') {
+		return MSG_MISPID;
+	}
+
+	bool pools_changed[total_pools];
+	int new_prio[total_pools];
+	for (i = 0; i < total_pools; ++i)
+		pools_changed[i] = false;
+
+	next = param;
+	while (next && *next) {
+		ptr = next;
+		next = strchr(ptr, ',');
+		if (next)
+			*(next++) = '\0';
+
+		i = atoi(ptr);
+		if (i < 0 || i >= total_pools) {
+			*pid = i;
+			return MSG_INVPID;
+		}
+
+		if (pools_changed[i]) {
+			*pid = i;
+			return MSG_DUPPID;
+		}
+
+		pools_changed[i] = true;
+		new_prio[i] = prio++;
+	}
+
+	// Only change them if no errors
+	for (i = 0; i < total_pools; i++) {
+		if (pools_changed[i])
+			pools[i]->prio = new_prio[i];
+	}
+
+	// In priority order, cycle through the unchanged pools and append them
+	for (pr = 0; pr < total_pools; pr++)
+		for (i = 0; i < total_pools; i++) {
+			if (!pools_changed[i] && pools[i]->prio == pr) {
+				pools[i]->prio = prio++;
+				pools_changed[i] = true;
+				break;
+			}
+		}
+
+	if (current_pool()->prio)
+		switch_pools(NULL);
+
+	return MSG_POOLPRIO;
+}
+
+void validate_pool_priorities(void)
+{
+	// TODO: this should probably do some sort of logging
+	int i, j;
+	bool used[total_pools];
+	bool valid[total_pools];
+
+	for (i = 0; i < total_pools; i++)
+		used[i] = valid[i] = false;
+
+	for (i = 0; i < total_pools; i++) {
+		if (pools[i]->prio >=0 && pools[i]->prio < total_pools) {
+			if (!used[pools[i]->prio]) {
+				valid[i] = true;
+				used[pools[i]->prio] = true;
+			}
+		}
+	}
+
+	for (i = 0; i < total_pools; i++) {
+		if (!valid[i]) {
+			for (j = 0; j < total_pools; j++) {
+				if (!used[j]) {
+					applog(LOG_WARNING, "Pool %d priority changed from %d to %d", i, pools[i]->prio, j);
+					pools[i]->prio = j;
+					used[j] = true;
+					break;
+				}
+			}
+		}
+	}
+}
+
 void switch_pools(struct pool *selected)
 void switch_pools(struct pool *selected)
 {
 {
 	struct pool *pool, *last_pool;
 	struct pool *pool, *last_pool;
@@ -4308,8 +4419,9 @@ void write_config(FILE *fcfg)
 		if (pools[i]->rpc_proxy)
 		if (pools[i]->rpc_proxy)
 			fprintf(fcfg, "\n\t\t\"pool-proxy\" : \"%s\",", json_escape(pools[i]->rpc_proxy));
 			fprintf(fcfg, "\n\t\t\"pool-proxy\" : \"%s\",", json_escape(pools[i]->rpc_proxy));
 		fprintf(fcfg, "\n\t\t\"user\" : \"%s\",", json_escape(pools[i]->rpc_user));
 		fprintf(fcfg, "\n\t\t\"user\" : \"%s\",", json_escape(pools[i]->rpc_user));
-		fprintf(fcfg, "\n\t\t\"pass\" : \"%s\"\n\t}", json_escape(pools[i]->rpc_pass));
-		}
+		fprintf(fcfg, "\n\t\t\"pass\" : \"%s\",", json_escape(pools[i]->rpc_pass));
+		fprintf(fcfg, "\n\t\t\"pool-priority\" : \"%d\"\n\t}", pools[i]->prio);
+	}
 	fputs("\n]\n", fcfg);
 	fputs("\n]\n", fcfg);
 
 
 #ifdef HAVE_OPENCL
 #ifdef HAVE_OPENCL
@@ -4480,55 +4592,62 @@ void write_config(FILE *fcfg)
 static void display_pools(void)
 static void display_pools(void)
 {
 {
 	struct pool *pool;
 	struct pool *pool;
-	int selected, i;
+	int selected, i, j;
 	char input;
 	char input;
 
 
 	opt_loginput = true;
 	opt_loginput = true;
 	immedok(logwin, true);
 	immedok(logwin, true);
 	clear_logwin();
 	clear_logwin();
 updated:
 updated:
-	for (i = 0; i < total_pools; i++) {
-		pool = pools[i];
+	for (j = 0; j < total_pools; j++) {
+		for (i = 0; i < total_pools; i++) {
+			pool = pools[i];
 
 
-		if (pool == current_pool())
-			wattron(logwin, A_BOLD);
-		if (pool->enabled != POOL_ENABLED)
-			wattron(logwin, A_DIM);
-		wlogprint("%d: ", pool->pool_no);
-		switch (pool->enabled) {
-			case POOL_ENABLED:
-				wlogprint("Enabled  ");
-				break;
-			case POOL_DISABLED:
-				wlogprint("Disabled ");
-				break;
-			case POOL_REJECTING:
-				wlogprint("Rejectin ");
-				break;
-		}
-		if (pool->idle)
-			wlogprint("Dead ");
-		else
-		if (pool->has_stratum)
-			wlogprint("Strtm");
-		else
-		if (pool->lp_url && pool->proto != pool->lp_proto)
-			wlogprint("Mixed");
-		else
-			switch (pool->proto) {
-				case PLP_GETBLOCKTEMPLATE:
-					wlogprint(" GBT ");
+			if (pool->prio != j)
+				continue;
+
+			if (pool == current_pool())
+				wattron(logwin, A_BOLD);
+			if (pool->enabled != POOL_ENABLED)
+				wattron(logwin, A_DIM);
+			wlogprint("%d: ", pool->prio);
+			switch (pool->enabled) {
+				case POOL_ENABLED:
+					wlogprint("Enabled  ");
+					break;
+				case POOL_DISABLED:
+					wlogprint("Disabled ");
 					break;
 					break;
-				case PLP_GETWORK:
-					wlogprint("GWork");
+				case POOL_REJECTING:
+					wlogprint("Rejectin ");
 					break;
 					break;
-				default:
-					wlogprint("Alive");
 			}
 			}
-		wlogprint(" Priority %d: %s  User:%s\n",
-			pool->prio,
-			pool->rpc_url, pool->rpc_user);
-		wattroff(logwin, A_BOLD | A_DIM);
+			if (pool->idle)
+				wlogprint("Dead ");
+			else
+			if (pool->has_stratum)
+				wlogprint("Strtm");
+			else
+			if (pool->lp_url && pool->proto != pool->lp_proto)
+				wlogprint("Mixed");
+			else
+				switch (pool->proto) {
+					case PLP_GETBLOCKTEMPLATE:
+						wlogprint(" GBT ");
+						break;
+					case PLP_GETWORK:
+						wlogprint("GWork");
+						break;
+					default:
+						wlogprint("Alive");
+				}
+			wlogprint(" Pool %d: %s  User:%s\n",
+				pool->pool_no,
+				pool->rpc_url, pool->rpc_user);
+			wattroff(logwin, A_BOLD | A_DIM);
+
+			break; //for (i = 0; i < total_pools; i++)
+		}
 	}
 	}
 retry:
 retry:
 	wlogprint("\nCurrent pool management strategy: %s\n",
 	wlogprint("\nCurrent pool management strategy: %s\n",
@@ -4536,7 +4655,7 @@ retry:
 	if (pool_strategy == POOL_ROTATE)
 	if (pool_strategy == POOL_ROTATE)
 		wlogprint("Set to rotate every %d minutes\n", opt_rotate_period);
 		wlogprint("Set to rotate every %d minutes\n", opt_rotate_period);
 	wlogprint("[F]ailover only %s\n", opt_fail_only ? "enabled" : "disabled");
 	wlogprint("[F]ailover only %s\n", opt_fail_only ? "enabled" : "disabled");
-	wlogprint("[A]dd pool [R]emove pool [D]isable pool [E]nable pool\n");
+	wlogprint("[A]dd pool [R]emove pool [D]isable pool [E]nable pool [P]rioritize pool\n");
 	wlogprint("[C]hange management strategy [S]witch pool [I]nformation\n");
 	wlogprint("[C]hange management strategy [S]witch pool [I]nformation\n");
 	wlogprint("Or press any other key to continue\n");
 	wlogprint("Or press any other key to continue\n");
 	input = getch();
 	input = getch();
@@ -4632,6 +4751,24 @@ retry:
 	} else if (!strncasecmp(&input, "f", 1)) {
 	} else if (!strncasecmp(&input, "f", 1)) {
 		opt_fail_only ^= true;
 		opt_fail_only ^= true;
 		goto updated;
 		goto updated;
+        } else if (!strncasecmp(&input, "p", 1)) {
+        	switch (prioritize_pools(curses_input("Enter new pool priority (comma separated list)"), &i)) {
+        		case MSG_NOPOOL:
+        			wlogprint("No pools\n");
+        			goto retry;
+        		case MSG_MISPID:
+        			wlogprint("Missing pool id parameter\n");
+        			goto retry;
+        		case MSG_INVPID:
+        			wlogprint("Invalid pool id %d - range is 0 - %d\n", i, total_pools - 1);
+        			goto retry;
+        		case MSG_DUPPID:
+        			wlogprint("Duplicate pool specified %d\n", i);
+        			goto retry;
+        		case MSG_POOLPRIO:
+        		default:
+        			goto updated;
+        	}
 	} else
 	} else
 		clear_logwin();
 		clear_logwin();
 
 
@@ -7513,8 +7650,14 @@ int main(int argc, char *argv[])
 			sprintf(pool->rpc_userpass, "%s:%s", pool->rpc_user, pool->rpc_pass);
 			sprintf(pool->rpc_userpass, "%s:%s", pool->rpc_user, pool->rpc_pass);
 		}
 		}
 	}
 	}
-	/* Set the currentpool to pool 0 */
-	currentpool = pools[0];
+	/* Set the currentpool to pool wiht priority 0 */
+	validate_pool_priorities();
+	for (i = 0; i < total_pools; i++) {
+		struct pool *pool  = pools[i];
+
+		if (!pool->prio)
+			currentpool = pool;
+	}
 
 
 #ifdef HAVE_SYSLOG_H
 #ifdef HAVE_SYSLOG_H
 	if (use_syslog)
 	if (use_syslog)
@@ -7573,18 +7716,24 @@ int main(int argc, char *argv[])
 	applog(LOG_NOTICE, "Probing for an alive pool");
 	applog(LOG_NOTICE, "Probing for an alive pool");
 	do {
 	do {
 		/* Look for at least one active pool before starting */
 		/* Look for at least one active pool before starting */
-		for (i = 0; i < total_pools; i++) {
-			struct pool *pool  = pools[i];
-			if (pool_active(pool, false)) {
-				if (!currentpool)
-					currentpool = pool;
-				applog(LOG_INFO, "Pool %d %s active", pool->pool_no, pool->rpc_url);
-				pools_active = true;
-				break;
-			} else {
-				if (pool == currentpool)
-					currentpool = NULL;
-				applog(LOG_WARNING, "Unable to get work from pool %d %s", pool->pool_no, pool->rpc_url);
+		for (j = 0; j < total_pools; j++) {
+			for (i = 0; i < total_pools; i++) {
+				struct pool *pool  = pools[i];
+
+				if (pool->prio != j)
+					continue;
+
+				if (pool_active(pool, false)) {
+					if (!currentpool)
+						currentpool = pool;
+					applog(LOG_INFO, "Pool %d %s active", pool->pool_no, pool->rpc_url);
+					pools_active = true;
+					break;
+				} else {
+					if (pool == currentpool)
+						currentpool = NULL;
+					applog(LOG_WARNING, "Unable to get work from pool %d %s", pool->pool_no, pool->rpc_url);
+				}
 			}
 			}
 		}
 		}
 
 

+ 10 - 0
miner.h

@@ -362,6 +362,14 @@ enum dev_reason {
 
 
 #define MIN_SEC_UNSET 99999999
 #define MIN_SEC_UNSET 99999999
 
 
+enum {
+	MSG_NOPOOL		= 8,
+	MSG_MISPID		= 25,
+	MSG_INVPID		= 26,
+	MSG_DUPPID		= 74,
+	MSG_POOLPRIO	= 73,
+};
+
 struct cgminer_stats {
 struct cgminer_stats {
 	uint32_t getwork_calls;
 	uint32_t getwork_calls;
 	struct timeval getwork_wait;
 	struct timeval getwork_wait;
@@ -1053,6 +1061,8 @@ extern void wlogprint(const char *f, ...);
 extern int curses_int(const char *query);
 extern int curses_int(const char *query);
 extern char *curses_input(const char *query);
 extern char *curses_input(const char *query);
 extern void kill_work(void);
 extern void kill_work(void);
+extern int prioritize_pools(char *param, int *pid);
+extern void validate_pool_priorities(void);
 extern void switch_pools(struct pool *selected);
 extern void switch_pools(struct pool *selected);
 extern void remove_pool(struct pool *pool);
 extern void remove_pool(struct pool *pool);
 extern void write_config(FILE *fcfg);
 extern void write_config(FILE *fcfg);