stats.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  19. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  21. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  24. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  25. * OF SUCH DAMAGE.
  26. *
  27. * This file is part of the lwIP TCP/IP stack.
  28. *
  29. * Author: Adam Dunkels <adam@sics.se>
  30. *
  31. */
  32. #ifndef __LWIP_STATS_H__
  33. #define __LWIP_STATS_H__
  34. #include "lwip/opt.h"
  35. #include "lwip/mem.h"
  36. #include "lwip/memp.h"
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #if LWIP_STATS
  41. #ifndef LWIP_STATS_LARGE
  42. #define LWIP_STATS_LARGE 0
  43. #endif
  44. #if LWIP_STATS_LARGE
  45. #define STAT_COUNTER u32_t
  46. #define STAT_COUNTER_F U32_F
  47. #else
  48. #define STAT_COUNTER u16_t
  49. #define STAT_COUNTER_F U16_F
  50. #endif
  51. struct stats_proto {
  52. STAT_COUNTER xmit; /* Transmitted packets. */
  53. STAT_COUNTER recv; /* Received packets. */
  54. STAT_COUNTER fw; /* Forwarded packets. */
  55. STAT_COUNTER drop; /* Dropped packets. */
  56. STAT_COUNTER chkerr; /* Checksum error. */
  57. STAT_COUNTER lenerr; /* Invalid length error. */
  58. STAT_COUNTER memerr; /* Out of memory error. */
  59. STAT_COUNTER rterr; /* Routing error. */
  60. STAT_COUNTER proterr; /* Protocol error. */
  61. STAT_COUNTER opterr; /* Error in options. */
  62. STAT_COUNTER err; /* Misc error. */
  63. STAT_COUNTER cachehit;
  64. };
  65. struct stats_igmp {
  66. STAT_COUNTER xmit; /* Transmitted packets. */
  67. STAT_COUNTER recv; /* Received packets. */
  68. STAT_COUNTER drop; /* Dropped packets. */
  69. STAT_COUNTER chkerr; /* Checksum error. */
  70. STAT_COUNTER lenerr; /* Invalid length error. */
  71. STAT_COUNTER memerr; /* Out of memory error. */
  72. STAT_COUNTER proterr; /* Protocol error. */
  73. STAT_COUNTER rx_v1; /* Received v1 frames. */
  74. STAT_COUNTER rx_group; /* Received group-specific queries. */
  75. STAT_COUNTER rx_general; /* Received general queries. */
  76. STAT_COUNTER rx_report; /* Received reports. */
  77. STAT_COUNTER tx_join; /* Sent joins. */
  78. STAT_COUNTER tx_leave; /* Sent leaves. */
  79. STAT_COUNTER tx_report; /* Sent reports. */
  80. };
  81. struct stats_mem {
  82. #ifdef LWIP_DEBUG
  83. const char *name;
  84. #endif /* LWIP_DEBUG */
  85. mem_size_t avail;
  86. mem_size_t used;
  87. mem_size_t max;
  88. STAT_COUNTER err;
  89. STAT_COUNTER illegal;
  90. };
  91. struct stats_syselem {
  92. STAT_COUNTER used;
  93. STAT_COUNTER max;
  94. STAT_COUNTER err;
  95. };
  96. struct stats_sys {
  97. struct stats_syselem sem;
  98. struct stats_syselem mutex;
  99. struct stats_syselem mbox;
  100. };
  101. struct stats_ {
  102. #if LINK_STATS
  103. struct stats_proto link;
  104. #endif
  105. #if ETHARP_STATS
  106. struct stats_proto etharp;
  107. #endif
  108. #if IPFRAG_STATS
  109. struct stats_proto ip_frag;
  110. #endif
  111. #if IP_STATS
  112. struct stats_proto ip;
  113. #endif
  114. #if ICMP_STATS
  115. struct stats_proto icmp;
  116. #endif
  117. #if IGMP_STATS
  118. struct stats_igmp igmp;
  119. #endif
  120. #if UDP_STATS
  121. struct stats_proto udp;
  122. #endif
  123. #if TCP_STATS
  124. struct stats_proto tcp;
  125. #endif
  126. #if MEM_STATS
  127. struct stats_mem mem;
  128. #endif
  129. #if MEMP_STATS
  130. struct stats_mem memp[MEMP_MAX];
  131. #endif
  132. #if SYS_STATS
  133. struct stats_sys sys;
  134. #endif
  135. };
  136. extern struct stats_ lwip_stats;
  137. void stats_init(void)ICACHE_FLASH_ATTR;
  138. #define STATS_INC(x) ++lwip_stats.x
  139. #define STATS_DEC(x) --lwip_stats.x
  140. #define STATS_INC_USED(x, y) do { lwip_stats.x.used += y; \
  141. if (lwip_stats.x.max < lwip_stats.x.used) { \
  142. lwip_stats.x.max = lwip_stats.x.used; \
  143. } \
  144. } while(0)
  145. #else /* LWIP_STATS */
  146. #define stats_init()
  147. #define STATS_INC(x)
  148. #define STATS_DEC(x)
  149. #define STATS_INC_USED(x)
  150. #endif /* LWIP_STATS */
  151. #if TCP_STATS
  152. #define TCP_STATS_INC(x) STATS_INC(x)
  153. #define TCP_STATS_DISPLAY() stats_display_proto(&lwip_stats.tcp, "TCP")
  154. #else
  155. #define TCP_STATS_INC(x)
  156. #define TCP_STATS_DISPLAY()
  157. #endif
  158. #if UDP_STATS
  159. #define UDP_STATS_INC(x) STATS_INC(x)
  160. #define UDP_STATS_DISPLAY() stats_display_proto(&lwip_stats.udp, "UDP")
  161. #else
  162. #define UDP_STATS_INC(x)
  163. #define UDP_STATS_DISPLAY()
  164. #endif
  165. #if ICMP_STATS
  166. #define ICMP_STATS_INC(x) STATS_INC(x)
  167. #define ICMP_STATS_DISPLAY() stats_display_proto(&lwip_stats.icmp, "ICMP")
  168. #else
  169. #define ICMP_STATS_INC(x)
  170. #define ICMP_STATS_DISPLAY()
  171. #endif
  172. #if IGMP_STATS
  173. #define IGMP_STATS_INC(x) STATS_INC(x)
  174. #define IGMP_STATS_DISPLAY() stats_display_igmp(&lwip_stats.igmp)
  175. #else
  176. #define IGMP_STATS_INC(x)
  177. #define IGMP_STATS_DISPLAY()
  178. #endif
  179. #if IP_STATS
  180. #define IP_STATS_INC(x) STATS_INC(x)
  181. #define IP_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip, "IP")
  182. #else
  183. #define IP_STATS_INC(x)
  184. #define IP_STATS_DISPLAY()
  185. #endif
  186. #if IPFRAG_STATS
  187. #define IPFRAG_STATS_INC(x) STATS_INC(x)
  188. #define IPFRAG_STATS_DISPLAY() stats_display_proto(&lwip_stats.ip_frag, "IP_FRAG")
  189. #else
  190. #define IPFRAG_STATS_INC(x)
  191. #define IPFRAG_STATS_DISPLAY()
  192. #endif
  193. #if ETHARP_STATS
  194. #define ETHARP_STATS_INC(x) STATS_INC(x)
  195. #define ETHARP_STATS_DISPLAY() stats_display_proto(&lwip_stats.etharp, "ETHARP")
  196. #else
  197. #define ETHARP_STATS_INC(x)
  198. #define ETHARP_STATS_DISPLAY()
  199. #endif
  200. #if LINK_STATS
  201. #define LINK_STATS_INC(x) STATS_INC(x)
  202. #define LINK_STATS_DISPLAY() stats_display_proto(&lwip_stats.link, "LINK")
  203. #else
  204. #define LINK_STATS_INC(x)
  205. #define LINK_STATS_DISPLAY()
  206. #endif
  207. #if MEM_STATS
  208. #define MEM_STATS_AVAIL(x, y) lwip_stats.mem.x = y
  209. #define MEM_STATS_INC(x) STATS_INC(mem.x)
  210. #define MEM_STATS_INC_USED(x, y) STATS_INC_USED(mem, y)
  211. #define MEM_STATS_DEC_USED(x, y) lwip_stats.mem.x -= y
  212. #define MEM_STATS_DISPLAY() stats_display_mem(&lwip_stats.mem, "HEAP")
  213. #else
  214. #define MEM_STATS_AVAIL(x, y)
  215. #define MEM_STATS_INC(x)
  216. #define MEM_STATS_INC_USED(x, y)
  217. #define MEM_STATS_DEC_USED(x, y)
  218. #define MEM_STATS_DISPLAY()
  219. #endif
  220. #if MEMP_STATS
  221. #define MEMP_STATS_AVAIL(x, i, y) lwip_stats.memp[i].x = y
  222. #define MEMP_STATS_INC(x, i) STATS_INC(memp[i].x)
  223. #define MEMP_STATS_DEC(x, i) STATS_DEC(memp[i].x)
  224. #define MEMP_STATS_INC_USED(x, i) STATS_INC_USED(memp[i], 1)
  225. #define MEMP_STATS_DISPLAY(i) stats_display_memp(&lwip_stats.memp[i], i)
  226. #else
  227. #define MEMP_STATS_AVAIL(x, i, y)
  228. #define MEMP_STATS_INC(x, i)
  229. #define MEMP_STATS_DEC(x, i)
  230. #define MEMP_STATS_INC_USED(x, i)
  231. #define MEMP_STATS_DISPLAY(i)
  232. #endif
  233. #if SYS_STATS
  234. #define SYS_STATS_INC(x) STATS_INC(sys.x)
  235. #define SYS_STATS_DEC(x) STATS_DEC(sys.x)
  236. #define SYS_STATS_INC_USED(x) STATS_INC_USED(sys.x, 1)
  237. #define SYS_STATS_DISPLAY() stats_display_sys(&lwip_stats.sys)
  238. #else
  239. #define SYS_STATS_INC(x)
  240. #define SYS_STATS_DEC(x)
  241. #define SYS_STATS_INC_USED(x)
  242. #define SYS_STATS_DISPLAY()
  243. #endif
  244. /* Display of statistics */
  245. #if LWIP_STATS_DISPLAY
  246. void stats_display(void)ICACHE_FLASH_ATTR;
  247. void stats_display_proto(struct stats_proto *proto, char *name)ICACHE_FLASH_ATTR;
  248. void stats_display_igmp(struct stats_igmp *igmp)ICACHE_FLASH_ATTR;
  249. void stats_display_mem(struct stats_mem *mem, char *name)ICACHE_FLASH_ATTR;
  250. void stats_display_memp(struct stats_mem *mem, int index)ICACHE_FLASH_ATTR;
  251. void stats_display_sys(struct stats_sys *sys)ICACHE_FLASH_ATTR;
  252. #else /* LWIP_STATS_DISPLAY */
  253. #define stats_display()
  254. #define stats_display_proto(proto, name)
  255. #define stats_display_igmp(igmp)
  256. #define stats_display_mem(mem, name)
  257. #define stats_display_memp(mem, index)
  258. #define stats_display_sys(sys)
  259. #endif /* LWIP_STATS_DISPLAY */
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif /* __LWIP_STATS_H__ */