status.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* Copyright (c) 2010-2013, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /**
  4. * \file status.c
  5. * \brief Keep status information and log the heartbeat messages.
  6. **/
  7. #include "or.h"
  8. #include "config.h"
  9. #include "status.h"
  10. #include "nodelist.h"
  11. #include "relay.h"
  12. #include "router.h"
  13. #include "circuitlist.h"
  14. #include "main.h"
  15. #include "rephist.h"
  16. #include "hibernate.h"
  17. /** Return the total number of circuits. */
  18. static int
  19. count_circuits(void)
  20. {
  21. circuit_t *circ;
  22. int nr=0;
  23. for (circ = circuit_get_global_list_(); circ; circ = circ->next)
  24. nr++;
  25. return nr;
  26. }
  27. /** Take seconds <b>secs</b> and return a newly allocated human-readable
  28. * uptime string */
  29. static char *
  30. secs_to_uptime(long secs)
  31. {
  32. long int days = secs / 86400;
  33. int hours = (int)((secs - (days * 86400)) / 3600);
  34. int minutes = (int)((secs - (days * 86400) - (hours * 3600)) / 60);
  35. char *uptime_string = NULL;
  36. switch (days) {
  37. case 0:
  38. tor_asprintf(&uptime_string, "%d:%02d hours", hours, minutes);
  39. break;
  40. case 1:
  41. tor_asprintf(&uptime_string, "%ld day %d:%02d hours",
  42. days, hours, minutes);
  43. break;
  44. default:
  45. tor_asprintf(&uptime_string, "%ld days %d:%02d hours",
  46. days, hours, minutes);
  47. break;
  48. }
  49. return uptime_string;
  50. }
  51. /** Take <b>bytes</b> and returns a newly allocated human-readable usage
  52. * string. */
  53. static char *
  54. bytes_to_usage(uint64_t bytes)
  55. {
  56. char *bw_string = NULL;
  57. if (bytes < (1<<20)) { /* Less than a megabyte. */
  58. tor_asprintf(&bw_string, U64_FORMAT" kB", U64_PRINTF_ARG(bytes>>10));
  59. } else if (bytes < (1<<30)) { /* Megabytes. Let's add some precision. */
  60. double bw = U64_TO_DBL(bytes);
  61. tor_asprintf(&bw_string, "%.2f MB", bw/(1<<20));
  62. } else { /* Gigabytes. */
  63. double bw = U64_TO_DBL(bytes);
  64. tor_asprintf(&bw_string, "%.2f GB", bw/(1<<30));
  65. }
  66. return bw_string;
  67. }
  68. /** Log a "heartbeat" message describing Tor's status and history so that the
  69. * user can know that there is indeed a running Tor. Return 0 on success and
  70. * -1 on failure. */
  71. int
  72. log_heartbeat(time_t now)
  73. {
  74. char *bw_sent = NULL;
  75. char *bw_rcvd = NULL;
  76. char *uptime = NULL;
  77. const routerinfo_t *me;
  78. double r = tls_get_write_overhead_ratio();
  79. const int hibernating = we_are_hibernating();
  80. const or_options_t *options = get_options();
  81. (void)now;
  82. if (public_server_mode(options) && !hibernating) {
  83. /* Let's check if we are in the current cached consensus. */
  84. if (!(me = router_get_my_routerinfo()))
  85. return -1; /* Something stinks, we won't even attempt this. */
  86. else
  87. if (!node_get_by_id(me->cache_info.identity_digest))
  88. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: It seems like we are not "
  89. "in the cached consensus.");
  90. }
  91. uptime = secs_to_uptime(get_uptime());
  92. bw_rcvd = bytes_to_usage(get_bytes_read());
  93. bw_sent = bytes_to_usage(get_bytes_written());
  94. log_fn(LOG_NOTICE, LD_HEARTBEAT, "Heartbeat: Tor's uptime is %s, with %d "
  95. "circuits open. I've sent %s and received %s.%s",
  96. uptime, count_circuits(),bw_sent,bw_rcvd,
  97. hibernating?" We are currently hibernating.":"");
  98. if (stats_n_data_cells_packaged && !hibernating)
  99. log_notice(LD_HEARTBEAT, "Average packaged cell fullness: %2.3f%%",
  100. 100*(U64_TO_DBL(stats_n_data_bytes_packaged) /
  101. U64_TO_DBL(stats_n_data_cells_packaged*RELAY_PAYLOAD_SIZE)) );
  102. if (r > 1.0) {
  103. double overhead = ( r - 1.0 ) * 100.0;
  104. log_notice(LD_HEARTBEAT, "TLS write overhead: %.f%%", overhead);
  105. }
  106. /* Also commandeer this opportunity to log how our circuit handshake
  107. * stats have been doing. */
  108. if (public_server_mode(options))
  109. rep_hist_log_circuit_handshake_stats(now);
  110. tor_free(uptime);
  111. tor_free(bw_sent);
  112. tor_free(bw_rcvd);
  113. return 0;
  114. }