Merge pull request #247 from kanoi/master api.c data structure + updated get_api_stats/driver-icarus.c
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
diff --git a/API-README b/API-README
index 52f67ae..9c633bd 100644
--- a/API-README
+++ b/API-README
@@ -309,13 +309,23 @@ miner.php - an example web page to access the API
Feature Changelog for external applications using the API:
-API V1.13
+API V1.14
+
+Modified API commands:
+ 'stats' - more icarus timing stats added
+
+The internal code for handling data was rewritten (~25% of the code)
+Completely backward compatible
+
+----------
+
+API V1.13 (cgminer v2.4.4)
Added API commands:
'check'
Support was added to cgminer for API access groups with the --api-groups option
-It's 100% backwards compatible with previous --api-access commands
+It's 100% backward compatible with previous --api-access commands
----------
diff --git a/api.c b/api.c
index c88c915..b381393 100644
--- a/api.c
+++ b/api.c
@@ -166,7 +166,7 @@ static const char SEPARATOR = '|';
#define SEPSTR "|"
static const char GPUSEP = ',';
-static const char *APIVERSION = "1.13";
+static const char *APIVERSION = "1.14";
static const char *DEAD = "Dead";
static const char *SICK = "Sick";
static const char *NOSTART = "NoStart";
@@ -179,6 +179,7 @@ static const char *DYNAMIC = _DYNAMIC;
static const char *YES = "Y";
static const char *NO = "N";
+static const char *NULLSTR = "(null)";
static const char *DEVICECODE = ""
#ifdef HAVE_OPENCL
@@ -251,7 +252,8 @@ static const char ISJSON = '{';
#define JSON1 "\""
#define JSON2 "\":["
#define JSON3 "]"
-#define JSON4 ",\"id\":1}"
+#define JSON4 ",\"id\":1"
+#define JSON5 "}"
#define JSON_START JSON0
#define JSON_DEVS JSON1 _DEVS JSON2
@@ -280,7 +282,7 @@ static const char ISJSON = '{';
#define JSON_CLOSE JSON3
#define JSON_MINESTATS JSON1 _MINESTATS JSON2
#define JSON_CHECK JSON1 _CHECK JSON2
-#define JSON_END JSON4
+#define JSON_END JSON4 JSON5
static const char *JSON_COMMAND = "command";
static const char *JSON_PARAMETER = "parameter";
@@ -630,6 +632,334 @@ static char *escape_string(char *str, bool isjson)
return buf;
}
+static struct api_data *api_add_extra(struct api_data *root, struct api_data *extra)
+{
+ struct api_data *tmp;
+
+ if (root)
+ {
+ if (extra) {
+ // extra tail
+ tmp = extra->prev;
+
+ // extra prev = root tail
+ extra->prev = root->prev;
+
+ // root tail next = extra
+ root->prev->next = extra;
+
+ // extra tail next = root
+ tmp->next = root;
+
+ // root prev = extra tail
+ root->prev = tmp;
+ }
+ }
+ else
+ root = extra;
+
+ return root;
+}
+
+static struct api_data *api_add_data_full(struct api_data *root, char *name, enum api_data_type type, void *data, bool copy_data)
+{
+ struct api_data *api_data;
+
+ api_data = (struct api_data *)malloc(sizeof(struct api_data));
+
+ api_data->name = name;
+ api_data->type = type;
+
+ if (root == NULL) {
+ root = api_data;
+ root->prev = root;
+ root->next = root;
+ }
+ else {
+ api_data->prev = root->prev;
+ root->prev = api_data;
+ api_data->next = root;
+ api_data->prev->next = api_data;
+ }
+
+ api_data->data_was_malloc = copy_data;
+
+ // Avoid crashing on bad data
+ if (data == NULL) {
+ api_data->type = type = API_CONST;
+ data = (void *)NULLSTR;
+ api_data->data_was_malloc = copy_data = false;
+ }
+
+ if (!copy_data)
+ api_data->data = data;
+ else
+ switch(type) {
+ case API_ESCAPE:
+ case API_STRING:
+ case API_CONST:
+ api_data->data = (void *)malloc(strlen((char *)data) + 1);
+ strcpy((char*)(api_data->data), (char *)data);
+ break;
+ case API_INT:
+ api_data->data = (void *)malloc(sizeof(int));
+ *((int *)(api_data->data)) = *((int *)data);
+ break;
+ case API_UINT:
+ api_data->data = (void *)malloc(sizeof(unsigned int));
+ *((unsigned int *)(api_data->data)) = *((unsigned int *)data);
+ break;
+ case API_UINT32:
+ api_data->data = (void *)malloc(sizeof(uint32_t));
+ *((uint32_t *)(api_data->data)) = *((uint32_t *)data);
+ break;
+ case API_UINT64:
+ api_data->data = (void *)malloc(sizeof(uint64_t));
+ *((uint64_t *)(api_data->data)) = *((uint64_t *)data);
+ break;
+ case API_DOUBLE:
+ case API_ELAPSED:
+ case API_MHS:
+ case API_MHTOTAL:
+ case API_UTILITY:
+ case API_FREQ:
+ case API_HS:
+ api_data->data = (void *)malloc(sizeof(double));
+ *((double *)(api_data->data)) = *((double *)data);
+ break;
+ case API_BOOL:
+ api_data->data = (void *)malloc(sizeof(bool));
+ *((bool *)(api_data->data)) = *((bool *)data);
+ break;
+ case API_TIMEVAL:
+ api_data->data = (void *)malloc(sizeof(struct timeval));
+ memcpy(api_data->data, data, sizeof(struct timeval));
+ break;
+ case API_TIME:
+ api_data->data = (void *)malloc(sizeof(time_t));
+ *(time_t *)(api_data->data) = *((time_t *)data);
+ break;
+ case API_VOLTS:
+ case API_TEMP:
+ api_data->data = (void *)malloc(sizeof(float));
+ *((float *)(api_data->data)) = *((float *)data);
+ break;
+ default:
+ applog(LOG_ERR, "API: unknown1 data type %d ignored", type);
+ api_data->type = API_STRING;
+ api_data->data_was_malloc = false;
+ api_data->data = (void *)UNKNOWN;
+ break;
+ }
+
+ return root;
+}
+
+struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_ESCAPE, (void *)data, copy_data);
+}
+
+struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_STRING, (void *)data, copy_data);
+}
+
+struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_CONST, (void *)data, copy_data);
+}
+
+struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_INT, (void *)data, copy_data);
+}
+
+struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_UINT, (void *)data, copy_data);
+}
+
+struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_UINT32, (void *)data, copy_data);
+}
+
+struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_UINT64, (void *)data, copy_data);
+}
+
+struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_DOUBLE, (void *)data, copy_data);
+}
+
+struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_ELAPSED, (void *)data, copy_data);
+}
+
+struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_BOOL, (void *)data, copy_data);
+}
+
+struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_TIMEVAL, (void *)data, copy_data);
+}
+
+struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_TIME, (void *)data, copy_data);
+}
+
+struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_MHS, (void *)data, copy_data);
+}
+
+struct api_data *api_add_mhtotal(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_MHTOTAL, (void *)data, copy_data);
+}
+
+struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_TEMP, (void *)data, copy_data);
+}
+
+struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_UTILITY, (void *)data, copy_data);
+}
+
+struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_FREQ, (void *)data, copy_data);
+}
+
+struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_VOLTS, (void *)data, copy_data);
+}
+
+struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data)
+{
+ return api_add_data_full(root, name, API_HS, (void *)data, copy_data);
+}
+
+static struct api_data *print_data(struct api_data *root, char *buf, bool isjson)
+{
+ struct api_data *tmp;
+ bool first = true;
+ char *original, *escape;
+ char *quote;
+
+ if (isjson) {
+ strcpy(buf, JSON0);
+ buf = strchr(buf, '\0');
+ quote = JSON1;
+ } else
+ quote = (char *)BLANK;
+
+ while (root) {
+ if (!first)
+ *(buf++) = *COMMA;
+ else
+ first = false;
+
+ sprintf(buf, "%s%s%s%s", quote, root->name, quote, isjson ? ":" : "=");
+
+ buf = strchr(buf, '\0');
+
+ switch(root->type) {
+ case API_STRING:
+ case API_CONST:
+ sprintf(buf, "%s%s%s", quote, (char *)(root->data), quote);
+ break;
+ case API_ESCAPE:
+ original = (char *)(root->data);
+ escape = escape_string((char *)(root->data), isjson);
+ sprintf(buf, "%s%s%s", quote, escape, quote);
+ if (escape != original)
+ free(escape);
+ break;
+ case API_INT:
+ sprintf(buf, "%d", *((int *)(root->data)));
+ break;
+ case API_UINT:
+ sprintf(buf, "%u", *((unsigned int *)(root->data)));
+ break;
+ case API_UINT32:
+ sprintf(buf, "%"PRIu32, *((uint32_t *)(root->data)));
+ break;
+ case API_UINT64:
+ sprintf(buf, "%"PRIu64, *((uint64_t *)(root->data)));
+ break;
+ case API_TIME:
+ sprintf(buf, "%lu", *((unsigned long *)(root->data)));
+ break;
+ case API_DOUBLE:
+ sprintf(buf, "%f", *((double *)(root->data)));
+ break;
+ case API_ELAPSED:
+ sprintf(buf, "%.0f", *((double *)(root->data)));
+ break;
+ case API_UTILITY:
+ case API_FREQ:
+ case API_MHS:
+ sprintf(buf, "%.2f", *((double *)(root->data)));
+ break;
+ case API_VOLTS:
+ sprintf(buf, "%.3f", *((float *)(root->data)));
+ break;
+ case API_MHTOTAL:
+ sprintf(buf, "%.4f", *((double *)(root->data)));
+ break;
+ case API_HS:
+ sprintf(buf, "%.15f", *((double *)(root->data)));
+ break;
+ case API_BOOL:
+ sprintf(buf, "%s", *((bool *)(root->data)) ? "true" : "false");
+ break;
+ case API_TIMEVAL:
+ sprintf(buf, "%ld.%06ld",
+ ((struct timeval *)(root->data))->tv_sec,
+ ((struct timeval *)(root->data))->tv_usec);
+ break;
+ case API_TEMP:
+ sprintf(buf, "%.2f", *((float *)(root->data)));
+ break;
+ default:
+ applog(LOG_ERR, "API: unknown2 data type %d ignored", root->type);
+ sprintf(buf, "%s%s%s", quote, UNKNOWN, quote);
+ break;
+ }
+
+ buf = strchr(buf, '\0');
+
+ if (root->data_was_malloc)
+ free(root->data);
+
+ if (root->next == root) {
+ free(root);
+ root = NULL;
+ } else {
+ tmp = root;
+ root = tmp->next;
+ root->prev = tmp->prev;
+ root->prev->next = root;
+ free(tmp);
+ }
+ }
+
+ strcpy(buf, isjson ? JSON5 : SEPSTR);
+
+ return root;
+}
+
#ifdef HAVE_AN_FPGA
static int numpgas()
{
@@ -691,7 +1021,9 @@ static int pgadevice(int pgaid)
// and send_result() adds JSON_END at the end
static char *message(int messageid, int paramid, char *param2, bool isjson)
{
- char severity;
+ struct api_data *root = NULL;
+ char buf[TMPBUFSIZ];
+ char severity[2];
char *ptr;
#ifdef HAVE_AN_FPGA
int pga;
@@ -701,49 +1033,50 @@ static char *message(int messageid, int paramid, char *param2, bool isjson)
#endif
int i;
+ if (!isjson)
+ msg_buffer[0] = '\0';
+ else
+ strcpy(msg_buffer, JSON_START JSON_STATUS);
+
+ ptr = strchr(msg_buffer, '\0');
+
for (i = 0; codes[i].severity != SEVERITY_FAIL; i++) {
if (codes[i].code == messageid) {
switch (codes[i].severity) {
case SEVERITY_WARN:
- severity = 'W';
+ severity[0] = 'W';
break;
case SEVERITY_INFO:
- severity = 'I';
+ severity[0] = 'I';
break;
case SEVERITY_SUCC:
- severity = 'S';
+ severity[0] = 'S';
break;
case SEVERITY_ERR:
default:
- severity = 'E';
+ severity[0] = 'E';
break;
}
-
- sprintf(msg_buffer, isjson
- ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"%c\",\"When\":%lu,\"Code\":%d,\"Msg\":\""
- : _STATUS "=%c,When=%lu,Code=%d,Msg=",
- severity, (unsigned long)when, messageid);
-
- ptr = msg_buffer + strlen(msg_buffer);
+ severity[1] = '\0';
switch(codes[i].params) {
case PARAM_GPU:
case PARAM_PGA:
case PARAM_CPU:
- sprintf(ptr, codes[i].description, paramid);
+ sprintf(buf, codes[i].description, paramid);
break;
case PARAM_POOL:
- sprintf(ptr, codes[i].description, paramid, pools[paramid]->rpc_url);
+ sprintf(buf, codes[i].description, paramid, pools[paramid]->rpc_url);
break;
#ifdef HAVE_OPENCL
case PARAM_GPUMAX:
- sprintf(ptr, codes[i].description, paramid, nDevs - 1);
+ sprintf(buf, codes[i].description, paramid, nDevs - 1);
break;
#endif
#ifdef HAVE_AN_FPGA
case PARAM_PGAMAX:
pga = numpgas();
- sprintf(ptr, codes[i].description, paramid, pga - 1);
+ sprintf(buf, codes[i].description, paramid, pga - 1);
break;
#endif
#ifdef WANT_CPUMINE
@@ -752,14 +1085,14 @@ static char *message(int messageid, int paramid, char *param2, bool isjson)
cpu = num_processors;
else
cpu = 0;
- sprintf(ptr, codes[i].description, paramid, cpu - 1);
+ sprintf(buf, codes[i].description, paramid, cpu - 1);
break;
#endif
case PARAM_PMAX:
- sprintf(ptr, codes[i].description, total_pools);
+ sprintf(buf, codes[i].description, total_pools);
break;
case PARAM_POOLMAX:
- sprintf(ptr, codes[i].description, paramid, total_pools - 1);
+ sprintf(buf, codes[i].description, paramid, total_pools - 1);
break;
case PARAM_DMAX:
#ifdef HAVE_AN_FPGA
@@ -772,7 +1105,7 @@ static char *message(int messageid, int paramid, char *param2, bool isjson)
cpu = 0;
#endif
- sprintf(ptr, codes[i].description
+ sprintf(buf, codes[i].description
#ifdef HAVE_OPENCL
, nDevs
#endif
@@ -785,49 +1118,68 @@ static char *message(int messageid, int paramid, char *param2, bool isjson)
);
break;
case PARAM_CMD:
- sprintf(ptr, codes[i].description, JSON_COMMAND);
+ sprintf(buf, codes[i].description, JSON_COMMAND);
break;
case PARAM_STR:
- sprintf(ptr, codes[i].description, param2);
+ sprintf(buf, codes[i].description, param2);
break;
case PARAM_BOTH:
- sprintf(ptr, codes[i].description, paramid, param2);
+ sprintf(buf, codes[i].description, paramid, param2);
break;
case PARAM_NONE:
default:
- strcpy(ptr, codes[i].description);
+ strcpy(buf, codes[i].description);
}
- ptr = msg_buffer + strlen(msg_buffer);
-
- sprintf(ptr, isjson
- ? "\",\"Description\":\"%s\"}" JSON_CLOSE
- : ",Description=%s" SEPSTR,
- opt_api_description);
+ root = api_add_string(root, _STATUS, severity, false);
+ root = api_add_time(root, "When", &when, false);
+ root = api_add_int(root, "Code", &messageid, false);
+ root = api_add_string(root, "Msg", buf, false);
+ root = api_add_string(root, "Description", opt_api_description, false);
+ root = print_data(root, ptr, isjson);
+ if (isjson)
+ strcat(ptr, JSON_CLOSE);
return msg_buffer;
}
}
- sprintf(msg_buffer, isjson
- ? JSON_START JSON_STATUS "{\"" _STATUS "\":\"F\",\"When\":%lu,\"Code\":-1,\"Msg\":\"%d\",\"Description\":\"%s\"}" JSON_CLOSE
- : _STATUS "=F,When=%lu,Code=-1,Msg=%d,Description=%s" SEPSTR,
- (unsigned long)when, messageid, opt_api_description);
+ root = api_add_string(root, _STATUS, "F", false);
+ root = api_add_time(root, "When", &when, false);
+ int id = -1;
+ root = api_add_int(root, "Code", &id, false);
+ sprintf(buf, "%d", messageid);
+ root = api_add_string(root, "Msg", buf, false);
+ root = api_add_string(root, "Description", opt_api_description, false);
+ root = print_data(root, ptr, isjson);
+ if (isjson)
+ strcat(ptr, JSON_CLOSE);
return msg_buffer;
}
static void apiversion(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
+ char buf[TMPBUFSIZ];
+
sprintf(io_buffer, isjson
- ? "%s," JSON_VERSION "{\"CGMiner\":\"%s\",\"API\":\"%s\"}" JSON_CLOSE
- : "%s" _VERSION ",CGMiner=%s,API=%s" SEPSTR,
- message(MSG_VERSION, 0, NULL, isjson),
- VERSION, APIVERSION);
+ ? "%s," JSON_VERSION
+ : "%s" _VERSION ",",
+ message(MSG_VERSION, 0, NULL, isjson));
+
+ root = api_add_string(root, "CGMiner", VERSION, false);
+ root = api_add_const(root, "API", APIVERSION, false);
+
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
+ strcat(io_buffer, buf);
}
static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int gpucount = 0;
int pgacount = 0;
@@ -859,20 +1211,31 @@ static void minerconfig(__maybe_unused SOCKETTYPE c, __maybe_unused char *param,
cpucount = opt_n_threads > 0 ? num_processors : 0;
#endif
- strcpy(io_buffer, message(MSG_MINECON, 0, NULL, isjson));
-
- sprintf(buf, isjson
- ? "," JSON_MINECON "{\"GPU Count\":%d,\"PGA Count\":%d,\"CPU Count\":%d,\"Pool Count\":%d,\"ADL\":\"%s\",\"ADL in use\":\"%s\",\"Strategy\":\"%s\",\"Log Interval\":%d,\"Device Code\":\"%s\",\"OS\":\"%s\"}" JSON_CLOSE
- : _MINECON ",GPU Count=%d,PGA Count=%d,CPU Count=%d,Pool Count=%d,ADL=%s,ADL in use=%s,Strategy=%s,Log Interval=%d,Device Code=%s,OS=%s" SEPSTR,
-
- gpucount, pgacount, cpucount, total_pools, adl, adlinuse,
- strategies[pool_strategy].s, opt_log_interval, DEVICECODE, OSINFO);
-
+ sprintf(io_buffer, isjson
+ ? "%s," JSON_MINECON
+ : "%s" _MINECON ",",
+ message(MSG_MINECON, 0, NULL, isjson));
+
+ root = api_add_int(root, "GPU Count", &gpucount, false);
+ root = api_add_int(root, "PGA Count", &pgacount, false);
+ root = api_add_int(root, "CPU Count", &cpucount, false);
+ root = api_add_int(root, "Pool Count", &total_pools, false);
+ root = api_add_const(root, "ADL", (char *)adl, false);
+ root = api_add_string(root, "ADL in use", adlinuse, false);
+ root = api_add_const(root, "Strategy", strategies[pool_strategy].s, false);
+ root = api_add_int(root, "Log Interval", &opt_log_interval, false);
+ root = api_add_const(root, "Device Code", DEVICECODE, false);
+ root = api_add_const(root, "OS", OSINFO, false);
+
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
#ifdef HAVE_OPENCL
static void gpustatus(int gpu, bool isjson)
{
+ struct api_data *root = NULL;
char intensity[20];
char buf[TMPBUFSIZ];
char *enabled;
@@ -909,16 +1272,34 @@ static void gpustatus(int gpu, bool isjson)
else
sprintf(intensity, "%d", cgpu->intensity);
- sprintf(buf, isjson
- ? "{\"GPU\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"Fan Speed\":%d,\"Fan Percent\":%d,\"GPU Clock\":%d,\"Memory Clock\":%d,\"GPU Voltage\":%.3f,\"GPU Activity\":%d,\"Powertune\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Intensity\":\"%s\",\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}"
- : "GPU=%d,Enabled=%s,Status=%s,Temperature=%.2f,Fan Speed=%d,Fan Percent=%d,GPU Clock=%d,Memory Clock=%d,GPU Voltage=%.3f,GPU Activity=%d,Powertune=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Intensity=%s,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f" SEPSTR,
- gpu, enabled, status, gt, gf, gp, gc, gm, gv, ga, pt,
- cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
- cgpu->accepted, cgpu->rejected, cgpu->hw_errors,
- cgpu->utility, intensity,
- ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
- (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
-
+ root = api_add_int(root, "GPU", &gpu, false);
+ root = api_add_string(root, "Enabled", enabled, false);
+ root = api_add_string(root, "Status", status, false);
+ root = api_add_temp(root, "Temperature", >, false);
+ root = api_add_int(root, "Fan Speed", &gf, false);
+ root = api_add_int(root, "Fan Percent", &gp, false);
+ root = api_add_int(root, "GPU Clock", &gc, false);
+ root = api_add_int(root, "Memory Clock", &gm, false);
+ root = api_add_volts(root, "GPU Voltage", &gv, false);
+ root = api_add_int(root, "GPU Activity", &ga, false);
+ root = api_add_int(root, "Powertune", &pt, false);
+ double mhs = cgpu->total_mhashes / total_secs;
+ root = api_add_mhs(root, "MHS av", &mhs, false);
+ char mhsname[27];
+ sprintf(mhsname, "MHS %ds", opt_log_interval);
+ root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
+ root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
+ root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
+ root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
+ root = api_add_utility(root, "Utility", &(cgpu->utility), false);
+ root = api_add_string(root, "Intensity", intensity, false);
+ int last_share_pool = cgpu->last_share_pool_time > 0 ?
+ cgpu->last_share_pool : -1;
+ root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
+ root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
+ root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
+
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
@@ -926,6 +1307,7 @@ static void gpustatus(int gpu, bool isjson)
#ifdef HAVE_AN_FPGA
static void pgastatus(int pga, bool isjson)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *enabled;
char *status;
@@ -984,16 +1366,29 @@ static void pgastatus(int pga, bool isjson)
else
status = (char *)ALIVE;
- sprintf(buf, isjson
- ? "{\"PGA\":%d,\"Name\":\"%s\",\"ID\":%d,\"Enabled\":\"%s\",\"Status\":\"%s\",\"Temperature\":%.2f,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f,\"Frequency\":%.2f}"
- : "PGA=%d,Name=%s,ID=%d,Enabled=%s,Status=%s,Temperature=%.2f,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f,Frequency=%.2f" SEPSTR,
- pga, cgpu->api->name, cgpu->device_id,
- enabled, status, temp,
- cgpu->total_mhashes / total_secs, opt_log_interval, cgpu->rolling,
- cgpu->accepted, cgpu->rejected, cgpu->hw_errors, cgpu->utility,
- ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
- (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes, frequency);
-
+ root = api_add_int(root, "PGA", &pga, false);
+ root = api_add_string(root, "Name", cgpu->api->name, false);
+ root = api_add_int(root, "ID", &(cgpu->device_id), false);
+ root = api_add_string(root, "Enabled", enabled, false);
+ root = api_add_string(root, "Status", status, false);
+ root = api_add_temp(root, "Temperature", &temp, false);
+ double mhs = cgpu->total_mhashes / total_secs;
+ root = api_add_mhs(root, "MHS av", &mhs, false);
+ char mhsname[27];
+ sprintf(mhsname, "MHS %ds", opt_log_interval);
+ root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
+ root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
+ root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
+ root = api_add_int(root, "Hardware Errors", &(cgpu->hw_errors), false);
+ root = api_add_utility(root, "Utility", &(cgpu->utility), false);
+ int last_share_pool = cgpu->last_share_pool_time > 0 ?
+ cgpu->last_share_pool : -1;
+ root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
+ root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
+ root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
+ root = api_add_freq(root, "Frequency", &frequency, false);
+
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
@@ -1002,6 +1397,7 @@ static void pgastatus(int pga, bool isjson)
#ifdef WANT_CPUMINE
static void cpustatus(int cpu, bool isjson)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
if (opt_n_threads > 0 && cpu >= 0 && cpu < num_processors) {
@@ -1009,16 +1405,22 @@ static void cpustatus(int cpu, bool isjson)
cgpu->utility = cgpu->accepted / ( total_secs ? total_secs : 1 ) * 60;
- sprintf(buf, isjson
- ? "{\"CPU\":%d,\"MHS av\":%.2f,\"MHS %ds\":%.2f,\"Accepted\":%d,\"Rejected\":%d,\"Utility\":%.2f,\"Last Share Pool\":%d,\"Last Share Time\":%lu,\"Total MH\":%.4f}"
- : "CPU=%d,MHS av=%.2f,MHS %ds=%.2f,Accepted=%d,Rejected=%d,Utility=%.2f,Last Share Pool=%d,Last Share Time=%lu,Total MH=%.4f" SEPSTR,
- cpu, cgpu->total_mhashes / total_secs,
- opt_log_interval, cgpu->rolling,
- cgpu->accepted, cgpu->rejected,
- cgpu->utility,
- ((unsigned long)(cgpu->last_share_pool_time) > 0) ? cgpu->last_share_pool : -1,
- (unsigned long)(cgpu->last_share_pool_time), cgpu->total_mhashes);
-
+ root = api_add_int(root, "CPU", &cpu, false);
+ double mhs = cgpu->total_mhashes / total_secs;
+ root = api_add_mhs(root, "MHS av", &mhs, false);
+ char mhsname[27];
+ sprintf(mhsname, "MHS %ds", opt_log_interval);
+ root = api_add_mhs(root, mhsname, &(cgpu->rolling), false);
+ root = api_add_int(root, "Accepted", &(cgpu->accepted), false);
+ root = api_add_int(root, "Rejected", &(cgpu->rejected), false);
+ root = api_add_utility(root, "Utility", &(cgpu->utility), false);
+ int last_share_pool = cgpu->last_share_pool_time > 0 ?
+ cgpu->last_share_pool : -1;
+ root = api_add_int(root, "Last Share Pool", &last_share_pool, false);
+ root = api_add_time(root, "Last Share Time", &(cgpu->last_share_pool_time), false);
+ root = api_add_mhtotal(root, "Total MH", &(cgpu->total_mhashes), false);
+
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
}
@@ -1291,10 +1693,9 @@ static void cpudev(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __mayb
static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *status, *lp;
- char *rpc_url;
- char *rpc_user;
int i;
if (total_pools == 0) {
@@ -1335,31 +1736,26 @@ static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param,
else
lp = (char *)NO;
- rpc_url = escape_string(pool->rpc_url, isjson);
- rpc_user = escape_string(pool->rpc_user, isjson);
-
- sprintf(buf, isjson
- ? "%s{\"POOL\":%d,\"URL\":\"%s\",\"Status\":\"%s\",\"Priority\":%d,\"Long Poll\":\"%s\",\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Remote Failures\":%d,\"User\":\"%s\",\"Last Share Time\":%lu}"
- : "%sPOOL=%d,URL=%s,Status=%s,Priority=%d,Long Poll=%s,Getworks=%d,Accepted=%d,Rejected=%d,Discarded=%d,Stale=%d,Get Failures=%d,Remote Failures=%d,User=%s,Last Share Time=%lu" SEPSTR,
- (isjson && (i > 0)) ? COMMA : BLANK,
- i, rpc_url, status, pool->prio, lp,
- pool->getwork_requested,
- pool->accepted, pool->rejected,
- pool->discarded_work,
- pool->stale_shares,
- pool->getfail_occasions,
- pool->remotefail_occasions,
- rpc_user, pool->last_share_time);
+ root = api_add_int(root, "POOL", &i, false);
+ root = api_add_escape(root, "URL", pool->rpc_url, false);
+ root = api_add_string(root, "Status", status, false);
+ root = api_add_int(root, "Priority", &(pool->prio), false);
+ root = api_add_string(root, "Long Poll", lp, false);
+ root = api_add_uint(root, "Getworks", &(pool->getwork_requested), false);
+ root = api_add_int(root, "Accepted", &(pool->accepted), false);
+ root = api_add_int(root, "Rejected", &(pool->rejected), false);
+ root = api_add_uint(root, "Discarded", &(pool->discarded_work), false);
+ root = api_add_uint(root, "Stale", &(pool->stale_shares), false);
+ root = api_add_uint(root, "Get Failures", &(pool->getfail_occasions), false);
+ root = api_add_uint(root, "Remote Failures", &(pool->remotefail_occasions), false);
+ root = api_add_escape(root, "User", pool->rpc_user, false);
+ root = api_add_time(root, "Last Share Time", &(pool->last_share_time), false);
+
+ if (isjson && (i > 0))
+ strcat(io_buffer, COMMA);
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
-
- if (rpc_url != pool->rpc_url)
- free(rpc_url);
- rpc_url = NULL;
-
- if (rpc_user != pool->rpc_user)
- free(rpc_user);
- rpc_user = NULL;
}
if (isjson)
@@ -1368,36 +1764,47 @@ static void poolstatus(__maybe_unused SOCKETTYPE c, __maybe_unused char *param,
static void summary(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
+ char buf[TMPBUFSIZ];
double utility, mhs;
#ifdef WANT_CPUMINE
char *algo = (char *)(algo_names[opt_algo]);
if (algo == NULL)
- algo = "(null)";
+ algo = (char *)NULLSTR;
#endif
utility = total_accepted / ( total_secs ? total_secs : 1 ) * 60;
mhs = total_mhashes_done / total_secs;
-#ifdef WANT_CPUMINE
- sprintf(io_buffer, isjson
- ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"Algorithm\":\"%s\",\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
- : "%s" _SUMMARY ",Elapsed=%.0f,Algorithm=%s,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
- message(MSG_SUMM, 0, NULL, isjson),
- total_secs, algo, mhs, found_blocks,
- total_getworks, total_accepted, total_rejected,
- hw_errors, utility, total_discarded, total_stale,
- total_go, local_work, total_ro, new_blocks, total_mhashes_done);
-#else
sprintf(io_buffer, isjson
- ? "%s," JSON_SUMMARY "{\"Elapsed\":%.0f,\"MHS av\":%.2f,\"Found Blocks\":%d,\"Getworks\":%d,\"Accepted\":%d,\"Rejected\":%d,\"Hardware Errors\":%d,\"Utility\":%.2f,\"Discarded\":%d,\"Stale\":%d,\"Get Failures\":%d,\"Local Work\":%u,\"Remote Failures\":%u,\"Network Blocks\":%u,\"Total MH\":%.4f}" JSON_CLOSE
- : "%s" _SUMMARY ",Elapsed=%.0f,MHS av=%.2f,Found Blocks=%d,Getworks=%d,Accepted=%d,Rejected=%d,Hardware Errors=%d,Utility=%.2f,Discarded=%d,Stale=%d,Get Failures=%d,Local Work=%u,Remote Failures=%u,Network Blocks=%u,Total MH=%.4f" SEPSTR,
- message(MSG_SUMM, 0, NULL, isjson),
- total_secs, mhs, found_blocks,
- total_getworks, total_accepted, total_rejected,
- hw_errors, utility, total_discarded, total_stale,
- total_go, local_work, total_ro, new_blocks, total_mhashes_done);
-#endif
+ ? "%s," JSON_SUMMARY
+ : "%s" _SUMMARY ",",
+ message(MSG_SUMM, 0, NULL, isjson));
+
+ root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
+#ifdef WANT_CPUMINE
+ root = api_add_string(root, "Algorithm", algo, false);
+#endif
+ root = api_add_mhs(root, "MHS av", &(mhs), false);
+ root = api_add_uint(root, "Found Blocks", &(found_blocks), false);
+ root = api_add_int(root, "Getworks", &(total_getworks), false);
+ root = api_add_int(root, "Accepted", &(total_accepted), false);
+ root = api_add_int(root, "Rejected", &(total_rejected), false);
+ root = api_add_int(root, "Hardware Errors", &(hw_errors), false);
+ root = api_add_utility(root, "Utility", &(utility), false);
+ root = api_add_int(root, "Discarded", &(total_discarded), false);
+ root = api_add_int(root, "Stale", &(total_stale), false);
+ root = api_add_uint(root, "Get Failures", &(total_go), false);
+ root = api_add_uint(root, "Local Work", &(local_work), false);
+ root = api_add_uint(root, "Remote Failures", &(total_ro), false);
+ root = api_add_uint(root, "Network Blocks", &(new_blocks), false);
+ root = api_add_mhtotal(root, "Total MH", &(total_mhashes_done), false);
+
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
+ strcat(io_buffer, buf);
}
#ifdef HAVE_OPENCL
static void gpuenable(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unused char group)
@@ -1503,6 +1910,7 @@ static void gpurestart(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __
#endif
static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int numgpu = 0;
@@ -1510,18 +1918,22 @@ static void gpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bo
numgpu = nDevs;
#endif
- strcpy(io_buffer, message(MSG_NUMGPU, 0, NULL, isjson));
+ sprintf(io_buffer, isjson
+ ? "%s," JSON_GPUS
+ : "%s" _GPUS ",",
+ message(MSG_NUMGPU, 0, NULL, isjson));
- sprintf(buf, isjson
- ? "," JSON_GPUS "{\"Count\":%d}" JSON_CLOSE
- : _GPUS ",Count=%d" SEPSTR,
- numgpu);
+ root = api_add_int(root, "Count", &numgpu, false);
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int count = 0;
@@ -1529,18 +1941,22 @@ static void pgacount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bo
count = numpgas();
#endif
- strcpy(io_buffer, message(MSG_NUMPGA, 0, NULL, isjson));
+ sprintf(io_buffer, isjson
+ ? "%s," JSON_PGAS
+ : "%s" _PGAS ",",
+ message(MSG_NUMPGA, 0, NULL, isjson));
- sprintf(buf, isjson
- ? "," JSON_PGAS "{\"Count\":%d}" JSON_CLOSE
- : _PGAS ",Count=%d" SEPSTR,
- count);
+ root = api_add_int(root, "Count", &count, false);
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
int count = 0;
@@ -1548,13 +1964,16 @@ static void cpucount(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bo
count = opt_n_threads > 0 ? num_processors : 0;
#endif
- strcpy(io_buffer, message(MSG_NUMCPU, 0, NULL, isjson));
+ sprintf(io_buffer, isjson
+ ? "%s," JSON_CPUS
+ : "%s" _CPUS ",",
+ message(MSG_NUMCPU, 0, NULL, isjson));
- sprintf(buf, isjson
- ? "," JSON_CPUS "{\"Count\":%d}" JSON_CLOSE
- : _CPUS ",Count=%d" SEPSTR,
- count);
+ root = api_add_int(root, "Count", &count, false);
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
@@ -1968,6 +2387,7 @@ void privileged(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool is
void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char *reason;
@@ -2005,18 +2425,26 @@ void notifystatus(int device, struct cgpu_info *cgpu, bool isjson, __maybe_unuse
}
// ALL counters (and only counters) must start the name with a '*'
- // Simplifies future external support for adding new counters
- sprintf(buf, isjson
- ? "%s{\"NOTIFY\":%d,\"Name\":\"%s\",\"ID\":%d,\"Last Well\":%lu,\"Last Not Well\":%lu,\"Reason Not Well\":\"%s\",\"*Thread Fail Init\":%d,\"*Thread Zero Hash\":%d,\"*Thread Fail Queue\":%d,\"*Dev Sick Idle 60s\":%d,\"*Dev Dead Idle 600s\":%d,\"*Dev Nostart\":%d,\"*Dev Over Heat\":%d,\"*Dev Thermal Cutoff\":%d}"
- : "%sNOTIFY=%d,Name=%s,ID=%d,Last Well=%lu,Last Not Well=%lu,Reason Not Well=%s,*Thread Fail Init=%d,*Thread Zero Hash=%d,*Thread Fail Queue=%d,*Dev Sick Idle 60s=%d,*Dev Dead Idle 600s=%d,*Dev Nostart=%d,*Dev Over Heat=%d,*Dev Thermal Cutoff=%d" SEPSTR,
- (isjson && (device > 0)) ? COMMA : BLANK,
- device, cgpu->api->name, cgpu->device_id,
- cgpu->device_last_well, cgpu->device_last_not_well, reason,
- cgpu->thread_fail_init_count, cgpu->thread_zero_hash_count,
- cgpu->thread_fail_queue_count, cgpu->dev_sick_idle_60_count,
- cgpu->dev_dead_idle_600_count, cgpu->dev_nostart_count,
- cgpu->dev_over_heat_count, cgpu->dev_thermal_cutoff_count);
+ // Simplifies future external support for identifying new counters
+ root = api_add_int(root, "NOTIFY", &device, false);
+ root = api_add_string(root, "Name", cgpu->api->name, false);
+ root = api_add_int(root, "ID", &(cgpu->device_id), false);
+ root = api_add_time(root, "Last Well", &(cgpu->device_last_well), false);
+ root = api_add_time(root, "Last Not Well", &(cgpu->device_last_not_well), false);
+ root = api_add_string(root, "Reason Not Well", reason, false);
+ root = api_add_int(root, "*Thread Fail Init", &(cgpu->thread_fail_init_count), false);
+ root = api_add_int(root, "*Thread Zero Hash", &(cgpu->thread_zero_hash_count), false);
+ root = api_add_int(root, "*Thread Fail Queue", &(cgpu->thread_fail_queue_count), false);
+ root = api_add_int(root, "*Dev Sick Idle 60s", &(cgpu->dev_sick_idle_60_count), false);
+ root = api_add_int(root, "*Dev Dead Idle 600s", &(cgpu->dev_dead_idle_600_count), false);
+ root = api_add_int(root, "*Dev Nostart", &(cgpu->dev_nostart_count), false);
+ root = api_add_int(root, "*Dev Over Heat", &(cgpu->dev_over_heat_count), false);
+ root = api_add_int(root, "*Dev Thermal Cutoff", &(cgpu->dev_thermal_cutoff_count), false);
+
+ if (isjson && (device > 0))
+ strcat(io_buffer, COMMA);
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
@@ -2045,6 +2473,7 @@ static void notify(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool
static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
struct cgpu_info *cgpu;
int i;
@@ -2064,14 +2493,18 @@ static void devdetails(__maybe_unused SOCKETTYPE c, __maybe_unused char *param,
for (i = 0; i < total_devices; i++) {
cgpu = devices[i];
- sprintf(buf, isjson
- ? "%s{\"DEVDETAILS\":%d,\"Name\":\"%s\",\"ID\":%d,\"Driver\":\"%s\",\"Kernel\":\"%s\",\"Model\":\"%s\",\"Device Path\":\"%s\"}"
- : "%sDEVDETAILS=%d,Name=%s,ID=%d,Driver=%s,Kernel=%s,Model=%s,Device Path=%s" SEPSTR,
- (isjson && (i > 0)) ? COMMA : BLANK,
- i, cgpu->api->name, cgpu->device_id,
- cgpu->api->dname, cgpu->kname ? : BLANK,
- cgpu->name ? : BLANK, cgpu->device_path ? : BLANK);
+ root = api_add_int(root, "DEVDETAILS", &i, false);
+ root = api_add_string(root, "Name", cgpu->api->name, false);
+ root = api_add_int(root, "ID", &(cgpu->device_id), false);
+ root = api_add_string(root, "Driver", cgpu->api->dname, false);
+ root = api_add_const(root, "Kernel", cgpu->kname ? : BLANK, false);
+ root = api_add_const(root, "Model", cgpu->name ? : BLANK, false);
+ root = api_add_const(root, "Device Path", cgpu->device_path ? : BLANK, false);
+
+ if (isjson && (i > 0))
+ strcat(io_buffer, COMMA);
+ root = print_data(root, buf, isjson);
strcat(io_buffer, buf);
}
@@ -2110,55 +2543,43 @@ void dosave(__maybe_unused SOCKETTYPE c, char *param, bool isjson, __maybe_unuse
ptr = NULL;
}
-static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, char *extra, bool isjson)
+static int itemstats(int i, char *id, struct cgminer_stats *stats, struct cgminer_pool_stats *pool_stats, struct api_data *extra, bool isjson)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
- if (stats->getwork_calls || (extra != NULL && *extra))
- {
- if (extra == NULL)
- extra = (char *)BLANK;
-
- sprintf(buf, isjson
- ? "%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);
-
- strcat(io_buffer, buf);
+ root = api_add_int(root, "STATS", &i, false);
+ root = api_add_string(root, "ID", id, false);
+ root = api_add_elapsed(root, "Elapsed", &(total_secs), false);
+ root = api_add_uint32(root, "Calls", &(stats->getwork_calls), false);
+ root = api_add_timeval(root, "Wait", &(stats->getwork_wait), false);
+ root = api_add_timeval(root, "Max", &(stats->getwork_wait_max), false);
+ root = api_add_timeval(root, "Min", &(stats->getwork_wait_min), false);
- 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 Av\":%f"
- : ",Pool Calls=%d,Pool Attempts=%d,Pool Wait=%ld.%06ld,Pool Max=%ld.%06ld,Pool Min=%ld.%06ld,Pool Av=%f",
- 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,
- pool_stats->getwork_wait_rolling);
-
- strcat(io_buffer, buf);
- }
+ if (pool_stats) {
+ root = api_add_uint32(root, "Pool Calls", &(pool_stats->getwork_calls), false);
+ root = api_add_uint32(root, "Pool Attempts", &(pool_stats->getwork_attempts), false);
+ root = api_add_timeval(root, "Pool Wait", &(pool_stats->getwork_wait), false);
+ root = api_add_timeval(root, "Pool Max", &(pool_stats->getwork_wait_max), false);
+ root = api_add_timeval(root, "Pool Min", &(pool_stats->getwork_wait_min), false);
+ root = api_add_double(root, "Pool Av", &(pool_stats->getwork_wait_rolling), false);
+ }
- sprintf(buf, isjson
- ? "%s%s}"
- : "%s%s" SEPSTR,
- *extra ? COMMA : BLANK, extra);
+ if (extra)
+ root = api_add_extra(root, extra);
- strcat(io_buffer, buf);
+ if (isjson && (i > 0))
+ strcat(io_buffer, COMMA);
- i++;
- }
+ root = print_data(root, buf, isjson);
+ strcat(io_buffer, buf);
- return i;
+ return ++i;
}
static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param, bool isjson, __maybe_unused char group)
{
- char extra[TMPBUFSIZ];
+ struct api_data *extra;
char id[20];
int i, j;
@@ -2175,9 +2596,9 @@ static void minerstats(__maybe_unused SOCKETTYPE c, __maybe_unused char *param,
if (cgpu && cgpu->api) {
if (cgpu->api->get_api_stats)
- cgpu->api->get_api_stats(extra, cgpu, isjson);
+ extra = cgpu->api->get_api_stats(cgpu);
else
- extra[0] = '\0';
+ extra = NULL;
sprintf(id, "%s%d", cgpu->api->name, cgpu->device_id);
i = itemstats(i, id, &(cgpu->cgminer_stats), NULL, extra, isjson);
@@ -2249,6 +2670,7 @@ struct CMDS {
static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson, char group)
{
+ struct api_data *root = NULL;
char buf[TMPBUFSIZ];
char cmdbuf[100];
bool found, access;
@@ -2273,14 +2695,17 @@ static void checkcommand(__maybe_unused SOCKETTYPE c, char *param, bool isjson,
}
}
- strcpy(io_buffer, message(MSG_CHECK, 0, NULL, isjson));
+ sprintf(io_buffer, isjson
+ ? "%s," JSON_CHECK
+ : "%s" _CHECK ",",
+ message(MSG_CHECK, 0, NULL, isjson));
- sprintf(buf, isjson
- ? "," JSON_CHECK "{\"Exists\":\"%s\",\"Access\":\"%s\"}" JSON_CLOSE
- : _CHECK ",Exists=%s,Access=%s" SEPSTR,
- found ? YES : NO,
- access ? YES : NO);
+ root = api_add_const(root, "Exists", found ? YES : NO, false);
+ root = api_add_const(root, "Access", access ? YES : NO, false);
+ root = print_data(root, buf, isjson);
+ if (isjson)
+ strcat(buf, JSON_CLOSE);
strcat(io_buffer, buf);
}
diff --git a/driver-icarus.c b/driver-icarus.c
index 442eb9d..2fe561e 100644
--- a/driver-icarus.c
+++ b/driver-icarus.c
@@ -676,23 +676,30 @@ static uint64_t icarus_scanhash(struct thr_info *thr, struct work *work,
return hash_count;
}
-static void icarus_api_stats(char *buf, struct cgpu_info *cgpu, bool isjson)
+static struct api_data *icarus_api_stats(struct cgpu_info *cgpu)
{
+ struct api_data *root = NULL;
struct ICARUS_INFO *info = icarus_info[cgpu->device_id];
// Warning, access to these is not locked - but we don't really
// care since hashing performance is way more important than
// locking access to displaying API debug 'stats'
- sprintf(buf, isjson
- ? "\"read_count\":%d,\"fullnonce\":%f,\"count\":%d,\"Hs\":%.15f,\"W\":%f,\"total_values\":%u,\"range\":%"PRIu64",\"history_count\":%"PRIu64",\"history_time\":%f,\"min_data_count\":%u,\"timing_values\":%u"
- : "read_count=%d,fullnonce=%f,count=%d,Hs=%.15f,W=%f,total_values=%u,range=%"PRIu64",history_count=%"PRIu64",history_time=%f,min_data_count=%u,timing_values=%u",
- info->read_count, info->fullnonce,
- info->count, info->Hs, info->W,
- info->values, info->hash_count_range,
- info->history_count,
- (double)(info->history_time.tv_sec)
- + ((double)(info->history_time.tv_usec))/((double)1000000),
- info->min_data_count, info->history[0].values);
+ // If locking becomes an issue for any of them, use copy_data=true also
+ root = api_add_int(root, "read_count", &(info->read_count), false);
+ root = api_add_double(root, "fullnonce", &(info->fullnonce), false);
+ root = api_add_int(root, "count", &(info->count), false);
+ root = api_add_hs(root, "Hs", &(info->Hs), false);
+ root = api_add_double(root, "W", &(info->W), false);
+ root = api_add_uint(root, "total_values", &(info->values), false);
+ root = api_add_uint64(root, "range", &(info->hash_count_range), false);
+ root = api_add_uint64(root, "history_count", &(info->history_count), false);
+ root = api_add_timeval(root, "history_time", &(info->history_time), false);
+ root = api_add_uint(root, "min_data_count", &(info->min_data_count), false);
+ root = api_add_uint(root, "timing_values", &(info->history[0].values), false);
+ root = api_add_const(root, "timing_mode", timing_mode_str(info->timing_mode), false);
+ root = api_add_bool(root, "is_timing", &(info->do_icarus_timing), false);
+
+ return root;
}
static void icarus_shutdown(struct thr_info *thr)
diff --git a/miner.h b/miner.h
index 9965a98..8feaa79 100644
--- a/miner.h
+++ b/miner.h
@@ -220,6 +220,7 @@ struct gpu_adl {
};
#endif
+struct api_data;
struct thr_info;
struct work;
@@ -234,7 +235,7 @@ struct device_api {
void (*reinit_device)(struct cgpu_info*);
void (*get_statline_before)(char*, struct cgpu_info*);
void (*get_statline)(char*, struct cgpu_info*);
- void (*get_api_stats)(char*, struct cgpu_info*, bool);
+ struct api_data *(*get_api_stats)(struct cgpu_info*);
// Thread-specific functions
bool (*thread_prepare)(struct thr_info*);
@@ -793,4 +794,55 @@ extern bool successful_connect;
extern void adl(void);
extern void app_restart(void);
+enum api_data_type {
+ API_ESCAPE,
+ API_STRING,
+ API_CONST,
+ API_INT,
+ API_UINT,
+ API_UINT32,
+ API_UINT64,
+ API_DOUBLE,
+ API_ELAPSED,
+ API_BOOL,
+ API_TIMEVAL,
+ API_TIME,
+ API_MHS,
+ API_MHTOTAL,
+ API_TEMP,
+ API_UTILITY,
+ API_FREQ,
+ API_VOLTS,
+ API_HS
+};
+
+struct api_data {
+ enum api_data_type type;
+ char *name;
+ void *data;
+ bool data_was_malloc;
+ struct api_data *prev;
+ struct api_data *next;
+};
+
+extern struct api_data *api_add_escape(struct api_data *root, char *name, char *data, bool copy_data);
+extern struct api_data *api_add_string(struct api_data *root, char *name, char *data, bool copy_data);
+extern struct api_data *api_add_const(struct api_data *root, char *name, const char *data, bool copy_data);
+extern struct api_data *api_add_int(struct api_data *root, char *name, int *data, bool copy_data);
+extern struct api_data *api_add_uint(struct api_data *root, char *name, unsigned int *data, bool copy_data);
+extern struct api_data *api_add_uint32(struct api_data *root, char *name, uint32_t *data, bool copy_data);
+extern struct api_data *api_add_uint64(struct api_data *root, char *name, uint64_t *data, bool copy_data);
+extern struct api_data *api_add_double(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_elapsed(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_bool(struct api_data *root, char *name, bool *data, bool copy_data);
+extern struct api_data *api_add_timeval(struct api_data *root, char *name, struct timeval *data, bool copy_data);
+extern struct api_data *api_add_time(struct api_data *root, char *name, time_t *data, bool copy_data);
+extern struct api_data *api_add_mhs(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_mhstotal(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_temp(struct api_data *root, char *name, float *data, bool copy_data);
+extern struct api_data *api_add_utility(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_freq(struct api_data *root, char *name, double *data, bool copy_data);
+extern struct api_data *api_add_volts(struct api_data *root, char *name, float *data, bool copy_data);
+extern struct api_data *api_add_hs(struct api_data *root, char *name, double *data, bool copy_data);
+
#endif /* __MINER_H__ */