2
0

tcp_stats.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 iptelorg GmbH
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. /** tcp statistics.
  19. * @file tcp_stats.c
  20. * @ingroup: core
  21. */
  22. /*
  23. * History:
  24. * --------
  25. * 2010-08-08 initial version (andrei)
  26. */
  27. #ifdef USE_TCP
  28. #include "tcp_stats.h"
  29. #ifdef USE_TCP_STATS
  30. #include "counters.h"
  31. #include "tcp_info.h"
  32. struct tcp_counters_h tcp_cnts_h;
  33. enum tcp_info_req { TCP_INFO_NONE, TCP_INFO_CONN_NO, TCP_INFO_WR_QUEUE_SZ };
  34. static counter_val_t tcp_info(counter_handle_t h, void* what);
  35. /* tcp counters definitions */
  36. counter_def_t tcp_cnt_defs[] = {
  37. {&tcp_cnts_h.established, "established", 0, 0, 0,
  38. "incremented each time a tcp connection is established."},
  39. {&tcp_cnts_h.passive_open, "passive_open", 0, 0, 0,
  40. "total number of accepted connections (so far)."},
  41. {&tcp_cnts_h.connect_success, "connect_success", 0, 0, 0,
  42. "total number of successfully active opened connections"
  43. " (successful connect()s)."},
  44. {&tcp_cnts_h.connect_failed, "connect_failed", 0, 0, 0,
  45. "number of failed active connection attempts."},
  46. {&tcp_cnts_h.local_reject, "local_reject", 0, 0, 0,
  47. "number of rejected incoming connections."},
  48. {&tcp_cnts_h.con_timeout, "con_timeout", 0, 0, 0,
  49. "total number of connections that did timeout (idle for too long)."},
  50. {&tcp_cnts_h.con_reset, "con_reset", 0, 0, 0,
  51. "total number of TCP_RSTs received on established connections."},
  52. {&tcp_cnts_h.send_timeout, "send_timeout", 0, 0, 0,
  53. "number of send attempts that failed due to a timeout"
  54. "(note: works only in tcp async mode)."},
  55. {&tcp_cnts_h.sendq_full, "sendq_full", 0, 0, 0,
  56. "number of send attempts that failed because of exceeded buffering"
  57. "capacity (send queue full, works only in tcp async mode)."},
  58. {0, "current_opened_connections", 0,
  59. tcp_info, (void*)(long)TCP_INFO_CONN_NO,
  60. "number of currently opened connections."},
  61. {0, "current_write_queue_size", 0,
  62. tcp_info, (void*)(long)TCP_INFO_WR_QUEUE_SZ,
  63. "current sum of all the connections write queue sizes."},
  64. {0, 0, 0, 0, 0, 0 }
  65. };
  66. /** helper function for some stats (which are kept internally inside tcp).
  67. */
  68. static counter_val_t tcp_info(counter_handle_t h, void* what)
  69. {
  70. enum tcp_info_req w;
  71. struct tcp_gen_info ti;
  72. if (tcp_disable)
  73. return 0;
  74. w = (int)(long)what;
  75. tcp_get_info(&ti);
  76. switch(w) {
  77. case TCP_INFO_CONN_NO:
  78. return ti.tcp_connections_no;
  79. case TCP_INFO_WR_QUEUE_SZ:
  80. return ti.tcp_write_queued;
  81. case TCP_INFO_NONE:
  82. break;
  83. };
  84. return 0;
  85. }
  86. /** intialize tcp statistics.
  87. * Must be called before forking.
  88. * @return < 0 on errror, 0 on success.
  89. */
  90. int tcp_stats_init()
  91. {
  92. #define TCP_REG_COUNTER(name) \
  93. if (counter_register(&tcp_cnts_h.name, "tcp", # name, 0, 0, 0, 0) < 0) \
  94. goto error;
  95. if (counter_register_array("tcp", tcp_cnt_defs) < 0)
  96. goto error;
  97. return 0;
  98. error:
  99. return -1;
  100. }
  101. void tcp_stats_destroy()
  102. {
  103. /* do nothing */
  104. }
  105. #endif /* USE_TCP_STATS */
  106. #endif /* USE_TCP */
  107. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */