netdebug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. struct seq_file *seq;
  157. int ret;
  158. dummy_nst = kmalloc(sizeof(struct o2net_send_tracking), GFP_KERNEL);
  159. if (dummy_nst == NULL) {
  160. ret = -ENOMEM;
  161. goto out;
  162. }
  163. dummy_nst->st_task = NULL;
  164. ret = seq_open(file, &nst_seq_ops);
  165. if (ret)
  166. goto out;
  167. seq = file->private_data;
  168. seq->private = dummy_nst;
  169. o2net_debug_add_nst(dummy_nst);
  170. dummy_nst = NULL;
  171. out:
  172. kfree(dummy_nst);
  173. return ret;
  174. }
  175. static int nst_fop_release(struct inode *inode, struct file *file)
  176. {
  177. struct seq_file *seq = file->private_data;
  178. struct o2net_send_tracking *dummy_nst = seq->private;
  179. o2net_debug_del_nst(dummy_nst);
  180. return seq_release_private(inode, file);
  181. }
  182. static const struct file_operations nst_seq_fops = {
  183. .open = nst_fop_open,
  184. .read = seq_read,
  185. .llseek = seq_lseek,
  186. .release = nst_fop_release,
  187. };
  188. void o2net_debug_add_sc(struct o2net_sock_container *sc)
  189. {
  190. spin_lock(&o2net_debug_lock);
  191. list_add(&sc->sc_net_debug_item, &sock_containers);
  192. spin_unlock(&o2net_debug_lock);
  193. }
  194. void o2net_debug_del_sc(struct o2net_sock_container *sc)
  195. {
  196. spin_lock(&o2net_debug_lock);
  197. list_del_init(&sc->sc_net_debug_item);
  198. spin_unlock(&o2net_debug_lock);
  199. }
  200. struct o2net_sock_debug {
  201. int dbg_ctxt;
  202. struct o2net_sock_container *dbg_sock;
  203. };
  204. static struct o2net_sock_container
  205. *next_sc(struct o2net_sock_container *sc_start)
  206. {
  207. struct o2net_sock_container *sc, *ret = NULL;
  208. assert_spin_locked(&o2net_debug_lock);
  209. list_for_each_entry(sc, &sc_start->sc_net_debug_item,
  210. sc_net_debug_item) {
  211. /* discover the head of the list miscast as a sc */
  212. if (&sc->sc_net_debug_item == &sock_containers)
  213. break;
  214. /* use sc_page to detect real scs in the list */
  215. if (sc->sc_page != NULL) {
  216. ret = sc;
  217. break;
  218. }
  219. }
  220. return ret;
  221. }
  222. static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
  223. {
  224. struct o2net_sock_debug *sd = seq->private;
  225. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  226. spin_lock(&o2net_debug_lock);
  227. sc = next_sc(dummy_sc);
  228. spin_unlock(&o2net_debug_lock);
  229. return sc;
  230. }
  231. static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  232. {
  233. struct o2net_sock_debug *sd = seq->private;
  234. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  235. spin_lock(&o2net_debug_lock);
  236. sc = next_sc(dummy_sc);
  237. list_del_init(&dummy_sc->sc_net_debug_item);
  238. if (sc)
  239. list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
  240. spin_unlock(&o2net_debug_lock);
  241. return sc; /* unused, just needs to be null when done */
  242. }
  243. #ifdef CONFIG_OCFS2_FS_STATS
  244. # define sc_send_count(_s) ((_s)->sc_send_count)
  245. # define sc_recv_count(_s) ((_s)->sc_recv_count)
  246. # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
  247. # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
  248. # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
  249. # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
  250. #else
  251. # define sc_send_count(_s) (0U)
  252. # define sc_recv_count(_s) (0U)
  253. # define sc_tv_acquiry_total_ns(_s) (0LL)
  254. # define sc_tv_send_total_ns(_s) (0LL)
  255. # define sc_tv_status_total_ns(_s) (0LL)
  256. # define sc_tv_process_total_ns(_s) (0LL)
  257. #endif
  258. /* So that debugfs.ocfs2 can determine which format is being used */
  259. #define O2NET_STATS_STR_VERSION 1
  260. static void sc_show_sock_stats(struct seq_file *seq,
  261. struct o2net_sock_container *sc)
  262. {
  263. if (!sc)
  264. return;
  265. seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
  266. sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
  267. (long long)sc_tv_acquiry_total_ns(sc),
  268. (long long)sc_tv_send_total_ns(sc),
  269. (long long)sc_tv_status_total_ns(sc),
  270. (unsigned long)sc_recv_count(sc),
  271. (long long)sc_tv_process_total_ns(sc));
  272. }
  273. static void sc_show_sock_container(struct seq_file *seq,
  274. struct o2net_sock_container *sc)
  275. {
  276. struct inet_sock *inet = NULL;
  277. __be32 saddr = 0, daddr = 0;
  278. __be16 sport = 0, dport = 0;
  279. if (!sc)
  280. return;
  281. if (sc->sc_sock) {
  282. inet = inet_sk(sc->sc_sock->sk);
  283. /* the stack's structs aren't sparse endian clean */
  284. saddr = (__force __be32)inet->inet_saddr;
  285. daddr = (__force __be32)inet->inet_daddr;
  286. sport = (__force __be16)inet->inet_sport;
  287. dport = (__force __be16)inet->inet_dport;
  288. }
  289. /* XXX sigh, inet-> doesn't have sparse annotation so any
  290. * use of it here generates a warning with -Wbitwise */
  291. seq_printf(seq, "%p:\n"
  292. " krefs: %d\n"
  293. " sock: %pI4:%u -> "
  294. "%pI4:%u\n"
  295. " remote node: %s\n"
  296. " page off: %zu\n"
  297. " handshake ok: %u\n"
  298. " timer: %lld usecs\n"
  299. " data ready: %lld usecs\n"
  300. " advance start: %lld usecs\n"
  301. " advance stop: %lld usecs\n"
  302. " func start: %lld usecs\n"
  303. " func stop: %lld usecs\n"
  304. " func key: 0x%08x\n"
  305. " func type: %u\n",
  306. sc,
  307. atomic_read(&sc->sc_kref.refcount),
  308. &saddr, inet ? ntohs(sport) : 0,
  309. &daddr, inet ? ntohs(dport) : 0,
  310. sc->sc_node->nd_name,
  311. sc->sc_page_off,
  312. sc->sc_handshake_ok,
  313. (long long)ktime_to_us(sc->sc_tv_timer),
  314. (long long)ktime_to_us(sc->sc_tv_data_ready),
  315. (long long)ktime_to_us(sc->sc_tv_advance_start),
  316. (long long)ktime_to_us(sc->sc_tv_advance_stop),
  317. (long long)ktime_to_us(sc->sc_tv_func_start),
  318. (long long)ktime_to_us(sc->sc_tv_func_stop),
  319. sc->sc_msg_key,
  320. sc->sc_msg_type);
  321. }
  322. static int sc_seq_show(struct seq_file *seq, void *v)
  323. {
  324. struct o2net_sock_debug *sd = seq->private;
  325. struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
  326. spin_lock(&o2net_debug_lock);
  327. sc = next_sc(dummy_sc);
  328. if (sc) {
  329. if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
  330. sc_show_sock_container(seq, sc);
  331. else
  332. sc_show_sock_stats(seq, sc);
  333. }
  334. spin_unlock(&o2net_debug_lock);
  335. return 0;
  336. }
  337. static void sc_seq_stop(struct seq_file *seq, void *v)
  338. {
  339. }
  340. static const struct seq_operations sc_seq_ops = {
  341. .start = sc_seq_start,
  342. .next = sc_seq_next,
  343. .stop = sc_seq_stop,
  344. .show = sc_seq_show,
  345. };
  346. static int sc_common_open(struct file *file, struct o2net_sock_debug *sd)
  347. {
  348. struct o2net_sock_container *dummy_sc;
  349. struct seq_file *seq;
  350. int ret;
  351. dummy_sc = kmalloc(sizeof(struct o2net_sock_container), GFP_KERNEL);
  352. if (dummy_sc == NULL) {
  353. ret = -ENOMEM;
  354. goto out;
  355. }
  356. dummy_sc->sc_page = NULL;
  357. ret = seq_open(file, &sc_seq_ops);
  358. if (ret)
  359. goto out;
  360. seq = file->private_data;
  361. seq->private = sd;
  362. sd->dbg_sock = dummy_sc;
  363. o2net_debug_add_sc(dummy_sc);
  364. dummy_sc = NULL;
  365. out:
  366. kfree(dummy_sc);
  367. return ret;
  368. }
  369. static int sc_fop_release(struct inode *inode, struct file *file)
  370. {
  371. struct seq_file *seq = file->private_data;
  372. struct o2net_sock_debug *sd = seq->private;
  373. struct o2net_sock_container *dummy_sc = sd->dbg_sock;
  374. o2net_debug_del_sc(dummy_sc);
  375. return seq_release_private(inode, file);
  376. }
  377. static int stats_fop_open(struct inode *inode, struct file *file)
  378. {
  379. struct o2net_sock_debug *sd;
  380. sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
  381. if (sd == NULL)
  382. return -ENOMEM;
  383. sd->dbg_ctxt = SHOW_SOCK_STATS;
  384. sd->dbg_sock = NULL;
  385. return sc_common_open(file, sd);
  386. }
  387. static const struct file_operations stats_seq_fops = {
  388. .open = stats_fop_open,
  389. .read = seq_read,
  390. .llseek = seq_lseek,
  391. .release = sc_fop_release,
  392. };
  393. static int sc_fop_open(struct inode *inode, struct file *file)
  394. {
  395. struct o2net_sock_debug *sd;
  396. sd = kmalloc(sizeof(struct o2net_sock_debug), GFP_KERNEL);
  397. if (sd == NULL)
  398. return -ENOMEM;
  399. sd->dbg_ctxt = SHOW_SOCK_CONTAINERS;
  400. sd->dbg_sock = NULL;
  401. return sc_common_open(file, sd);
  402. }
  403. static const struct file_operations sc_seq_fops = {
  404. .open = sc_fop_open,
  405. .read = seq_read,
  406. .llseek = seq_lseek,
  407. .release = sc_fop_release,
  408. };
  409. static int o2net_fill_bitmap(char *buf, int len)
  410. {
  411. unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
  412. int i = -1, out = 0;
  413. o2net_fill_node_map(map, sizeof(map));
  414. while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
  415. out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
  416. out += snprintf(buf + out, PAGE_SIZE - out, "\n");
  417. return out;
  418. }
  419. static int nodes_fop_open(struct inode *inode, struct file *file)
  420. {
  421. char *buf;
  422. buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  423. if (!buf)
  424. return -ENOMEM;
  425. i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
  426. file->private_data = buf;
  427. return 0;
  428. }
  429. static int o2net_debug_release(struct inode *inode, struct file *file)
  430. {
  431. kfree(file->private_data);
  432. return 0;
  433. }
  434. static ssize_t o2net_debug_read(struct file *file, char __user *buf,
  435. size_t nbytes, loff_t *ppos)
  436. {
  437. return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
  438. i_size_read(file->f_mapping->host));
  439. }
  440. static const struct file_operations nodes_fops = {
  441. .open = nodes_fop_open,
  442. .release = o2net_debug_release,
  443. .read = o2net_debug_read,
  444. .llseek = generic_file_llseek,
  445. };
  446. void o2net_debugfs_exit(void)
  447. {
  448. debugfs_remove(nodes_dentry);
  449. debugfs_remove(stats_dentry);
  450. debugfs_remove(sc_dentry);
  451. debugfs_remove(nst_dentry);
  452. debugfs_remove(o2net_dentry);
  453. }
  454. int o2net_debugfs_init(void)
  455. {
  456. umode_t mode = S_IFREG|S_IRUSR;
  457. o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
  458. if (o2net_dentry)
  459. nst_dentry = debugfs_create_file(NST_DEBUG_NAME, mode,
  460. o2net_dentry, NULL, &nst_seq_fops);
  461. if (nst_dentry)
  462. sc_dentry = debugfs_create_file(SC_DEBUG_NAME, mode,
  463. o2net_dentry, NULL, &sc_seq_fops);
  464. if (sc_dentry)
  465. stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, mode,
  466. o2net_dentry, NULL, &stats_seq_fops);
  467. if (stats_dentry)
  468. nodes_dentry = debugfs_create_file(NODES_DEBUG_NAME, mode,
  469. o2net_dentry, NULL, &nodes_fops);
  470. if (nodes_dentry)
  471. return 0;
  472. o2net_debugfs_exit();
  473. mlog_errno(-ENOMEM);
  474. return -ENOMEM;
  475. }
  476. #endif /* CONFIG_DEBUG_FS */