Commit 07549b0d0462a9406f9861f5e93ef3989af0d480

Con Kolivas 2012-06-10T00:00:58

Merge pull request #218 from kanoi/morestats API add stats for pool getworks

diff --git a/API-README b/API-README
index 0782457..c73bb2a 100644
--- a/API-README
+++ b/API-README
@@ -285,7 +285,14 @@ miner.php - an example web page to access the API
 Feature Changelog for external applications using the API:
 
 
-API V1.11
+API V1.12
+
+Modified API commands:
+ 'stats' - more pool stats added
+
+----------
+
+API V1.11 (cgminer v2.4.2)
 
 Modified API commands:
  'save' no longer requires a filename (use default if not specified)
diff --git a/api.c b/api.c
index b644149..6a4d715 100644
--- a/api.c
+++ b/api.c
@@ -161,7 +161,7 @@ static const char SEPARATOR = '|';
 #define SEPSTR "|"
 static const char GPUSEP = ',';
 
-static const char *APIVERSION = "1.11";
+static const char *APIVERSION = "1.12";
 static const char *DEAD = "Dead";
 static const char *SICK = "Sick";
 static const char *NOSTART = "NoStart";
@@ -2008,7 +2008,7 @@ void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson)
 	ptr = NULL;
 }
 
-static int itemstats(int i, char *id, struct cgminer_stats *stats, char *extra, bool isjson)
+static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, char *extra, bool isjson)
 {
 	char buf[TMPBUFSIZ];
 
@@ -2018,13 +2018,31 @@ static int itemstats(int i, char *id, struct cgminer_stats *stats, char *extra, 
 			extra = (char *)BLANK;
 
 		sprintf(buf, isjson
-			? "%s{\"STATS\":%d,\"ID\":\"%s\",\"Elapsed\":%.0f,\"Calls\":%d,\"Wait\":%ld.%06ld,\"Max\":%ld.%06ld,\"Min\":%ld.%06ld%s%s}"
-			: "%sSTATS=%d,ID=%s,Elapsed=%.0f,Calls=%d,Wait=%ld.%06ld,Max=%ld.%06ld,Min=%ld.%06ld%s%s" SEPSTR,
+			? "%s{\"STATS\":%d,\"ID\":\"%s\",\"Elapsed\":%.0f,\"Calls\":%d,\"Wait\":%ld.%06ld,\"Max\":%ld.%06ld,\"Min\":%ld.%06ld"
+			: "%sSTATS=%d,ID=%s,Elapsed=%.0f,Calls=%d,Wait=%ld.%06ld,Max=%ld.%06ld,Min=%ld.%06ld",
 			(isjson && (i > 0)) ? COMMA : BLANK,
 			i, id, total_secs, stats->getwork_calls,
 			stats->getwork_wait.tv_sec, stats->getwork_wait.tv_usec,
 			stats->getwork_wait_max.tv_sec, stats->getwork_wait_max.tv_usec,
-			stats->getwork_wait_min.tv_sec, stats->getwork_wait_min.tv_usec,
+			stats->getwork_wait_min.tv_sec, stats->getwork_wait_min.tv_usec);
+
+		strcat(io_buffer, buf);
+
+		if (pool_stats) {
+			sprintf(buf, isjson
+				? ",\"Pool Calls\":%d,\"Pool Attempts\":%d,\"Pool Wait\":%ld.%06ld,\"Pool Max\":%ld.%06ld,\"Pool Min\":%ld.%06ld"
+				: ",Pool Calls=%d,Pool Attempts=%d,Pool Wait=%ld.%06ld,Pool Max=%ld.%06ld,Pool Min=%ld.%06ld",
+				pool_stats->getwork_calls, pool_stats->getwork_attempts,
+				pool_stats->getwork_wait.tv_sec, pool_stats->getwork_wait.tv_usec,
+				pool_stats->getwork_wait_max.tv_sec, pool_stats->getwork_wait_max.tv_usec,
+				pool_stats->getwork_wait_min.tv_sec, pool_stats->getwork_wait_min.tv_usec);
+
+			strcat(io_buffer, buf);
+		}
+
+		sprintf(buf, isjson
+			? "%s%s}"
+			: "%s%s" SEPSTR,
 			*extra ? COMMA : BLANK, extra);
 
 		strcat(io_buffer, buf);
@@ -2058,7 +2076,7 @@ static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, 
 				extra[0] = '\0';
 
 			sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
-			i = itemstats(i, id, &(cgpu->cgminer_stats), extra, isjson);
+			i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
 		}
 	}
 
@@ -2066,7 +2084,7 @@ static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, 
 		struct pool *pool = pools[j];
 
 		sprintf(id, "POOL%d", j);
-		i = itemstats(i, id, &(pool->cgminer_stats), NULL, isjson);
+		i = itemstats(i, id, &(pool->cgminer_stats), &(pool->cgminer_pool_stats), NULL, isjson);
 	}
 
 	if (isjson)
diff --git a/cgminer.c b/cgminer.c
index cb4f6f2..2bbfb0a 100644
--- a/cgminer.c
+++ b/cgminer.c
@@ -1816,6 +1816,8 @@ static void get_benchmark_work(struct work *work)
 static bool get_upstream_work(struct work *work, CURL *curl)
 {
 	struct pool *pool = work->pool;
+	struct cgminer_pool_stats *pool_stats = &(pool->cgminer_pool_stats);
+	struct timeval tv_start, tv_end, tv_elapsed;
 	json_t *val = NULL;
 	bool rc = false;
 	int retries = 0;
@@ -1830,8 +1832,11 @@ retry:
 	 * there may be temporary denied messages etc. falsely reporting
 	 * failure so retry a few times before giving up */
 	while (!val && retries++ < 3) {
+		pool_stats->getwork_attempts++;
+		gettimeofday(&tv_start, NULL);
 		val = json_rpc_call(curl, url, pool->rpc_userpass, rpc_req,
 			    false, false, &work->rolltime, pool, false);
+		gettimeofday(&tv_end, NULL);
 	}
 	if (unlikely(!val)) {
 		applog(LOG_DEBUG, "Failed json_rpc_call in get_upstream_work");
@@ -1846,6 +1851,18 @@ retry:
 	total_getworks++;
 	pool->getwork_requested++;
 
+	timersub(&tv_end, &tv_start, &tv_elapsed);
+	timeradd(&tv_elapsed, &(pool_stats->getwork_wait), &(pool_stats->getwork_wait));
+	if (timercmp(&tv_elapsed, &(pool_stats->getwork_wait_max), >)) {
+		pool_stats->getwork_wait_max.tv_sec = tv_elapsed.tv_sec;
+		pool_stats->getwork_wait_max.tv_usec = tv_elapsed.tv_usec;
+	}
+	if (timercmp(&tv_elapsed, &(pool_stats->getwork_wait_min), <)) {
+		pool_stats->getwork_wait_min.tv_sec = tv_elapsed.tv_sec;
+		pool_stats->getwork_wait_min.tv_usec = tv_elapsed.tv_usec;
+	}
+	pool_stats->getwork_calls++;
+
 	json_decref(val);
 out:
 
@@ -5009,6 +5026,7 @@ int main(int argc, char *argv[])
 		struct pool *pool = pools[i];
 
 		pool->cgminer_stats.getwork_wait_min.tv_sec = MIN_SEC_UNSET;
+		pool->cgminer_pool_stats.getwork_wait_min.tv_sec = MIN_SEC_UNSET;
 
 		if (!pool->rpc_userpass) {
 			if (!pool->rpc_user || !pool->rpc_pass)
diff --git a/miner.h b/miner.h
index ec9d725..f358e2b 100644
--- a/miner.h
+++ b/miner.h
@@ -291,6 +291,15 @@ struct cgminer_stats {
 	struct timeval getwork_wait_min;
 };
 
+// Just the actual network getworks to the pool
+struct cgminer_pool_stats {
+	uint32_t getwork_calls;
+	uint32_t getwork_attempts;
+	struct timeval getwork_wait;
+	struct timeval getwork_wait_max;
+	struct timeval getwork_wait_min;
+};
+
 struct cgpu_info {
 	int cgminer_id;
 	struct device_api *api;
@@ -698,6 +707,7 @@ struct pool {
 	time_t last_share_time;
 
 	struct cgminer_stats cgminer_stats;
+	struct cgminer_pool_stats cgminer_pool_stats;
 };
 
 struct work {