netdebug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. #define STATS_DEBUG_NAME "stats"
  44. #define SHOW_SOCK_CONTAINERS 0
  45. #define SHOW_SOCK_STATS 1
  46. static struct dentry *o2net_dentry;
  47. static struct dentry *sc_dentry;
  48. static struct dentry *nst_dentry;
  49. static struct dentry *stats_dentry;
  50. static DEFINE_SPINLOCK(o2net_debug_lock);
  51. static LIST_HEAD(sock_containers);
  52. static LIST_HEAD(send_tracking);
  53. void o2net_debug_add_nst(struct o2net_send_tracking *nst)
  54. {
  55. spin_lock(&o2net_debug_lock);
  56. list_add(&nst->st_net_debug_item, &send_tracking);
  57. spin_unlock(&o2net_debug_lock);
  58. }
  59. void o2net_debug_del_nst(struct o2net_send_tracking *nst)
  60. {
  61. spin_lock(&o2net_debug_lock);
  62. if (!list_empty(&nst->st_net_debug_item))
  63. list_del_init(&nst->st_net_debug_item);
  64. spin_unlock(&o2net_debug_lock);
  65. }
  66. static struct o2net_send_tracking
  67. *next_nst(struct o2net_send_tracking *nst_start)
  68. {
  69. struct o2net_send_tracking *nst, *ret = NULL;
  70. assert_spin_locked(&o2net_debug_lock);
  71. list_for_each_entry(nst, &nst_start->st_net_debug_item,
  72. st_net_debug_item) {
  73. /* discover the head of the list */
  74. if (&nst->st_net_debug_item == &send_tracking)
  75. break;
  76. /* use st_task to detect real nsts in the list */
  77. if (nst->st_task != NULL) {
  78. ret = nst;
  79. break;
  80. }
  81. }
  82. return ret;
  83. }
  84. static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
  85. {
  86. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  87. spin_lock(&o2net_debug_lock);
  88. nst = next_nst(dummy_nst);
  89. spin_unlock(&o2net_debug_lock);
  90. return nst;
  91. }
  92. static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  93. {
  94. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  95. spin_lock(&o2net_debug_lock);
  96. nst = next_nst(dummy_nst);
  97. list_del_init(&dummy_nst->st_net_debug_item);
  98. if (nst)
  99. list_add(&dummy_nst->st_net_debug_item,
  100. &nst->st_net_debug_item);
  101. spin_unlock(&o2net_debug_lock);
  102. return nst; /* unused, just needs to be null when done */
  103. }
  104. static int nst_seq_show(struct seq_file *seq, void *v)
  105. {
  106. struct o2net_send_tracking *nst, *dummy_nst = seq->private;
  107. ktime_t now;
  108. s64 sock, send, status;
  109. spin_lock(&o2net_debug_lock);
  110. nst = next_nst(dummy_nst);
  111. if (!nst)
  112. goto out;
  113. now = ktime_get();
  114. sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
  115. send = ktime_to_us(ktime_sub(now, nst->st_send_time));
  116. status = ktime_to_us(ktime_sub(now, nst->st_status_time));
  117. /* get_task_comm isn't exported. oh well. */
  118. seq_printf(seq, "%p:\n"
  119. " pid: %lu\n"
  120. " tgid: %lu\n"
  121. " process name: %s\n"
  122. " node: %u\n"
  123. " sc: %p\n"
  124. " message id: %d\n"
  125. " message type: %u\n"
  126. " message key: 0x%08x\n"
  127. " sock acquiry: %lld usecs ago\n"
  128. " send start: %lld usecs ago\n"
  129. " wait start: %lld usecs ago\n",
  130. nst, (unsigned long)task_pid_nr(nst->st_task),
  131. (unsigned long)nst->st_task->tgid,
  132. nst->st_task->comm, nst->st_node,
  133. nst->st_sc, nst->st_id, nst->st_msg_type,
  134. nst->st_msg_key,
  135. (long long)sock,
  136. (long long)send,
  137. (long long)status);
  138. out:
  139. spin_unlock(&o2net_debug_lock);
  140. return 0;
  141. }
  142. static void nst_seq_stop(struct seq_file *seq, void *v)
  143. {
  144. }
  145. static const struct seq_operations nst_seq_ops = {
  146. .start = nst_seq_start,
  147. .next = nst_seq_next,
  148. .stop = nst_seq_stop,
  149. .show = nst_seq_show,
  150. };
  151. static int nst_fop_open(struct inode *inode, struct file *file)
  152. {
  153. struct o2net_send_tracking *dummy_nst;
  154. struct seq_file *seq;
  155. int ret;
  156. dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
  157. if (dummy_nst == NULL) {
  158. ret = -ENOMEM;
  159. goto out;
  160. }
  161. dummy_nst->st_task = NULL;
  162. ret = seq_open(file, &nst_seq_ops);
  163. if (ret)
  164. goto out;
  165. seq = file->private_data;
  166. seq->private = dummy_nst;
  167. o2net_debug_add_nst(dummy_nst);
  168. dummy_nst = NULL;
  169. out:
  170. kfree(dummy_nst);
  171. return ret;
  172. }
  173. static int nst_fop_release(struct inode *inode, struct file *file)
  174. {
  175. struct seq_file *seq = file->private_data;
  176. struct o2net_send_tracking *dummy_nst = seq->private;
  177. o2net_debug_del_nst(dummy_nst);
  178. return seq_release_private(inode, file);
  179. }
  180. static const struct file_operations nst_seq_fops = {
  181. .open = nst_fop_open,
  182. .read = seq_read,
  183. .llseek = seq_lseek,
  184. .release = nst_fop_release,
  185. };
  186. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  187. {
  188. spin_lock(&o2net_debug_lock);
  189. list_add(&sc->sc_net_debug_item, &sock_containers);
  190. spin_unlock(&o2net_debug_lock);
  191. }
  192. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  193. {
  194. spin_lock(&o2net_debug_lock);
  195. list_del_init(&sc->sc_net_debug_item);
  196. spin_unlock(&o2net_debug_lock);
  197. }
  198. struct o2net_sock_debug {
  199. int dbg_ctxt;
  200. struct o2net_sock_container *dbg_sock;
  201. };
  202. static struct o2net_sock_container
  203. *next_sc(struct o2net_sock_container *sc_start)
  204. {
  205. struct o2net_sock_container *sc, *ret = NULL;
  206. assert_spin_locked(&o2net_debug_lock);
  207. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  208. sc_net_debug_item) {
  209. /* discover the head of the list miscast as a sc */
  210. if (&sc->sc_net_debug_item == &sock_containers)
  211. break;
  212. /* use sc_page to detect real scs in the list */
  213. if (sc->sc_page != NULL) {
  214. ret = sc;
  215. break;
  216. }
  217. }
  218. return ret;
  219. }
  220. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  221. {
  222. struct o2net_sock_debug *sd = seq->private;
  223. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  224. spin_lock(&o2net_debug_lock);
  225. sc = next_sc(dummy_sc);
  226. spin_unlock(&o2net_debug_lock);
  227. return sc;
  228. }
  229. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  230. {
  231. struct o2net_sock_debug *sd = seq->private;
  232. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  233. spin_lock(&o2net_debug_lock);
  234. sc = next_sc(dummy_sc);
  235. list_del_init(&dummy_sc->sc_net_debug_item);
  236. if (sc)
  237. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  238. spin_unlock(&o2net_debug_lock);
  239. return sc; /* unused, just needs to be null when done */
  240. }
  241. #ifdef CONFIG_OCFS2_FS_STATS
  242. # define sc_send_count(_s) ((_s)->sc_send_count)
  243. # define sc_recv_count(_s) ((_s)->sc_recv_count)
  244. # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
  245. # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
  246. # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
  247. # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
  248. #else
  249. # define sc_send_count(_s) (0U)
  250. # define sc_recv_count(_s) (0U)
  251. # define sc_tv_acquiry_total_ns(_s) (0LL)
  252. # define sc_tv_send_total_ns(_s) (0LL)
  253. # define sc_tv_status_total_ns(_s) (0LL)
  254. # define sc_tv_process_total_ns(_s) (0LL)
  255. #endif
  256. /* So that debugfs.ocfs2 can determine which format is being used */
  257. #define O2NET_STATS_STR_VERSION 1
  258. static void sc_show_sock_stats(struct seq_file *seq,
  259. struct o2net_sock_container *sc)
  260. {
  261. if (!sc)
  262. return;
  263. seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
  264. sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
  265. (long long)sc_tv_acquiry_total_ns(sc),
  266. (long long)sc_tv_send_total_ns(sc),
  267. (long long)sc_tv_status_total_ns(sc),
  268. (unsigned long)sc_recv_count(sc),
  269. (long long)sc_tv_process_total_ns(sc));
  270. }
  271. static void sc_show_sock_container(struct seq_file *seq,
  272. struct o2net_sock_container *sc)
  273. {
  274. struct inet_sock *inet = NULL;
  275. __be32 saddr = 0, daddr = 0;
  276. __be16 sport = 0, dport = 0;
  277. if (!sc)
  278. return;
  279. if (sc->sc_sock) {
  280. inet = inet_sk(sc->sc_sock->sk);
  281. /* the stack's structs aren't sparse endian clean */
  282. saddr = (__force __be32)inet->inet_saddr;
  283. daddr = (__force __be32)inet->inet_daddr;
  284. sport = (__force __be16)inet->inet_sport;
  285. dport = (__force __be16)inet->inet_dport;
  286. }
  287. /* XXX sigh, inet-> doesn't have sparse annotation so any
  288. * use of it here generates a warning with -Wbitwise */
  289. seq_printf(seq, "%p:\n"
  290. " krefs: %d\n"
  291. " sock: %pI4:%u -> "
  292. "%pI4:%u\n"
  293. " remote node: %s\n"
  294. " page off: %zu\n"
  295. " handshake ok: %u\n"
  296. " timer: %lld usecs\n"
  297. " data ready: %lld usecs\n"
  298. " advance start: %lld usecs\n"
  299. " advance stop: %lld usecs\n"
  300. " func start: %lld usecs\n"
  301. " func stop: %lld usecs\n"
  302. " func key: 0x%08x\n"
  303. " func type: %u\n",
  304. sc,
  305. atomic_read(&sc->sc_kref.refcount),
  306. &saddr, inet ? ntohs(sport) : 0,
  307. &daddr, inet ? ntohs(dport) : 0,
  308. sc->sc_node->nd_name,
  309. sc->sc_page_off,
  310. sc->sc_handshake_ok,
  311. (long long)ktime_to_us(sc->sc_tv_timer),
  312. (long long)ktime_to_us(sc->sc_tv_data_ready),
  313. (long long)ktime_to_us(sc->sc_tv_advance_start),
  314. (long long)ktime_to_us(sc->sc_tv_advance_stop),
  315. (long long)ktime_to_us(sc->sc_tv_func_start),
  316. (long long)ktime_to_us(sc->sc_tv_func_stop),
  317. sc->sc_msg_key,
  318. sc->sc_msg_type);
  319. }
  320. static int sc_seq_show(struct seq_file *seq, void *v)
  321. {
  322. struct o2net_sock_debug *sd = seq->private;
  323. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  324. spin_lock(&o2net_debug_lock);
  325. sc = next_sc(dummy_sc);
  326. if (sc) {
  327. if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
  328. sc_show_sock_container(seq, sc);
  329. else
  330. sc_show_sock_stats(seq, sc);
  331. }
  332. spin_unlock(&o2net_debug_lock);
  333. return 0;
  334. }
  335. static void sc_seq_stop(struct seq_file *seq, void *v)
  336. {
  337. }
  338. static const struct seq_operations sc_seq_ops = {
  339. .start = sc_seq_start,
  340. .next = sc_seq_next,
  341. .stop = sc_seq_stop,
  342. .show = sc_seq_show,
  343. };
  344. static int sc_common_open(struct file *file, struct o2net_sock_debug *sd)
  345. {
  346. struct o2net_sock_container *dummy_sc;
  347. struct seq_file *seq;
  348. int ret;
  349. dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
  350. if (dummy_sc == NULL) {
  351. ret = -ENOMEM;
  352. goto out;
  353. }
  354. dummy_sc->sc_page = NULL;
  355. ret = seq_open(file, &sc_seq_ops);
  356. if (ret)
  357. goto out;
  358. seq = file->private_data;
  359. seq->private = sd;
  360. sd->dbg_sock = dummy_sc;
  361. o2net_debug_add_sc(dummy_sc);
  362. dummy_sc = NULL;
  363. out:
  364. kfree(dummy_sc);
  365. return ret;
  366. }
  367. static int sc_fop_release(struct inode *inode, struct file *file)
  368. {
  369. struct seq_file *seq = file->private_data;
  370. struct o2net_sock_debug *sd = seq->private;
  371. struct o2net_sock_container *dummy_sc = sd->dbg_sock;
  372. o2net_debug_del_sc(dummy_sc);
  373. return seq_release_private(inode, file);
  374. }
  375. static int stats_fop_open(struct inode *inode, struct file *file)
  376. {
  377. struct o2net_sock_debug *sd;
  378. sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
  379. if (sd == NULL)
  380. return -ENOMEM;
  381. sd->dbg_ctxt = SHOW_SOCK_STATS;
  382. sd->dbg_sock = NULL;
  383. return sc_common_open(file, sd);
  384. }
  385. static const struct file_operations stats_seq_fops = {
  386. .open = stats_fop_open,
  387. .read = seq_read,
  388. .llseek = seq_lseek,
  389. .release = sc_fop_release,
  390. };
  391. static int sc_fop_open(struct inode *inode, struct file *file)
  392. {
  393. struct o2net_sock_debug *sd;
  394. sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
  395. if (sd == NULL)
  396. return -ENOMEM;
  397. sd->dbg_ctxt = SHOW_SOCK_CONTAINERS;
  398. sd->dbg_sock = NULL;
  399. return sc_common_open(file, sd);
  400. }
  401. static const struct file_operations sc_seq_fops = {
  402. .open = sc_fop_open,
  403. .read = seq_read,
  404. .llseek = seq_lseek,
  405. .release = sc_fop_release,
  406. };
  407. int o2net_debugfs_init(void)
  408. {
  409. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  410. if (!o2net_dentry) {
  411. mlog_errno(-ENOMEM);
  412. goto bail;
  413. }
  414. nst_dentry = debugfs_create_file(NST_DEBUG_NAME, S_IFREG|S_IRUSR,
  415. o2net_dentry, NULL,
  416. &nst_seq_fops);
  417. if (!nst_dentry) {
  418. mlog_errno(-ENOMEM);
  419. goto bail;
  420. }
  421. sc_dentry = debugfs_create_file(SC_DEBUG_NAME, S_IFREG|S_IRUSR,
  422. o2net_dentry, NULL,
  423. &sc_seq_fops);
  424. if (!sc_dentry) {
  425. mlog_errno(-ENOMEM);
  426. goto bail;
  427. }
  428. stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, S_IFREG|S_IRUSR,
  429. o2net_dentry, NULL,
  430. &stats_seq_fops);
  431. if (!stats_dentry) {
  432. mlog_errno(-ENOMEM);
  433. goto bail;
  434. }
  435. return 0;
  436. bail:
  437. debugfs_remove(stats_dentry);
  438. debugfs_remove(sc_dentry);
  439. debugfs_remove(nst_dentry);
  440. debugfs_remove(o2net_dentry);
  441. return -ENOMEM;
  442. }
  443. void o2net_debugfs_exit(void)
  444. {
  445. debugfs_remove(stats_dentry);
  446. debugfs_remove(sc_dentry);
  447. debugfs_remove(nst_dentry);
  448. debugfs_remove(o2net_dentry);
  449. }
  450. #endif /* CONFIG_DEBUG_FS */