netdebug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * netdebug.c
  5. *
  6. * debug functionality for o2net
  7. *
  8. * Copyright (C) 2005, 2008 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #ifdef CONFIG_DEBUG_FS
  27. #include <linux/module.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/idr.h>
  31. #include <linux/kref.h>
  32. #include <linux/seq_file.h>
  33. #include <linux/debugfs.h>
  34. #include <linux/uaccess.h>
  35. #include "tcp.h"
  36. #include "nodemanager.h"
  37. #define MLOG_MASK_PREFIX ML_TCP
  38. #include "masklog.h"
  39. #include "tcp_internal.h"
  40. #define O2NET_DEBUG_DIR "o2net"
  41. #define SC_DEBUG_NAME "sock_containers"
  42. #define NST_DEBUG_NAME "send_tracking"
  43. static struct dentry *o2net_dentry;
  44. static struct dentry *sc_dentry;
  45. static struct dentry *nst_dentry;
  46. static DEFINE_SPINLOCK(o2net_debug_lock);
  47. static LIST_HEAD(sock_containers);
  48. static LIST_HEAD(send_tracking);
  49. void o2net_debug_add_nst(struct o2net_send_tracking *nst)
  50. {
  51. spin_lock(&o2net_debug_lock);
  52. list_add(&nst->st_net_debug_item, &send_tracking);
  53. spin_unlock(&o2net_debug_lock);
  54. }
  55. void o2net_debug_del_nst(struct o2net_send_tracking *nst)
  56. {
  57. spin_lock(&o2net_debug_lock);
  58. if (!list_empty(&nst->st_net_debug_item))
  59. list_del_init(&nst->st_net_debug_item);
  60. spin_unlock(&o2net_debug_lock);
  61. }
  62. static struct o2net_send_tracking
  63. *next_nst(struct o2net_send_tracking *nst_start)
  64. {
  65. struct o2net_send_tracking *nst, *ret = NULL;
  66. assert_spin_locked(&o2net_debug_lock);
  67. list_for_each_entry(nst, &nst_start->st_net_debug_item,
  68. st_net_debug_item) {
  69. /* discover the head of the list */
  70. if (&nst->st_net_debug_item == &send_tracking)
  71. break;
  72. /* use st_task to detect real nsts in the list */
  73. if (nst->st_task != NULL) {
  74. ret = nst;
  75. break;
  76. }
  77. }
  78. return ret;
  79. }
  80. static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
  81. {
  82. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  83. spin_lock(&o2net_debug_lock);
  84. nst = next_nst(dummy_nst);
  85. spin_unlock(&o2net_debug_lock);
  86. return nst;
  87. }
  88. static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  89. {
  90. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  91. spin_lock(&o2net_debug_lock);
  92. nst = next_nst(dummy_nst);
  93. list_del_init(&dummy_nst->st_net_debug_item);
  94. if (nst)
  95. list_add(&dummy_nst->st_net_debug_item,
  96. &nst->st_net_debug_item);
  97. spin_unlock(&o2net_debug_lock);
  98. return nst; /* unused, just needs to be null when done */
  99. }
  100. static int nst_seq_show(struct seq_file *seq, void *v)
  101. {
  102. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  103. ktime_t now;
  104. s64 sock, send, status;
  105. spin_lock(&o2net_debug_lock);
  106. nst = next_nst(dummy_nst);
  107. now = ktime_get();
  108. sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
  109. send = ktime_to_us(ktime_sub(now, nst->st_send_time));
  110. status = ktime_to_us(ktime_sub(now, nst->st_status_time));
  111. if (nst != NULL) {
  112. /* get_task_comm isn't exported. oh well. */
  113. seq_printf(seq, "%p:\n"
  114. " pid: %lu\n"
  115. " tgid: %lu\n"
  116. " process name: %s\n"
  117. " node: %u\n"
  118. " sc: %p\n"
  119. " message id: %d\n"
  120. " message type: %u\n"
  121. " message key: 0x%08x\n"
  122. " sock acquiry: %lld usecs ago\n"
  123. " send start: %lld usecs ago\n"
  124. " wait start: %lld usecs ago\n",
  125. nst, (unsigned long)task_pid_nr(nst->st_task),
  126. (unsigned long)nst->st_task->tgid,
  127. nst->st_task->comm, nst->st_node,
  128. nst->st_sc, nst->st_id, nst->st_msg_type,
  129. nst->st_msg_key,
  130. (long long)sock,
  131. (long long)send,
  132. (long long)status);
  133. }
  134. spin_unlock(&o2net_debug_lock);
  135. return 0;
  136. }
  137. static void nst_seq_stop(struct seq_file *seq, void *v)
  138. {
  139. }
  140. static const struct seq_operations nst_seq_ops = {
  141. .start = nst_seq_start,
  142. .next = nst_seq_next,
  143. .stop = nst_seq_stop,
  144. .show = nst_seq_show,
  145. };
  146. static int nst_fop_open(struct inode *inode, struct file *file)
  147. {
  148. struct o2net_send_tracking *dummy_nst;
  149. struct seq_file *seq;
  150. int ret;
  151. dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
  152. if (dummy_nst == NULL) {
  153. ret = -ENOMEM;
  154. goto out;
  155. }
  156. dummy_nst->st_task = NULL;
  157. ret = seq_open(file, &nst_seq_ops);
  158. if (ret)
  159. goto out;
  160. seq = file->private_data;
  161. seq->private = dummy_nst;
  162. o2net_debug_add_nst(dummy_nst);
  163. dummy_nst = NULL;
  164. out:
  165. kfree(dummy_nst);
  166. return ret;
  167. }
  168. static int nst_fop_release(struct inode *inode, struct file *file)
  169. {
  170. struct seq_file *seq = file->private_data;
  171. struct o2net_send_tracking *dummy_nst = seq->private;
  172. o2net_debug_del_nst(dummy_nst);
  173. return seq_release_private(inode, file);
  174. }
  175. static const struct file_operations nst_seq_fops = {
  176. .open = nst_fop_open,
  177. .read = seq_read,
  178. .llseek = seq_lseek,
  179. .release = nst_fop_release,
  180. };
  181. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  182. {
  183. spin_lock(&o2net_debug_lock);
  184. list_add(&sc->sc_net_debug_item, &sock_containers);
  185. spin_unlock(&o2net_debug_lock);
  186. }
  187. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  188. {
  189. spin_lock(&o2net_debug_lock);
  190. list_del_init(&sc->sc_net_debug_item);
  191. spin_unlock(&o2net_debug_lock);
  192. }
  193. static struct o2net_sock_container
  194. *next_sc(struct o2net_sock_container *sc_start)
  195. {
  196. struct o2net_sock_container *sc, *ret = NULL;
  197. assert_spin_locked(&o2net_debug_lock);
  198. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  199. sc_net_debug_item) {
  200. /* discover the head of the list miscast as a sc */
  201. if (&sc->sc_net_debug_item == &sock_containers)
  202. break;
  203. /* use sc_page to detect real scs in the list */
  204. if (sc->sc_page != NULL) {
  205. ret = sc;
  206. break;
  207. }
  208. }
  209. return ret;
  210. }
  211. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  212. {
  213. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  214. spin_lock(&o2net_debug_lock);
  215. sc = next_sc(dummy_sc);
  216. spin_unlock(&o2net_debug_lock);
  217. return sc;
  218. }
  219. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  220. {
  221. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  222. spin_lock(&o2net_debug_lock);
  223. sc = next_sc(dummy_sc);
  224. list_del_init(&dummy_sc->sc_net_debug_item);
  225. if (sc)
  226. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  227. spin_unlock(&o2net_debug_lock);
  228. return sc; /* unused, just needs to be null when done */
  229. }
  230. #define TV_SEC_USEC(TV) TV.tv_sec, (long)TV.tv_usec
  231. static int sc_seq_show(struct seq_file *seq, void *v)
  232. {
  233. struct o2net_sock_container *sc, *dummy_sc = seq->private;
  234. spin_lock(&o2net_debug_lock);
  235. sc = next_sc(dummy_sc);
  236. if (sc != NULL) {
  237. struct inet_sock *inet = NULL;
  238. __be32 saddr = 0, daddr = 0;
  239. __be16 sport = 0, dport = 0;
  240. if (sc->sc_sock) {
  241. inet = inet_sk(sc->sc_sock->sk);
  242. /* the stack's structs aren't sparse endian clean */
  243. saddr = (__force __be32)inet->inet_saddr;
  244. daddr = (__force __be32)inet->inet_daddr;
  245. sport = (__force __be16)inet->inet_sport;
  246. dport = (__force __be16)inet->inet_dport;
  247. }
  248. /* XXX sigh, inet-> doesn't have sparse annotation so any
  249. * use of it here generates a warning with -Wbitwise */
  250. seq_printf(seq, "%p:\n"
  251. " krefs: %d\n"
  252. " sock: %pI4:%u -> "
  253. "%pI4:%u\n"
  254. " remote node: %s\n"
  255. " page off: %zu\n"
  256. " handshake ok: %u\n"
  257. " timer: %lu.%ld\n"
  258. " data ready: %lu.%ld\n"
  259. " advance start: %lu.%ld\n"
  260. " advance stop: %lu.%ld\n"
  261. " func start: %lu.%ld\n"
  262. " func stop: %lu.%ld\n"
  263. " func key: %u\n"
  264. " func type: %u\n",
  265. sc,
  266. atomic_read(&sc->sc_kref.refcount),
  267. &saddr, inet ? ntohs(sport) : 0,
  268. &daddr, inet ? ntohs(dport) : 0,
  269. sc->sc_node->nd_name,
  270. sc->sc_page_off,
  271. sc->sc_handshake_ok,
  272. TV_SEC_USEC(sc->sc_tv_timer),
  273. TV_SEC_USEC(sc->sc_tv_data_ready),
  274. TV_SEC_USEC(sc->sc_tv_advance_start),
  275. TV_SEC_USEC(sc->sc_tv_advance_stop),
  276. TV_SEC_USEC(sc->sc_tv_func_start),
  277. TV_SEC_USEC(sc->sc_tv_func_stop),
  278. sc->sc_msg_key,
  279. sc->sc_msg_type);
  280. }
  281. spin_unlock(&o2net_debug_lock);
  282. return 0;
  283. }
  284. static void sc_seq_stop(struct seq_file *seq, void *v)
  285. {
  286. }
  287. static const struct seq_operations sc_seq_ops = {
  288. .start = sc_seq_start,
  289. .next = sc_seq_next,
  290. .stop = sc_seq_stop,
  291. .show = sc_seq_show,
  292. };
  293. static int sc_fop_open(struct inode *inode, struct file *file)
  294. {
  295. struct o2net_sock_container *dummy_sc;
  296. struct seq_file *seq;
  297. int ret;
  298. dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
  299. if (dummy_sc == NULL) {
  300. ret = -ENOMEM;
  301. goto out;
  302. }
  303. dummy_sc->sc_page = NULL;
  304. ret = seq_open(file, &sc_seq_ops);
  305. if (ret)
  306. goto out;
  307. seq = file->private_data;
  308. seq->private = dummy_sc;
  309. o2net_debug_add_sc(dummy_sc);
  310. dummy_sc = NULL;
  311. out:
  312. kfree(dummy_sc);
  313. return ret;
  314. }
  315. static int sc_fop_release(struct inode *inode, struct file *file)
  316. {
  317. struct seq_file *seq = file->private_data;
  318. struct o2net_sock_container *dummy_sc = seq->private;
  319. o2net_debug_del_sc(dummy_sc);
  320. return seq_release_private(inode, file);
  321. }
  322. static const struct file_operations sc_seq_fops = {
  323. .open = sc_fop_open,
  324. .read = seq_read,
  325. .llseek = seq_lseek,
  326. .release = sc_fop_release,
  327. };
  328. int o2net_debugfs_init(void)
  329. {
  330. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  331. if (!o2net_dentry) {
  332. mlog_errno(-ENOMEM);
  333. goto bail;
  334. }
  335. nst_dentry = debugfs_create_file(NST_DEBUG_NAME, S_IFREG|S_IRUSR,
  336. o2net_dentry, NULL,
  337. &nst_seq_fops);
  338. if (!nst_dentry) {
  339. mlog_errno(-ENOMEM);
  340. goto bail;
  341. }
  342. sc_dentry = debugfs_create_file(SC_DEBUG_NAME, S_IFREG|S_IRUSR,
  343. o2net_dentry, NULL,
  344. &sc_seq_fops);
  345. if (!sc_dentry) {
  346. mlog_errno(-ENOMEM);
  347. goto bail;
  348. }
  349. return 0;
  350. bail:
  351. debugfs_remove(sc_dentry);
  352. debugfs_remove(nst_dentry);
  353. debugfs_remove(o2net_dentry);
  354. return -ENOMEM;
  355. }
  356. void o2net_debugfs_exit(void)
  357. {
  358. debugfs_remove(sc_dentry);
  359. debugfs_remove(nst_dentry);
  360. debugfs_remove(o2net_dentry);
  361. }
  362. #endif /* CONFIG_DEBUG_FS */