aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/evsel.h
blob: f79bb2c09a6cbea510e31d260a1fabb778e13950 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#ifndef __PERF_EVSEL_H
#define __PERF_EVSEL_H 1

#include <linux/list.h>
#include <stdbool.h>
#include "../../../include/linux/perf_event.h"
#include "types.h"
#include "xyarray.h"
#include "cgroup.h"
#include "hist.h"
 
struct perf_counts_values {
	union {
		struct {
			u64 val;
			u64 ena;
			u64 run;
		};
		u64 values[3];
	};
};

struct perf_counts {
	s8		   	  scaled;
	struct perf_counts_values aggr;
	struct perf_counts_values cpu[];
};

struct perf_evsel;

/*
 * Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are
 * more than one entry in the evlist.
 */
struct perf_sample_id {
	struct hlist_node 	node;
	u64		 	id;
	struct perf_evsel	*evsel;
};

/** struct perf_evsel - event selector
 *
 * @name - Can be set to retain the original event name passed by the user,
 *         so that when showing results in tools such as 'perf stat', we
 *         show the name used, not some alias.
 */
struct perf_evsel {
	struct list_head	node;
	struct perf_event_attr	attr;
	char			*filter;
	struct xyarray		*fd;
	struct xyarray		*sample_id;
	u64			*id;
	struct perf_counts	*counts;
	int			idx;
	int			ids;
	struct hists		hists;
	char			*name;
	union {
		void		*priv;
		off_t		id_offset;
	};
	struct cgroup_sel	*cgrp;
};

struct cpu_map;
struct thread_map;
struct perf_evlist;

struct perf_evsel *perf_evsel__new(struct perf_event_attr *attr, int idx);
void perf_evsel__init(struct perf_evsel *evsel,
		      struct perf_event_attr *attr, int idx);
void perf_evsel__exit(struct perf_evsel *evsel);
void perf_evsel__delete(struct perf_evsel *evsel);

int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads);
int perf_evsel__alloc_counts(struct perf_evsel *evsel, int ncpus);
void perf_evsel__free_fd(struct perf_evsel *evsel);
void perf_evsel__free_id(struct perf_evsel *evsel);
void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads);

int perf_evsel__open_per_cpu(struct perf_evsel *evsel,
			     struct cpu_map *cpus, bool group);
int perf_evsel__open_per_thread(struct perf_evsel *evsel,
				struct thread_map *threads, bool group);
int perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
		     struct thread_map *threads, bool group);

#define perf_evsel__match(evsel, t, c)		\
	(evsel->attr.type == PERF_TYPE_##t &&	\
	 evsel->attr.config == PERF_COUNT_##c)

int __perf_evsel__read_on_cpu(struct perf_evsel *evsel,
			      int cpu, int thread, bool scale);

/**
 * perf_evsel__read_on_cpu - Read out the results on a CPU and thread
 *
 * @evsel - event selector to read value
 * @cpu - CPU of interest
 * @thread - thread of interest
 */
static inline int perf_evsel__read_on_cpu(struct perf_evsel *evsel,
					  int cpu, int thread)
{
	return __perf_evsel__read_on_cpu(evsel, cpu, thread, false);
}

/**
 * perf_evsel__read_on_cpu_scaled - Read out the results on a CPU and thread, scaled
 *
 * @evsel - event selector to read value
 * @cpu - CPU of interest
 * @thread - thread of interest
 */
static inline int perf_evsel__read_on_cpu_scaled(struct perf_evsel *evsel,
						 int cpu, int thread)
{
	return __perf_evsel__read_on_cpu(evsel, cpu, thread, true);
}

int __perf_evsel__read(struct perf_evsel *evsel, int ncpus, int nthreads,
		       bool scale);

/**
 * perf_evsel__read - Read the aggregate results on all CPUs
 *
 * @evsel - event selector to read value
 * @ncpus - Number of cpus affected, from zero
 * @nthreads - Number of threads affected, from zero
 */
static inline int perf_evsel__read(struct perf_evsel *evsel,
				    int ncpus, int nthreads)
{
	return __perf_evsel__read(evsel, ncpus, nthreads, false);
}

/**
 * perf_evsel__read_scaled - Read the aggregate results on all CPUs, scaled
 *
 * @evsel - event selector to read value
 * @ncpus - Number of cpus affected, from zero
 * @nthreads - Number of threads affected, from zero
 */
static inline int perf_evsel__read_scaled(struct perf_evsel *evsel,
					  int ncpus, int nthreads)
{
	return __perf_evsel__read(evsel, ncpus, nthreads, true);
}

#endif /* __PERF_EVSEL_H */
om> 2006-07-25 14:44:31 -0400 committer Steven Whitehouse <swhiteho@redhat.com> 2006-07-26 08:41:37 -0400 [DLM] more info through debugfs' href='/cgit/cgit.cgi/litmus-rt.git/commit/fs/dlm/debug_fs.c?h=v2.6.33-rc5&id=5de6319b1839300ba6b461ed19531cdab90db9fc'>5de6319b1839
9dd592d70be0
892c4467e335
9dd592d70be0

892c4467e335

9dd592d70be0
eeda418d8c26

892c4467e335
9dd592d70be0

d292c0cc489f

9dd592d70be0

eeda418d8c26

9dd592d70be0
eeda418d8c26
ac90a2552500
9dd592d70be0
892c4467e335















9dd592d70be0

d022509d1c54
9dd592d70be0

892c4467e335
9dd592d70be0


892c4467e335




9dd592d70be0
892c4467e335




d022509d1c54
892c4467e335





d022509d1c54
892c4467e335
d022509d1c54

892c4467e335

d022509d1c54

892c4467e335
d022509d1c54





892c4467e335

















d022509d1c54






892c4467e335
d022509d1c54


892c4467e335










d022509d1c54













892c4467e335


d022509d1c54







892c4467e335


d022509d1c54

892c4467e335



e7fd41792fc0
e7fd41792fc0
892c4467e335



e7fd41792fc0

892c4467e335



e7fd41792fc0

892c4467e335



e7fd41792fc0
892c4467e335


e7fd41792fc0

892c4467e335





e7fd41792fc0
892c4467e335






e7fd41792fc0
892c4467e335

e7fd41792fc0
d022509d1c54

892c4467e335
d022509d1c54

9dd592d70be0
892c4467e335


9dd592d70be0

892c4467e335
d022509d1c54


892c4467e335
d022509d1c54

892c4467e335
d022509d1c54
9dd592d70be0
e7fd41792fc0
892c4467e335
e7fd41792fc0

88e9d34c7278


e7fd41792fc0
892c4467e335
e7fd41792fc0
892c4467e335




e7fd41792fc0
892c4467e335

9dd592d70be0
892c4467e335

9dd592d70be0
573c24c4af66
9dd592d70be0

892c4467e335
9dd592d70be0
892c4467e335






c7be761a8163
892c4467e335






c7be761a8163
892c4467e335


9dd592d70be0
c7be761a8163
9dd592d70be0
892c4467e335


9dd592d70be0
892c4467e335

9dd592d70be0
892c4467e335


9dd592d70be0
892c4467e335

9dd592d70be0

9dd592d70be0
c7be761a8163
892c4467e335





c7be761a8163
892c4467e335


c7be761a8163
892c4467e335
9dd592d70be0

892c4467e335
9dd592d70be0
892c4467e335












c7be761a8163
892c4467e335






c7be761a8163
892c4467e335



c7be761a8163
892c4467e335
d022509d1c54
892c4467e335


d022509d1c54
892c4467e335

d022509d1c54
892c4467e335


d022509d1c54
892c4467e335



d022509d1c54
c7be761a8163
892c4467e335





c7be761a8163
892c4467e335


c7be761a8163
d022509d1c54
d022509d1c54

892c4467e335
d022509d1c54
892c4467e335
d022509d1c54
892c4467e335


d022509d1c54
d022509d1c54

88e9d34c7278
892c4467e335



d022509d1c54

88e9d34c7278
892c4467e335





88e9d34c7278
892c4467e335










d022509d1c54

892c4467e335







d022509d1c54
d022509d1c54



892c4467e335
d022509d1c54


892c4467e335
















d022509d1c54
892c4467e335
d022509d1c54





5de6319b1839




bba9dfd83587
5de6319b1839







06442440bc44
5de6319b1839





06442440bc44





5de6319b1839







00977a59b951
5de6319b1839




d022509d1c54











e7fd41792fc0

5de6319b1839

d022509d1c54

5de6319b1839



892c4467e335
20abf975f75d
d022509d1c54
5de6319b1839
d022509d1c54
5de6319b1839
9dd592d70be0
ac90a2552500





892c4467e335
d022509d1c54











892c4467e335
d022509d1c54












9dd592d70be0
5de6319b1839
e7fd41792fc0
d022509d1c54


e7fd41792fc0

30727174b627
e7fd41792fc0
5de6319b1839
e7fd41792fc0








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729


                                                                               
                                                               














                                                                               
                 
 


                                         


                               





















                                     

                                                                      


                                                                               

                                                   

                                                                        










                                                                           
                                   

 
                                                                 

                            
                                                                        
               
 

                      




                                                            





                                                              
 
                                

                                                                           
                                      
                                                          
                                       

                                                                         
            



                                                               











                                                                       


                                         

         



                                                           




                                                                              

         

                                                       




                                                                        

                                            




                                                                          

                                         




                                                                       
 




                                                                    

                                                                 

                                                                            
                                         

         
                        
                  

 

                                                                      
 

                    
               

                                            

                                               

         

                                                                     
 
                                                                          
                                   
 















                                                                                 

 
                                                               

                            
                   


                    




                                                                      
 




                                                                        
 





                                                                     
                      
                  

 

                                                                      

                    
               





                                               

















                                                                                         






                                                               
               


                    










                                                           













                                                                              


                                 







                                                                        


                                 

          



                                                                      
         
 



                                                                        

         



                                                                     

         



                                                                  
         


                      

 





                            
 






                                                                     
 

                                          
 

                             
                                                 

                      
                                 


                                                                          

                                       
                                                 


                                 
                                                                             

                                       
                                                 
                      
         
 
                  

 


                                                   
 
                                                               
 




                                         
 

                                      
 

                                         
 
                                                           

                            
                   
                               






                                        
                                               






                                                                   
                                                                         


                                          
         
                                                 
 


                                                             
 

                                
 


                               
 

                                                   

                                    
 
                                                       





                                                                            
                                                                 


                                  
                                                         
         

 
                                                                              
 












                                                  
                                               






                                                                    
                                                         



                                
                                                 
                        
 


                                                             
 

                                
 


                               
 



                                                   
 
                                                       





                                                                            
                                                                 


                                  
                                                         
         

 
                                                                
 
                                          
 


                                     
         

 
                                                      



                                 

  
                                                      





                                 
                                                      










                                                             

                             







                                                       
 



                                 
                                                         


                 
















                                                    
                               
                              





                              




                                                               
                                              







                                                                    
                                                         





                                                                   





                                                                             







                                                                           
                                                    




                                











                                                            

                                            

                                       

                      



                                                                        
                                                                     
                                     
                          
 
                      
 
                                      





                                                                          
                                                                       











                                                                        
                                                                     












                                                                            
 
                 
 


                                  

 
                                     
 
                                    








                                                   
/******************************************************************************
*******************************************************************************
**
**  Copyright (C) 2005-2009 Red Hat, Inc.  All rights reserved.
**
**  This copyrighted material is made available to anyone wishing to use,
**  modify, copy, or redistribute it subject to the terms and conditions
**  of the GNU General Public License v.2.
**
*******************************************************************************
******************************************************************************/

#include <linux/pagemap.h>
#include <linux/seq_file.h>
#include <linux/module.h>
#include <linux/ctype.h>
#include <linux/debugfs.h>

#include "dlm_internal.h"
#include "lock.h"

#define DLM_DEBUG_BUF_LEN 4096
static char debug_buf[DLM_DEBUG_BUF_LEN];
static struct mutex debug_buf_lock;

static struct dentry *dlm_root;

static char *print_lockmode(int mode)
{
	switch (mode) {
	case DLM_LOCK_IV:
		return "--";
	case DLM_LOCK_NL:
		return "NL";
	case DLM_LOCK_CR:
		return "CR";
	case DLM_LOCK_CW:
		return "CW";
	case DLM_LOCK_PR:
		return "PR";
	case DLM_LOCK_PW:
		return "PW";
	case DLM_LOCK_EX:
		return "EX";
	default:
		return "??";
	}
}

static int print_format1_lock(struct seq_file *s, struct dlm_lkb *lkb,
			      struct dlm_rsb *res)
{
	seq_printf(s, "%08x %s", lkb->lkb_id, print_lockmode(lkb->lkb_grmode));

	if (lkb->lkb_status == DLM_LKSTS_CONVERT ||
	    lkb->lkb_status == DLM_LKSTS_WAITING)
		seq_printf(s, " (%s)", print_lockmode(lkb->lkb_rqmode));

	if (lkb->lkb_nodeid) {
		if (lkb->lkb_nodeid != res->res_nodeid)
			seq_printf(s, " Remote: %3d %08x", lkb->lkb_nodeid,
				   lkb->lkb_remid);
		else
			seq_printf(s, " Master:     %08x", lkb->lkb_remid);
	}

	if (lkb->lkb_wait_type)
		seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);

	return seq_printf(s, "\n");
}

static int print_format1(struct dlm_rsb *res, struct seq_file *s)
{
	struct dlm_lkb *lkb;
	int i, lvblen = res->res_ls->ls_lvblen, recover_list, root_list;
	int rv;

	lock_rsb(res);

	rv = seq_printf(s, "\nResource %p Name (len=%d) \"",
			res, res->res_length);
	if (rv)
		goto out;

	for (i = 0; i < res->res_length; i++) {
		if (isprint(res->res_name[i]))
			seq_printf(s, "%c", res->res_name[i]);
		else
			seq_printf(s, "%c", '.');
	}

	if (res->res_nodeid > 0)
		rv = seq_printf(s, "\"  \nLocal Copy, Master is node %d\n",
				res->res_nodeid);
	else if (res->res_nodeid == 0)
		rv = seq_printf(s, "\"  \nMaster Copy\n");
	else if (res->res_nodeid == -1)
		rv = seq_printf(s, "\"  \nLooking up master (lkid %x)\n",
			   	res->res_first_lkid);
	else
		rv = seq_printf(s, "\"  \nInvalid master %d\n",
				res->res_nodeid);
	if (rv)
		goto out;

	/* Print the LVB: */
	if (res->res_lvbptr) {
		seq_printf(s, "LVB: ");
		for (i = 0; i < lvblen; i++) {
			if (i == lvblen / 2)
				seq_printf(s, "\n     ");
			seq_printf(s, "%02x ",
				   (unsigned char) res->res_lvbptr[i]);
		}
		if (rsb_flag(res, RSB_VALNOTVALID))
			seq_printf(s, " (INVALID)");
		rv = seq_printf(s, "\n");
		if (rv)
			goto out;
	}

	root_list = !list_empty(&res->res_root_list);
	recover_list = !list_empty(&res->res_recover_list);

	if (root_list || recover_list) {
		rv = seq_printf(s, "Recovery: root %d recover %d flags %lx "
				"count %d\n", root_list, recover_list,
			   	res->res_flags, res->res_recover_locks_count);
		if (rv)
			goto out;
	}

	/* Print the locks attached to this resource */
	seq_printf(s, "Granted Queue\n");
	list_for_each_entry(lkb, &res->res_grantqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}

	seq_printf(s, "Conversion Queue\n");
	list_for_each_entry(lkb, &res->res_convertqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}

	seq_printf(s, "Waiting Queue\n");
	list_for_each_entry(lkb, &res->res_waitqueue, lkb_statequeue) {
		rv = print_format1_lock(s, lkb, res);
		if (rv)
			goto out;
	}

	if (list_empty(&res->res_lookup))
		goto out;

	seq_printf(s, "Lookup Queue\n");
	list_for_each_entry(lkb, &res->res_lookup, lkb_rsb_lookup) {
		rv = seq_printf(s, "%08x %s", lkb->lkb_id,
				print_lockmode(lkb->lkb_rqmode));
		if (lkb->lkb_wait_type)
			seq_printf(s, " wait_type: %d", lkb->lkb_wait_type);
		rv = seq_printf(s, "\n");
	}
 out:
	unlock_rsb(res);
	return rv;
}

static int print_format2_lock(struct seq_file *s, struct dlm_lkb *lkb,
			      struct dlm_rsb *r)
{
	u64 xid = 0;
	u64 us;
	int rv;

	if (lkb->lkb_flags & DLM_IFL_USER) {
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
	}

	/* microseconds since lkb was added to current queue */
	us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_timestamp));

	/* id nodeid remid pid xid exflags flags sts grmode rqmode time_us
	   r_nodeid r_len r_name */

	rv = seq_printf(s, "%x %d %x %u %llu %x %x %d %d %d %llu %u %d \"%s\"\n",
			lkb->lkb_id,
			lkb->lkb_nodeid,
			lkb->lkb_remid,
			lkb->lkb_ownpid,
			(unsigned long long)xid,
			lkb->lkb_exflags,
			lkb->lkb_flags,
			lkb->lkb_status,
			lkb->lkb_grmode,
			lkb->lkb_rqmode,
			(unsigned long long)us,
			r->res_nodeid,
			r->res_length,
			r->res_name);
	return rv;
}

static int print_format2(struct dlm_rsb *r, struct seq_file *s)
{
	struct dlm_lkb *lkb;
	int rv = 0;

	lock_rsb(r);

	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}

	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}

	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
		rv = print_format2_lock(s, lkb, r);
		if (rv)
			goto out;
	}
 out:
	unlock_rsb(r);
	return rv;
}

static int print_format3_lock(struct seq_file *s, struct dlm_lkb *lkb,
			      int rsb_lookup)
{
	u64 xid = 0;
	int rv;

	if (lkb->lkb_flags & DLM_IFL_USER) {
		if (lkb->lkb_ua)
			xid = lkb->lkb_ua->xid;
	}

	rv = seq_printf(s, "lkb %x %d %x %u %llu %x %x %d %d %d %d %d %d %u %llu %llu\n",
			lkb->lkb_id,
			lkb->lkb_nodeid,
			lkb->lkb_remid,
			lkb->lkb_ownpid,
			(unsigned long long)xid,
			lkb->lkb_exflags,
			lkb->lkb_flags,
			lkb->lkb_status,
			lkb->lkb_grmode,
			lkb->lkb_rqmode,
			lkb->lkb_highbast,
			rsb_lookup,
			lkb->lkb_wait_type,
			lkb->lkb_lvbseq,
			(unsigned long long)ktime_to_ns(lkb->lkb_timestamp),
			(unsigned long long)ktime_to_ns(lkb->lkb_time_bast));
	return rv;
}

static int print_format3(struct dlm_rsb *r, struct seq_file *s)
{
	struct dlm_lkb *lkb;
	int i, lvblen = r->res_ls->ls_lvblen;
	int print_name = 1;
	int rv;

	lock_rsb(r);

	rv = seq_printf(s, "rsb %p %d %x %lx %d %d %u %d ",
			r,
			r->res_nodeid,
			r->res_first_lkid,
			r->res_flags,
			!list_empty(&r->res_root_list),
			!list_empty(&r->res_recover_list),
			r->res_recover_locks_count,
			r->res_length);
	if (rv)
		goto out;

	for (i = 0; i < r->res_length; i++) {
		if (!isascii(r->res_name[i]) || !isprint(r->res_name[i]))
			print_name = 0;
	}

	seq_printf(s, "%s", print_name ? "str " : "hex");

	for (i = 0; i < r->res_length; i++) {
		if (print_name)
			seq_printf(s, "%c", r->res_name[i]);
		else
			seq_printf(s, " %02x", (unsigned char)r->res_name[i]);
	}
	rv = seq_printf(s, "\n");
	if (rv)
		goto out;

	if (!r->res_lvbptr)
		goto do_locks;

	seq_printf(s, "lvb %u %d", r->res_lvbseq, lvblen);

	for (i = 0; i < lvblen; i++)
		seq_printf(s, " %02x", (unsigned char)r->res_lvbptr[i]);
	rv = seq_printf(s, "\n");
	if (rv)
		goto out;

 do_locks:
	list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
	}

	list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
	}

	list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue) {
		rv = print_format3_lock(s, lkb, 0);
		if (rv)
			goto out;
	}

	list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup) {
		rv = print_format3_lock(s, lkb, 1);
		if (rv)
			goto out;
	}
 out:
	unlock_rsb(r);
	return rv;
}

struct rsbtbl_iter {
	struct dlm_rsb *rsb;
	unsigned bucket;
	int format;
	int header;
};

/* seq_printf returns -1 if the buffer is full, and 0 otherwise.
   If the buffer is full, seq_printf can be called again, but it
   does nothing and just returns -1.  So, the these printing routines
   periodically check the return value to avoid wasting too much time
   trying to print to a full buffer. */

static int table_seq_show(struct seq_file *seq, void *iter_ptr)
{
	struct rsbtbl_iter *ri = iter_ptr;
	int rv = 0;

	switch (ri->format) {
	case 1:
		rv = print_format1(ri->rsb, seq);
		break;
	case 2:
		if (ri->header) {
			seq_printf(seq, "id nodeid remid pid xid exflags "
					"flags sts grmode rqmode time_ms "
					"r_nodeid r_len r_name\n");
			ri->header = 0;
		}
		rv = print_format2(ri->rsb, seq);
		break;
	case 3:
		if (ri->header) {
			seq_printf(seq, "version rsb 1.1 lvb 1.1 lkb 1.1\n");
			ri->header = 0;
		}
		rv = print_format3(ri->rsb, seq);
		break;
	}

	return rv;
}

static const struct seq_operations format1_seq_ops;
static const struct seq_operations format2_seq_ops;
static const struct seq_operations format3_seq_ops;

static void *table_seq_start(struct seq_file *seq, loff_t *pos)
{
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri;
	struct dlm_rsb *r;
	loff_t n = *pos;
	unsigned bucket, entry;

	bucket = n >> 32;
	entry = n & ((1LL << 32) - 1);

	if (bucket >= ls->ls_rsbtbl_size)
		return NULL;

	ri = kzalloc(sizeof(struct rsbtbl_iter), GFP_NOFS);
	if (!ri)
		return NULL;
	if (n == 0)
		ri->header = 1;
	if (seq->op == &format1_seq_ops)
		ri->format = 1;
	if (seq->op == &format2_seq_ops)
		ri->format = 2;
	if (seq->op == &format3_seq_ops)
		ri->format = 3;

	spin_lock(&ls->ls_rsbtbl[bucket].lock);
	if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
		list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list,
				    res_hashchain) {
			if (!entry--) {
				dlm_hold_rsb(r);
				ri->rsb = r;
				ri->bucket = bucket;
				spin_unlock(&ls->ls_rsbtbl[bucket].lock);
				return ri;
			}
		}
	}
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);

	/*
	 * move to the first rsb in the next non-empty bucket
	 */

	/* zero the entry */
	n &= ~((1LL << 32) - 1);

	while (1) {
		bucket++;
		n += 1LL << 32;

		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
			return NULL;
		}

		spin_lock(&ls->ls_rsbtbl[bucket].lock);
		if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
			r = list_first_entry(&ls->ls_rsbtbl[bucket].list,
					     struct dlm_rsb, res_hashchain);
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
			*pos = n;
			return ri;
		}
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
	}
}

static void *table_seq_next(struct seq_file *seq, void *iter_ptr, loff_t *pos)
{
	struct dlm_ls *ls = seq->private;
	struct rsbtbl_iter *ri = iter_ptr;
	struct list_head *next;
	struct dlm_rsb *r, *rp;
	loff_t n = *pos;
	unsigned bucket;

	bucket = n >> 32;

	/*
	 * move to the next rsb in the same bucket
	 */

	spin_lock(&ls->ls_rsbtbl[bucket].lock);
	rp = ri->rsb;
	next = rp->res_hashchain.next;

	if (next != &ls->ls_rsbtbl[bucket].list) {
		r = list_entry(next, struct dlm_rsb, res_hashchain);
		dlm_hold_rsb(r);
		ri->rsb = r;
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
		dlm_put_rsb(rp);
		++*pos;
		return ri;
	}
	spin_unlock(&ls->ls_rsbtbl[bucket].lock);
	dlm_put_rsb(rp);

	/*
	 * move to the first rsb in the next non-empty bucket
	 */

	/* zero the entry */
	n &= ~((1LL << 32) - 1);

	while (1) {
		bucket++;
		n += 1LL << 32;

		if (bucket >= ls->ls_rsbtbl_size) {
			kfree(ri);
			return NULL;
		}

		spin_lock(&ls->ls_rsbtbl[bucket].lock);
		if (!list_empty(&ls->ls_rsbtbl[bucket].list)) {
			r = list_first_entry(&ls->ls_rsbtbl[bucket].list,
					     struct dlm_rsb, res_hashchain);
			dlm_hold_rsb(r);
			ri->rsb = r;
			ri->bucket = bucket;
			spin_unlock(&ls->ls_rsbtbl[bucket].lock);
			*pos = n;
			return ri;
		}
		spin_unlock(&ls->ls_rsbtbl[bucket].lock);
	}
}

static void table_seq_stop(struct seq_file *seq, void *iter_ptr)
{
	struct rsbtbl_iter *ri = iter_ptr;

	if (ri) {
		dlm_put_rsb(ri->rsb);
		kfree(ri);
	}
}

static const struct seq_operations format1_seq_ops = {
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

static const struct seq_operations format2_seq_ops = {
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

static const struct seq_operations format3_seq_ops = {
	.start = table_seq_start,
	.next  = table_seq_next,
	.stop  = table_seq_stop,
	.show  = table_seq_show,
};

static const struct file_operations format1_fops;
static const struct file_operations format2_fops;
static const struct file_operations format3_fops;

static int table_open(struct inode *inode, struct file *file)
{
	struct seq_file *seq;
	int ret = -1;

	if (file->f_op == &format1_fops)
		ret = seq_open(file, &format1_seq_ops);
	else if (file->f_op == &format2_fops)
		ret = seq_open(file, &format2_seq_ops);
	else if (file->f_op == &format3_fops)
		ret = seq_open(file, &format3_seq_ops);

	if (ret)
		return ret;

	seq = file->private_data;
	seq->private = inode->i_private; /* the dlm_ls */
	return 0;
}

static const struct file_operations format1_fops = {
	.owner   = THIS_MODULE,
	.open    = table_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format2_fops = {
	.owner   = THIS_MODULE,
	.open    = table_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

static const struct file_operations format3_fops = {
	.owner   = THIS_MODULE,
	.open    = table_open,
	.read    = seq_read,
	.llseek  = seq_lseek,
	.release = seq_release
};

/*
 * dump lkb's on the ls_waiters list
 */

static int waiters_open(struct inode *inode, struct file *file)
{
	file->private_data = inode->i_private;
	return 0;
}

static ssize_t waiters_read(struct file *file, char __user *userbuf,
			    size_t count, loff_t *ppos)
{
	struct dlm_ls *ls = file->private_data;
	struct dlm_lkb *lkb;
	size_t len = DLM_DEBUG_BUF_LEN, pos = 0, ret, rv;

	mutex_lock(&debug_buf_lock);
	mutex_lock(&ls->ls_waiters_mutex);
	memset(debug_buf, 0, sizeof(debug_buf));

	list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
		ret = snprintf(debug_buf + pos, len - pos, "%x %d %d %s\n",
			       lkb->lkb_id, lkb->lkb_wait_type,
			       lkb->lkb_nodeid, lkb->lkb_resource->res_name);
		if (ret >= len - pos)
			break;
		pos += ret;
	}
	mutex_unlock(&ls->ls_waiters_mutex);

	rv = simple_read_from_buffer(userbuf, count, ppos, debug_buf, pos);
	mutex_unlock(&debug_buf_lock);
	return rv;
}

static const struct file_operations waiters_fops = {
	.owner   = THIS_MODULE,
	.open    = waiters_open,
	.read    = waiters_read
};

void dlm_delete_debug_file(struct dlm_ls *ls)
{
	if (ls->ls_debug_rsb_dentry)
		debugfs_remove(ls->ls_debug_rsb_dentry);
	if (ls->ls_debug_waiters_dentry)
		debugfs_remove(ls->ls_debug_waiters_dentry);
	if (ls->ls_debug_locks_dentry)
		debugfs_remove(ls->ls_debug_locks_dentry);
	if (ls->ls_debug_all_dentry)
		debugfs_remove(ls->ls_debug_all_dentry);
}

int dlm_create_debug_file(struct dlm_ls *ls)
{
	char name[DLM_LOCKSPACE_LEN+8];

	/* format 1 */

	ls->ls_debug_rsb_dentry = debugfs_create_file(ls->ls_name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
						      &format1_fops);
	if (!ls->ls_debug_rsb_dentry)
		goto fail;

	/* format 2 */

	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_locks", ls->ls_name);

	ls->ls_debug_locks_dentry = debugfs_create_file(name,
							S_IFREG | S_IRUGO,
							dlm_root,
							ls,
							&format2_fops);
	if (!ls->ls_debug_locks_dentry)
		goto fail;

	/* format 3 */

	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_all", ls->ls_name);

	ls->ls_debug_all_dentry = debugfs_create_file(name,
						      S_IFREG | S_IRUGO,
						      dlm_root,
						      ls,
						      &format3_fops);
	if (!ls->ls_debug_all_dentry)
		goto fail;

	memset(name, 0, sizeof(name));
	snprintf(name, DLM_LOCKSPACE_LEN+8, "%s_waiters", ls->ls_name);

	ls->ls_debug_waiters_dentry = debugfs_create_file(name,
							  S_IFREG | S_IRUGO,
							  dlm_root,
							  ls,
							  &waiters_fops);
	if (!ls->ls_debug_waiters_dentry)
		goto fail;

	return 0;

 fail:
	dlm_delete_debug_file(ls);
	return -ENOMEM;
}

int __init dlm_register_debugfs(void)
{
	mutex_init(&debug_buf_lock);
	dlm_root = debugfs_create_dir("dlm", NULL);
	return dlm_root ? 0 : -ENOMEM;
}

void dlm_unregister_debugfs(void)
{
	debugfs_remove(dlm_root);
}