summaryrefslogtreecommitdiff
path: root/drivers/md/dm-vdo/message-stats.c
blob: 5be8503ed96eb907cb6785174155f5fff6974d43 (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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright 2023 Red Hat
 */

#include "dedupe.h"
#include "logger.h"
#include "memory-alloc.h"
#include "message-stats.h"
#include "statistics.h"
#include "thread-device.h"
#include "vdo.h"

static int write_u64(char *prefix,
		     u64 value,
		     char *suffix,
		     char **buf,
		     unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%llu%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_u32(char *prefix,
		     u32 value,
		     char *suffix,
		     char **buf,
		     unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%u%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_block_count_t(char *prefix,
			       block_count_t value,
			       char *suffix,
			       char **buf,
			       unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%llu%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_string(char *prefix,
			char *value,
			char *suffix,
			char **buf,
			unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%s%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_bool(char *prefix,
		      bool value,
		      char *suffix,
		      char **buf,
		      unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%d%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_u8(char *prefix,
		    u8 value,
		    char *suffix,
		    char **buf,
		    unsigned int *maxlen)
{
	int count = scnprintf(*buf, *maxlen, "%s%u%s",
			      prefix == NULL ? "" : prefix,
			      value,
			      suffix == NULL ? "" : suffix);
	*buf += count;
	*maxlen -= count;
	if (count >= *maxlen)
		return VDO_UNEXPECTED_EOF;
	return VDO_SUCCESS;
}

static int write_block_allocator_statistics(char *prefix,
					    struct block_allocator_statistics *stats,
					    char *suffix,
					    char **buf,
					    unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The total number of slabs from which blocks may be allocated */
	result = write_u64("slabCount : ",
			   stats->slab_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The total number of slabs from which blocks have ever been allocated */
	result = write_u64("slabsOpened : ",
			   stats->slabs_opened,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The number of times since loading that a slab has been re-opened */
	result = write_u64("slabsReopened : ",
			   stats->slabs_reopened,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_commit_statistics(char *prefix,
				   struct commit_statistics *stats,
				   char *suffix,
				   char **buf,
				   unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The total number of items on which processing has started */
	result = write_u64("started : ",
			   stats->started,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The total number of items for which a write operation has been issued */
	result = write_u64("written : ",
			   stats->written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The total number of items for which a write operation has completed */
	result = write_u64("committed : ",
			   stats->committed,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_recovery_journal_statistics(char *prefix,
					     struct recovery_journal_statistics *stats,
					     char *suffix,
					     char **buf,
					     unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the on-disk journal was full */
	result = write_u64("diskFull : ",
			   stats->disk_full,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the recovery journal requested slab journal commits. */
	result = write_u64("slabJournalCommitsRequested : ",
			   stats->slab_journal_commits_requested,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Write/Commit totals for individual journal entries */
	result = write_commit_statistics("entries : ",
					 &stats->entries,
					 ", ",
					 buf,
					 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Write/Commit totals for journal blocks */
	result = write_commit_statistics("blocks : ",
					 &stats->blocks,
					 ", ",
					 buf,
					 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_packer_statistics(char *prefix,
				   struct packer_statistics *stats,
				   char *suffix,
				   char **buf,
				   unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of compressed data items written since startup */
	result = write_u64("compressedFragmentsWritten : ",
			   stats->compressed_fragments_written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of blocks containing compressed items written since startup */
	result = write_u64("compressedBlocksWritten : ",
			   stats->compressed_blocks_written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of VIOs that are pending in the packer */
	result = write_u64("compressedFragmentsInPacker : ",
			   stats->compressed_fragments_in_packer,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_slab_journal_statistics(char *prefix,
					 struct slab_journal_statistics *stats,
					 char *suffix,
					 char **buf,
					 unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the on-disk journal was full */
	result = write_u64("diskFullCount : ",
			   stats->disk_full_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times an entry was added over the flush threshold */
	result = write_u64("flushCount : ",
			   stats->flush_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times an entry was added over the block threshold */
	result = write_u64("blockedCount : ",
			   stats->blocked_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times a tail block was written */
	result = write_u64("blocksWritten : ",
			   stats->blocks_written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times we had to wait for the tail to write */
	result = write_u64("tailBusyCount : ",
			   stats->tail_busy_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_slab_summary_statistics(char *prefix,
					 struct slab_summary_statistics *stats,
					 char *suffix,
					 char **buf,
					 unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of blocks written */
	result = write_u64("blocksWritten : ",
			   stats->blocks_written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_ref_counts_statistics(char *prefix,
				       struct ref_counts_statistics *stats,
				       char *suffix,
				       char **buf,
				       unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of reference blocks written */
	result = write_u64("blocksWritten : ",
			   stats->blocks_written,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_block_map_statistics(char *prefix,
				      struct block_map_statistics *stats,
				      char *suffix,
				      char **buf,
				      unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of dirty (resident) pages */
	result = write_u32("dirtyPages : ",
			   stats->dirty_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of clean (resident) pages */
	result = write_u32("cleanPages : ",
			   stats->clean_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of free pages */
	result = write_u32("freePages : ",
			   stats->free_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of pages in failed state */
	result = write_u32("failedPages : ",
			   stats->failed_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of pages incoming */
	result = write_u32("incomingPages : ",
			   stats->incoming_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of pages outgoing */
	result = write_u32("outgoingPages : ",
			   stats->outgoing_pages,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* how many times free page not avail */
	result = write_u32("cachePressure : ",
			   stats->cache_pressure,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of get_vdo_page() calls for read */
	result = write_u64("readCount : ",
			   stats->read_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of get_vdo_page() calls for write */
	result = write_u64("writeCount : ",
			   stats->write_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of times pages failed to read */
	result = write_u64("failedReads : ",
			   stats->failed_reads,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of times pages failed to write */
	result = write_u64("failedWrites : ",
			   stats->failed_writes,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets that are reclaimed */
	result = write_u64("reclaimed : ",
			   stats->reclaimed,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets for outgoing pages */
	result = write_u64("readOutgoing : ",
			   stats->read_outgoing,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets that were already there */
	result = write_u64("foundInCache : ",
			   stats->found_in_cache,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets requiring discard */
	result = write_u64("discardRequired : ",
			   stats->discard_required,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets enqueued for their page */
	result = write_u64("waitForPage : ",
			   stats->wait_for_page,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of gets that have to fetch */
	result = write_u64("fetchRequired : ",
			   stats->fetch_required,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of page fetches */
	result = write_u64("pagesLoaded : ",
			   stats->pages_loaded,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of page saves */
	result = write_u64("pagesSaved : ",
			   stats->pages_saved,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* the number of flushes issued */
	result = write_u64("flushCount : ",
			   stats->flush_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_hash_lock_statistics(char *prefix,
				      struct hash_lock_statistics *stats,
				      char *suffix,
				      char **buf,
				      unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the UDS advice proved correct */
	result = write_u64("dedupeAdviceValid : ",
			   stats->dedupe_advice_valid,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the UDS advice proved incorrect */
	result = write_u64("dedupeAdviceStale : ",
			   stats->dedupe_advice_stale,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of writes with the same data as another in-flight write */
	result = write_u64("concurrentDataMatches : ",
			   stats->concurrent_data_matches,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of writes whose hash collided with an in-flight write */
	result = write_u64("concurrentHashCollisions : ",
			   stats->concurrent_hash_collisions,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Current number of dedupe queries that are in flight */
	result = write_u32("currDedupeQueries : ",
			   stats->curr_dedupe_queries,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_error_statistics(char *prefix,
				  struct error_statistics *stats,
				  char *suffix,
				  char **buf,
				  unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of times VDO got an invalid dedupe advice PBN from UDS */
	result = write_u64("invalidAdvicePBNCount : ",
			   stats->invalid_advice_pbn_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of times a VIO completed with a VDO_NO_SPACE error */
	result = write_u64("noSpaceErrorCount : ",
			   stats->no_space_error_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of times a VIO completed with a VDO_READ_ONLY error */
	result = write_u64("readOnlyErrorCount : ",
			   stats->read_only_error_count,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_bio_stats(char *prefix,
			   struct bio_stats *stats,
			   char *suffix,
			   char **buf,
			   unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of REQ_OP_READ bios */
	result = write_u64("read : ",
			   stats->read,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of REQ_OP_WRITE bios with data */
	result = write_u64("write : ",
			   stats->write,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of bios tagged with REQ_PREFLUSH and containing no data */
	result = write_u64("emptyFlush : ",
			   stats->empty_flush,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of REQ_OP_DISCARD bios */
	result = write_u64("discard : ",
			   stats->discard,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of bios tagged with REQ_PREFLUSH */
	result = write_u64("flush : ",
			   stats->flush,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of bios tagged with REQ_FUA */
	result = write_u64("fua : ",
			   stats->fua,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_memory_usage(char *prefix,
			      struct memory_usage *stats,
			      char *suffix,
			      char **buf,
			      unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Tracked bytes currently allocated. */
	result = write_u64("bytesUsed : ",
			   stats->bytes_used,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Maximum tracked bytes allocated. */
	result = write_u64("peakBytesUsed : ",
			   stats->peak_bytes_used,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_index_statistics(char *prefix,
				  struct index_statistics *stats,
				  char *suffix,
				  char **buf,
				  unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of records stored in the index */
	result = write_u64("entriesIndexed : ",
			   stats->entries_indexed,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of post calls that found an existing entry */
	result = write_u64("postsFound : ",
			   stats->posts_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of post calls that added a new entry */
	result = write_u64("postsNotFound : ",
			   stats->posts_not_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of query calls that found an existing entry */
	result = write_u64("queriesFound : ",
			   stats->queries_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of query calls that added a new entry */
	result = write_u64("queriesNotFound : ",
			   stats->queries_not_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of update calls that found an existing entry */
	result = write_u64("updatesFound : ",
			   stats->updates_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of update calls that added a new entry */
	result = write_u64("updatesNotFound : ",
			   stats->updates_not_found,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of entries discarded */
	result = write_u64("entriesDiscarded : ",
			   stats->entries_discarded,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

static int write_vdo_statistics(char *prefix,
				struct vdo_statistics *stats,
				char *suffix,
				char **buf,
				unsigned int *maxlen)
{
	int result;

	result = write_string(prefix, "{ ", NULL, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_u32("version : ",
			   stats->version,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of blocks used for data */
	result = write_u64("dataBlocksUsed : ",
			   stats->data_blocks_used,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of blocks used for VDO metadata */
	result = write_u64("overheadBlocksUsed : ",
			   stats->overhead_blocks_used,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of logical blocks that are currently mapped to physical blocks */
	result = write_u64("logicalBlocksUsed : ",
			   stats->logical_blocks_used,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of physical blocks */
	result = write_block_count_t("physicalBlocks : ",
				     stats->physical_blocks,
				     ", ",
				     buf,
				     maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* number of logical blocks */
	result = write_block_count_t("logicalBlocks : ",
				     stats->logical_blocks,
				     ", ",
				     buf,
				     maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Size of the block map page cache, in bytes */
	result = write_u64("blockMapCacheSize : ",
			   stats->block_map_cache_size,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The physical block size */
	result = write_u64("blockSize : ",
			   stats->block_size,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the VDO has successfully recovered */
	result = write_u64("completeRecoveries : ",
			   stats->complete_recoveries,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the VDO has recovered from read-only mode */
	result = write_u64("readOnlyRecoveries : ",
			   stats->read_only_recoveries,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* String describing the operating mode of the VDO */
	result = write_string("mode : ",
			      stats->mode,
			      ", ",
			      buf,
			      maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Whether the VDO is in recovery mode */
	result = write_bool("inRecoveryMode : ",
			    stats->in_recovery_mode,
			    ", ",
			    buf,
			    maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* What percentage of recovery mode work has been completed */
	result = write_u8("recoveryPercentage : ",
			  stats->recovery_percentage,
			  ", ",
			  buf,
			  maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the compressed block packer */
	result = write_packer_statistics("packer : ",
					 &stats->packer,
					 ", ",
					 buf,
					 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Counters for events in the block allocator */
	result = write_block_allocator_statistics("allocator : ",
						  &stats->allocator,
						  ", ",
						  buf,
						  maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Counters for events in the recovery journal */
	result = write_recovery_journal_statistics("journal : ",
						   &stats->journal,
						   ", ",
						   buf,
						   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the slab journals */
	result = write_slab_journal_statistics("slabJournal : ",
					       &stats->slab_journal,
					       ", ",
					       buf,
					       maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the slab summary */
	result = write_slab_summary_statistics("slabSummary : ",
					       &stats->slab_summary,
					       ", ",
					       buf,
					       maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the reference counts */
	result = write_ref_counts_statistics("refCounts : ",
					     &stats->ref_counts,
					     ", ",
					     buf,
					     maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the block map */
	result = write_block_map_statistics("blockMap : ",
					    &stats->block_map,
					    ", ",
					    buf,
					    maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The dedupe statistics from hash locks */
	result = write_hash_lock_statistics("hashLock : ",
					    &stats->hash_lock,
					    ", ",
					    buf,
					    maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Counts of error conditions */
	result = write_error_statistics("errors : ",
					&stats->errors,
					", ",
					buf,
					maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The VDO instance */
	result = write_u32("instance : ",
			   stats->instance,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Current number of active VIOs */
	result = write_u32("currentVIOsInProgress : ",
			   stats->current_vios_in_progress,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Maximum number of active VIOs */
	result = write_u32("maxVIOs : ",
			   stats->max_vios,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of times the UDS index was too slow in responding */
	result = write_u64("dedupeAdviceTimeouts : ",
			   stats->dedupe_advice_timeouts,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Number of flush requests submitted to the storage device */
	result = write_u64("flushOut : ",
			   stats->flush_out,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Logical block size */
	result = write_u64("logicalBlockSize : ",
			   stats->logical_block_size,
			   ", ",
			   buf,
			   maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Bios submitted into VDO from above */
	result = write_bio_stats("biosIn : ",
				 &stats->bios_in,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosInPartial : ",
				 &stats->bios_in_partial,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Bios submitted onward for user data */
	result = write_bio_stats("biosOut : ",
				 &stats->bios_out,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Bios submitted onward for metadata */
	result = write_bio_stats("biosMeta : ",
				 &stats->bios_meta,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosJournal : ",
				 &stats->bios_journal,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosPageCache : ",
				 &stats->bios_page_cache,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosOutCompleted : ",
				 &stats->bios_out_completed,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosMetaCompleted : ",
				 &stats->bios_meta_completed,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosJournalCompleted : ",
				 &stats->bios_journal_completed,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosPageCacheCompleted : ",
				 &stats->bios_page_cache_completed,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosAcknowledged : ",
				 &stats->bios_acknowledged,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_bio_stats("biosAcknowledgedPartial : ",
				 &stats->bios_acknowledged_partial,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Current number of bios in progress */
	result = write_bio_stats("biosInProgress : ",
				 &stats->bios_in_progress,
				 ", ",
				 buf,
				 maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* Memory usage stats. */
	result = write_memory_usage("memoryUsage : ",
				    &stats->memory_usage,
				    ", ",
				    buf,
				    maxlen);
	if (result != VDO_SUCCESS)
		return result;
	/* The statistics for the UDS index */
	result = write_index_statistics("index : ",
					&stats->index,
					", ",
					buf,
					maxlen);
	if (result != VDO_SUCCESS)
		return result;
	result = write_string(NULL, "}", suffix, buf, maxlen);
	if (result != VDO_SUCCESS)
		return result;
	return VDO_SUCCESS;
}

int vdo_write_stats(struct vdo *vdo,
		    char *buf,
		    unsigned int maxlen)
{
	struct vdo_statistics *stats;
	int result;

	result = uds_allocate(1, struct vdo_statistics, __func__, &stats);
	if (result != VDO_SUCCESS)
		return result;

	vdo_fetch_statistics(vdo, stats);
	result = write_vdo_statistics(NULL, stats, NULL, &buf, &maxlen);
	uds_free(stats);
	return result;
}