PX4 Firmware
PX4 Autopilot Software http://px4.io
test_file2.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_file2.c
36  * File write test.
37  */
38 
39 #include <px4_platform_common/defines.h>
40 #include <sys/stat.h>
41 #include <dirent.h>
42 #include <stdio.h>
43 #include <stddef.h>
44 #include <unistd.h>
45 #include <fcntl.h>
46 #include <perf/perf_counter.h>
47 #include <string.h>
48 #include <stdlib.h>
49 #include <px4_platform_common/getopt.h>
50 
51 #include "tests_main.h"
52 
53 #define FLAG_FSYNC 1
54 #define FLAG_LSEEK 2
55 
56 #define LOG_PATH PX4_STORAGEDIR
57 
58 /*
59  return a predictable value for any file offset to allow detection of corruption
60  */
61 static uint8_t get_value(uint32_t ofs)
62 {
63  union {
64  uint32_t ofs;
65  uint8_t buf[4];
66  } u;
67  u.ofs = ofs;
68  return u.buf[ofs % 4];
69 }
70 
71 static int test_corruption(const char *filename, uint32_t write_chunk, uint32_t write_size, uint16_t flags)
72 {
73  printf("Testing on %s with write_chunk=%u write_size=%u\n",
74  filename, (unsigned)write_chunk, (unsigned)write_size);
75 
76  uint32_t ofs = 0;
77  int fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, PX4_O_MODE_666);
78 
79  if (fd == -1) {
80  perror(filename);
81  return 1;
82  }
83 
84  // create a file of size write_size, in write_chunk blocks
85  uint8_t counter = 0;
86 
87  while (ofs < write_size) {
88  uint8_t buffer[write_chunk];
89 
90  for (uint16_t j = 0; j < write_chunk; j++) {
91  buffer[j] = get_value(ofs);
92  ofs++;
93  }
94 
95  if (write(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) {
96  printf("write failed at offset %u\n", ofs);
97  return 1;
98  }
99 
100  if (flags & FLAG_FSYNC) {
101  fsync(fd);
102  }
103 
104  if (counter % 100 == 0) {
105  printf("write ofs=%u\r", ofs);
106  }
107 
108  counter++;
109  }
110 
111  close(fd);
112 
113  printf("write ofs=%u\n", ofs);
114 
115  // read and check
116  fd = open(filename, O_RDONLY);
117 
118  if (fd == -1) {
119  perror(filename);
120  return 1;
121  }
122 
123  counter = 0;
124  ofs = 0;
125 
126  while (ofs < write_size) {
127  uint8_t buffer[write_chunk];
128 
129  if (counter % 100 == 0) {
130  printf("read ofs=%u\r", ofs);
131  }
132 
133  counter++;
134 
135  if (read(fd, buffer, sizeof(buffer)) != (int)sizeof(buffer)) {
136  printf("read failed at offset %u\n", ofs);
137  close(fd);
138  return 1;
139  }
140 
141  for (uint16_t j = 0; j < write_chunk; j++) {
142  if (buffer[j] != get_value(ofs)) {
143  printf("corruption at ofs=%u got %u\n", ofs, buffer[j]);
144  close(fd);
145  return 1;
146  }
147 
148  ofs++;
149  }
150 
151  if (flags & FLAG_LSEEK) {
152  lseek(fd, 0, SEEK_CUR);
153  }
154  }
155 
156  printf("read ofs=%u\n", ofs);
157  close(fd);
158  unlink(filename);
159  printf("All OK\n");
160  return 0;
161 }
162 
163 static void usage(void)
164 {
165  printf("test file2 [options] [filename]\n");
166  printf("\toptions:\n");
167  printf("\t-s SIZE set file size\n");
168  printf("\t-c CHUNK set IO chunk size\n");
169  printf("\t-F fsync on every write\n");
170  printf("\t-L lseek on every read\n");
171 }
172 
173 int test_file2(int argc, char *argv[])
174 {
175  int opt;
176  uint16_t flags = 0;
177  const char *filename = LOG_PATH "/testfile2.dat";
178  uint32_t write_chunk = 64;
179  uint32_t write_size = 5 * 1024;
180 
181  int myoptind = 1;
182  const char *myoptarg = NULL;
183 
184  while ((opt = px4_getopt(argc, argv, "c:s:FLh", &myoptind, &myoptarg)) != EOF) {
185  switch (opt) {
186  case 'F':
187  flags |= FLAG_FSYNC;
188  break;
189 
190  case 'L':
191  flags |= FLAG_LSEEK;
192  break;
193 
194  case 's':
195  write_size = strtoul(myoptarg, NULL, 0);
196  break;
197 
198  case 'c':
199  write_chunk = strtoul(myoptarg, NULL, 0);
200  break;
201 
202  case 'h':
203  default:
204  usage();
205  return 1;
206  }
207  }
208 
209  argc -= myoptind;
210  argv += myoptind;
211 
212  if (argc > 0) {
213  filename = argv[0];
214  }
215 
216  /* check if microSD card is mounted */
217  struct stat buffer;
218 
219  if (stat(LOG_PATH, &buffer)) {
220  fprintf(stderr, "no microSD card mounted, aborting file test");
221  return 1;
222  }
223 
224  return test_corruption(filename, write_chunk, write_size, flags);
225 }
226 
#define FLAG_FSYNC
Definition: test_file2.c:53
static int test_corruption(const char *filename, uint32_t write_chunk, uint32_t write_size, uint16_t flags)
Definition: test_file2.c:71
static uint8_t get_value(uint32_t ofs)
Definition: test_file2.c:61
#define LOG_PATH
Definition: test_file2.c:56
static void read(bootloader_app_shared_t *pshared)
#define FLAG_LSEEK
Definition: test_file2.c:54
static void usage(void)
Definition: test_file2.c:163
Tests declaration file.
static unsigned counter
Definition: safety.c:56
int fd
Definition: dataman.cpp:146
int test_file2(int argc, char *argv[])
Definition: test_file2.c:173
static void write(bootloader_app_shared_t *pshared)
Performance measuring tools.