driver-proxy.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright 2013-2014 Luke Dashjr
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License as published by the Free
  6. * Software Foundation; either version 3 of the License, or (at your option)
  7. * any later version. See COPYING for more details.
  8. */
  9. #include "config.h"
  10. #include <unistd.h>
  11. #include <pthread.h>
  12. #include <uthash.h>
  13. #include "deviceapi.h"
  14. #include "driver-proxy.h"
  15. #include "miner.h"
  16. #include "util.h"
  17. BFG_REGISTER_DRIVER(proxy_drv)
  18. static const struct bfg_set_device_definition proxy_set_device_funcs[];
  19. static
  20. struct proxy_client *proxy_clients;
  21. static
  22. pthread_mutex_t proxy_clients_mutex = PTHREAD_MUTEX_INITIALIZER;
  23. static
  24. void prune_worklog()
  25. {
  26. struct proxy_client *client, *tmp;
  27. struct work *work, *tmp2;
  28. struct timeval tv_now;
  29. timer_set_now(&tv_now);
  30. mutex_lock(&proxy_clients_mutex);
  31. HASH_ITER(hh, proxy_clients, client, tmp)
  32. {
  33. HASH_ITER(hh, client->work, work, tmp2)
  34. {
  35. if (timer_elapsed(&work->tv_work_start, &tv_now) <= opt_expiry)
  36. break;
  37. HASH_DEL(client->work, work);
  38. free_work(work);
  39. }
  40. }
  41. mutex_unlock(&proxy_clients_mutex);
  42. }
  43. static
  44. pthread_t prune_worklog_pth;
  45. static
  46. void *prune_worklog_thread(void *userdata)
  47. {
  48. struct cgpu_info *cgpu = userdata;
  49. pthread_detach(pthread_self());
  50. RenameThread("PXY_pruner");
  51. while (!cgpu->shutdown)
  52. {
  53. prune_worklog();
  54. sleep(60);
  55. }
  56. return NULL;
  57. }
  58. static
  59. float proxy_min_nonce_diff(struct cgpu_info * const proc, const struct mining_algorithm * const malgo)
  60. {
  61. return minimum_pdiff;
  62. }
  63. static
  64. void proxy_first_client(struct cgpu_info *cgpu)
  65. {
  66. pthread_create(&prune_worklog_pth, NULL, prune_worklog_thread, cgpu);
  67. }
  68. struct proxy_client *proxy_find_or_create_client(const char *username)
  69. {
  70. struct proxy_client *client;
  71. struct cgpu_info *cgpu;
  72. char *user;
  73. int b;
  74. if (!username)
  75. return NULL;
  76. mutex_lock(&proxy_clients_mutex);
  77. HASH_FIND_STR(proxy_clients, username, client);
  78. if (!client)
  79. {
  80. user = strdup(username);
  81. cgpu = malloc(sizeof(*cgpu));
  82. client = malloc(sizeof(*client));
  83. *cgpu = (struct cgpu_info){
  84. .drv = &proxy_drv,
  85. .set_device_funcs = proxy_set_device_funcs,
  86. .threads = 0,
  87. .device_data = client,
  88. .device_path = user,
  89. };
  90. timer_set_now(&cgpu->cgminer_stats.start_tv);
  91. if (unlikely(!create_new_cgpus(add_cgpu_live, cgpu)))
  92. {
  93. free(client);
  94. free(cgpu);
  95. free(user);
  96. return NULL;
  97. }
  98. *client = (struct proxy_client){
  99. .username = user,
  100. .cgpu = cgpu,
  101. .desired_share_pdiff = 0.,
  102. };
  103. b = HASH_COUNT(proxy_clients);
  104. HASH_ADD_KEYPTR(hh, proxy_clients, client->username, strlen(user), client);
  105. mutex_unlock(&proxy_clients_mutex);
  106. if (!b)
  107. proxy_first_client(cgpu);
  108. cgpu_set_defaults(cgpu);
  109. }
  110. else
  111. {
  112. mutex_unlock(&proxy_clients_mutex);
  113. cgpu = client->cgpu;
  114. }
  115. thread_reportin(cgpu->thr[0]);
  116. return client;
  117. }
  118. // See also, stratumsrv_init_diff in driver-stratum.c
  119. static
  120. const char *proxy_set_diff(struct cgpu_info * const proc, const char * const optname, const char * const newvalue, char * const replybuf, enum bfg_set_device_replytype * const success)
  121. {
  122. struct proxy_client * const client = proc->device_data;
  123. double nv = atof(newvalue);
  124. if (nv < 0)
  125. return "Invalid difficulty";
  126. if (nv <= minimum_pdiff)
  127. nv = minimum_pdiff;
  128. client->desired_share_pdiff = nv;
  129. #ifdef USE_LIBEVENT
  130. stratumsrv_client_changed_diff(client);
  131. #endif
  132. return NULL;
  133. }
  134. #ifdef HAVE_CURSES
  135. static
  136. void proxy_wlogprint_status(struct cgpu_info *cgpu)
  137. {
  138. struct proxy_client *client = cgpu->device_data;
  139. wlogprint("Username: %s\n", client->username);
  140. }
  141. #endif
  142. static const struct bfg_set_device_definition proxy_set_device_funcs[] = {
  143. {"diff", proxy_set_diff, "desired share difficulty for clients"},
  144. {NULL},
  145. };
  146. struct device_drv proxy_drv = {
  147. .dname = "proxy",
  148. .name = "PXY",
  149. .drv_min_nonce_diff = proxy_min_nonce_diff,
  150. #ifdef HAVE_CURSES
  151. .proc_wlogprint_status = proxy_wlogprint_status,
  152. #endif
  153. };