1 // SPDX-License-Identifier: GPL-2.0-or-later
5 * debug functionality for o2net
7 * Copyright (C) 2005, 2008 Oracle. All rights reserved.
10 #ifdef CONFIG_DEBUG_FS
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/idr.h>
16 #include <linux/kref.h>
17 #include <linux/seq_file.h>
18 #include <linux/debugfs.h>
20 #include <linux/uaccess.h>
23 #include "nodemanager.h"
24 #define MLOG_MASK_PREFIX ML_TCP
27 #include "tcp_internal.h"
29 #define O2NET_DEBUG_DIR "o2net"
30 #define SC_DEBUG_NAME "sock_containers"
31 #define NST_DEBUG_NAME "send_tracking"
32 #define STATS_DEBUG_NAME "stats"
33 #define NODES_DEBUG_NAME "connected_nodes"
35 #define SHOW_SOCK_CONTAINERS 0
36 #define SHOW_SOCK_STATS 1
38 static struct dentry *o2net_dentry;
40 static DEFINE_SPINLOCK(o2net_debug_lock);
42 static LIST_HEAD(sock_containers);
43 static LIST_HEAD(send_tracking);
45 void o2net_debug_add_nst(struct o2net_send_tracking *nst)
47 spin_lock(&o2net_debug_lock);
48 list_add(&nst->st_net_debug_item, &send_tracking);
49 spin_unlock(&o2net_debug_lock);
52 void o2net_debug_del_nst(struct o2net_send_tracking *nst)
54 spin_lock(&o2net_debug_lock);
55 if (!list_empty(&nst->st_net_debug_item))
56 list_del_init(&nst->st_net_debug_item);
57 spin_unlock(&o2net_debug_lock);
60 static struct o2net_send_tracking
61 *next_nst(struct o2net_send_tracking *nst_start)
63 struct o2net_send_tracking *nst, *ret = NULL;
65 assert_spin_locked(&o2net_debug_lock);
67 list_for_each_entry(nst, &nst_start->st_net_debug_item,
69 /* discover the head of the list */
70 if (&nst->st_net_debug_item == &send_tracking)
73 /* use st_task to detect real nsts in the list */
74 if (nst->st_task != NULL) {
83 static void *nst_seq_start(struct seq_file *seq, loff_t *pos)
85 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);
94 static void *nst_seq_next(struct seq_file *seq, void *v, loff_t *pos)
96 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
98 spin_lock(&o2net_debug_lock);
99 nst = next_nst(dummy_nst);
100 list_del_init(&dummy_nst->st_net_debug_item);
102 list_add(&dummy_nst->st_net_debug_item,
103 &nst->st_net_debug_item);
104 spin_unlock(&o2net_debug_lock);
106 return nst; /* unused, just needs to be null when done */
109 static int nst_seq_show(struct seq_file *seq, void *v)
111 struct o2net_send_tracking *nst, *dummy_nst = seq->private;
113 s64 sock, send, status;
115 spin_lock(&o2net_debug_lock);
116 nst = next_nst(dummy_nst);
121 sock = ktime_to_us(ktime_sub(now, nst->st_sock_time));
122 send = ktime_to_us(ktime_sub(now, nst->st_send_time));
123 status = ktime_to_us(ktime_sub(now, nst->st_status_time));
125 /* get_task_comm isn't exported. oh well. */
126 seq_printf(seq, "%p:\n"
129 " process name: %s\n"
133 " message type: %u\n"
134 " message key: 0x%08x\n"
135 " sock acquiry: %lld usecs ago\n"
136 " send start: %lld usecs ago\n"
137 " wait start: %lld usecs ago\n",
138 nst, (unsigned long)task_pid_nr(nst->st_task),
139 (unsigned long)nst->st_task->tgid,
140 nst->st_task->comm, nst->st_node,
141 nst->st_sc, nst->st_id, nst->st_msg_type,
148 spin_unlock(&o2net_debug_lock);
153 static void nst_seq_stop(struct seq_file *seq, void *v)
157 static const struct seq_operations nst_seq_ops = {
158 .start = nst_seq_start,
159 .next = nst_seq_next,
160 .stop = nst_seq_stop,
161 .show = nst_seq_show,
164 static int nst_fop_open(struct inode *inode, struct file *file)
166 struct o2net_send_tracking *dummy_nst;
168 dummy_nst = __seq_open_private(file, &nst_seq_ops, sizeof(*dummy_nst));
171 o2net_debug_add_nst(dummy_nst);
176 static int nst_fop_release(struct inode *inode, struct file *file)
178 struct seq_file *seq = file->private_data;
179 struct o2net_send_tracking *dummy_nst = seq->private;
181 o2net_debug_del_nst(dummy_nst);
182 return seq_release_private(inode, file);
185 static const struct file_operations nst_seq_fops = {
186 .open = nst_fop_open,
189 .release = nst_fop_release,
192 void o2net_debug_add_sc(struct o2net_sock_container *sc)
194 spin_lock(&o2net_debug_lock);
195 list_add(&sc->sc_net_debug_item, &sock_containers);
196 spin_unlock(&o2net_debug_lock);
199 void o2net_debug_del_sc(struct o2net_sock_container *sc)
201 spin_lock(&o2net_debug_lock);
202 list_del_init(&sc->sc_net_debug_item);
203 spin_unlock(&o2net_debug_lock);
206 struct o2net_sock_debug {
208 struct o2net_sock_container *dbg_sock;
211 static struct o2net_sock_container
212 *next_sc(struct o2net_sock_container *sc_start)
214 struct o2net_sock_container *sc, *ret = NULL;
216 assert_spin_locked(&o2net_debug_lock);
218 list_for_each_entry(sc, &sc_start->sc_net_debug_item,
220 /* discover the head of the list miscast as a sc */
221 if (&sc->sc_net_debug_item == &sock_containers)
224 /* use sc_page to detect real scs in the list */
225 if (sc->sc_page != NULL) {
234 static void *sc_seq_start(struct seq_file *seq, loff_t *pos)
236 struct o2net_sock_debug *sd = seq->private;
237 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
239 spin_lock(&o2net_debug_lock);
240 sc = next_sc(dummy_sc);
241 spin_unlock(&o2net_debug_lock);
246 static void *sc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
248 struct o2net_sock_debug *sd = seq->private;
249 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
251 spin_lock(&o2net_debug_lock);
252 sc = next_sc(dummy_sc);
253 list_del_init(&dummy_sc->sc_net_debug_item);
255 list_add(&dummy_sc->sc_net_debug_item, &sc->sc_net_debug_item);
256 spin_unlock(&o2net_debug_lock);
258 return sc; /* unused, just needs to be null when done */
261 #ifdef CONFIG_OCFS2_FS_STATS
262 # define sc_send_count(_s) ((_s)->sc_send_count)
263 # define sc_recv_count(_s) ((_s)->sc_recv_count)
264 # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
265 # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
266 # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
267 # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
269 # define sc_send_count(_s) (0U)
270 # define sc_recv_count(_s) (0U)
271 # define sc_tv_acquiry_total_ns(_s) (0LL)
272 # define sc_tv_send_total_ns(_s) (0LL)
273 # define sc_tv_status_total_ns(_s) (0LL)
274 # define sc_tv_process_total_ns(_s) (0LL)
277 /* So that debugfs.ocfs2 can determine which format is being used */
278 #define O2NET_STATS_STR_VERSION 1
279 static void sc_show_sock_stats(struct seq_file *seq,
280 struct o2net_sock_container *sc)
285 seq_printf(seq, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION,
286 sc->sc_node->nd_num, (unsigned long)sc_send_count(sc),
287 (long long)sc_tv_acquiry_total_ns(sc),
288 (long long)sc_tv_send_total_ns(sc),
289 (long long)sc_tv_status_total_ns(sc),
290 (unsigned long)sc_recv_count(sc),
291 (long long)sc_tv_process_total_ns(sc));
294 static void sc_show_sock_container(struct seq_file *seq,
295 struct o2net_sock_container *sc)
297 struct inet_sock *inet = NULL;
298 __be32 saddr = 0, daddr = 0;
299 __be16 sport = 0, dport = 0;
305 inet = inet_sk(sc->sc_sock->sk);
306 /* the stack's structs aren't sparse endian clean */
307 saddr = (__force __be32)inet->inet_saddr;
308 daddr = (__force __be32)inet->inet_daddr;
309 sport = (__force __be16)inet->inet_sport;
310 dport = (__force __be16)inet->inet_dport;
313 /* XXX sigh, inet-> doesn't have sparse annotation so any
314 * use of it here generates a warning with -Wbitwise */
315 seq_printf(seq, "%p:\n"
321 " handshake ok: %u\n"
322 " timer: %lld usecs\n"
323 " data ready: %lld usecs\n"
324 " advance start: %lld usecs\n"
325 " advance stop: %lld usecs\n"
326 " func start: %lld usecs\n"
327 " func stop: %lld usecs\n"
328 " func key: 0x%08x\n"
331 kref_read(&sc->sc_kref),
332 &saddr, inet ? ntohs(sport) : 0,
333 &daddr, inet ? ntohs(dport) : 0,
334 sc->sc_node->nd_name,
337 (long long)ktime_to_us(sc->sc_tv_timer),
338 (long long)ktime_to_us(sc->sc_tv_data_ready),
339 (long long)ktime_to_us(sc->sc_tv_advance_start),
340 (long long)ktime_to_us(sc->sc_tv_advance_stop),
341 (long long)ktime_to_us(sc->sc_tv_func_start),
342 (long long)ktime_to_us(sc->sc_tv_func_stop),
347 static int sc_seq_show(struct seq_file *seq, void *v)
349 struct o2net_sock_debug *sd = seq->private;
350 struct o2net_sock_container *sc, *dummy_sc = sd->dbg_sock;
352 spin_lock(&o2net_debug_lock);
353 sc = next_sc(dummy_sc);
356 if (sd->dbg_ctxt == SHOW_SOCK_CONTAINERS)
357 sc_show_sock_container(seq, sc);
359 sc_show_sock_stats(seq, sc);
362 spin_unlock(&o2net_debug_lock);
367 static void sc_seq_stop(struct seq_file *seq, void *v)
371 static const struct seq_operations sc_seq_ops = {
372 .start = sc_seq_start,
378 static int sc_common_open(struct file *file, int ctxt)
380 struct o2net_sock_debug *sd;
381 struct o2net_sock_container *dummy_sc;
383 dummy_sc = kzalloc(sizeof(*dummy_sc), GFP_KERNEL);
387 sd = __seq_open_private(file, &sc_seq_ops, sizeof(*sd));
394 sd->dbg_sock = dummy_sc;
396 o2net_debug_add_sc(dummy_sc);
401 static int sc_fop_release(struct inode *inode, struct file *file)
403 struct seq_file *seq = file->private_data;
404 struct o2net_sock_debug *sd = seq->private;
405 struct o2net_sock_container *dummy_sc = sd->dbg_sock;
407 o2net_debug_del_sc(dummy_sc);
409 return seq_release_private(inode, file);
412 static int stats_fop_open(struct inode *inode, struct file *file)
414 return sc_common_open(file, SHOW_SOCK_STATS);
417 static const struct file_operations stats_seq_fops = {
418 .open = stats_fop_open,
421 .release = sc_fop_release,
424 static int sc_fop_open(struct inode *inode, struct file *file)
426 return sc_common_open(file, SHOW_SOCK_CONTAINERS);
429 static const struct file_operations sc_seq_fops = {
433 .release = sc_fop_release,
436 static int o2net_fill_bitmap(char *buf, int len)
438 unsigned long map[BITS_TO_LONGS(O2NM_MAX_NODES)];
441 o2net_fill_node_map(map, O2NM_MAX_NODES);
443 while ((i = find_next_bit(map, O2NM_MAX_NODES, i + 1)) < O2NM_MAX_NODES)
444 out += scnprintf(buf + out, PAGE_SIZE - out, "%d ", i);
445 out += scnprintf(buf + out, PAGE_SIZE - out, "\n");
450 static int nodes_fop_open(struct inode *inode, struct file *file)
454 buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
458 i_size_write(inode, o2net_fill_bitmap(buf, PAGE_SIZE));
460 file->private_data = buf;
465 static int o2net_debug_release(struct inode *inode, struct file *file)
467 kfree(file->private_data);
471 static ssize_t o2net_debug_read(struct file *file, char __user *buf,
472 size_t nbytes, loff_t *ppos)
474 return simple_read_from_buffer(buf, nbytes, ppos, file->private_data,
475 i_size_read(file->f_mapping->host));
478 static const struct file_operations nodes_fops = {
479 .open = nodes_fop_open,
480 .release = o2net_debug_release,
481 .read = o2net_debug_read,
482 .llseek = generic_file_llseek,
485 void o2net_debugfs_exit(void)
487 debugfs_remove_recursive(o2net_dentry);
490 void o2net_debugfs_init(void)
492 umode_t mode = S_IFREG|S_IRUSR;
494 o2net_dentry = debugfs_create_dir(O2NET_DEBUG_DIR, NULL);
496 debugfs_create_file(NST_DEBUG_NAME, mode, o2net_dentry, NULL,
498 debugfs_create_file(SC_DEBUG_NAME, mode, o2net_dentry, NULL,
500 debugfs_create_file(STATS_DEBUG_NAME, mode, o2net_dentry, NULL,
502 debugfs_create_file(NODES_DEBUG_NAME, mode, o2net_dentry, NULL,
506 #endif /* CONFIG_DEBUG_FS */