Merge branch 'master' into gsoc-ifdef-cleanup
[kugel-rb.git] / apps / plugins / test_disk.c
blob08e1a39704786b26c1f7d80ccade6c8c4f2724fd
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007 Jens Arnold
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include "plugin.h"
23 #include "lib/helper.h"
25 PLUGIN_HEADER
27 #define TESTBASEDIR "/__TEST__"
28 #define TEST_FILE TESTBASEDIR "/test_disk.tmp"
29 #define FRND_SEED 0x78C3 /* arbirary */
31 #if (CONFIG_STORAGE & STORAGE_MMC)
32 #define TEST_SIZE (20*1024*1024)
33 #else
34 #define TEST_SIZE (300*1024*1024)
35 #endif
36 #define TEST_TIME 10 /* in seconds */
38 static unsigned char* audiobuf;
39 static ssize_t audiobuflen;
41 static unsigned short frnd_buffer;
42 static int line = 0;
43 static int max_line = 0;
44 static int log_fd;
45 static char logfilename[MAX_PATH];
46 static const char testbasedir[] = TESTBASEDIR;
48 static void mem_fill_frnd(unsigned char *addr, int len)
50 unsigned char *end = addr + len;
51 unsigned random = frnd_buffer;
53 while (addr < end)
55 random = 75 * random + 74;
56 *addr++ = random >> 8;
58 frnd_buffer = random;
61 static bool mem_cmp_frnd(unsigned char *addr, int len)
63 unsigned char *end = addr + len;
64 unsigned random = frnd_buffer;
66 while (addr < end)
68 random = 75 * random + 74;
69 if (*addr++ != ((random >> 8) & 0xff))
70 return false;
72 frnd_buffer = random;
73 return true;
76 static bool log_init(void)
78 int h;
80 rb->lcd_getstringsize("A", NULL, &h);
81 max_line = LCD_HEIGHT / h;
82 line = 0;
83 rb->lcd_clear_display();
84 rb->lcd_update();
86 rb->create_numbered_filename(logfilename, "/", "test_disk_log_", ".txt",
87 2 IF_CNFN_NUM_(, NULL));
88 log_fd = rb->open(logfilename, O_RDWR|O_CREAT|O_TRUNC, 0666);
89 return log_fd >= 0;
92 static void log_text(char *text, bool advance)
94 rb->lcd_puts(0, line, text);
95 rb->lcd_update();
96 if (advance)
98 if (++line >= max_line)
99 line = 0;
100 rb->fdprintf(log_fd, "%s\n", text);
104 static void log_close(void)
106 rb->close(log_fd);
109 static bool test_fs(void)
111 unsigned char text_buf[32];
112 int total, current, align;
113 int fd, ret;
115 log_init();
116 log_text("test_disk WRITE&VERIFY", true);
117 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
118 rb->snprintf(text_buf, sizeof(text_buf), "CPU clock: %ld Hz",
119 *rb->cpu_frequency);
120 log_text(text_buf, true);
121 #endif
122 log_text("----------------------", true);
123 rb->snprintf(text_buf, sizeof text_buf, "Data size: %dKB", (TEST_SIZE>>10));
124 log_text(text_buf, true);
126 fd = rb->creat(TEST_FILE, 0666);
127 if (fd < 0)
129 rb->splashf(HZ, "creat() failed: %d", fd);
130 goto error;
133 frnd_buffer = FRND_SEED;
134 total = TEST_SIZE;
135 while (total > 0)
137 align = rb->rand() & 0xf;
138 current = rb->rand() % (audiobuflen - align);
139 current = MIN(current, total);
140 rb->snprintf(text_buf, sizeof text_buf, "Wrt %dKB, %dKB left",
141 current >> 10, total >> 10);
142 log_text(text_buf, false);
144 mem_fill_frnd(audiobuf + align, current);
145 ret = rb->write(fd, audiobuf + align, current);
146 if (current != ret)
148 rb->splashf(0, "write() failed: %d/%d", ret, current);
149 rb->close(fd);
150 goto error;
152 total -= current;
154 rb->close(fd);
156 fd = rb->open(TEST_FILE, O_RDONLY);
157 if (fd < 0)
159 rb->splashf(0, "open() failed: %d", ret);
160 goto error;
163 frnd_buffer = FRND_SEED;
164 total = TEST_SIZE;
165 while (total > 0)
167 align = rb->rand() & 0xf;
168 current = rb->rand() % (audiobuflen - align);
169 current = MIN(current, total);
170 rb->snprintf(text_buf, sizeof text_buf, "Cmp %dKB, %dKB left",
171 current >> 10, total >> 10);
172 log_text(text_buf, false);
174 ret = rb->read(fd, audiobuf + align, current);
175 if (current != ret)
177 rb->splashf(0, "read() failed: %d/%d", ret, current);
178 rb->close(fd);
179 goto error;
181 if (!mem_cmp_frnd(audiobuf + align, current))
183 log_text(text_buf, true);
184 log_text("Compare error.", true);
185 rb->close(fd);
186 goto error;
188 total -= current;
190 rb->close(fd);
191 log_text(text_buf, true);
192 log_text("Test passed.", true);
194 error:
195 log_close();
196 rb->remove(TEST_FILE);
197 rb->button_clear_queue();
198 rb->button_get(true);
200 return false;
203 static bool file_speed(int chunksize, bool align)
205 unsigned char text_buf[64];
206 int fd, ret;
207 long filesize = 0;
208 long size, time;
210 if (chunksize >= audiobuflen)
211 return false;
213 log_text("--------------------", true);
215 /* File creation write speed */
216 fd = rb->creat(TEST_FILE, 0666);
217 if (fd < 0)
219 rb->splashf(HZ, "creat() failed: %d", fd);
220 goto error;
222 time = *rb->current_tick;
223 while (TIME_BEFORE(*rb->current_tick, time + TEST_TIME*HZ))
225 ret = rb->write(fd, audiobuf + (align ? 0 : 1), chunksize);
226 if (chunksize != ret)
228 rb->splashf(HZ, "write() failed: %d/%d", ret, chunksize);
229 rb->close(fd);
230 goto error;
232 filesize += chunksize;
234 time = *rb->current_tick - time;
235 rb->close(fd);
236 rb->snprintf(text_buf, sizeof text_buf, "Create (%d,%c): %ld KB/s",
237 chunksize, align ? 'A' : 'U', (25 * (filesize>>8) / time) );
238 log_text(text_buf, true);
240 /* Existing file write speed */
241 fd = rb->open(TEST_FILE, O_WRONLY);
242 if (fd < 0)
244 rb->splashf(0, "open() failed: %d", fd);
245 goto error;
247 time = *rb->current_tick;
248 for (size = filesize; size > 0; size -= chunksize)
250 ret = rb->write(fd, audiobuf + (align ? 0 : 1), chunksize);
251 if (chunksize != ret)
253 rb->splashf(0, "write() failed: %d/%d", ret, chunksize);
254 rb->close(fd);
255 goto error;
258 time = *rb->current_tick - time;
259 rb->close(fd);
260 rb->snprintf(text_buf, sizeof text_buf, "Write (%d,%c): %ld KB/s",
261 chunksize, align ? 'A' : 'U', (25 * (filesize>>8) / time) );
262 log_text(text_buf, true);
264 /* File read speed */
265 fd = rb->open(TEST_FILE, O_RDONLY);
266 if (fd < 0)
268 rb->splashf(0, "open() failed: %d", fd);
269 goto error;
271 time = *rb->current_tick;
272 for (size = filesize; size > 0; size -= chunksize)
274 ret = rb->read(fd, audiobuf + (align ? 0 : 1), chunksize);
275 if (chunksize != ret)
277 rb->splashf(0, "read() failed: %d/%d", ret, chunksize);
278 rb->close(fd);
279 goto error;
282 time = *rb->current_tick - time;
283 rb->close(fd);
284 rb->snprintf(text_buf, sizeof text_buf, "Read (%d,%c): %ld KB/s",
285 chunksize, align ? 'A' : 'U', (25 * (filesize>>8) / time) );
286 log_text(text_buf, true);
287 rb->remove(TEST_FILE);
288 return true;
290 error:
291 rb->remove(TEST_FILE);
292 return false;
295 static bool test_speed(void)
297 unsigned char text_buf[64];
298 DIR *dir = NULL;
299 struct dirent *entry = NULL;
300 int fd, last_file;
301 int i, n;
302 long time;
304 rb->memset(audiobuf, 'T', audiobuflen);
305 log_init();
306 log_text("test_disk SPEED TEST", true);
307 #if (CONFIG_PLATFORM & PLATFORM_NATIVE)
308 rb->snprintf(text_buf, sizeof(text_buf), "CPU clock: %ld Hz",
309 *rb->cpu_frequency);
310 log_text(text_buf, true);
311 #endif
312 log_text("--------------------", true);
314 /* File creation speed */
315 time = *rb->current_tick + TEST_TIME*HZ;
316 for (i = 0; TIME_BEFORE(*rb->current_tick, time); i++)
318 rb->snprintf(text_buf, sizeof(text_buf), TESTBASEDIR "/%08x.tmp", i);
319 fd = rb->creat(text_buf, 0666);
320 if (fd < 0)
322 last_file = i;
323 rb->splashf(HZ, "creat() failed: %d", fd);
324 goto error;
326 rb->close(fd);
328 last_file = i;
329 rb->snprintf(text_buf, sizeof(text_buf), "Create: %d files/s",
330 last_file / TEST_TIME);
331 log_text(text_buf, true);
333 /* File open speed */
334 time = *rb->current_tick + TEST_TIME*HZ;
335 for (n = 0, i = 0; TIME_BEFORE(*rb->current_tick, time); n++, i++)
337 if (i >= last_file)
338 i = 0;
339 rb->snprintf(text_buf, sizeof(text_buf), TESTBASEDIR "/%08x.tmp", i);
340 fd = rb->open(text_buf, O_RDONLY);
341 if (fd < 0)
343 rb->splashf(HZ, "open() failed: %d", fd);
344 goto error;
346 rb->close(fd);
348 rb->snprintf(text_buf, sizeof(text_buf), "Open: %d files/s", n / TEST_TIME);
349 log_text(text_buf, true);
351 /* Directory scan speed */
352 time = *rb->current_tick + TEST_TIME*HZ;
353 for (n = 0; TIME_BEFORE(*rb->current_tick, time); n++)
355 if (entry == NULL)
357 if (dir != NULL)
358 rb->closedir(dir);
359 dir = rb->opendir(testbasedir);
360 if (dir == NULL)
362 rb->splash(HZ, "opendir() failed.");
363 goto error;
366 entry = rb->readdir(dir);
368 rb->closedir(dir);
369 rb->snprintf(text_buf, sizeof(text_buf), "Dirscan: %d files/s", n / TEST_TIME);
370 log_text(text_buf, true);
372 /* File delete speed */
373 time = *rb->current_tick;
374 for (i = 0; i < last_file; i++)
376 rb->snprintf(text_buf, sizeof(text_buf), TESTBASEDIR "/%08x.tmp", i);
377 rb->remove(text_buf);
379 rb->snprintf(text_buf, sizeof(text_buf), "Delete: %ld files/s",
380 last_file * HZ / (*rb->current_tick - time));
381 log_text(text_buf, true);
383 if (file_speed(512, true)
384 && file_speed(512, false)
385 && file_speed(4096, true)
386 && file_speed(4096, false)
387 && file_speed(1048576, true))
388 file_speed(1048576, false);
390 log_text("DONE", false);
391 log_close();
392 rb->button_clear_queue();
393 rb->button_get(true);
394 return false;
396 error:
397 for (i = 0; i < last_file; i++)
399 rb->snprintf(text_buf, sizeof(text_buf), TESTBASEDIR "/%08x.tmp", i);
400 rb->remove(text_buf);
402 log_text("DONE", false);
403 log_close();
404 rb->button_clear_queue();
405 rb->button_get(true);
406 return false;
410 /* this is the plugin entry point */
411 enum plugin_status plugin_start(const void* parameter)
413 MENUITEM_STRINGLIST(menu, "Test Disk Menu", NULL,
414 "Disk speed", "Write & verify");
415 int selected=0;
416 bool quit = false;
417 int align;
418 DIR *dir;
420 (void)parameter;
422 if ((dir = rb->opendir(testbasedir)) == NULL)
424 if (rb->mkdir(testbasedir) < 0)
426 rb->splash(HZ*2, "Can't create test directory.");
427 return PLUGIN_ERROR;
430 else
432 rb->closedir(dir);
435 audiobuf = rb->plugin_get_audio_buffer((size_t *)&audiobuflen);
436 /* align start and length to 32 bit */
437 align = (-(int)audiobuf) & 3;
438 audiobuf += align;
439 audiobuflen = (audiobuflen - align) & ~3;
441 rb->srand(*rb->current_tick);
443 /* Turn off backlight timeout */
444 backlight_force_on(); /* backlight control in lib/helper.c */
446 while(!quit)
448 switch(rb->do_menu(&menu, &selected, NULL, false))
450 case 0:
451 test_speed();
452 break;
453 case 1:
454 test_fs();
455 break;
456 default:
457 quit = true;
458 break;
462 /* Turn on backlight timeout (revert to settings) */
463 backlight_use_settings(); /* backlight control in lib/helper.c */
465 rb->rmdir(testbasedir);
467 return PLUGIN_OK;