netdebug.c 14 KB

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