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
;
42 static DEFINE_SPINLOCK(o2net_debug_lock
);
44 static LIST_HEAD(sock_containers
);
45 static LIST_HEAD(send_tracking
);
47 void o2net_debug_add_nst(struct o2net_send_tracking
*nst
)
49 spin_lock(&o2net_debug_lock
);
50 list_add(&nst
->st_net_debug_item
, &send_tracking
);
51 spin_unlock(&o2net_debug_lock
);
54 void o2net_debug_del_nst(struct o2net_send_tracking
*nst
)
56 spin_lock(&o2net_debug_lock
);
57 if (!list_empty(&nst
->st_net_debug_item
))
58 list_del_init(&nst
->st_net_debug_item
);
59 spin_unlock(&o2net_debug_lock
);
62 static struct o2net_send_tracking
63 *next_nst(struct o2net_send_tracking
*nst_start
)
65 struct o2net_send_tracking
*nst
, *ret
= NULL
;
67 assert_spin_locked(&o2net_debug_lock
);
69 list_for_each_entry(nst
, &nst_start
->st_net_debug_item
,
71 /* discover the head of the list */
72 if (&nst
->st_net_debug_item
== &send_tracking
)
75 /* use st_task to detect real nsts in the list */
76 if (nst
->st_task
!= NULL
) {
85 static void *nst_seq_start(struct seq_file
*seq
, loff_t
*pos
)
87 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
);
96 static void *nst_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
98 struct o2net_send_tracking
*nst
, *dummy_nst
= seq
->private;
100 spin_lock(&o2net_debug_lock
);
101 nst
= next_nst(dummy_nst
);
102 list_del_init(&dummy_nst
->st_net_debug_item
);
104 list_add(&dummy_nst
->st_net_debug_item
,
105 &nst
->st_net_debug_item
);
106 spin_unlock(&o2net_debug_lock
);
108 return nst
; /* unused, just needs to be null when done */
111 static int nst_seq_show(struct seq_file
*seq
, void *v
)
113 struct o2net_send_tracking
*nst
, *dummy_nst
= seq
->private;
115 s64 sock
, send
, status
;
117 spin_lock(&o2net_debug_lock
);
118 nst
= next_nst(dummy_nst
);
123 sock
= ktime_to_us(ktime_sub(now
, nst
->st_sock_time
));
124 send
= ktime_to_us(ktime_sub(now
, nst
->st_send_time
));
125 status
= ktime_to_us(ktime_sub(now
, nst
->st_status_time
));
127 /* get_task_comm isn't exported. oh well. */
128 seq_printf(seq
, "%p:\n"
131 " process name: %s\n"
135 " message type: %u\n"
136 " message key: 0x%08x\n"
137 " sock acquiry: %lld usecs ago\n"
138 " send start: %lld usecs ago\n"
139 " wait start: %lld usecs ago\n",
140 nst
, (unsigned long)task_pid_nr(nst
->st_task
),
141 (unsigned long)nst
->st_task
->tgid
,
142 nst
->st_task
->comm
, nst
->st_node
,
143 nst
->st_sc
, nst
->st_id
, nst
->st_msg_type
,
150 spin_unlock(&o2net_debug_lock
);
155 static void nst_seq_stop(struct seq_file
*seq
, void *v
)
159 static const struct seq_operations nst_seq_ops
= {
160 .start
= nst_seq_start
,
161 .next
= nst_seq_next
,
162 .stop
= nst_seq_stop
,
163 .show
= nst_seq_show
,
166 static int nst_fop_open(struct inode
*inode
, struct file
*file
)
168 struct o2net_send_tracking
*dummy_nst
;
170 dummy_nst
= __seq_open_private(file
, &nst_seq_ops
, sizeof(*dummy_nst
));
173 o2net_debug_add_nst(dummy_nst
);
178 static int nst_fop_release(struct inode
*inode
, struct file
*file
)
180 struct seq_file
*seq
= file
->private_data
;
181 struct o2net_send_tracking
*dummy_nst
= seq
->private;
183 o2net_debug_del_nst(dummy_nst
);
184 return seq_release_private(inode
, file
);
187 static const struct file_operations nst_seq_fops
= {
188 .open
= nst_fop_open
,
191 .release
= nst_fop_release
,
194 void o2net_debug_add_sc(struct o2net_sock_container
*sc
)
196 spin_lock(&o2net_debug_lock
);
197 list_add(&sc
->sc_net_debug_item
, &sock_containers
);
198 spin_unlock(&o2net_debug_lock
);
201 void o2net_debug_del_sc(struct o2net_sock_container
*sc
)
203 spin_lock(&o2net_debug_lock
);
204 list_del_init(&sc
->sc_net_debug_item
);
205 spin_unlock(&o2net_debug_lock
);
208 struct o2net_sock_debug
{
210 struct o2net_sock_container
*dbg_sock
;
213 static struct o2net_sock_container
214 *next_sc(struct o2net_sock_container
*sc_start
)
216 struct o2net_sock_container
*sc
, *ret
= NULL
;
218 assert_spin_locked(&o2net_debug_lock
);
220 list_for_each_entry(sc
, &sc_start
->sc_net_debug_item
,
222 /* discover the head of the list miscast as a sc */
223 if (&sc
->sc_net_debug_item
== &sock_containers
)
226 /* use sc_page to detect real scs in the list */
227 if (sc
->sc_page
!= NULL
) {
236 static void *sc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
238 struct o2net_sock_debug
*sd
= seq
->private;
239 struct o2net_sock_container
*sc
, *dummy_sc
= sd
->dbg_sock
;
241 spin_lock(&o2net_debug_lock
);
242 sc
= next_sc(dummy_sc
);
243 spin_unlock(&o2net_debug_lock
);
248 static void *sc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
250 struct o2net_sock_debug
*sd
= seq
->private;
251 struct o2net_sock_container
*sc
, *dummy_sc
= sd
->dbg_sock
;
253 spin_lock(&o2net_debug_lock
);
254 sc
= next_sc(dummy_sc
);
255 list_del_init(&dummy_sc
->sc_net_debug_item
);
257 list_add(&dummy_sc
->sc_net_debug_item
, &sc
->sc_net_debug_item
);
258 spin_unlock(&o2net_debug_lock
);
260 return sc
; /* unused, just needs to be null when done */
263 #ifdef CONFIG_OCFS2_FS_STATS
264 # define sc_send_count(_s) ((_s)->sc_send_count)
265 # define sc_recv_count(_s) ((_s)->sc_recv_count)
266 # define sc_tv_acquiry_total_ns(_s) (ktime_to_ns((_s)->sc_tv_acquiry_total))
267 # define sc_tv_send_total_ns(_s) (ktime_to_ns((_s)->sc_tv_send_total))
268 # define sc_tv_status_total_ns(_s) (ktime_to_ns((_s)->sc_tv_status_total))
269 # define sc_tv_process_total_ns(_s) (ktime_to_ns((_s)->sc_tv_process_total))
271 # define sc_send_count(_s) (0U)
272 # define sc_recv_count(_s) (0U)
273 # define sc_tv_acquiry_total_ns(_s) (0LL)
274 # define sc_tv_send_total_ns(_s) (0LL)
275 # define sc_tv_status_total_ns(_s) (0LL)
276 # define sc_tv_process_total_ns(_s) (0LL)
279 /* So that debugfs.ocfs2 can determine which format is being used */
280 #define O2NET_STATS_STR_VERSION 1
281 static void sc_show_sock_stats(struct seq_file
*seq
,
282 struct o2net_sock_container
*sc
)
287 seq_printf(seq
, "%d,%u,%lu,%lld,%lld,%lld,%lu,%lld\n", O2NET_STATS_STR_VERSION
,
288 sc
->sc_node
->nd_num
, (unsigned long)sc_send_count(sc
),
289 (long long)sc_tv_acquiry_total_ns(sc
),
290 (long long)sc_tv_send_total_ns(sc
),
291 (long long)sc_tv_status_total_ns(sc
),
292 (unsigned long)sc_recv_count(sc
),
293 (long long)sc_tv_process_total_ns(sc
));
296 static void sc_show_sock_container(struct seq_file
*seq
,
297 struct o2net_sock_container
*sc
)
299 struct inet_sock
*inet
= NULL
;
300 __be32 saddr
= 0, daddr
= 0;
301 __be16 sport
= 0, dport
= 0;
307 inet
= inet_sk(sc
->sc_sock
->sk
);
308 /* the stack's structs aren't sparse endian clean */
309 saddr
= (__force __be32
)inet
->inet_saddr
;
310 daddr
= (__force __be32
)inet
->inet_daddr
;
311 sport
= (__force __be16
)inet
->inet_sport
;
312 dport
= (__force __be16
)inet
->inet_dport
;
315 /* XXX sigh, inet-> doesn't have sparse annotation so any
316 * use of it here generates a warning with -Wbitwise */
317 seq_printf(seq
, "%p:\n"
323 " handshake ok: %u\n"
324 " timer: %lld usecs\n"
325 " data ready: %lld usecs\n"
326 " advance start: %lld usecs\n"
327 " advance stop: %lld usecs\n"
328 " func start: %lld usecs\n"
329 " func stop: %lld usecs\n"
330 " func key: 0x%08x\n"
333 kref_read(&sc
->sc_kref
),
334 &saddr
, inet
? ntohs(sport
) : 0,
335 &daddr
, inet
? ntohs(dport
) : 0,
336 sc
->sc_node
->nd_name
,
339 (long long)ktime_to_us(sc
->sc_tv_timer
),
340 (long long)ktime_to_us(sc
->sc_tv_data_ready
),
341 (long long)ktime_to_us(sc
->sc_tv_advance_start
),
342 (long long)ktime_to_us(sc
->sc_tv_advance_stop
),
343 (long long)ktime_to_us(sc
->sc_tv_func_start
),
344 (long long)ktime_to_us(sc
->sc_tv_func_stop
),
349 static int sc_seq_show(struct seq_file
*seq
, void *v
)
351 struct o2net_sock_debug
*sd
= seq
->private;
352 struct o2net_sock_container
*sc
, *dummy_sc
= sd
->dbg_sock
;
354 spin_lock(&o2net_debug_lock
);
355 sc
= next_sc(dummy_sc
);
358 if (sd
->dbg_ctxt
== SHOW_SOCK_CONTAINERS
)
359 sc_show_sock_container(seq
, sc
);
361 sc_show_sock_stats(seq
, sc
);
364 spin_unlock(&o2net_debug_lock
);
369 static void sc_seq_stop(struct seq_file
*seq
, void *v
)
373 static const struct seq_operations sc_seq_ops
= {
374 .start
= sc_seq_start
,
380 static int sc_common_open(struct file
*file
, int ctxt
)
382 struct o2net_sock_debug
*sd
;
383 struct o2net_sock_container
*dummy_sc
;
385 dummy_sc
= kzalloc(sizeof(*dummy_sc
), GFP_KERNEL
);
389 sd
= __seq_open_private(file
, &sc_seq_ops
, sizeof(*sd
));
396 sd
->dbg_sock
= dummy_sc
;
398 o2net_debug_add_sc(dummy_sc
);
403 static int sc_fop_release(struct inode
*inode
, struct file
*file
)
405 struct seq_file
*seq
= file
->private_data
;
406 struct o2net_sock_debug
*sd
= seq
->private;
407 struct o2net_sock_container
*dummy_sc
= sd
->dbg_sock
;
409 o2net_debug_del_sc(dummy_sc
);
411 return seq_release_private(inode
, file
);
414 static int stats_fop_open(struct inode
*inode
, struct file
*file
)
416 return sc_common_open(file
, SHOW_SOCK_STATS
);
419 static const struct file_operations stats_seq_fops
= {
420 .open
= stats_fop_open
,
423 .release
= sc_fop_release
,
426 static int sc_fop_open(struct inode
*inode
, struct file
*file
)
428 return sc_common_open(file
, SHOW_SOCK_CONTAINERS
);
431 static const struct file_operations sc_seq_fops
= {
435 .release
= sc_fop_release
,
438 static int o2net_fill_bitmap(char *buf
, int len
)
440 unsigned long map
[BITS_TO_LONGS(O2NM_MAX_NODES
)];
443 o2net_fill_node_map(map
, sizeof(map
));
445 while ((i
= find_next_bit(map
, O2NM_MAX_NODES
, i
+ 1)) < O2NM_MAX_NODES
)
446 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "%d ", i
);
447 out
+= snprintf(buf
+ out
, PAGE_SIZE
- out
, "\n");
452 static int nodes_fop_open(struct inode
*inode
, struct file
*file
)
456 buf
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
460 i_size_write(inode
, o2net_fill_bitmap(buf
, PAGE_SIZE
));
462 file
->private_data
= buf
;
467 static int o2net_debug_release(struct inode
*inode
, struct file
*file
)
469 kfree(file
->private_data
);
473 static ssize_t
o2net_debug_read(struct file
*file
, char __user
*buf
,
474 size_t nbytes
, loff_t
*ppos
)
476 return simple_read_from_buffer(buf
, nbytes
, ppos
, file
->private_data
,
477 i_size_read(file
->f_mapping
->host
));
480 static const struct file_operations nodes_fops
= {
481 .open
= nodes_fop_open
,
482 .release
= o2net_debug_release
,
483 .read
= o2net_debug_read
,
484 .llseek
= generic_file_llseek
,
487 void o2net_debugfs_exit(void)
489 debugfs_remove_recursive(o2net_dentry
);
492 void o2net_debugfs_init(void)
494 umode_t mode
= S_IFREG
|S_IRUSR
;
496 o2net_dentry
= debugfs_create_dir(O2NET_DEBUG_DIR
, NULL
);
498 debugfs_create_file(NST_DEBUG_NAME
, mode
, o2net_dentry
, NULL
,
500 debugfs_create_file(SC_DEBUG_NAME
, mode
, o2net_dentry
, NULL
,
502 debugfs_create_file(STATS_DEBUG_NAME
, mode
, o2net_dentry
, NULL
,
504 debugfs_create_file(NODES_DEBUG_NAME
, mode
, o2net_dentry
, NULL
,
508 #endif /* CONFIG_DEBUG_FS */