summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src/statedump.c
blob: 4600817858158891f3010ac7b232b2f5da2181ea (plain)
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
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/*
  Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
  This file is part of GlusterFS.

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include <stdarg.h>
#include "glusterfs.h"
#include "logging.h"
#include "iobuf.h"
#include "statedump.h"
#include "stack.h"
#include "common-utils.h"


#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif /* MALLOC_H */

/* We don't want gf_log in this function because it may cause
   'deadlock' with statedump. This is because statedump happens
   inside a signal handler and cannot afford to block on a lock.*/
#ifdef gf_log
# undef gf_log
#endif

#define GF_PROC_DUMP_IS_OPTION_ENABLED(opt)     \
        (dump_options.dump_##opt == _gf_true)

#define GF_PROC_DUMP_IS_XL_OPTION_ENABLED(opt)                  \
        (dump_options.xl_options.dump_##opt == _gf_true)

extern xlator_t global_xlator;

static pthread_mutex_t  gf_proc_dump_mutex;
static int gf_dump_fd = -1;
gf_dump_options_t dump_options;

static strfd_t *gf_dump_strfd = NULL;

static void
gf_proc_dump_lock (void)
{
        pthread_mutex_lock (&gf_proc_dump_mutex);
}


static void
gf_proc_dump_unlock (void)
{
        pthread_mutex_unlock (&gf_proc_dump_mutex);
}

static int
gf_proc_dump_open (char *tmpname)
{
        int  dump_fd = -1;

        dump_fd = mkstemp (tmpname);
        if (dump_fd < 0)
                return -1;

        gf_dump_fd = dump_fd;
        return 0;
}

static void
gf_proc_dump_close (void)
{
        close (gf_dump_fd);
        gf_dump_fd = -1;
}

static int
gf_proc_dump_set_path (char *dump_options_file)
{
        int     ret = -1;
        FILE    *fp = NULL;
        char    buf[256];
        char    *key = NULL, *value = NULL;
        char    *saveptr = NULL;

        fp = fopen (dump_options_file, "r");
        if (!fp)
                goto out;

        ret = fscanf (fp, "%s", buf);

        while (ret != EOF) {
                key = strtok_r (buf, "=", &saveptr);
                if (!key) {
                        ret = fscanf (fp, "%s", buf);
                        continue;
                }

                value = strtok_r (NULL, "=", &saveptr);

                if (!value) {
                        ret = fscanf (fp, "%s", buf);
                        continue;
                }
                if (!strcmp (key, "path")) {
                        dump_options.dump_path = gf_strdup (value);
                        break;
                }
        }

out:
        if (fp)
                fclose (fp);
        return ret;
}

int
gf_proc_dump_add_section_fd (char *key, va_list ap)
{

        char buf[GF_DUMP_MAX_BUF_LEN];

        GF_ASSERT(key);

        memset (buf, 0, sizeof(buf));
        snprintf (buf, GF_DUMP_MAX_BUF_LEN, "\n[");
        vsnprintf (buf + strlen(buf),
                   GF_DUMP_MAX_BUF_LEN - strlen (buf), key, ap);
        snprintf (buf + strlen(buf),
                  GF_DUMP_MAX_BUF_LEN - strlen (buf),  "]\n");
        return write (gf_dump_fd, buf, strlen (buf));
}


int
gf_proc_dump_add_section_strfd (char *key, va_list ap)
{
	int ret = 0;

        ret += strprintf (gf_dump_strfd, "[");
	ret += strvprintf (gf_dump_strfd, key, ap);
	ret += strprintf (gf_dump_strfd,  "]\n");

	return ret;
}


int
gf_proc_dump_add_section (char *key, ...)
{
	va_list ap;
	int ret = 0;

	va_start (ap, key);
	if (gf_dump_strfd)
		ret = gf_proc_dump_add_section_strfd (key, ap);
	else
		ret = gf_proc_dump_add_section_fd (key, ap);
	va_end (ap);

	return ret;
}


int
gf_proc_dump_write_fd (char *key, char *value, va_list ap)
{

        char         buf[GF_DUMP_MAX_BUF_LEN];
        int          offset = 0;

        GF_ASSERT (key);

        offset = strlen (key);

        memset (buf, 0, GF_DUMP_MAX_BUF_LEN);
        snprintf (buf, GF_DUMP_MAX_BUF_LEN, "%s", key);
        snprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, "=");
        offset += 1;
        vsnprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, value, ap);

        offset = strlen (buf);
        snprintf (buf + offset, GF_DUMP_MAX_BUF_LEN - offset, "\n");
        return write (gf_dump_fd, buf, strlen (buf));
}


int
gf_proc_dump_write_strfd (char *key, char *value, va_list ap)
{
	int ret = 0;

	ret += strprintf (gf_dump_strfd, "%s = ", key);
	ret += strvprintf (gf_dump_strfd, value, ap);
	ret += strprintf (gf_dump_strfd, "\n");

	return ret;
}


int
gf_proc_dump_write (char *key, char *value, ...)
{
	int ret = 0;
	va_list ap;

	va_start (ap, value);
	if (gf_dump_strfd)
		ret = gf_proc_dump_write_strfd (key, value, ap);
	else
		ret = gf_proc_dump_write_fd (key, value, ap);
	va_end (ap);

	return ret;
}


static void
gf_proc_dump_xlator_mem_info (xlator_t *xl)
{
        int     i = 0;
        struct mem_acct rec = {0,};

        if (!xl)
                return;

        if (!xl->mem_acct.rec)
                return;

        gf_proc_dump_add_section ("%s.%s - Memory usage", xl->type, xl->name);
        gf_proc_dump_write ("num_types", "%d", xl->mem_acct.num_types);

        for (i = 0; i < xl->mem_acct.num_types; i++) {
                if (!(memcmp (&xl->mem_acct.rec[i], &rec,
                              sizeof (struct mem_acct))))
                        continue;

                gf_proc_dump_add_section ("%s.%s - usage-type %s memusage",
                                          xl->type, xl->name,
                                          xl->mem_acct.rec[i].typestr);
                gf_proc_dump_write ("size", "%u", xl->mem_acct.rec[i].size);
                gf_proc_dump_write ("num_allocs", "%u",
                                    xl->mem_acct.rec[i].num_allocs);
                gf_proc_dump_write ("max_size", "%u",
                                    xl->mem_acct.rec[i].max_size);
                gf_proc_dump_write ("max_num_allocs", "%u",
                                    xl->mem_acct.rec[i].max_num_allocs);
                gf_proc_dump_write ("total_allocs", "%u",
                                    xl->mem_acct.rec[i].total_allocs);
        }

        return;
}

static void
gf_proc_dump_xlator_mem_info_only_in_use (xlator_t *xl)
{
        int     i = 0;

        if (!xl)
                return;

        if (!xl->mem_acct.rec)
                return;

        gf_proc_dump_add_section ("%s.%s - Memory usage", xl->type, xl->name);
        gf_proc_dump_write ("num_types", "%d", xl->mem_acct.num_types);

        for (i = 0; i < xl->mem_acct.num_types; i++) {
                if (!xl->mem_acct.rec[i].size)
                        continue;

                gf_proc_dump_add_section ("%s.%s - usage-type %d", xl->type,
                                          xl->name,i);

                gf_proc_dump_write ("size", "%u",
                                    xl->mem_acct.rec[i].size);
                gf_proc_dump_write ("max_size", "%u",
                                    xl->mem_acct.rec[i].max_size);
                gf_proc_dump_write ("num_allocs", "%u",
                                    xl->mem_acct.rec[i].num_allocs);
                gf_proc_dump_write ("max_num_allocs", "%u",
                                    xl->mem_acct.rec[i].max_num_allocs);
                gf_proc_dump_write ("total_allocs", "%u",
                                    xl->mem_acct.rec[i].total_allocs);
        }

        return;
}



/* Currently this dumps only mallinfo. More can be built on here */
void
gf_proc_dump_mem_info ()
{
#ifdef HAVE_MALLOC_STATS
        struct mallinfo info;

        memset (&info, 0, sizeof (struct mallinfo));
        info = mallinfo ();

        gf_proc_dump_add_section ("mallinfo");
        gf_proc_dump_write ("mallinfo_arena", "%d", info.arena);
        gf_proc_dump_write ("mallinfo_ordblks", "%d", info.ordblks);
        gf_proc_dump_write ("mallinfo_smblks", "%d", info.smblks);
        gf_proc_dump_write ("mallinfo_hblks", "%d", info.hblks);
        gf_proc_dump_write ("mallinfo_hblkhd", "%d", info.hblkhd);
        gf_proc_dump_write ("mallinfo_usmblks", "%d", info.usmblks);
        gf_proc_dump_write ("mallinfo_fsmblks", "%d", info.fsmblks);
        gf_proc_dump_write ("mallinfo_uordblks", "%d", info.uordblks);
        gf_proc_dump_write ("mallinfo_fordblks", "%d", info.fordblks);
        gf_proc_dump_write ("mallinfo_keepcost", "%d", info.keepcost);
#endif
        gf_proc_dump_xlator_mem_info(&global_xlator);

}

void
gf_proc_dump_mem_info_to_dict (dict_t *dict)
{
        if (!dict)
                return;
#ifdef HAVE_MALLOC_STATS
        struct  mallinfo info;
        int     ret = -1;

        memset (&info, 0, sizeof(struct mallinfo));
        info = mallinfo ();

        ret = dict_set_int32 (dict, "mallinfo.arena", info.arena);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.ordblks", info.ordblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.smblks", info.smblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.hblks", info.hblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.hblkhd", info.hblkhd);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.usmblks", info.usmblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.fsmblks", info.fsmblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.uordblks", info.uordblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.fordblks", info.fordblks);
        if (ret)
                return;

        ret = dict_set_int32 (dict, "mallinfo.keepcost", info.keepcost);
        if (ret)
                return;
#endif
        return;
}

void
gf_proc_dump_mempool_info (glusterfs_ctx_t *ctx)
{
        struct mem_pool *pool = NULL;

        gf_proc_dump_add_section ("mempool");

        list_for_each_entry (pool, &ctx->mempool_list, global_list) {
                gf_proc_dump_write ("-----", "-----");
                gf_proc_dump_write ("pool-name", "%s", pool->name);
                gf_proc_dump_write ("hot-count", "%d", pool->hot_count);
                gf_proc_dump_write ("cold-count", "%d", pool->cold_count);
                gf_proc_dump_write ("padded_sizeof", "%lu",
                                    pool->padded_sizeof_type);
                gf_proc_dump_write ("alloc-count", "%"PRIu64, pool->alloc_count);
                gf_proc_dump_write ("max-alloc", "%d", pool->max_alloc);

                gf_proc_dump_write ("pool-misses", "%"PRIu64, pool->pool_misses);
                gf_proc_dump_write ("max-stdalloc", "%d", pool->max_stdalloc);
        }
}

void
gf_proc_dump_mempool_info_to_dict (glusterfs_ctx_t *ctx, dict_t *dict)
{
        struct mem_pool *pool = NULL;
        char            key[GF_DUMP_MAX_BUF_LEN] = {0,};
        int             count = 0;
        int             ret = -1;

        if (!ctx || !dict)
                return;

        list_for_each_entry (pool, &ctx->mempool_list, global_list) {
                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.name", count);
                ret = dict_set_str (dict, key, pool->name);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.hotcount", count);
                ret = dict_set_int32 (dict, key, pool->hot_count);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.coldcount", count);
                ret = dict_set_int32 (dict, key, pool->cold_count);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.paddedsizeof", count);
                ret = dict_set_uint64 (dict, key, pool->padded_sizeof_type);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.alloccount", count);
                ret = dict_set_uint64 (dict, key, pool->alloc_count);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.max_alloc", count);
                ret = dict_set_int32 (dict, key, pool->max_alloc);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.max-stdalloc", count);
                ret = dict_set_int32 (dict, key, pool->max_stdalloc);
                if (ret)
                        return;

                memset (key, 0, sizeof (key));
                snprintf (key, sizeof (key), "pool%d.pool-misses", count);
                ret = dict_set_uint64 (dict, key, pool->pool_misses);
                if (ret)
                        return;
                count++;
        }
        ret = dict_set_int32 (dict, "mempool-count", count);

        return;
}

void gf_proc_dump_latency_info (xlator_t *xl);

void
gf_proc_dump_xlator_info (xlator_t *top)
{
        xlator_t        *trav = NULL;
        glusterfs_ctx_t *ctx = NULL;
        char             itable_key[1024] = {0,};

        if (!top)
                return;

        ctx = top->ctx;

        trav = top;
        while (trav) {

                if (ctx->measure_latency)
                        gf_proc_dump_latency_info (trav);

                gf_proc_dump_xlator_mem_info(trav);

                if (GF_PROC_DUMP_IS_XL_OPTION_ENABLED (inode) &&
                    (trav->itable)) {
                        snprintf (itable_key, 1024, "%d.%s.itable",
                                  ctx->graph_id, trav->name);
                }

                if (!trav->dumpops) {
                        trav = trav->next;
                        continue;
                }

                if (trav->dumpops->priv &&
                    GF_PROC_DUMP_IS_XL_OPTION_ENABLED (priv))
                        trav->dumpops->priv (trav);

                if (GF_PROC_DUMP_IS_XL_OPTION_ENABLED (inode) &&
                    (trav->dumpops->inode))
                        trav->dumpops->inode (trav);

                if (trav->dumpops->fd &&
                    GF_PROC_DUMP_IS_XL_OPTION_ENABLED (fd))
                        trav->dumpops->fd (trav);

                if (trav->dumpops->history &&
                    GF_PROC_DUMP_IS_XL_OPTION_ENABLED (history))
                        trav->dumpops->history (trav);

                trav = trav->next;
        }

        return;
}

static void
gf_proc_dump_oldgraph_xlator_info (xlator_t *top)
{
        xlator_t        *trav = NULL;

        if (!top)
                return;

        trav = top;
        while (trav) {
                gf_proc_dump_xlator_mem_info_only_in_use (trav);

                if (GF_PROC_DUMP_IS_XL_OPTION_ENABLED (inode) &&
                    (trav->itable)) {
                        /*TODO: dump inode table info if necessary by
                          printing the graph id (taken by glusterfs_cbtx_t)
                          in the key
                        */
                }

                if (!trav->dumpops) {
                        trav = trav->next;
                        continue;
                }

                if (GF_PROC_DUMP_IS_XL_OPTION_ENABLED (inode) &&
                    (trav->dumpops->inode))
                        trav->dumpops->inode (trav);

                if (trav->dumpops->fd &&
                    GF_PROC_DUMP_IS_XL_OPTION_ENABLED (fd))
                        trav->dumpops->fd (trav);

                trav = trav->next;
        }

        return;
}

static int
gf_proc_dump_enable_all_options ()
{

        GF_PROC_DUMP_SET_OPTION (dump_options.dump_mem, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_iobuf, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_callpool, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_priv, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_inode, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_fd, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_inodectx,
                                 _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_fdctx, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_history,
                                 _gf_true);

        return 0;
}

gf_boolean_t
is_gf_proc_dump_all_disabled ()
{
        gf_boolean_t all_disabled = _gf_true;

        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.dump_mem, all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.dump_iobuf, all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.dump_callpool, all_disabled,
                                   out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_priv,
                                   all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_inode,
                                   all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_fd,
                                   all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_inodectx,
                                   all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_fdctx,
                                   all_disabled, out);
        GF_CHECK_DUMP_OPTION_ENABLED (dump_options.xl_options.dump_history,
                                   all_disabled, out);

out:
        return all_disabled;
}

/* These options are dumped by default if glusterdump.options
   file exists and it is emtpty
*/
static int
gf_proc_dump_enable_default_options ()
{
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_mem, _gf_true);
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_callpool, _gf_true);

        return 0;
}

static int
gf_proc_dump_disable_all_options ()
{

        GF_PROC_DUMP_SET_OPTION (dump_options.dump_mem, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_iobuf, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.dump_callpool, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_priv, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_inode,
                                 _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_fd, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_inodectx,
                                 _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_fdctx, _gf_false);
        GF_PROC_DUMP_SET_OPTION (dump_options.xl_options.dump_history,
                                 _gf_false);
        return 0;
}

static int
gf_proc_dump_parse_set_option (char *key, char *value)
{
        gf_boolean_t    *opt_key = NULL;
        gf_boolean_t    opt_value = _gf_false;
        char buf[GF_DUMP_MAX_BUF_LEN];
        int ret = -1;

        if (!strcasecmp (key, "all")) {
                (void)gf_proc_dump_enable_all_options ();
                return 0;
        } else if (!strcasecmp (key, "mem")) {
                opt_key = &dump_options.dump_mem;
        } else if (!strcasecmp (key, "iobuf")) {
                opt_key = &dump_options.dump_iobuf;
        } else if (!strcasecmp (key, "callpool")) {
                opt_key = &dump_options.dump_callpool;
        } else if (!strcasecmp (key, "priv")) {
                opt_key = &dump_options.xl_options.dump_priv;
        } else if (!strcasecmp (key, "fd")) {
                opt_key = &dump_options.xl_options.dump_fd;
        } else if (!strcasecmp (key, "inode")) {
                opt_key = &dump_options.xl_options.dump_inode;
        } else if (!strcasecmp (key, "inodectx")) {
                opt_key = &dump_options.xl_options.dump_inodectx;
        } else if (!strcasecmp (key, "fdctx")) {
                opt_key = &dump_options.xl_options.dump_fdctx;
        } else if (!strcasecmp (key, "history")) {
                opt_key = &dump_options.xl_options.dump_history;
        }

        if (!opt_key) {
                //None of dump options match the key, return back
                snprintf (buf, sizeof (buf), "[Warning]:None of the options "
                          "matched key : %s\n", key);
                ret = write (gf_dump_fd, buf, strlen (buf));

                if (ret >= 0)
                        ret = -1;
                goto out;

        }

        opt_value = (strncasecmp (value, "yes", 3) ?
                     _gf_false: _gf_true);

        GF_PROC_DUMP_SET_OPTION (*opt_key, opt_value);

        ret = 0;
out:
        return ret;
}

static int
gf_proc_dump_options_init ()
{
        int     ret = -1;
        FILE    *fp = NULL;
        char    buf[256];
        char    *key = NULL, *value = NULL;
        char    *saveptr = NULL;
        char    dump_option_file[PATH_MAX];

        /* glusterd will create a file glusterdump.<pid>.options and
           sets the statedump options for the process and the file is removed
           after the statedump is taken. Direct issue of SIGUSR1 does not have
           mechanism for considering the statedump options. So to have a way
           of configuring the statedump of all the glusterfs processes through
           both cli command and SIGUSR1, glusterdump.options file is searched
           and the options mentioned in it are given the higher priority.
        */
        snprintf (dump_option_file, sizeof (dump_option_file),
                  DEFAULT_VAR_RUN_DIRECTORY
                  "/glusterdump.options");
        fp = fopen (dump_option_file, "r");
        if (!fp) {
                snprintf (dump_option_file, sizeof (dump_option_file),
                          DEFAULT_VAR_RUN_DIRECTORY
                          "/glusterdump.%d.options", getpid ());

                fp = fopen (dump_option_file, "r");

                if (!fp) {
                        //ENOENT, return success
                        (void) gf_proc_dump_enable_all_options ();
                        return 0;
                }
        }

        (void) gf_proc_dump_disable_all_options ();

        // swallow the errors if setting statedump file path is failed.
        ret = gf_proc_dump_set_path (dump_option_file);

        ret = fscanf (fp, "%s", buf);

        while (ret != EOF) {
                key = strtok_r (buf, "=", &saveptr);
                if (!key) {
                        ret = fscanf (fp, "%s", buf);
                        continue;
                }

                value = strtok_r (NULL, "=", &saveptr);

                if (!value) {
                        ret = fscanf (fp, "%s", buf);
                        continue;
                }

                gf_proc_dump_parse_set_option (key, value);
        }

        if (is_gf_proc_dump_all_disabled ())
                (void) gf_proc_dump_enable_default_options ();

        if (fp)
                fclose (fp);

        return 0;
}

void
gf_proc_dump_info (int signum, glusterfs_ctx_t *ctx)
{
        int                i                       = 0;
        int                ret                     = -1;
        glusterfs_graph_t *trav                    = NULL;
        char               brick_name[PATH_MAX]    = {0,};
        char               timestr[256]            = {0,};
        char               sign_string[512]        = {0,};
        char               tmp_dump_name[PATH_MAX] = {0,};
        char               path[PATH_MAX]          = {0,};
        struct timeval     tv                      = {0,};

        gf_proc_dump_lock ();

        if (!ctx)
                goto out;

        if (ctx->cmd_args.brick_name) {
                GF_REMOVE_SLASH_FROM_PATH (ctx->cmd_args.brick_name, brick_name);
        } else
                strncpy (brick_name, "glusterdump", sizeof (brick_name));

        ret = gf_proc_dump_options_init ();
        if (ret < 0)
                goto out;

        snprintf (path, sizeof (path), "%s/%s.%d.dump.%"PRIu64,
                  ((dump_options.dump_path != NULL)?dump_options.dump_path:
                   ((ctx->statedump_path != NULL)?ctx->statedump_path:
                    DEFAULT_VAR_RUN_DIRECTORY)), brick_name, getpid(),
                  (uint64_t) time (NULL));

        snprintf (tmp_dump_name, PATH_MAX, "%s/dumpXXXXXX",
                  ((dump_options.dump_path != NULL)?dump_options.dump_path:
                   ((ctx->statedump_path != NULL)?ctx->statedump_path:
                    DEFAULT_VAR_RUN_DIRECTORY)));

        ret = gf_proc_dump_open (tmp_dump_name);
        if (ret < 0)
                goto out;

        //continue even though gettimeofday() has failed
        ret = gettimeofday (&tv, NULL);
        if (0 == ret) {
                gf_time_fmt (timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT);
                snprintf (timestr + strlen (timestr),
                          sizeof timestr - strlen (timestr),
                          ".%"GF_PRI_SUSECONDS, tv.tv_usec);
        }

        snprintf (sign_string, sizeof (sign_string), "DUMP-START-TIME: %s\n",
                  timestr);

        //swallow the errors of write for start and end marker
        ret = write (gf_dump_fd, sign_string, strlen (sign_string));

        memset (sign_string, 0, sizeof (sign_string));
        memset (timestr, 0, sizeof (timestr));
        memset (&tv, 0, sizeof (tv));

        if (GF_PROC_DUMP_IS_OPTION_ENABLED (mem)) {
                gf_proc_dump_mem_info ();
                gf_proc_dump_mempool_info (ctx);
        }

        if (GF_PROC_DUMP_IS_OPTION_ENABLED (iobuf))
                iobuf_stats_dump (ctx->iobuf_pool);
        if (GF_PROC_DUMP_IS_OPTION_ENABLED (callpool))
                gf_proc_dump_pending_frames (ctx->pool);

        if (ctx->master) {
                gf_proc_dump_add_section ("fuse");
                gf_proc_dump_xlator_info (ctx->master);
        }

        if (ctx->active) {
                gf_proc_dump_add_section ("active graph - %d", ctx->graph_id);
                gf_proc_dump_xlator_info (ctx->active->top);
        }

        i = 0;
        list_for_each_entry (trav, &ctx->graphs, list) {
                if (trav == ctx->active)
                        continue;

                gf_proc_dump_add_section ("oldgraph[%d]", i);

                gf_proc_dump_oldgraph_xlator_info (trav->top);
                i++;
        }

        ret = gettimeofday (&tv, NULL);
        if (0 == ret) {
                gf_time_fmt (timestr, sizeof timestr, tv.tv_sec, gf_timefmt_FT);
                snprintf (timestr + strlen (timestr),
                          sizeof timestr - strlen (timestr),
                          ".%"GF_PRI_SUSECONDS, tv.tv_usec);
        }

        snprintf (sign_string, sizeof (sign_string), "\nDUMP-END-TIME: %s",
                  timestr);
        ret = write (gf_dump_fd, sign_string, strlen (sign_string));

out:
        if (gf_dump_fd != -1)
                gf_proc_dump_close ();
        rename (tmp_dump_name, path);
        GF_FREE (dump_options.dump_path);
        dump_options.dump_path = NULL;
        gf_proc_dump_unlock ();

        return;
}


void
gf_proc_dump_fini (void)
{
        pthread_mutex_destroy (&gf_proc_dump_mutex);
}


void
gf_proc_dump_init ()
{
        pthread_mutex_init (&gf_proc_dump_mutex, NULL);

        return;
}


void
gf_proc_dump_cleanup (void)
{
        pthread_mutex_destroy (&gf_proc_dump_mutex);
}


void
gf_proc_dump_xlator_private (xlator_t *this, strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;

		if (this->dumpops && this->dumpops->priv)
			this->dumpops->priv (this);

		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}


void
gf_proc_dump_mallinfo (strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;

		gf_proc_dump_mem_info ();

		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}


void
gf_proc_dump_xlator_history (xlator_t *this, strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;

		if (this->dumpops && this->dumpops->history)
			this->dumpops->history (this);

		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}


void
gf_proc_dump_xlator_itable (xlator_t *this, strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;


		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}


void
gf_proc_dump_xlator_meminfo (xlator_t *this, strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;

		gf_proc_dump_xlator_mem_info (this);

		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}


void
gf_proc_dump_xlator_profile (xlator_t *this, strfd_t *strfd)
{
        gf_proc_dump_lock ();
	{
		gf_dump_strfd = strfd;

		gf_proc_dump_latency_info (this);

		gf_dump_strfd = NULL;
	}
	gf_proc_dump_unlock ();
}