A clone of btpd with my configuration changes.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <math.h>
  2. #include "btcli.h"
  3. void
  4. usage_stat(void)
  5. {
  6. printf(
  7. "Display stats for active torrents.\n"
  8. "\n"
  9. "Usage: stat [-i] [-w seconds] [file ...]\n"
  10. "\n"
  11. "Arguments:\n"
  12. "file ...\n"
  13. "\tOnly display stats for the given torrent(s).\n"
  14. "\n"
  15. "Options:\n"
  16. "-i\n"
  17. "\tDisplay individual lines for each torrent.\n"
  18. "\n"
  19. "-n\n"
  20. "\tDisplay the name of each torrent. Implies '-i'.\n"
  21. "\n"
  22. "-w n\n"
  23. "\tDisplay stats every n seconds.\n"
  24. "\n");
  25. exit(1);
  26. }
  27. struct btstat {
  28. unsigned num;
  29. enum ipc_tstate state;
  30. unsigned peers, tr_errors;
  31. long long content_got, content_size, downloaded, uploaded, rate_up,
  32. rate_down;
  33. uint32_t pieces_seen, torrent_pieces;
  34. };
  35. struct cbarg {
  36. int individual, names;
  37. struct btstat tot;
  38. };
  39. static enum ipc_tval stkeys[] = {
  40. IPC_TVAL_STATE,
  41. IPC_TVAL_NUM,
  42. IPC_TVAL_NAME,
  43. IPC_TVAL_PCOUNT,
  44. IPC_TVAL_TRERR,
  45. IPC_TVAL_PCCOUNT,
  46. IPC_TVAL_PCSEEN,
  47. IPC_TVAL_SESSUP,
  48. IPC_TVAL_SESSDWN,
  49. IPC_TVAL_RATEUP,
  50. IPC_TVAL_RATEDWN,
  51. IPC_TVAL_CGOT,
  52. IPC_TVAL_CSIZE
  53. };
  54. static size_t nstkeys = sizeof(stkeys) / sizeof(stkeys[0]);
  55. static void
  56. print_stat(struct btstat *st)
  57. {
  58. printf("%5.1f%% %6.1fM %7.2fkB/s %6.1fM %7.2fkB/s %5u %5.1f%%",
  59. floor(1000.0 * st->content_got / st->content_size) / 10,
  60. (double)st->downloaded / (1 << 20),
  61. (double)st->rate_down / (20 << 10),
  62. (double)st->uploaded / (1 << 20),
  63. (double)st->rate_up / (20 << 10),
  64. st->peers,
  65. floor(1000.0 * st->pieces_seen / st->torrent_pieces) / 10);
  66. if (st->tr_errors > 0)
  67. printf(" E%u", st->tr_errors);
  68. printf("\n");
  69. }
  70. void
  71. stat_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
  72. {
  73. struct cbarg *cba = arg;
  74. struct btstat st, *tot = &cba->tot;
  75. if (objerr != IPC_OK || res[IPC_TVAL_STATE].v.num == IPC_TSTATE_INACTIVE)
  76. return;
  77. bzero(&st, sizeof(st));
  78. st.state = res[IPC_TVAL_STATE].v.num;
  79. st.num = res[IPC_TVAL_NUM].v.num;
  80. tot->torrent_pieces += (st.torrent_pieces = res[IPC_TVAL_PCCOUNT].v.num);
  81. tot->pieces_seen += (st.pieces_seen = res[IPC_TVAL_PCSEEN].v.num);
  82. tot->content_got += (st.content_got = res[IPC_TVAL_CGOT].v.num);
  83. tot->content_size += (st.content_size = res[IPC_TVAL_CSIZE].v.num);
  84. tot->downloaded += (st.downloaded = res[IPC_TVAL_SESSDWN].v.num);
  85. tot->uploaded += (st.uploaded = res[IPC_TVAL_SESSUP].v.num);
  86. tot->rate_down += (st.rate_down = res[IPC_TVAL_RATEDWN].v.num);
  87. tot->rate_up += (st.rate_up = res[IPC_TVAL_RATEUP].v.num);
  88. tot->peers += (st.peers = res[IPC_TVAL_PCOUNT].v.num);
  89. if ((st.tr_errors = res[IPC_TVAL_TRERR].v.num) > 0)
  90. tot->tr_errors++;
  91. if (cba->individual) {
  92. if (cba->names)
  93. printf("%.*s\n", (int)res[IPC_TVAL_NAME].v.str.l,
  94. res[IPC_TVAL_NAME].v.str.p);
  95. int n = printf("%u:", st.num);
  96. while (n < 7) {
  97. putchar(' ');
  98. n++;
  99. }
  100. printf("%c. ", tstate_char(st.state));
  101. print_stat(&st);
  102. }
  103. }
  104. static void
  105. do_stat(int individual, int names, int seconds, struct ipc_torrent *tps,
  106. int ntps)
  107. {
  108. enum ipc_err err;
  109. struct cbarg cba;
  110. if (names)
  111. individual = 1;
  112. if (individual)
  113. printf("NUM ST ");
  114. printf(" HAVE DLOAD RTDWN ULOAD RTUP PEERS AVAIL\n");
  115. cba.individual = individual;
  116. cba.names = names;
  117. again:
  118. bzero(&cba.tot, sizeof(cba.tot));
  119. cba.tot.state = IPC_TSTATE_INACTIVE;
  120. if (tps == NULL)
  121. err = btpd_tget_wc(ipc, IPC_TWC_ACTIVE, stkeys, nstkeys,
  122. stat_cb, &cba);
  123. else
  124. err = btpd_tget(ipc, tps, ntps, stkeys, nstkeys, stat_cb, &cba);
  125. if (handle_ipc_res(err, "stat") != IPC_OK)
  126. exit(1);
  127. if (names)
  128. printf("-----\n");
  129. if (individual)
  130. printf("Total: ");
  131. print_stat(&cba.tot);
  132. if (seconds > 0) {
  133. sleep(seconds);
  134. goto again;
  135. }
  136. }
  137. static struct option stat_opts [] = {
  138. { "help", no_argument, NULL, 'H' },
  139. {NULL, 0, NULL, 0}
  140. };
  141. void
  142. cmd_stat(int argc, char **argv)
  143. {
  144. int ch;
  145. int wflag = 0, iflag = 0, nflag = 0, seconds = 0;
  146. struct ipc_torrent *tps = NULL;
  147. int ntps = 0;
  148. char *endptr;
  149. while ((ch = getopt_long(argc, argv, "inw:", stat_opts, NULL)) != -1) {
  150. switch (ch) {
  151. case 'i':
  152. iflag = 1;
  153. break;
  154. case 'n':
  155. nflag = 1;
  156. break;
  157. case 'w':
  158. wflag = 1;
  159. seconds = strtol(optarg, &endptr, 10);
  160. if (*endptr != '\0' || seconds < 1)
  161. usage_stat();
  162. break;
  163. default:
  164. usage_stat();
  165. }
  166. }
  167. argc -= optind;
  168. argv += optind;
  169. if (argc > 0) {
  170. tps = malloc(argc * sizeof(*tps));
  171. for (int i = 0; i < argc; i++) {
  172. if (torrent_spec(argv[i], &tps[ntps]))
  173. ntps++;
  174. else
  175. exit(1);
  176. }
  177. }
  178. btpd_connect();
  179. do_stat(iflag, nflag, seconds, tps, ntps);
  180. }