1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
7 * debug functionality for o2net
9 * Copyright (C) 2005, 2008 Oracle. All rights reserved.
12 #ifdef CONFIG_DEBUG_FS
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/slab.h>
17 #include <linux/idr.h>
18 #include <linux/kref.h>
19 #include <linux/seq_file.h>
20 #include <linux/debugfs.h>
22 #include <linux/uaccess.h>
25 #include "nodemanager.h"
26 #define MLOG_MASK_PREFIX ML_TCP
29 #include "tcp_internal.h"
31 #define O2NET_DEBUG_DIR "o2net"
32 #define SC_DEBUG_NAME "sock_containers"
33 #define NST_DEBUG_NAME "send_tracking"
34 #define STATS_DEBUG_NAME "stats"
35 #define NODES_DEBUG_NAME "connected_nodes"
37 #define SHOW_SOCK_CONTAINERS 0
38 #define SHOW_SOCK_STATS 1
40 static struct dentry *o2net_dentry;
41 static struct dentry *sc_dentry;
42 static struct dentry *nst_dentry;
43 static struct dentry *stats_dentry;
44 static struct dentry *nodes_dentry;
46 static DEFINE_SPINLOCK(o2net_debug_lock);
48 static LIST_HEAD(sock_containers);
49 static LIST_HEAD(send_tracking);
51 void o2net_debug_add_nst(struct o2net_send_tracking *nst)
53 spin_lock(&o2net_debug_lock);
54 list_add(&nst->st_net_debug_item, &send_tracking);
55 spin_unlock(&o2net_debug_lock);
58 void o2net_debug_del_nst(struct o2net_send_tracking *nst)
60 spin_lock(&o2net_debug_lock);
61 if (!list_empty(&nst->st_net_debug_item))
62 list_del_init(&nst->st_net_debug_item);
63 spin_unlock(&o2net_debug_lock);
66 static struct o2net_send_tracking
67 *next_nst(struct o2net_send_tracking *nst_start)
69 struct o2net_send_tracking *nst, *ret = NULL;
71 assert_spin_locked(&o2net_debug_lock);
73 list_for_each_entry(nst, &nst_start->st_net_debug_item,
75 /* discover the head of the list */
76 if (&nst->st_net_debug_item == &send_tracking)
79 /* use st_task to detect real nsts in the list */
80 if (nst->st_task != NULL) {
89 static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
91 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
93 spin_lock(&o2net_debug_lock);
94 nst = next_nst(dummy_nst);
95 spin_unlock(&o2net_debug_lock);
100 static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
102 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
104 spin_lock(&o2net_debug_lock);
105 nst = next_nst(dummy_nst);
106 list_del_init(&dummy_nst->st_net_debug_item);
108 list_add(&dummy_nst->st_net_debug_item,
109 &nst->st_net_debug_item);
110 spin_unlock(&o2net_debug_lock);
112 return nst; /* unused, just needs to be null when done */
115 static int nst_seq_show(struct seq_file *seq, void *v)
117 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
119 s64 sock, send, status;
121 spin_lock(&o2net_debug_lock);
122 nst = next_nst(dummy_nst);
127 sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
128 send = ktime_to_us(ktime_sub(now, nst->st_send_time));
129 status = ktime_to_us(ktime_sub(now, nst->st_status_time));
131 /* get_task_comm isn't exported. oh well. */
132 seq_printf(seq, "%p:\n"
135 " process name: %s\n"
139 " message type: %u\n"
140 " message key: 0x%08x\n"
141 " sock acquiry: %lld usecs ago\n"
142 " send start: %lld usecs ago\n"
143 " wait start: %lld usecs ago\n",
144 nst, (unsigned long)task_pid_nr(nst->st_task),
145 (unsigned long)nst->st_task->tgid,
146 nst->st_task->comm, nst->st_node,
147 nst->st_sc, nst->st_id, nst->st_msg_type,
154 spin_unlock(&o2net_debug_lock);
159 static void nst_seq_stop(struct seq_file *seq, void *v)
163 static const struct seq_operations nst_seq_ops = {
164 .start = nst_seq_start,
165 .next = nst_seq_next,
166 .stop = nst_seq_stop,
167 .show = nst_seq_show,
170 static int nst_fop_open(struct inode *inode, struct file *file)
172 struct o2net_send_tracking *dummy_nst;
174 dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
177 o2net_debug_add_nst(dummy_nst);
182 static int nst_fop_release(struct inode *inode, struct file *file)
184 struct seq_file *seq = file->private_data;
185 struct o2net_send_tracking *dummy_nst = seq->private;
187 o2net_debug_del_nst(dummy_nst);
188 return seq_release_private(inode, file);
191 static const struct file_operations nst_seq_fops = {
192 .open = nst_fop_open,
195 .release = nst_fop_release,
198 void o2net_debug_add_sc(struct o2net_sock_container *sc)
200 spin_lock(&o2net_debug_lock);
201 list_add(&sc->sc_net_debug_item, &sock_containers);
202 spin_unlock(&o2net_debug_lock);
205 void o2net_debug_del_sc(struct o2net_sock_container *sc)
207 spin_lock(&o2net_debug_lock);
208 list_del_init(&sc->sc_net_debug_item);
209 spin_unlock(&o2net_debug_lock);
212 struct o2net_sock_debug {
214 struct o2net_sock_container *dbg_sock;
217 static struct o2net_sock_container
218 *next_sc(struct o2net_sock_container *sc_start)
220 struct o2net_sock_container *sc, *ret = NULL;
222 assert_spin_locked(&o2net_debug_lock);
224 list_for_each_entry(sc, &sc_start->sc_net_debug_item,
226 /* discover the head of the list miscast as a sc */
227 if (&sc->sc_net_debug_item == &sock_containers)
230 /* use sc_page to detect real scs in the list */
231 if (sc->sc_page != NULL) {
240 static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
242 struct o2net_sock_debug *sd = seq->private;
243 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
245 spin_lock(&o2net_debug_lock);
246 sc = next_sc(dummy_sc);
247 spin_unlock(&o2net_debug_lock);
252 static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
254 struct o2net_sock_debug *sd = seq->private;
255 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
257 spin_lock(&o2net_debug_lock);
258 sc = next_sc(dummy_sc);
259 list_del_init(&dummy_sc->sc_net_debug_item);
261 list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
262 spin_unlock(&o2net_debug_lock);
264 return sc; /* unused, just needs to be null when done */
267 #ifdef CONFIG_OCFS2_FS_STATS
268 # define sc_send_count(_s) ((_s)->sc_send_count)
269 # define sc_recv_count(_s) ((_s)->sc_recv_count)
270 # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
271 # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
272 # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
273 # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
275 # define sc_send_count(_s) (0U)
276 # define sc_recv_count(_s) (0U)
277 # define sc_tv_acquiry_total_ns(_s) (0LL)
278 # define sc_tv_send_total_ns(_s) (0LL)
279 # define sc_tv_status_total_ns(_s) (0LL)
280 # define sc_tv_process_total_ns(_s) (0LL)
283 /* So that debugfs.ocfs2 can determine which format is being used */
284 #define O2NET_STATS_STR_VERSION 1
285 static void sc_show_sock_stats(struct seq_file *seq,
286 struct o2net_sock_container *sc)
291 seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
292 sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
293 (long long)sc_tv_acquiry_total_ns(sc),
294 (long long)sc_tv_send_total_ns(sc),
295 (long long)sc_tv_status_total_ns(sc),
296 (unsigned long)sc_recv_count(sc),
297 (long long)sc_tv_process_total_ns(sc));
300 static void sc_show_sock_container(struct seq_file *seq,
301 struct o2net_sock_container *sc)
303 struct inet_sock *inet = NULL;
304 __be32 saddr = 0, daddr = 0;
305 __be16 sport = 0, dport = 0;
311 inet = inet_sk(sc->sc_sock->sk);
312 /* the stack's structs aren't sparse endian clean */
313 saddr = (__force __be32)inet->inet_saddr;
314 daddr = (__force __be32)inet->inet_daddr;
315 sport = (__force __be16)inet->inet_sport;
316 dport = (__force __be16)inet->inet_dport;
319 /* XXX sigh, inet-> doesn't have sparse annotation so any
320 * use of it here generates a warning with -Wbitwise */
321 seq_printf(seq, "%p:\n"
327 " handshake ok: %u\n"
328 " timer: %lld usecs\n"
329 " data ready: %lld usecs\n"
330 " advance start: %lld usecs\n"
331 " advance stop: %lld usecs\n"
332 " func start: %lld usecs\n"
333 " func stop: %lld usecs\n"
334 " func key: 0x%08x\n"
337 kref_read(&sc->sc_kref),
338 &saddr, inet ? ntohs(sport) : 0,
339 &daddr, inet ? ntohs(dport) : 0,
340 sc->sc_node->nd_name,
343 (long long)ktime_to_us(sc->sc_tv_timer),
344 (long long)ktime_to_us(sc->sc_tv_data_ready),
345 (long long)ktime_to_us(sc->sc_tv_advance_start),
346 (long long)ktime_to_us(sc->sc_tv_advance_stop),
347 (long long)ktime_to_us(sc->sc_tv_func_start),
348 (long long)ktime_to_us(sc->sc_tv_func_stop),
353 static int sc_seq_show(struct seq_file *seq, void *v)
355 struct o2net_sock_debug *sd = seq->private;
356 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
358 spin_lock(&o2net_debug_lock);
359 sc = next_sc(dummy_sc);
362 if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
363 sc_show_sock_container(seq, sc);
365 sc_show_sock_stats(seq, sc);
368 spin_unlock(&o2net_debug_lock);
373 static void sc_seq_stop(struct seq_file *seq, void *v)
377 static const struct seq_operations sc_seq_ops = {
378 .start = sc_seq_start,
384 static int sc_common_open(struct file *file, int ctxt)
386 struct o2net_sock_debug *sd;
387 struct o2net_sock_container *dummy_sc;
389 dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
393 sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
400 sd->dbg_sock = dummy_sc;
402 o2net_debug_add_sc(dummy_sc);
407 static int sc_fop_release(struct inode *inode, struct file *file)
409 struct seq_file *seq = file->private_data;
410 struct o2net_sock_debug *sd = seq->private;
411 struct o2net_sock_container *dummy_sc = sd->dbg_sock;
413 o2net_debug_del_sc(dummy_sc);
415 return seq_release_private(inode, file);
418 static int stats_fop_open(struct inode *inode, struct file *file)
420 return sc_common_open(file, SHOW_SOCK_STATS);
423 static const struct file_operations stats_seq_fops = {
424 .open = stats_fop_open,
427 .release = sc_fop_release,
430 static int sc_fop_open(struct inode *inode, struct file *file)
432 return sc_common_open(file, SHOW_SOCK_CONTAINERS);
435 static const struct file_operations sc_seq_fops = {
439 .release = sc_fop_release,
442 static int o2net_fill_bitmap(char *buf, int len)
444 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
447 o2net_fill_node_map(map, sizeof(map));
449 while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
450 out += snprintf(buf + out, PAGE_SIZE - out, "%d ", i);
451 out += snprintf(buf + out, PAGE_SIZE - out, "\n");
456 static int nodes_fop_open(struct inode *inode, struct file *file)
460 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
464 i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
466 file->private_data = buf;
471 static int o2net_debug_release(struct inode *inode, struct file *file)
473 kfree(file->private_data);
477 static ssize_t o2net_debug_read(struct file *file, char __user *buf,
478 size_t nbytes, loff_t *ppos)
480 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
481 i_size_read(file->f_mapping->host));
484 static const struct file_operations nodes_fops = {
485 .open = nodes_fop_open,
486 .release = o2net_debug_release,
487 .read = o2net_debug_read,
488 .llseek = generic_file_llseek,
491 void o2net_debugfs_exit(void)
493 debugfs_remove(nodes_dentry);
494 debugfs_remove(stats_dentry);
495 debugfs_remove(sc_dentry);
496 debugfs_remove(nst_dentry);
497 debugfs_remove(o2net_dentry);
500 int o2net_debugfs_init(void)
502 umode_t mode = S_IFREG|S_IRUSR;
504 o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
506 nst_dentry = debugfs_create_file(NST_DEBUG_NAME, mode,
507 o2net_dentry, NULL, &nst_seq_fops);
509 sc_dentry = debugfs_create_file(SC_DEBUG_NAME, mode,
510 o2net_dentry, NULL, &sc_seq_fops);
512 stats_dentry = debugfs_create_file(STATS_DEBUG_NAME, mode,
513 o2net_dentry, NULL, &stats_seq_fops);
515 nodes_dentry = debugfs_create_file(NODES_DEBUG_NAME, mode,
516 o2net_dentry, NULL, &nodes_fops);
520 o2net_debugfs_exit();
525 #endif /* CONFIG_DEBUG_FS */