summaryrefslogtreecommitdiffstats
path: root/plugins/virtium/virtium-nvme.c
blob: d9fc54e5bb73c84ddcbbdbefdc445827a75174d4 (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
// SPDX-License-Identifier: GPL-2.0-or-later
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <inttypes.h>
#include <stdbool.h>
#include <time.h>
#include <locale.h>

#include "common.h"
#include "nvme.h"
#include "libnvme.h"
#include "plugin.h"

#define CREATE_CMD
#include "virtium-nvme.h"

#define MIN2(a, b) ( ((a) < (b))? (a) : (b))

#define HOUR_IN_SECONDS     3600

#define MAX_HEADER_BUFF     (20 * 1024)
#define MAX_LOG_BUFF        4096
#define DEFAULT_TEST_NAME   "Put the name of your test here"

static char vt_default_log_file_name[256];

struct vtview_log_header {
	char			path[256];
	char			test_name[256];
	long int		time_stamp;
	struct nvme_id_ctrl	raw_ctrl;
	struct nvme_firmware_slot raw_fw;
};

struct vtview_smart_log_entry {
	char	path[256];
	long int	time_stamp;
	struct nvme_id_ns	raw_ns;
	struct nvme_id_ctrl	raw_ctrl;
	struct nvme_smart_log	raw_smart;
};

struct vtview_save_log_settings {
	double	run_time_hrs;
	double	log_record_frequency_hrs;
	const char*	output_file;
	const char*	test_name;
};

static long double int128_to_double(__u8 *data)
{
	int i;
	long double result = 0;

	for (i = 0; i < 16; i++) {
		result *= 256;
		result += data[15 - i];
	}
	return result;
}

static void vt_initialize_header_buffer(struct vtview_log_header *pbuff)
{
	memset(pbuff->path, 0, sizeof(pbuff->path));
	memset(pbuff->test_name, 0, sizeof(pbuff->test_name));
}

static void vt_convert_data_buffer_to_hex_string(const unsigned char *bufPtr,
		const unsigned int size, const bool isReverted, char *output)
{
	unsigned int i, pos;
	const char hextable[16] = {
		'0', '1', '2', '3',
		'4', '5', '6', '7',
		'8', '9', 'A', 'B',
		'C', 'D', 'E', 'F',
	};

	memset(output, 0, (size * 2) + 1);

	for (i = 0; i < size; i++) {
		if(isReverted)
			pos = size - 1 - i;
		else
			pos = i;
		output[2 * i] = hextable[(bufPtr[pos] & 0xF0) >> 4];
		output[2 * i + 1] = hextable[(bufPtr[pos] & 0x0F)];
	}
}

/*
 * Generate log file name.
 * Log file name will be generated automatically if user leave log file option blank.
 * Log file name will be generated as vtView-Smart-log-date-time.txt
 */
static void vt_generate_vtview_log_file_name(char* fname)
{
	time_t     current;
	struct tm  tstamp;
	char       temp[256];

	time(&current);

	tstamp = *localtime(&current);
	snprintf(temp, sizeof(temp), "./vtView-Smart-log-");
	strcat(fname, temp);
	strftime(temp, sizeof(temp), "%Y-%m-%d", &tstamp);
	strcat(fname, temp);
	snprintf(temp, sizeof(temp), ".txt");
	strcat(fname, temp);
}

static void vt_convert_smart_data_to_human_readable_format(struct vtview_smart_log_entry *smart, char *text)
{
	char tempbuff[1024] = "";
	int i;
	int temperature = ((smart->raw_smart.temperature[1] << 8) | smart->raw_smart.temperature[0]) - 273;
	double capacity;
	char *curlocale;
	char *templocale;
	__u8 lba_index;
	nvme_id_ns_flbas_to_lbaf_inuse(smart->raw_ns.flbas, &lba_index);

	curlocale = setlocale(LC_ALL, NULL);
	templocale = strdup(curlocale);

	if (NULL == templocale)
		printf("Cannot malloc buffer\n");

	setlocale(LC_ALL, "C");

	unsigned long long int lba = 1ULL << smart->raw_ns.lbaf[lba_index].ds;
	capacity = le64_to_cpu(smart->raw_ns.nsze) * lba;

	snprintf(tempbuff, sizeof(tempbuff), "log;%s;%lu;%s;%s;%-.*s;", smart->raw_ctrl.sn, smart->time_stamp, smart->path,
		smart->raw_ctrl.mn, (int)sizeof(smart->raw_ctrl.fr), smart->raw_ctrl.fr);
	strcpy(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Capacity;%lf;", capacity / 1000000000);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Critical_Warning;%u;", smart->raw_smart.critical_warning);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Temperature;%u;", temperature);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Available_Spare;%u;", smart->raw_smart.avail_spare);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Available_Spare_Threshold;%u;", smart->raw_smart.spare_thresh);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Percentage_Used;%u;", smart->raw_smart.percent_used);
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Data_Units_Read;%0.Lf;", int128_to_double(smart->raw_smart.data_units_read));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Data_Units_Written;%0.Lf;", int128_to_double(smart->raw_smart.data_units_written));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Host_Read_Commands;%0.Lf;", int128_to_double(smart->raw_smart.host_reads));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Host_Write_Commands;%0.Lf;", int128_to_double(smart->raw_smart.host_writes));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Controller_Busy_Time;%0.Lf;", int128_to_double(smart->raw_smart.ctrl_busy_time));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Power_Cycles;%0.Lf;", int128_to_double(smart->raw_smart.power_cycles));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Power_On_Hours;%0.Lf;", int128_to_double(smart->raw_smart.power_on_hours));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Unsafe_Shutdowns;%0.Lf;", int128_to_double(smart->raw_smart.unsafe_shutdowns));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Media_Errors;%0.Lf;", int128_to_double(smart->raw_smart.media_errors));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Num_Err_Log_Entries;%0.Lf;", int128_to_double(smart->raw_smart.num_err_log_entries));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Warning_Temperature_Time;%u;", le32_to_cpu(smart->raw_smart.warning_temp_time));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Critical_Composite_Temperature_Time;%u;", le32_to_cpu(smart->raw_smart.critical_comp_time));
	strcat(text, tempbuff);

	for (i = 0; i < 8; i++) {
		__s32 temp = le16_to_cpu(smart->raw_smart.temp_sensor[i]);
		if (0 == temp) {
			snprintf(tempbuff, sizeof(tempbuff), "Temperature_Sensor_%d;NC;", i);
			strcat(text, tempbuff);
			continue;
		}
		snprintf(tempbuff, sizeof(tempbuff), "Temperature_Sensor_%d;%d;", i, temp - 273);
		strcat(text, tempbuff);
	}

	snprintf(tempbuff, sizeof(tempbuff), "Thermal_Management_T1_Trans_Count;%u;", le32_to_cpu(smart->raw_smart.thm_temp1_trans_count));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Thermal_Management_T2_Trans_Count;%u;", le32_to_cpu(smart->raw_smart.thm_temp2_trans_count));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Thermal_Management_T1_Total_Time;%u;", le32_to_cpu(smart->raw_smart.thm_temp1_total_time));
	strcat(text, tempbuff);
	snprintf(tempbuff, sizeof(tempbuff), "Thermal_Management_T2_Total_Time;%u;", le32_to_cpu(smart->raw_smart.thm_temp2_total_time));
	strcat(text, tempbuff);

	snprintf(tempbuff, sizeof(tempbuff), "NandWrites;%d;\n", 0);
	strcat(text, tempbuff);

	setlocale(LC_ALL, templocale);
	free(templocale);
}

static void vt_header_to_string(const struct vtview_log_header *header, char *text)
{
	char timebuff[50] = "";
	char tempbuff[MAX_HEADER_BUFF] = "";
	char identext[16384] = "";
	char fwtext[2048] = "";

	strftime(timebuff, 50, "%Y-%m-%d %H:%M:%S", localtime(&(header->time_stamp)));
	snprintf(tempbuff, MAX_HEADER_BUFF, "header;{\"session\":{\"testName\":\"%s\",\"dateTime\":\"%s\"},",
		header->test_name, timebuff);
	strcpy(text, tempbuff);

	vt_convert_data_buffer_to_hex_string((unsigned char *)&(header->raw_ctrl), sizeof(header->raw_ctrl), false, identext);
	vt_convert_data_buffer_to_hex_string((unsigned char *)&(header->raw_fw), sizeof(header->raw_fw), false, fwtext);
	snprintf(tempbuff, MAX_HEADER_BUFF,
		"\"devices\":[{\"model\":\"%s\",\"port\":\"%s\",\"SN\":\"%s\",\"type\":\"NVMe\",\"identify\":\"%s\",\"firmwareSlot\":\"%s\"}]}\n",
		header->raw_ctrl.mn, header->path, header->raw_ctrl.sn, identext, fwtext);
	strcat(text, tempbuff);
}

static int vt_append_text_file(const char *text, const char *filename)
{
	FILE *f;

	f = fopen(filename, "a");
	if(NULL == f) {
		printf("Cannot open %s\n", filename);
		return -1;
	}

	fprintf(f, "%s", text);
	fclose(f);
	return 0;
}

static int vt_append_log(struct vtview_smart_log_entry *smart, const char *filename)
{
	char sm_log_text[MAX_LOG_BUFF] = "";

	vt_convert_smart_data_to_human_readable_format(smart, sm_log_text);
	return vt_append_text_file(sm_log_text, filename);
}

static int vt_append_header(const struct vtview_log_header *header, const char *filename)
{
	char header_text[MAX_HEADER_BUFF] = "";

	vt_header_to_string(header, header_text);
	return vt_append_text_file(header_text, filename);
}

static void vt_process_string(char *str, const size_t size)
{
	size_t i;

	if (size == 0)
		return;

	i = size - 1;
	while ((0 != i) && (' ' == str[i])) {
		str[i] = 0;
		i--;
	}
}

static int vt_add_entry_to_log(const int fd, const char *path, const struct vtview_save_log_settings *cfg)
{
	struct vtview_smart_log_entry smart;
	const char *filename;
	int ret = 0;
	unsigned nsid = 0;

	memset(smart.path, 0, sizeof(smart.path));
	strncpy(smart.path, path, sizeof(smart.path) - 1);
	if(NULL == cfg->output_file)
		filename = vt_default_log_file_name;
	else
		filename = cfg->output_file;

	smart.time_stamp = time(NULL);
	ret = nvme_get_nsid(fd, &nsid);

	if (ret < 0) {
		printf("Cannot read namespace-id\n");
		return -1;
	}

	ret = nvme_identify_ns(fd, nsid, &smart.raw_ns);
	if (ret) {
		printf("Cannot read namespace identify\n");
		return -1;
	}

	ret = nvme_identify_ctrl(fd, &smart.raw_ctrl);
	if (ret) {
		printf("Cannot read device identify controller\n");
		return -1;
	}

	ret = nvme_get_log_smart(fd, NVME_NSID_ALL, true, &smart.raw_smart);
	if (ret) {
		printf("Cannot read device SMART log\n");
		return -1;
	}

	vt_process_string(smart.raw_ctrl.sn, sizeof(smart.raw_ctrl.sn));
	vt_process_string(smart.raw_ctrl.mn, sizeof(smart.raw_ctrl.mn));

	ret = vt_append_log(&smart, filename);
	return (ret);
}

static int vt_update_vtview_log_header(const int fd, const char *path, const struct vtview_save_log_settings *cfg)
{
	struct vtview_log_header header;
	const char *filename;
	int ret = 0;

	vt_initialize_header_buffer(&header);
	if (strlen(path) > sizeof(header.path)) {
		printf("filename too long\n");
		errno = EINVAL;
		return -1;
	}
	strcpy(header.path, path);

	if (NULL == cfg->test_name)
		strcpy(header.test_name, DEFAULT_TEST_NAME);
	else {
		if (strlen(cfg->test_name) > sizeof(header.test_name)) {
			printf("test name too long\n");
			errno = EINVAL;
			return -1;
		}
		strcpy(header.test_name, cfg->test_name);
	}

	if(NULL == cfg->output_file)
		filename = vt_default_log_file_name;
	else
		filename = cfg->output_file;

	printf("Log file: %s\n", filename);
	header.time_stamp = time(NULL);

	ret = nvme_identify_ctrl(fd, &header.raw_ctrl);
	if (ret) {
		printf("Cannot read identify device\n");
		return -1;
	}

	ret = nvme_get_log_fw_slot(fd, true, &header.raw_fw);
	if (ret) {
		printf("Cannot read device firmware log\n");
		return -1;
	}

	vt_process_string(header.raw_ctrl.sn, sizeof(header.raw_ctrl.sn));
	vt_process_string(header.raw_ctrl.mn, sizeof(header.raw_ctrl.mn));

	ret = vt_append_header(&header, filename);
	return (ret);
}

static void vt_build_identify_lv2(unsigned int data, unsigned int start,
				  unsigned int count, const char **table,
				  bool isEnd)
{
	unsigned int i, end, pos, sh = 1;
	unsigned int temp;

	end = start + count;

	for (i = start; i < end; i++) {
		temp = ((data & (sh << i)) >> i);
		pos = i * 2;
		printf("        \"bit %u\":\"%ub  %s\"\n", i, temp, table[pos]);
		printf("                     %s", table[pos + 1]);

		if((end - 1) != i || !isEnd)
			printf(",\n");
		else
			printf("\n");
	}

	if(isEnd)
		printf("    },\n");
}

static void vt_build_power_state_descriptor(const struct nvme_id_ctrl *ctrl)
{
	unsigned int i;
	unsigned char *buf;

	printf("{\n");
	printf("\"Power State Descriptors\":{\n");
	printf("    \"NOPS\":\"Non-Operational State,\"\n");
	printf("    \"MPS\":\"Max Power Scale (0: in 0.01 Watts; 1: in 0.0001 Watts),\"\n");
	printf("    \"ENLAT\":\"Entry Latency in microseconds,\"\n");
	printf("    \"RWL\":\"Relative Write Latency,\"\n");
	printf("    \"RRL\":\"Relative Read Latency,\"\n");
	printf("    \"IPS\":\"Idle Power Scale (00b: Not reported; 01b: 0.0001 W; 10b: 0.01 W; 11b: Reserved),\"\n");
	printf("    \"APS\":\"Active Power Scale (00b: Not reported; 01b: 0.0001 W; 10b: 0.01 W; 11b: Reserved),\"\n");
	printf("    \"ACTP\":\"Active Power,\"\n");
	printf("    \"MP\":\"Maximum Power,\"\n");
	printf("    \"EXLAT\":\"Exit Latency in microsecond,\"\n");
	printf("    \"RWT\":\"Relative Write Throughput,\"\n");
	printf("    \"RRT\":\"Relative Read Throughput,\"\n");
	printf("    \"IDLP\":\"Idle Power,\"\n");
	printf("    \"APW\":\"Active Power Workload,\"\n");
	printf("    \"Ofs\":\"BYTE Offset,\"\n");

	printf("    \"Power State Descriptors\":\"\n");

	printf("%6s%10s%5s%4s%6s%10s%10s%10s%4s%4s%4s%4s%10s%4s%6s%10s%4s%5s%6s\n", "Entry", "0fs 00-03", "NOPS", "MPS", "MP", "ENLAT", "EXLAT", "0fs 12-15",
			"RWL", "RWT", "RRL", "RRT", "0fs 16-19", "IPS", "IDLP", "0fs 20-23", "APS", "APW", "ACTP");


	printf("%6s%10s%5s%4s%6s%10s%10s%10s%4s%4s%4s%4s%10s%4s%6s%10s%4s%5s%6s\n", "=====", "=========", "====", "===", "=====", "=========", "=========",
			"=========", "===", "===", "===", "===", "=========", "===", "=====", "=========", "===", "====", "=====");

	for (i = 0; i < 32; i++) {
		char s[100];
		unsigned int temp;

		printf("%6d", i);
		buf = (unsigned char*) (&ctrl->psd[i]);
		vt_convert_data_buffer_to_hex_string(&buf[0], 4, true, s);
		printf("%9sh", s);

		temp = ctrl->psd[i].flags;
		printf("%4ub", ((unsigned char)temp & 0x02));
		printf("%3ub", ((unsigned char)temp & 0x01));
		vt_convert_data_buffer_to_hex_string(&buf[0], 2, true, s);
		printf("%5sh", s);

		vt_convert_data_buffer_to_hex_string(&buf[4], 4, true, s);
		printf("%9sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[8], 4, true, s);
		printf("%9sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[12], 4, true, s);
		printf("%9sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[15], 1, true, s);
		printf("%3sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[14], 1, true, s);
		printf("%3sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[13], 1, true, s);
		printf("%3sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[12], 1, true, s);
		printf("%3sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[16], 4, true, s);
		printf("%9sh", s);

		temp = ctrl->psd[i].ips;
		snprintf(s, sizeof(s), "%u%u", (((unsigned char)temp >> 6) & 0x01), (((unsigned char)temp >> 7) & 0x01));
		printf("%3sb", s);

		vt_convert_data_buffer_to_hex_string(&buf[16], 2, true, s);
		printf("%5sh", s);
		vt_convert_data_buffer_to_hex_string(&buf[20], 4, true, s);
		printf("%9sh", s);

		temp = ctrl->psd[i].apws;
		snprintf(s, sizeof(s), "%u%u", (((unsigned char)temp >> 6) & 0x01), (((unsigned char)temp >> 7) & 0x01));
		printf("%3sb", s);
		snprintf(s, sizeof(s), "%u%u%u", (((unsigned char)temp) & 0x01), (((unsigned char)temp >> 1) & 0x01), (((unsigned char)temp >> 2) & 0x01));
		printf("%4sb", s);

		vt_convert_data_buffer_to_hex_string(&buf[20], 2, true, s);
		printf("%5sh", s);
		printf("\n");
	}

	printf("    \"}\n}\n");

}

static void vt_dump_hex_data(const unsigned char *pbuff, size_t pbuffsize) {

	char textbuf[33];
	unsigned long int i, j;

	textbuf[32] = '\0';
	printf("[%08X] ", 0);
	for (i = 0; i < pbuffsize; i++) {
		printf("%02X ", pbuff[i]);

		if (pbuff[i] >= ' ' && pbuff[i] <= '~') 
			textbuf[i % 32] = pbuff[i];
		else 
			textbuf[i % 32] = '.';

		if ((((i + 1) % 8) == 0) || ((i + 1) == pbuffsize)) {
			printf(" ");
			if ((i + 1) % 32 == 0) {
				printf(" %s\n", textbuf);
				if((i + 1) != pbuffsize)
				    printf("[%08lX] ", (i + 1));
			} 
			else if (i + 1 == pbuffsize) {
				textbuf[(i + 1) % 32] = '\0';
				if(((i + 1) % 8) == 0)
					printf(" ");

				for (j = ((i + 1) % 32); j < 32; j++) {
					printf("   ");
					if(((j + 1) % 8) == 0)
						printf(" ");
				}

				printf("%s\n", textbuf);
			}
		}
	}
}

static void vt_parse_detail_identify(const struct nvme_id_ctrl *ctrl)
{
	unsigned char *buf;
	unsigned int temp, pos;
	char s[1024] = "";

	const char *CMICtable[6] = {"0 = the NVM subsystem contains only a single NVM subsystem port",
								"1 = the NVM subsystem may contain more than one subsystem ports",
								"0 = the NVM subsystem contains only a single controller",
								"1 = the NVM subsystem may contain two or more controllers (see section 1.4.1)",
								"0 = the controller is associated with a PCI Function or a Fabrics connection",
								"1 = the controller is associated with an SR-IOV Virtual Function"};

	const char *OAEStable[20] = {"Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "0 = does not support sending the Namespace Attribute Notices event nor the associated Changed Namespace List log page",
								 "1 = supports sending the Namespace Attribute Notices  & the associated Changed Namespace List log page",
								 "0 = does not support sending Firmware Activation Notices event",
								 "1 = supports sending Firmware Activation Notices"};

	const char *CTRATTtable[4] = {"0 = does not support a 128-bit Host Identifier",
								  "1 = supports a 128-bit Host Identifier",
								  "0 = does not support Non-Operational Power State Permissive Mode",
								  "1 = supports Non-Operational Power State Permissive Mode"};

	const char *OACStable[18] = {"0 = does not support the Security Send and Security Receive commands",
								 "1 = supports the Security Send and Security Receive commands",
								 "0 = does not support the Format NVM command",
								 "1 = supports the Format NVM command",
								 "0 = does not support the Firmware Commit and Firmware Image Download commands",
								 "1 = supports the Firmware Commit and Firmware Image Download commands",
								 "0 = does not support the Namespace Management capability",
								 "1 = supports the Namespace Management capability",
								 "0 = does not support the Device Self-test command",
								 "1 = supports the Device Self-test command",
								 "0 = does not support Directives",
								 "1 = supports Directive Send & Directive Receive commands",
								 "0 = does not support the NVMe-MI Send and NVMe-MI Receive commands",
								 "1 = supports the NVMe-MI Send and NVMe-MI Receive commands",
								 "0 = does not support the Virtualization Management command",
								 "1 = supports the Virtualization Management command",
								 "0 = does not support the Doorbell Buffer Config command",
								 "1 = supports the Doorbell Buffer Config command"};

	const char *FRMWtable[10] = {"0 = the 1st firmware slot (slot 1) is read/write",
								 "1 = the 1st firmware slot (slot 1) is read only",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "Reserved",
								 "0 = requires a reset for firmware to be activated",
								 "1 = supports firmware activation without a reset"};

	const char *LPAtable[8] = {"0 = does not support the SMART / Health information log page on a per namespace basis",
							   "1 = supports the SMART / Health information log page on a per namespace basis",
							   "0 = does not support the Commands Supported & Effects log page",
							   "1 = supports the Commands Supported Effects log page",
							   "0 = does not support extended data for Get Log Page",
							   "1 = supports extended data for Get Log Page (including extended Number of Dwords and Log Page Offset fields)",
							   "0 = does not support the Telemetry Host-Initiated and Telemetry Controller-Initiated log pages and Telemetry Log Notices events",
							   "1 = supports the Telemetry Host-Initiated and Telemetry Controller-Initiated log pages and sending Telemetry Log Notices"							   };

	const char *AVSCCtable[2] = {"0 = the format of all Admin Vendor Specific Commands are vendor specific",
								 "1 = all Admin Vendor Specific Commands use the format defined in NVM Express specification"};

	const char *APSTAtable[2] = {"0 = does not support autonomous power state transitions",
								 "1 = supports autonomous power state transitions"};

	const char *DSTOtable[2] =  {"0 = the NVM subsystem supports one device self-test operation per controller at a time",
								 "1 = the NVM subsystem supports only one device self-test operation in progress at a time"};

	const char *HCTMAtable[2] = {"0 = does not support host controlled thermal management",
								 "1 = supports host controlled thermal management. Supports Set Features & Get Features commands with the Feature Identifier field set to 10h"};

	const char *SANICAPtable[6] =  {"0 = does not support the Crypto Erase sanitize operation",
									"1 = supports the Crypto Erase sanitize operation",
									"0 = does not support the Block Erase sanitize operation",
									"1 = supports the Block Erase sanitize operation",
									"0 = does not support the Overwrite sanitize operation",
									"1 = supports the Overwrite sanitize operation"};

	const char *ONCStable[14] =  {"0 = does not support the Compare command",
								  "1 = supports the Compare command",
								  "0 = does not support the Write Uncorrectable command",
								  "1 = supports the Write Uncorrectable command",
								  "0 = does not support the Dataset Management command",
								  "1 = supports the Dataset Management command",
								  "0 = does not support the Write Zeroes command",
								  "1 = supports the Write Zeroes command",
								  "0 = does not support the Save field set to a non-zero value in the Set Features and the Get Features commands",
								  "1 = supports the Save field set to a non-zero value in the Set Features and the Get Features commands", \
								  "0 = does not support reservations",
								  "1 = supports reservations",
								  "0 = does not support the Timestamp feature (refer to section 5.21.1.14)",
								  "1 = supports the Timestamp feature"};

	const char *FUSEStable[2] = {"0 =  does not support the Compare and Write fused operation",
								 "1 =  supports the Compare and Write fused operation"};

	const char *FNAtable[6] = {"0 = supports format on a per namespace basis",
							   "1 = all namespaces shall be configured with the same attributes and a format (excluding secure erase) of any namespace results in a format of all namespaces in an NVM subsystem",
							   "0 = any secure erase performed as part of a format results in a secure erase of a particular namespace specified",
							   "1 = any secure erase performed as part of a format operation results in a secure erase of all namespaces in the NVM subsystem",
							   "0 = cryptographic erase is not supported",
							   "1 = cryptographic erase is supported as part of the secure erase functionality"};

	const char *VWCtable[2] = {"0 = a volatile write cache is not present",
							   "1 = a volatile write cache is present"};

	const char *ICSVSCCtable[2] = {"0 = the format of all NVM Vendor Specific Commands are vendor specific",
								 "1 = all NVM Vendor Specific Commands use the format defined in NVM Express specification"};

	const char *SGLSSubtable[4] =  {"00b = SGLs are not supported",
									"01b = SGLs are supported. There is no alignment nor granularity requirement for Data Blocks",
									"10b = SGLs are supported. There is a Dword alignment and granularity requirement for Data Blocks",
									"11b = Reserved"};

	const char *SGLStable[42] =  {"Used",
								  "Used",
								  "Used",
								  "Used",
								  "0 = does not support the Keyed SGL Data Block descriptor",
								  "1 = supports the Keyed SGL Data Block descriptor",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "Reserved",
								  "0 = the SGL Bit Bucket descriptor is not supported",
								  "1 = the SGL Bit Bucket descriptor is supported",
								  "0 = use of a byte aligned contiguous physical buffer of metadata is not supported",
								  "1 = use of a byte aligned contiguous physical buffer of metadata is supported",
								  "0 = the SGL length shall be equal to the amount of data to be transferred",
								  "1 = supports commands that contain a data or metadata SGL of a length larger than the amount of data to be transferred",
								  "0 = use of Metadata Pointer (MPTR) that contains an address of an SGL segment containing exactly one SGL Descriptor that is Qword aligned is not supported",
								  "1 = use of Metadata Pointer (MPTR) that contains an address of an SGL segment containing exactly one SGL Descriptor that is Qword aligned is supported",
								  "0 = the Address field specifying an offset is not supported",
	                              "1 = supports the Address field in SGL Data Block, SGL Segment, and SGL Last Segment descriptor types specifying an offset"};

	buf = (unsigned char *)(ctrl);

	printf("{\n");
	vt_convert_data_buffer_to_hex_string(buf, 2, true, s);
	printf("    \"PCI Vendor ID\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[2], 2, true, s);
	printf("    \"PCI Subsystem Vendor ID\":\"%sh\",\n",  s);
	printf("    \"Serial Number\":\"%s\",\n", ctrl->sn);
	printf("    \"Model Number\":\"%s\",\n", ctrl->mn);
	printf("    \"Firmware Revision\":\"%-.*s\",\n", (int)sizeof(ctrl->fr), ctrl->fr);
	vt_convert_data_buffer_to_hex_string(&buf[72], 1, true, s);
	printf("    \"Recommended Arbitration Burst\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[73], 3, true, s);
	printf("    \"IEEE OUI Identifier\":\"%sh\",\n", s);

	temp = ctrl->cmic;
	printf("    \"Controller Multi-Path I/O and Namespace Sharing Capabilities\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[76], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 3, CMICtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[77], 1, true, s);
	printf("    \"Maximum Data Transfer Size\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[78], 2, true, s);
	printf("    \"Controller ID\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[80], 4, true, s);
	printf("    \"Version\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[84], 4, true, s);
	printf("    \"RTD3 Resume Latency\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[88], 4, true, s);
	printf("    \"RTD3 Entry Latency\":\"%sh\",\n", s);

	temp = le32_to_cpu(ctrl->oaes);
	printf("    \"Optional Asynchronous Events Supported\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[92], 4, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 8, 2, OAEStable, true);

	temp = le32_to_cpu(ctrl->ctratt);
	printf("    \"Controller Attributes\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[96], 4, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 2, CTRATTtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[122], 16, true, s);
	printf("    \"FRU Globally Unique Identifier\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[240], 16, true, s);
	printf("    \"NVMe Management Interface Specification\":\"%sh\",\n", s);

	temp = le16_to_cpu(ctrl->oacs);
	printf("    \"Optional Admin Command Support\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[256], 2, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 9, OACStable, true);

	vt_convert_data_buffer_to_hex_string(&buf[258], 1, true, s);
	printf("    \"Abort Command Limit\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[259], 1, true, s);
	printf("    \"Asynchronous Event Request Limit\":\"%sh\",\n", s);

	temp = ctrl->frmw;
	printf("    \"Firmware Updates\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[260], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, FRMWtable, false);
	vt_convert_data_buffer_to_hex_string(&buf[260], 1, true, s);
	printf("        \"Firmware Slot\":\"%uh\",\n", ((ctrl->frmw >> 1) & 0x07));
	vt_build_identify_lv2(temp, 4, 1, FRMWtable, true);

	temp = ctrl->lpa;
	printf("    \"Log Page Attributes\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[261], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 4, LPAtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[262], 1, true, s);
	printf("    \"Error Log Page Entries\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[263], 1, true, s);
	printf("    \"Number of Power States Support\":\"%sh\",\n", s);

	temp = ctrl->avscc;
	printf("    \"Admin Vendor Specific Command Configuration\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[264], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, AVSCCtable, true);

	temp = ctrl->apsta;
	printf("    \"Autonomous Power State Transition Attributes\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[265], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, APSTAtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[266], 2, true, s);
	printf("    \"Warning Composite Temperature Threshold\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[268], 2, true, s);
	printf("    \"Critical Composite Temperature Threshold\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[270], 2, true, s);
	printf("    \"Maximum Time for Firmware Activation\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[272], 4, true, s);
	printf("    \"Host Memory Buffer Preferred Size\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[276], 4, true, s);
	printf("    \"Host Memory Buffer Minimum Size\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[280], 16, true, s);
	printf("    \"Total NVM Capacity\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[296], 16, true, s);
	printf("    \"Unallocated NVM Capacity\":\"%sh\",\n", s);

	temp = le32_to_cpu(ctrl->rpmbs); 
	printf("    \"Replay Protected Memory Block Support\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[312], 4, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	printf("        \"Number of RPMB Units\":\"%u\",\n", (temp & 0x00000003));
	snprintf(s, sizeof(s), ((temp >> 3) & 0x00000007)? "Reserved" : "HMAC SHA-256");
	printf("        \"Authentication Method\":\"%u: %s\",\n", ((temp >> 3) & 0x00000007), s);
	printf("        \"Total Size\":\"%u\",\n", ((temp >> 16) & 0x000000FF));
	printf("        \"Access Size\":\"%u\",\n", ((temp >> 24) & 0x000000FF));
	printf("    },\n");

	vt_convert_data_buffer_to_hex_string(&buf[316], 2, true, s);
	printf("    \"Extended Device Self-test Time\":\"%sh\",\n", s);

	temp = ctrl->dsto;
	printf("    \"Device Self-test Options\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[318], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, DSTOtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[319], 1, true, s);
	printf("    \"Firmware Update Granularity\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[320], 1, true, s);
	printf("    \"Keep Alive Support\":\"%sh\",\n", s);

	temp = le16_to_cpu(ctrl->hctma);
	printf("    \"Host Controlled Thermal Management Attributes\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[322], 2, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, HCTMAtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[324], 2, true, s);
	printf("    \"Minimum Thermal Management Temperature\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[326], 2, true, s);
	printf("    \"Maximum Thermal Management Temperature\":\"%sh\",\n", s);

	temp = le16_to_cpu(ctrl->sanicap);
	printf("    \"Sanitize Capabilities\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[328], 2, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 3, SANICAPtable, true);

	temp = ctrl->sqes;
	printf("    \"Submission Queue Entry Size\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[512], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	printf("        \"Maximum Size\":\"%u\",\n", (temp & 0x0000000F));
	printf("        \"Required Size\":\"%u\",\n", ((temp >> 4) & 0x0000000F));
	printf("    }\n");

	temp = ctrl->cqes;
	printf("    \"Completion Queue Entry Size\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[513], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	printf("        \"Maximum Size\":\"%u\",\n", (temp & 0x0000000F));
	printf("        \"Required Size\":\"%u\",\n", ((temp >> 4) & 0x0000000F));
	printf("    }\n");

	vt_convert_data_buffer_to_hex_string(&buf[514], 2, true, s);
	printf("    \"Maximum Outstanding Commands\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[516], 4, true, s);
	printf("    \"Number of Namespaces\":\"%sh\",\n", s);

	temp = le16_to_cpu(ctrl->oncs);
	printf("    \"Optional NVM Command Support\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[520], 2, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 7, ONCStable, true);

	temp = le16_to_cpu(ctrl->fuses);
	printf("    \"Fused Operation Support\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[522], 2, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, FUSEStable, true);

	temp = ctrl->fna;
	printf("    \"Format NVM Attributes\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[524], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 3, FNAtable, true);

	temp = ctrl->vwc;
	printf("    \"Volatile Write Cache\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[525], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, VWCtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[526], 2, true, s);
	printf("    \"Atomic Write Unit Normal\":\"%sh\",\n", s);
	vt_convert_data_buffer_to_hex_string(&buf[528], 2, true, s);
	printf("    \"Atomic Write Unit Power Fail\":\"%sh\",\n", s);

	temp = ctrl->icsvscc;
	printf("    \"NVM Vendor Specific Command Configuration\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[530], 1, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	vt_build_identify_lv2(temp, 0, 1, ICSVSCCtable, true);

	vt_convert_data_buffer_to_hex_string(&buf[532], 2, true, s);
	printf("    \"Atomic Compare 0 Write Unit\":\"%sh\",\n", s);

	temp = le32_to_cpu(ctrl->sgls);
	printf("    \"SGL Support\":{\n");
	vt_convert_data_buffer_to_hex_string(&buf[536], 4, true, s);
	printf("        \"Value\":\"%sh\",\n", s);
	pos = (temp & 0x00000003);
	printf("        \"bit 1:0\":\"%s\",\n", SGLSSubtable[pos]);
	vt_build_identify_lv2(temp, 2, 1, SGLStable, false);
	vt_build_identify_lv2(temp, 16, 5, SGLStable, true);

	vt_convert_data_buffer_to_hex_string(&buf[768], 256, false, s);
	printf("    \"NVM Subsystem NVMe Qualified Name\":\"%s\",\n", s);
	printf("}\n\n");

	vt_build_power_state_descriptor(ctrl);


	printf("\n{\n");
	printf("\"Vendor Specific\":\"\n");
	vt_dump_hex_data(&buf[3072], 1024);
	printf("\"}\n");
}

static int vt_save_smart_to_vtview_log(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
	int err = 0;
	int fd, ret;
	long int total_time = 0;
	long int freq_time = 0;
	long int cur_time = 0;
	long int remain_time = 0;
	long int start_time = 0;
	long int end_time = 0;
	char path[256] = "";
	char *desc = "Save SMART data into log file with format that is easy to analyze (comma delimited). Maximum log file will be 4K.\n\n"
		"Typical usages:\n\n"
		"Temperature characterization: \n"
		"\tvirtium save-smart-to-vtview-log /dev/yourDevice --run-time=100 --record-frequency=0.25 --test-name=burn-in-at-(-40)\n\n"
		"Endurance testing : \n"
		"\tvirtium save-smart-to-vtview-log /dev/yourDevice --run-time=100 --record-frequency=1 --test-name=Endurance-test-JEDEG-219-workload\n\n"
		"Just logging :\n"
		"\tvirtium save-smart-to-vtview-log /dev/yourDevice";

	const char *run_time = "(optional) Number of hours to log data (default = 20 hours)";
	const char *freq = "(optional) How often you want to log SMART data (0.25 = 15' , 0.5 = 30' , 1 = 1 hour, 2 = 2 hours, etc.). Default = 10 hours.";
	const char *output_file = "(optional) Name of the log file (give it a name that easy for you to remember what the test is). You can leave it blank too, we will take care it for you.";
	const char *test_name = "(optional) Name of the test you are doing. We use this as part of the name of the log file.";

	struct vtview_save_log_settings cfg = {
		.run_time_hrs = 20,
		.log_record_frequency_hrs = 10,
		.output_file = NULL,
		.test_name = NULL,
	};

	OPT_ARGS(opts) = {
		OPT_DOUBLE("run-time",  'r', &cfg.run_time_hrs,             run_time),
		OPT_DOUBLE("freq",      'f', &cfg.log_record_frequency_hrs, freq),
		OPT_FILE("output-file", 'o', &cfg.output_file,              output_file),
		OPT_STRING("test-name", 'n', "NAME", &cfg.test_name,        test_name),
		OPT_END()
	};

	vt_generate_vtview_log_file_name(vt_default_log_file_name);

	if (argc >= 2) {
		if (strlen(argv[1]) > sizeof(path) - 1) {
			printf("Filename too long\n");
			return -1;
		}
		strcpy(path, argv[1]);
	}

	fd = parse_and_open(argc, argv, desc, opts);
	if (fd < 0) {
		printf("Error parse and open (fd = %d)\n", fd);
		return (fd);
	}

	printf("Running...\n");
	printf("Collecting data for device %s\n", path);
	printf("Running for %lf hour(s)\n", cfg.run_time_hrs);
	printf("Logging SMART data for every %lf hour(s)\n", cfg.log_record_frequency_hrs);

	ret = vt_update_vtview_log_header(fd, path, &cfg);
	if (ret) {
		err = EINVAL;
		close(fd);
		return (err);
	}

	total_time = cfg.run_time_hrs * (float)HOUR_IN_SECONDS;
	freq_time = cfg.log_record_frequency_hrs * (float)HOUR_IN_SECONDS;

	if(freq_time == 0)
		freq_time = 1;

	start_time = time(NULL);
	end_time = start_time + total_time;

	fflush(stdout);

	while (1) {
		cur_time = time(NULL);
		if(cur_time >= end_time)
			break;

		ret = vt_add_entry_to_log(fd, path, &cfg);
		if (ret) {
			printf("Cannot update driver log\n");
			break;
		}

		remain_time = end_time - cur_time;
		freq_time = MIN2(freq_time, remain_time);
		sleep(freq_time);
		fflush(stdout);
	}

	close (fd);
	return (err);
}

static int vt_show_identify(int argc, char **argv, struct command *cmd, struct plugin *plugin)
{
	int err = 0;
	int fd ,ret;
	struct nvme_id_ctrl ctrl;
	char *desc = "Parse identify data to json format\n\n"
		"Typical usages:\n\n"
		"virtium show-identify /dev/yourDevice\n";

	OPT_ARGS(opts) = {
		OPT_END()
	};

	fd = parse_and_open(argc, argv, desc, opts);
	if (fd < 0) {
		printf("Error parse and open (fd = %d)\n", fd);
		return (fd);
	}

	ret = nvme_identify_ctrl(fd, &ctrl);
	if (ret) {
		printf("Cannot read identify device\n");
		close (fd);
		return (-1);
	}

	vt_process_string(ctrl.sn, sizeof(ctrl.sn));
	vt_process_string(ctrl.mn, sizeof(ctrl.mn));
	vt_parse_detail_identify(&ctrl);

	close(fd);
	return (err);
}