knc: Make per-core statistics available through API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
diff --git a/driver-knc-spi-fpga.c b/driver-knc-spi-fpga.c
index a444c4d..b892c09 100644
--- a/driver-knc-spi-fpga.c
+++ b/driver-knc-spi-fpga.c
@@ -32,6 +32,7 @@
#define SPI_DELAY_USECS 0
/* Max number of ASICs permitted on one SPI device */
#define MAX_ASICS 6
+#define CORES_PER_ASIC 192
/* How many hardware errors in a row before disabling the core */
#define HW_ERR_LIMIT 10
@@ -44,8 +45,10 @@
#define WORK_STALE_US 60000000
+#define SECONDS_IN_MINUTE 60
+
/* Keep core disabled for no longer than 15 minutes */
-#define CORE_DISA_PERIOD_US (15 * 60 * 1000000)
+#define CORE_DISA_PERIOD_US (15 * SECONDS_IN_MINUTE * 1000000)
/* DP = Disable Policy */
bool opt_knc_DP_checkworkid = false;
@@ -170,6 +173,13 @@ struct knc_state {
#define KNC_DISA_CORES_SIZE (MAX_ASICS * 256)
struct core_disa_data disa_cores_fifo[KNC_DISA_CORES_SIZE];
+ /* Local stats */
+#define KNC_MINUTES_IN_STATS_BUFFER 60
+ unsigned int last_hour_shares[MAX_ASICS][256][KNC_MINUTES_IN_STATS_BUFFER];
+ unsigned int last_hour_hwerrs[MAX_ASICS][256][KNC_MINUTES_IN_STATS_BUFFER];
+ unsigned int last_hour_shares_index[MAX_ASICS][256];
+ unsigned int last_hour_hwerrs_index[MAX_ASICS][256];
+
pthread_mutex_t lock;
};
@@ -306,6 +316,103 @@ static int spi_transfer(struct spidev_context *ctx, uint8_t *txbuf,
return ret;
}
+static void stats_update(unsigned int *data, unsigned int *index, unsigned int cur_index)
+{
+ if (cur_index != *index) {
+ unsigned int i;
+ for (i = (*index + 1) % KNC_MINUTES_IN_STATS_BUFFER; i != cur_index; i = ((i + 1 ) % KNC_MINUTES_IN_STATS_BUFFER))
+ data[i] = 0;
+ data[cur_index] = 0;
+ *index = cur_index;
+ }
+
+ ++(data[cur_index]);
+}
+
+static unsigned int get_accumulated_stats(unsigned int *data)
+{
+ int i;
+ unsigned int res = 0;
+ for (i = 0; i < KNC_MINUTES_IN_STATS_BUFFER; ++i)
+ res += data[i];
+
+ return res;
+}
+
+static inline void stats_good_share(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
+{
+ if ((asic >= MAX_ASICS) || (core >= 256))
+ return;
+ unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % KNC_MINUTES_IN_STATS_BUFFER;
+ stats_update(knc->last_hour_shares[asic][core], &(knc->last_hour_shares_index[asic][core]), cur_minute);
+}
+
+static inline void stats_bad_share(struct knc_state *knc, uint32_t asic, uint32_t core, struct timespec *ts)
+{
+ if ((asic >= MAX_ASICS) || (core >= 256))
+ return;
+ unsigned int cur_minute = (ts->tv_sec / SECONDS_IN_MINUTE) % KNC_MINUTES_IN_STATS_BUFFER;
+ stats_update(knc->last_hour_hwerrs[asic][core], &(knc->last_hour_hwerrs_index[asic][core]), cur_minute);
+}
+
+static inline unsigned int get_hour_shares(struct knc_state *knc, uint32_t asic, uint32_t core)
+{
+ if ((asic >= MAX_ASICS) || (core >= 256))
+ return 0;
+ return get_accumulated_stats(knc->last_hour_shares[asic][core]);
+}
+
+static inline unsigned int get_hour_errors(struct knc_state *knc, uint32_t asic, uint32_t core)
+{
+ if ((asic >= MAX_ASICS) || (core >= 256))
+ return 0;
+ return get_accumulated_stats(knc->last_hour_hwerrs[asic][core]);
+}
+
+static struct api_data *knc_api_stats(struct cgpu_info *cgpu)
+{
+ struct knc_state *knc = cgpu->device_data;
+ struct api_data *root = NULL;
+ char buf[4096];
+ int asic, core;
+ int cursize, n;
+
+ for (asic = 0; asic < MAX_ASICS; ++asic) {
+ char asic_name[128];
+ snprintf(asic_name, sizeof(asic_name), "asic_%d_shares", asic + 1);
+ cursize = 0;
+ for (core = 0; core < CORES_PER_ASIC; ++core) {
+ unsigned int shares = get_hour_shares(knc, asic, core);
+ n = snprintf(buf + cursize, sizeof(buf) - cursize, "%d,", shares);
+ cursize += n;
+ if (sizeof(buf) < cursize) {
+ cursize = sizeof(buf);
+ break;
+ }
+ }
+ if (0 < cursize)
+ buf[cursize - 1] = '\0'; /* last comma */
+ root = api_add_string(root, asic_name, buf, true);
+
+ snprintf(asic_name, sizeof(asic_name), "asic_%d_hwerrs", asic + 1);
+ cursize = 0;
+ for (core = 0; core < CORES_PER_ASIC; ++core) {
+ unsigned int errors = get_hour_errors(knc, asic, core);
+ n = snprintf(buf + cursize, sizeof(buf) - cursize, "%d,", errors);
+ cursize += n;
+ if (sizeof(buf) < cursize) {
+ cursize = sizeof(buf);
+ break;
+ }
+ }
+ if (0 < cursize)
+ buf[cursize - 1] = '\0'; /* last comma */
+ root = api_add_string(root, asic_name, buf, true);
+ }
+
+ return root;
+}
+
static void disable_core(uint8_t asic, uint8_t core)
{
char str[256];
@@ -391,6 +498,7 @@ static int64_t knc_process_response(struct thr_info *thr, struct cgpu_info *cgpu
int submitted, completed, i, num_sent;
int next_read_q, next_read_a;
struct timeval now;
+ struct timespec ts_now;
struct work *work;
int64_t us;
@@ -409,6 +517,7 @@ static int64_t knc_process_response(struct thr_info *thr, struct cgpu_info *cgpu
}
/* move works_accepted number of items from queued_fifo to active_fifo */
gettimeofday(&now, NULL);
+ clock_gettime(CLOCK_MONOTONIC, &ts_now);
submitted = 0;
for (i = 0; i < rxbuf->works_accepted; ++i) {
@@ -492,12 +601,14 @@ static int64_t knc_process_response(struct thr_info *thr, struct cgpu_info *cgpu
if (submit_nonce(thr, work,
rxbuf->responses[i].nonce)) {
+ stats_good_share(knc, rxbuf->responses[i].asic, rxbuf->responses[i].core, &ts_now);
if (cidx < (int)sizeof(knc->hwerrs)) {
knc->hwerrs[cidx] = 0;
knc->disa_cnt[cidx] = 0;
knc->hwerr_work_id[cidx] = 0xFFFFFFFF;
}
} else {
+ stats_bad_share(knc, rxbuf->responses[i].asic, rxbuf->responses[i].core, &ts_now);
bool process_hwerr = (cidx < (int)sizeof(knc->hwerrs));
if (process_hwerr && opt_knc_DP_checkworkid &&
(knc->hwerr_work_id[cidx] == rxbuf->responses[i].work_id))
@@ -767,4 +878,6 @@ struct device_drv knc_drv = {
.scanwork = knc_scanwork,
.queue_full = knc_queue_full,
.flush_work = knc_flush_work,
+
+ .get_api_stats = knc_api_stats,
};