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
|
/******************************************************************************
* Project Persistency
* (c) copyright 2012
* Company XS Embedded GmbH
*****************************************************************************/
/******************************************************************************
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
* with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
******************************************************************************/
/**
* @file persistence_pfs_test.c
* @ingroup Persistence client library test
* @author Ingo Huerner
* @brief Persistence Power Fail Safe Test
* For this test a computer controlled lab power supply will be used.
* The persistence_pfs_test application sends a command to the power supply
* using the serial console to switch power off for a couple of seconds to
* reboot of the system and simulate unexpected power cut in order to proof
* power fail save of persistence.
*
* @attention To run the test the following things need to do to setup the target:
* There is a script to start this test binary.
* In otder to start this script on startup, copy the systemd service file "persistenc-pfs-test-start.service"
* to /lib/systemd/system/.
* Create also a link from /lib/systemd/system/multi-user.target.wants
* to the persistence-pfs-test-start.service in /lib/systemd/system/
*
* @see
*/
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>
#include <dlt.h>
#include <dlt_common.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include "../include/persistence_client_library.h"
#include "../include/persistence_client_library_key.h"
#include "../include/persistence_client_library_file.h"
#define LC_CNT_START 36
#define WR_CNT_START 43
#define BASE_STRING_END 31
DLT_DECLARE_CONTEXT(gPFSDLTContext);
static const char* gOrigPostfix = "ORIG";
static const char* gBackupPostfix = "BACK";
static int gLifecycleCounter = 0;
static const char* gTestInProgressFlag = "/Data/mnt-c/persistence_pfst_test_in_progress";
static const char* gDefaultKeyValueResName[] =
{
"keyValue_Resource_00",
"keyValue_Resource_01",
"keyValue_Resource_02",
"keyValue_Resource_03",
"keyValue_Resource_04",
"keyValue_Resource_05",
"keyValue_Resource_06",
"keyValue_Resource_07",
"keyValue_Resource_08",
"keyValue_Resource_09",
"keyValue_Resource_10",
"keyValue_Resource_11",
"keyValue_Resource_12",
"keyValue_Resource_13",
"keyValue_Resource_14",
"keyValue_Resource_15",
"keyValue_Resource_16",
"keyValue_Resource_17",
"keyValue_Resource_18",
"keyValue_Resource_19",
};
static const char* gDefaultKeyValueTestData[] =
{
"keyValue pair cache Zero - %s : 000000 000000",
"keyValue pair cache One - %s : 000000 000000",
"keyValue pair cache Two - %s : 000000 000000",
"keyValue pair cache Three - %s : 000000 000000",
"keyValue pair cache Four - %s : 000000 000000",
"keyValue pair cache Five - %s : 000000 000000",
"keyValue pair wt Six - %s : 000000 000000",
"keyValue pair wt Seven - %s : 000000 000000",
"keyValue pair wt eight - %s : 000000 000000",
"keyValue pair wt nine - %s : 000000 000000",
"keyValue pair wt ten - %s : 000000 000000",
"keyValue pair cache eleven - %s : 000000 000000",
"keyValue pair cache twelve - %s : 000000 000000",
"keyValue pair cache thirteen - %s : 000000 000000",
"keyValue pair cache fourteen - %s : 000000 000000",
"keyValue pair cache fifteen - %s : 000000 000000",
"keyValue pair wt sixteen - %s : 000000 000000",
"keyValue pair wt Seventeen - %s : 000000 000000",
"keyValue pair wt eighteen - %s : 000000 000000",
"keyValue pair wt nineteen - %s : 000000 000000",
};
static const char* gDefaultFileAPITestData[] =
{
"file API cache One : 000000 000000",
"file API cache Two : 000000 000000",
"file API cache Three : 000000 000000",
"file API cache Four : 000000 000000",
"file API wt One : 000000 000000",
"file API wt Two : 000000 000000",
"file API wt Three : 000000 000000",
"file API wt Four : 000000 000000"
};
static const char* gDefaultFileResNames[] =
{
"file API_Resource_01",
"file API_Resource_02",
"file API_Resource_03",
"file API_Resource_04",
"file API_Resource_05",
"file API_Resource_06",
"file API_Resource_07",
"file API_Resource_08"
};
// extdern declaration
extern void pcl_test_send_shutdown_command();
// forward declaration
/// verify written data form previous lifefycle
void verify_data_key_value();
void verify_data_file();
/// write data until power off occurs
void write_data_key_value(int numLoops, int counter);
void write_data_file(int numLoops);
int mount_persistence(const char* deviceName);
void unmount_persistence();
/// setup initial test data
int setup_test_data(const char* postfix)
{
int i = 0, ret = 0;
char databuffer[64] = {0};
unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
pclInitLibrary("pfs_test", shutdownReg); // register to persistence client library
// key/value data
for(i=0; i < (int)sizeof(gDefaultKeyValueTestData) / (int)sizeof(char*); i++)
{
memset(databuffer, 0, 64);
snprintf(databuffer, 64, gDefaultKeyValueTestData[i], postfix);
printf(" setup_test_data - [%.2d] => %s\n", i, databuffer);
ret = pclKeyWriteData(PCL_LDBID_LOCAL, gDefaultKeyValueResName[i], 1, 1, (unsigned char*)databuffer, strlen(databuffer));
if(ret < 0)
{
printf("setup_test_data => failed to write data: %d\n", ret );
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("write key/value - failed to write data"), DLT_INT(ret));
}
}
#if 0
// file data
for(i=0; i<sizeof(gDefaultFileAPITestData) / sizeof(char*); i++)
{
printf(" file - data[%d] => %s\n", i, gDefaultFileAPITestData[i]);
}
#endif
pclDeinitLibrary(); // unregister from persistence client library
sync();
return 1;
}
void verify_test_setup()
{
int i = 0, ret = 0;
unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
char buffer[64] = {0};
pclInitLibrary("pfs_test", shutdownReg); // register to persistence client library
for(i=0; i< (int)sizeof(gDefaultKeyValueTestData) / (int)sizeof(char*); i++)
{
ret = pclKeyReadData(PCL_LDBID_LOCAL, gDefaultKeyValueResName[i], 1, 1, (unsigned char*)buffer, 64);
if(ret < 0)
{
printf("verify_test_setup - key/value - pclKeyReadData FAILED: %s => \"%s\"\n", gDefaultKeyValueResName[i], buffer);
}
printf("verify_test_setup: [%.2d] => %s\n", i, buffer);
memset(buffer, 0, 64);
}
pclDeinitLibrary(); // unregister from persistence client library
}
/// after this function has been called the system reboots
void send_snmp_shutdown_cmd()
{
// command to power off the power supply
const char data[] = { "snmpset -v1 -c public 134.86.58.225 iso.3.6.1.4.1.1909.22.1.1.1.5.1 i 1" };
if(system(data) == -1)
{
printf("Failed to execute command: %s\n", data);
}
}
int update_test_progress_flag(const char* filename, int *counter)
{
int fd = -1, rval = 0;
if(access(filename, F_OK) == 0)
{
fd = open(filename, O_RDWR); // file exists, update content
if(fd != -1)
{
int size = 0;
char buffer[12] = {0};
if((size = read(fd, buffer, 12)) != -1)
{
rval = atoi(buffer);
*counter = rval;
snprintf(buffer, 12, "%d", *counter + 1);
lseek(fd, 0, SEEK_SET);
if((size = write(fd, buffer, strlen(buffer))) == -1)
{
printf(" Failed to write to file: %s » %s\n", filename, strerror(errno));
}
}
else
{
printf(" Failed to write to file: %s » %s\n", filename, strerror(errno));
rval = -1;
}
}
else
{
rval = -1;
}
}
else
{
fd = open(filename, O_CREAT|O_RDWR|O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH);
if(fd != -1)
{
int size = 0;
char buffer[12] = {0};
snprintf(buffer, 12, "%d", 1);
if((size = write(fd, buffer, strlen(buffer))) == -1)
{
printf(" Failed to write to file: %s » %s\n", filename, strerror(errno));
rval = -1;
}
*counter = 1;
}
else
{
rval = -1;
}
}
syncfs(fd);
close(fd);
return rval;
}
void update_test_data(char* buffer, int lc_counter, int write_counter)
{
int i = 0;
char lc_cnt_buff[7] = {0};
char wr_cnt_buff[7] = {0};
snprintf(lc_cnt_buff, 7, "%.6d", lc_counter);
snprintf(wr_cnt_buff, 7, "%.6d", write_counter);
for(i=0; i<6; i++)
{
buffer[LC_CNT_START+i] = lc_cnt_buff[i];
buffer[WR_CNT_START+i] = wr_cnt_buff[i];
}
}
int get_lifecycle_count(char* buf)
{
return atoi(&buf[LC_CNT_START]);
}
int get_write_count(char* buf)
{
return atoi(&buf[WR_CNT_START]);
}
int main(int argc, char *argv[])
{
int rVal = EXIT_SUCCESS;
unsigned int shutdownReg = PCL_SHUTDOWN_TYPE_FAST | PCL_SHUTDOWN_TYPE_NORMAL;
printf("------------------\n");
printf("P F S - Test start\n");
printf("------------------\n");
/// debug log and trace (DLT) setup
//DLT_REGISTER_APP("PFS","power fail safe test");
//DLT_REGISTER_CONTEXT(gPFSDLTContext,"PFS","Context for PCL PFS test logging");
#if 0
// mount persistence partitions
if(-1 != mount_persistence("/dev/sdb") )
{
#endif
int numLoops = 1000000, opt = 0;
while ((opt = getopt(argc, argv, "l:s:")) != -1)
{
switch (opt)
{
case 'l':
numLoops = atoi(optarg);
break;
}
}
if(update_test_progress_flag(gTestInProgressFlag, &gLifecycleCounter) == 0)
{
printf("PFS test - first lifecycle, setup test data\n");
//DLT_LOG(gPFSDLTContext, DLT_LOG_INFO, DLT_STRING("PFS test - first lifecycle, setup test data"));
printf("--- setup data --- \n");
setup_test_data(gBackupPostfix);
setup_test_data(gOrigPostfix);
printf("--- verify data setup --- \n");
verify_test_setup();
}
printf("PFS test - Lifecycle counter: %d - number of test loops: %d\n", gLifecycleCounter, numLoops);
/*
DLT_LOG(gPFSDLTContext, DLT_LOG_INFO, DLT_STRING("PFS test - Lifecycle counter:"), DLT_INT(gLifecycleCounter),
DLT_STRING("- number of write loops:"), DLT_INT(numLoops));
*/
pclInitLibrary("pfs_test", shutdownReg); // register to persistence client library
// verify the data form previous lifecycle
printf("--- Verify Data ---!!\n");
verify_data_key_value();
//verify_data_file();
// write data
printf("--- Write Data!! ---\n");
write_data_key_value(numLoops, gLifecycleCounter); // on odd lifecycle numbers, corrupt db data, otherwise corrupt db header
//write_data_file(numLoops);
printf("Deinit library\n");
pclDeinitLibrary(); // unregister from persistence client library
#if 0
unmount_persistence();
}
else
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("PFS test - failed to mount pers partition:"), DLT_INT(gLifecycleCounter) );
printf("Mount Failed\n");
rVal = EXIT_FAILURE;
}
#endif
printf("End of PFS app\n");
// unregister debug log and trace
//DLT_UNREGISTER_APP();
//dlt_free();
printf("Wait until shutdown thread has finished\n");
return rVal;
}
void verify_data_key_value()
{
int i=0, ret = 0;
char buffer[64] = {0};
// read data from previous lifecycle - key/value
for(i=0; i< (int)sizeof(gDefaultKeyValueTestData) / (int)sizeof(char*); i++)
{
memset(buffer, 0, 64);
ret = pclKeyReadData(PCL_LDBID_LOCAL, gDefaultKeyValueResName[i], 1, 1, (unsigned char*)buffer, 64);
if(ret < 0)
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_WARN, DLT_STRING("verify - key/value - => failed to read data"), DLT_INT(ret), DLT_STRING(buffer));
printf("verify - key/value - pclKeyReadData FAILED: %s => \"%s\"\n", gDefaultKeyValueResName[i], buffer);
}
else
{
int lc_count = get_lifecycle_count(buffer);
// first verify base string
if(0 == strncmp(gDefaultKeyValueTestData[i], buffer, BASE_STRING_END) )
{
char extendedStringOrig[64] = {0};
char extendedStringBack[64] = {0};
snprintf(extendedStringOrig, 64, gDefaultKeyValueTestData[i], gOrigPostfix);
snprintf(extendedStringBack, 64, gDefaultKeyValueTestData[i], gBackupPostfix);
/*printf("Reference: %s\n", extendedStringOrig);
printf(" found: %s\n",buffer);*/
// check if original or backup will be used
if(0 == strncmp(extendedStringOrig, buffer, LC_CNT_START))
{
/*
printf("ORIGINAL detected, everything OK:\n");
printf(" desired: %s\n", extendedStringOrig);
printf(" actual : %s\n", buffer);*/
}
else if(0 == strncmp(extendedStringBack, buffer, LC_CNT_START))
{
/*
printf("BACKUP detected, everything OK:\n");
printf(" desired: %s\n", extendedStringBack);
printf(" actual : %s\n", buffer); */
}
else
{
printf("----------------------------------------------------\n");
printf("!!!! F A I L U R E !!!!! => lifecycle count - current: %d - found: %d\n", gLifecycleCounter, lc_count);
printf(" desired: %s\n", gDefaultKeyValueTestData[i]);
printf(" actual : %s\n", buffer);
printf("----------------------------------------------------\n");
}
#if 0
if(lc_count != (gLifecycleCounter-1) )
{
printf(" Failure - LC count - current: %d - previous: %d - \"%s\"\n", gLifecycleCounter, lc_count, buffer);
/*DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("Failure - LC count - current:"), DLT_INT(gLifecycleCounter),
DLT_STRING("- previous:"), DLT_INT(lc_count),
DLT_STRING("- buf:"), DLT_STRING(buffer));*/
}
#endif
}
else
{
printf(" Failure - base string does not match - actual: \"%s\" - desired: \"%s\"\n", buffer, gDefaultKeyValueTestData[i]);
/*DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("Failure - base string does not match - actual: "), DLT_STRING(buffer),
DLT_STRING("- desired:"), DLT_STRING(gDefaultKeyValueTestData[i]));*/
}
}
}
printf("Persistence pfs test - verification ended!!!!\n");
}
void write_data_key_value(int numLoops, int counter)
{
int i=0, k=0, ret = 0;
if( (counter%2) == 1)
{
printf("Corrupt Data: numLoops: %d!!!!\n", numLoops);
}
else
{
printf("Do N O T corrupt data!!!\n");
}
#if 1
for(k=0; k<numLoops; k++)
{
// write key/value data
for(i=0; i< (int)sizeof(gDefaultKeyValueTestData) / (int)sizeof(char*); i++)
{
char buffer[64] = {0};
strncpy(buffer, gDefaultKeyValueTestData[i], 64);
update_test_data(buffer, gLifecycleCounter, k);
if( (k == (int)numLoops/((counter%20)+1))
&& (counter%2 == 1))
{
// unlock mutex
printf("Now POWER OFF => k: %d \n", k);
pcl_test_send_shutdown_command();
break;
}
ret = pclKeyWriteData(PCL_LDBID_LOCAL, gDefaultKeyValueResName[i], 1, 1, (unsigned char*)buffer, strlen(buffer));
if(ret < 0)
{
printf(" failed to write data: %d\n", ret );
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("write key/value - failed to write data"), DLT_INT(ret));
}
//printf("write data - key/value - \"%s\"\n", buffer);
}
} //num writes per lifecycle
printf("End of Test\n");
#endif
}
void verify_data_file()
{
int i=0, ret = 0;
int handles[128] = {0};
char buffer[64] = {0};
// open files
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
handles[i] = pclFileOpen(PCL_LDBID_LOCAL, gDefaultFileResNames[i], 1, 0);
}
// read data from previous lifecycle - file
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
memset(buffer, 0, 64);
ret = pclFileReadData(handles[i], buffer, 64);
if(ret < 0)
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("verify data - file - failed to read data"), DLT_INT(ret));
}
else
{
printf("verify file - file - \"%s\"\n", buffer);
//DLT_LOG(gPFSDLTContext, DLT_LOG_INFO, DLT_STRING("verify file: "), DLT_STRING(buffer));
}
}
// close fd's
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
ret = pclFileClose(handles[i]);
if(ret != 0)
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("close file - failed to close"), DLT_INT(ret));
}
}
}
void write_data_file(int numLoops)
{
int i=0, k=0, ret = 0;
int handles[128] = {0};
// open files
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
handles[i] = pclFileOpen(PCL_LDBID_LOCAL, gDefaultFileResNames[i], 1, 1);
}
for(k=0; k<numLoops; k++)
{
// write file data
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
char buffer[64] = {0};
strncpy(buffer, gDefaultFileAPITestData[i], 64);
update_test_data(buffer, gLifecycleCounter, k);
//DLT_LOG(gPFSDLTContext, DLT_LOG_INFO, DLT_STRING("- write file:"), DLT_STRING(buffer));
printf("write data - file - \"%s\"\n", buffer);
ret = pclFileWriteData(handles[i], buffer, strlen(buffer));
if(ret < 0)
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("write file - failed to write data"), DLT_INT(ret));
}
usleep((int)(rand()/100000)); // sleep a time random period
pclFileSeek(handles[i], 0, SEEK_SET);
}
} // writes per lifecycle
// close fd's
for(i=0; i< (int)sizeof(gDefaultFileAPITestData) / (int)sizeof(char*); i++)
{
ret = pclFileClose(handles[i]);
if(ret != 0)
{
//DLT_LOG(gPFSDLTContext, DLT_LOG_ERROR, DLT_STRING("close file - failed to close"), DLT_INT(ret));
}
}
}
void unmount_persistence()
{
if(umount2("/Data/mnt-c", MNT_DETACH) == -1)
{
printf("unmount /Data/mnt-c - FAILED: %s\n", strerror(errno));
}
if(umount2("/Data/mnt-wt", MNT_DETACH) == -1)
{
printf("unmount /Data/mnt-wt - FAILED: %s\n", strerror(errno));
}
}
int mount_persistence(const char* deviceName)
{
int rval = 0;
printf("Mount - /Data/mnt-c\n");
if(-1 != mount(deviceName, "/Data/mnt-c", "ext4", 0, ""))
{
printf("Mount - /Data/mnt-wt\n");
if(-1 == mount(deviceName, "/Data/mnt-wt", "ext4", 0, ""))
{
printf("Mount - FAILED - mnt-wt: %s\n", strerror(errno));
rval = -1;
}
else
{
printf("Mount - SUCCESS\n");
}
}
else
{
printf("Mount - FAILED - mnt-c: %s\n", strerror(errno));
rval = -1;
}
return rval;
}
|