flashbench-62/COPYING0000644000000000000000000004325411721171625012574 0ustar 00000000000000 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. flashbench-62/Makefile0000644000000000000000000000055411763551453013204 0ustar 00000000000000CC := gcc CFLAGS := -O2 -Wall -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -g2 LDFLAGS := -lrt all: flashbench erase dev.o: dev.c dev.h vm.o: vm.c vm.h dev.h flashbench.o: flashbench.c vm.h dev.h flashbench: flashbench.o dev.o vm.o $(CC) -o $@ flashbench.o dev.o vm.o $(LDFLAGS) erase: erase.o clean: rm flashbench flashbench.o dev.o vm.o flashbench-62/README0000644000000000000000000001173511527055447012427 0ustar 00000000000000= flashbench -- identify characteristics of flash media = This is the tool used to identify the properties of SD cards and other media for the Linaro flash memory survey at [1]. The latest version should be available at [2]. Please also check out the article on lwn.net [3]. A short introduction to the most useful commands follows. == Guess erase block and page sizes == ''flashbench -a '' This is a simple read-only test doing small reads across boundaries of various sizes. Example: $ sudo ./flashbench -a /dev/mmcblk0 --blocksize=1024 align 134217728 pre 735µs on 1.08ms post 780µs diff 324µs align 67108864 pre 736µs on 1.05ms post 763µs diff 300µs align 33554432 pre 722µs on 1.04ms post 763µs diff 294µs align 16777216 pre 727µs on 1.05ms post 772µs diff 302µs align 8388608 pre 724µs on 1.04ms post 768µs diff 299µs align 4194304 pre 741µs on 1.08ms post 788µs diff 317µs align 2097152 pre 745µs on 950µs post 811µs diff 171µs align 1048576 pre 745µs on 945µs post 807µs diff 169µs align 524288 pre 743µs on 936µs post 799µs diff 165µs align 262144 pre 746µs on 948µs post 809µs diff 171µs align 131072 pre 737µs on 935µs post 804µs diff 165µs align 65536 pre 735µs on 925µs post 796µs diff 159µs align 32768 pre 735µs on 925µs post 800µs diff 157µs align 16384 pre 745µs on 911µs post 781µs diff 148µs align 8192 pre 785µs on 808µs post 725µs diff 53.3µs align 4096 pre 784µs on 788µs post 779µs diff 5.85µs align 2048 pre 787µs on 793µs post 789µs diff 4.65µs This shows the access times to do two 1024 byte reads around the boundaries of power-of-two aligned blocks. Reading at the end of a 128 MB unit takes around 735 microseconds, reading x the last block of this unit together with the first block of the next one takes about 1080 microseconds and reading the first two blocks in a 128 MB unit takes around 780 microseconds. The most interesting number here is the last one, the difference between the second number and the average of the first and the third is 324 microseconds. These numbers all stay roughly the same for all units between 4 MB and 128 MB. However, from 2 MB down to 16 KB, the last column has a much lower value. This indicates that whatever the memory card does on a 4 MB boundary does not happen at other boundaries. The educated guess here is that 4 MB is the erase block size, also called the segment or allocation unit size. This erase blocksize will need to be used in other tests following this one. Similarly, both 16 KB and 8 KB boundaries are special. The logical explanation for this is that the card has 8 KB pages, but can use multi-plane accesses to read two 8 KB pages simultaneously. Some cards only show a clear pattern using accesses with certain block sizes, other cards do not show any pattern, which means that the numbers need to be determined differently. Also, cards that were never fully written may show a different behaviour because access times on pre-erased segments are different from those that have been written. == Create a scatter plot of access times == ''flashbench -s --scatter-order= --scatter-span= -o '' Writes a scatter plot into a file that can be used as input for a ''gnuplot -p -e 'plot "file"' '' == Finding the number of open erase blocks == ''flashbench --open-au --open-au-nr= --erasesize= [--random]'' Example: $ sudo ./flashbench -O --erasesize=$[4 * 1024 * 1024] \ --blocksize=$[256 * 1024] /dev/mmcblk0 --open-au-nr=2 4MiB 8.79M/s 2MiB 7.41M/s 1MiB 6.87M/s 512KiB 6.39M/s 256KiB 6.27M/s $ sudo ./flashbench -O --erasesize=$[4 * 1024 * 1024] \ --blocksize=$[256 * 1024] /dev/mmcblk0 --open-au-nr=3 4MiB 7.75M/s 2MiB 5.03M/s 1MiB 3.24M/s 512KiB 1.76M/s 256KiB 912K/s In this case, trying 2 open AUs shows fast accesses for small block sizes, but trying 3 open AUs is much slower, and degrades further at smaller sizes. Try varying numbers until hitting the cut-off point. For cards that are fast when using --random, this will find the cut-off more reliably. Some cards can do more open segments in linear mode than they can in random mode. == References == [1] https://wiki.linaro.org/WorkingGroups/KernelConsolidation/Projects/FlashCardSurvey [2] git clone git://git.linaro.org/people/arnd/flashbench.git [3] http://lwn.net/Articles/428XXX FIXME Feel free to reach the author by email for any questions about the latest version, Arnd Bergmann , or use the linaro-dev@lists.linaro.org mailing list for discussions. If you use this tool to measure memory cards, USB sticks or SSDs and get useful results, please share them at flashbench-results@lists.linaro.org. flashbench-62/dev.c0000644000000000000000000000547011535751056012466 0ustar 00000000000000#define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "dev.h" #define MAX_BUFSIZE (64 * 1024 * 1024) static inline long long time_to_ns(struct timespec *ts) { return (long long)ts->tv_sec * 1000 * 1000 * 1000 + ts->tv_nsec; } static long long get_ns(void) { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return time_to_ns(&ts); } long long time_read(struct device *dev, off_t pos, size_t size) { long long now = get_ns(); ssize_t ret; if (size > MAX_BUFSIZE) return -ENOMEM; do { ret = pread(dev->fd, dev->readbuf, size, pos % dev->size); if (ret > 0) { size -= ret; pos += ret; } } while (ret > 0 || errno == -EAGAIN); if (ret) { perror("time_read"); return 0; } return get_ns() - now; } long long time_write(struct device *dev, off_t pos, size_t size, enum writebuf which) { long long now = get_ns(); ssize_t ret; unsigned long *p; if (size > MAX_BUFSIZE) return -ENOMEM; p = dev->writebuf[which]; do { ret = pwrite(dev->fd, p, size, pos % dev->size); if (ret > 0) { size -= ret; pos += ret; } } while (ret > 0 || errno == -EAGAIN); if (ret) { perror("time_write"); return 0; } return get_ns() - now; } long long time_erase(struct device *dev, off_t pos, size_t size) { long long now = get_ns(); ssize_t ret; unsigned long long args[2] = { size, pos % dev->size }; if (size > MAX_BUFSIZE) return -ENOMEM; ret = ioctl(dev->fd, BLKDISCARD, &args); if (ret) { perror("time_erase"); } return get_ns() - now; } static void set_rtprio(void) { int ret; struct sched_param p = { .sched_priority = 10, }; ret = sched_setscheduler(0, SCHED_FIFO, &p); if (ret) perror("sched_setscheduler"); } int setup_dev(struct device *dev, const char *filename) { int err; void *p; set_rtprio(); dev->fd = open(filename, O_RDWR | O_DIRECT | O_SYNC | O_NOATIME); if (dev->fd < 0) { perror(filename); return -errno; } dev->size = lseek(dev->fd, 0, SEEK_END); if (dev->size < 0) { perror("seek"); return -errno; } err = posix_memalign(&dev->readbuf, 4096, MAX_BUFSIZE); if (err) return -err; err = posix_memalign(&p, 4096, MAX_BUFSIZE); if (err) return -err; memset(p, 0, MAX_BUFSIZE); dev->writebuf[WBUF_ZERO] = p; err = posix_memalign(&p, 4096, MAX_BUFSIZE); if (err) return -err; memset(p, 0xff, MAX_BUFSIZE); dev->writebuf[WBUF_ONE] = p; err = posix_memalign(&p , 4096, MAX_BUFSIZE); if (err) return -err; memset(p, 0x5a, MAX_BUFSIZE); dev->writebuf[WBUF_RAND] = p; return 0; } flashbench-62/dev.h0000644000000000000000000000101111513423351012444 0ustar 00000000000000#ifndef FLASHBENCH_DEV_H #define FLASHBENCH_DEV_H #include struct device { void *readbuf; void *writebuf[3]; int fd; off_t size; }; enum writebuf { WBUF_ZERO, WBUF_ONE, WBUF_RAND, }; extern int setup_dev(struct device *dev, const char *filename); long long time_write(struct device *dev, off_t pos, size_t size, enum writebuf which); long long time_read(struct device *dev, off_t pos, size_t size); long long time_erase(struct device *dev, off_t pos, size_t size); #endif /* FLASHBENCH_DEV_H */ flashbench-62/erase.c0000644000000000000000000000124711527023437013001 0ustar 00000000000000#define _GNU_SOURCE #include #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int fd = open(argv[1], O_RDWR | O_DIRECT); int ret; unsigned long long range[2]; if (argc != 4) { fprintf(stderr, "usage: %s \n", argv[0]); } if (fd < 0) { perror("open"); return errno; } range[0] = atoll(argv[2]); range[1] = atoll(argv[3]); printf("erasing %lld to %lld on %s\n", range[0], range[0] + range[1], argv[1]); ret = ioctl(fd, BLKDISCARD, range); if (ret) perror("ioctl"); return errno; } flashbench-62/flashbench.c0000644000000000000000000005301011763551557014005 0ustar 00000000000000#define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include #include #include #include "dev.h" #include "vm.h" typedef long long ns_t; #define returnif(x) do { typeof(x) __x = (x); if (__x < 0) return (__x); } while (0) static ns_t ns_min(int count, ns_t data[]) { int i; ns_t min = LLONG_MAX; for (i=0; i max) max = data[i]; } return max; } static ns_t ns_avg(int count, ns_t data[]) { int i; ns_t sum = 0; for (i=0; i ret) results[i] = ret; } return 0; } static void print_one_blocksize(int count, ns_t *times, off_t blocksize) { char min[8], avg[8], max[8]; format_ns(min, ns_min(count, times)); format_ns(avg, ns_avg(count, times)); format_ns(max, ns_max(count, times)); printf("%lld bytes: min %s avg %s max %s: %g MB/s\n", (long long)blocksize, min, avg, max, blocksize / (ns_min(count, times) / 1000.0)); } static int try_interval(struct device *dev, long blocksize, ns_t *min_time, int count) { int ret; ns_t times[count]; memset(times, 0, sizeof(times)); ret = time_read_interval(dev, count, times, blocksize, 0, blocksize * 9); returnif (ret); print_one_blocksize(count, times, blocksize); *min_time = ns_min(count, times); return 0; } static int try_intervals(struct device *dev, int count, int rounds) { const int ignore = 3; ns_t min[rounds]; off_t bytes[rounds]; ns_t atime; float throughput; int i; for (i=0; i= (1 << bits)) { fprintf(stderr, "flashbench: internal error\n"); exit(-EINVAL); } if (v == 0) v = ((1 << bits) - 1) & 0xace1; switch (bits) { case 8: /* x^8 + x^6 + x^5 + x^4 + 1 */ bit = ((v >> 0) ^ (v >> 2) ^ (v >> 3) ^ (v >> 4)) & 1; break; case 9: /* x9 + x5 + 1 */ bit = ((v >> 0) ^ (v >> 4)) & 1; break; case 10: /* x10 + x7 + 1 */ bit = ((v >> 0) ^ (v >> 3)) & 1; break; case 11: /* x11 + x9 + 1 */ bit = ((v >> 0) ^ (v >> 2)) & 1; break; case 12: bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 8)) & 1; break; case 13: /* x^13 + x^12 + x^11 + x^8 + 1 */ bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 5)) & 1; break; case 14: /* x^14 + x^13 + x^12 + x^2 + 1 */ bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 12)) & 1; break; case 15: /* x^15 + x^14 + 1 */ bit = ((v >> 0) ^ (v >> 1) ) & 1; break; case 16: /* x^16 + x^14 + x^13 + x^11 + 1 */ bit = ((v >> 0) ^ (v >> 2) ^ (v >> 3) ^ (v >> 5) ) & 1; break; default: fprintf(stderr, "flashbench: internal error\n"); exit(-EINVAL); } return v >> 1 | bit << (bits - 1); } static int try_scatter_io(struct device *dev, int tries, int scatter_order, int scatter_span, int blocksize, FILE *out) { int i, j; const int count = 1 << scatter_order; ns_t time; ns_t min[count]; unsigned long pos; memset(min, 0, sizeof(min)); for (i = 0; i < tries; i++) { pos = 0; for (j = 0; j < count; j++) { time = time_read(dev, (pos * blocksize), scatter_span * blocksize); returnif (time); if (i == 0 || time < min[pos]) min[pos] = time; pos = lfsr(pos, scatter_order); } } for (j = 0; j < count; j++) { fprintf(out, "%f %f\n", j * blocksize / (1024 * 1024.0), min[j] / 1000000.0); } return 0; } static unsigned int find_order(unsigned int large, unsigned int small) { unsigned int o; for (o=1; small < large; small <<= 1) o++; return o; } static int try_read_alignment(struct device *dev, int tries, int count, off_t maxalign, off_t align, size_t blocksize) { ns_t pre[count], on[count], post[count]; char pre_s[8], on_s[8], post_s[8], diff_s[8]; int i, ret; memset(pre, 0, sizeof(pre)); memset(on, 0, sizeof(on)); memset(post, 0, sizeof(post)); for (i = 0; i < tries; i++) { ret = time_read_interval(dev, count, pre, blocksize, align - blocksize, maxalign); returnif(ret); ret = time_read_interval(dev, count, on, blocksize, align - blocksize / 2, maxalign); returnif(ret); ret = time_read_interval(dev, count, post, blocksize, align, maxalign); returnif(ret); } format_ns(pre_s, ns_avg(count, pre)); format_ns(on_s, ns_avg(count, on)); format_ns(post_s, ns_avg(count, post)); format_ns(diff_s, ns_avg(count, on) - (ns_avg(count, pre) + ns_avg(count, post)) / 2); printf("align %lld\tpre %s\ton %s\tpost %s\tdiff %s\n", (long long)align, pre_s, on_s, post_s, diff_s); return 0; } static int try_read_alignments(struct device *dev, int tries, int blocksize) { const int count = 7; int ret; off_t align, maxalign; /* make sure we can fit eight power-of-two blocks in the device */ for (maxalign = blocksize * 2; maxalign < dev->size / count; maxalign *= 2) ; for (align = maxalign; align >= blocksize * 2; align /= 2) { ret = try_read_alignment(dev, tries, count, maxalign, align, blocksize); returnif (ret); } return 0; } static int try_program(struct device *dev) { #if 0 struct operation program[] = { {O_REPEAT, 4}, {O_SEQUENCE, 3}, {O_PRINT, .string = "Hello, World!\n"}, {O_DROP}, {O_PRINTF}, {O_FORMAT}, {O_REDUCE, 8, .aggregate = A_AVERAGE}, {O_LEN_POW2, 8, 512}, {O_OFF_LIN, 8, 4096 }, {O_SEQUENCE, 3}, {O_PRINTF}, {O_READ}, {O_NEWLINE}, {O_DROP}, {O_READ}, {O_END}, {O_NEWLINE}, {O_END}, {O_END}, }; #endif #if 0 struct operation program[] = { {O_SEQUENCE, 3}, {O_PRINT, .string="read by size\n"}, {O_LEN_POW2, 12, 512}, {O_DROP}, {O_SEQUENCE, 4}, {O_PRINTF}, {O_FORMAT}, {O_LENGTH}, {O_PRINT, .string = ": \t"}, {O_PRINTF}, {O_FORMAT}, {O_REDUCE, .aggregate = A_MINIMUM}, {O_OFF_LIN, 8, 4096 * 1024}, {O_READ}, {O_NEWLINE}, {O_END}, {O_NEWLINE}, {O_END}, {O_END}, }; #endif #if 1 /* show effect of type of access within AU */ struct operation program[] = { /* loop through power of two multiple of one sector */ {O_LEN_POW2, 13, -512}, {O_SEQUENCE, 3}, /* print block size */ {O_DROP}, {O_PRINTF}, {O_FORMAT}, {O_LENGTH}, /* start four units into the device, to skip FAT */ {O_OFF_FIXED, .val = 1024 * 4096 * 4}, {O_DROP}, /* print one line of aggregated per second results */ {O_PRINTF}, {O_SEQUENCE, 8}, /* write one block to clear state of block, ignore result */ // {O_DROP}, {O_LEN_FIXED, .val = 1024 * 4096}, // {O_WRITE_RAND}, #if 1 /* linear write zeroes */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_WRITE_ZERO}, /* linear write ones */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_WRITE_ONE}, #endif #if 0 /* Erase */ {O_FORMAT},{O_REDUCE, .aggregate = A_TOTAL},// {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_ERASE}, {O_FORMAT},{O_REDUCE, .aggregate = A_TOTAL},// {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_ERASE}, /* linear write 0x5a */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_WRITE_RAND}, #endif /* linear write 0x5a again */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_WRITE_RAND}, /* linear read */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_LIN, 8192, -1}, {O_READ}, #if 1 /* random write zeroes */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_WRITE_ZERO}, /* random write ones */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_WRITE_ONE}, #endif #if 0 /* random erase */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE},// {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_ERASE}, {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE},// {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_ERASE}, /* random write 0x5a */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_WRITE_RAND}, #endif /* random write 0x5a again */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_WRITE_RAND}, /* random read */ {O_FORMAT},{O_REDUCE, .aggregate = A_AVERAGE}, {O_BPS}, {O_OFF_RAND, 8192, -1}, {O_READ}, {O_END}, {O_NEWLINE}, {O_END}, {O_END}, }; call(program, dev, 0, 1 * 4096 * 1024, 0); #endif return 0; } #if 0 static int try_open_au_oob(struct device *dev, unsigned int erasesize, unsigned int blocksize, unsigned int count, bool random) { /* find maximum number of open AUs */ struct operation program[] = { /* loop through power of two multiple of one sector */ {O_LEN_POW2, find_order(erasesize, blocksize), -(long long)blocksize}, {O_SEQUENCE, 4}, /* print block size */ {O_DROP}, {O_PRINTF}, {O_FORMAT}, {O_LENGTH}, /* start 16 MB into the device, to skip FAT */ {O_OFF_FIXED, .val = 4 * 1024 * 4128}, {O_DROP}, /* print one line of aggregated per second results */ {O_PRINTF}, {O_FORMAT}, {O_BPS}, /* linear write 0x5a */ {O_REDUCE, .aggregate = A_MAXIMUM}, {O_REPEAT, 1}, {O_REDUCE, .aggregate = A_AVERAGE}, { (random ? O_OFF_RAND : O_OFF_LIN), erasesize / blocksize, -1}, {O_REDUCE, .aggregate = A_AVERAGE}, // {O_BPS}, {O_OFF_RAND, count, /* 2 * erasesize */ 2 * 4128 * 1024}, {O_WRITE_RAND}, {O_DROP}, {O_OFF_FIXED, .val = 4 * 1024 * 4128 + 4 * 1024 * 1024}, {O_DROP}, {O_LEN_FIXED, .val = 32 * 1024}, {O_OFF_RAND, count, 2 * 4128 * 1024}, {O_WRITE_RAND}, {O_NEWLINE}, {O_END}, {O_END}, }; call(program, dev, 0, erasesize, 0); return 0; } #endif static int try_open_au(struct device *dev, unsigned int erasesize, unsigned int blocksize, unsigned int count, unsigned long long offset, bool random) { /* start 16 MB into the device, to skip FAT, round up to full erase blocks */ if (offset == -1ull) offset = (1024 * 1024 * 16 + erasesize - 1) / erasesize * erasesize; /* find maximum number of open AUs */ struct operation program[] = { /* loop through power of two multiple of one sector */ {O_LEN_POW2, find_order(erasesize, blocksize), -(long long)blocksize}, {O_SEQUENCE, 3}, /* print block size */ {O_DROP}, {O_PRINTF}, {O_FORMAT}, {O_LENGTH}, {O_OFF_FIXED, .val = offset}, {O_DROP}, /* print one line of aggregated per second results */ {O_PRINTF}, {O_FORMAT}, {O_BPS}, /* linear write 0x5a */ {O_REDUCE, .aggregate = A_MAXIMUM}, {O_REPEAT, 1}, {O_REDUCE, .aggregate = A_AVERAGE}, { (random ? O_OFF_RAND : O_OFF_LIN), erasesize / blocksize, -1}, {O_REDUCE, .aggregate = A_AVERAGE}, {O_OFF_RAND, count, 12 * erasesize}, {O_WRITE_RAND}, {O_NEWLINE}, {O_END}, {O_END}, }; call(program, dev, 0, erasesize, 0); return 0; } static int try_find_fat(struct device *dev, unsigned int erasesize, unsigned int blocksize, unsigned int count, bool random) { /* Find FAT Units */ struct operation program[] = { /* loop through power of two multiple of one sector */ {O_LEN_POW2, find_order(erasesize, blocksize), - (long long)blocksize}, {O_SEQUENCE, 3}, /* print block size */ {O_DROP}, {O_PRINTF}, {O_FORMAT}, {O_LENGTH}, /* print one line of aggregated per second results */ {O_PRINTF}, {O_FORMAT}, {O_OFF_LIN, count, erasesize}, /* linear write 0x5a */ {O_REDUCE, .aggregate = A_MAXIMUM}, {O_REPEAT, 1}, {O_BPS}, {O_REDUCE, .aggregate = A_AVERAGE}, {random ? O_OFF_RAND : O_OFF_LIN, erasesize / blocksize, -1}, {O_WRITE_RAND}, {O_NEWLINE}, {O_END}, {O_END}, }; call(program, dev, 0, erasesize, 0); return 0; } static void print_help(const char *name) { printf("%s [OPTION]... [DEVICE]\n", name); printf("run tests on DEVICE, pointing to a flash storage medium.\n\n"); printf("-o, --out=FILE write output to FILE instead of stdout\n"); printf("-s, --scatter run scatter read test\n"); printf(" --scatter-order=N scatter across 2^N blocks\n"); printf(" --scatter-span=N span each write across N blocks\n"); printf("-f, --find-fat analyse first few erase blocks\n"); printf(" --fat-nr=N look through first N erase blocks (default: 6)\n"); printf("-O, --open-au find number of open erase blocks\n"); printf(" --open-au-nr=N try N open erase blocks\n"); printf(" --offset=N start at position N\n"); printf("-r, --random use pseudorandom access with erase block\n"); printf("-v, --verbose increase verbosity of output\n"); printf("-c, --count=N run each test N times (default: 8\n"); printf("-b, --blocksize=N use a blocksize of N (default:16K)\n"); printf("-e, --erasesize=N use a eraseblock size of N (default:4M)\n"); } struct arguments { const char *dev; const char *out; bool scatter, interval, program, fat, open_au, align; bool random; int count; int blocksize; int erasesize; unsigned long long offset; int scatter_order; int scatter_span; int interval_order; int fat_nr; int open_au_nr; }; static int parse_arguments(int argc, char **argv, struct arguments *args) { static const struct option long_options[] = { { "out", 1, NULL, 'o' }, { "scatter", 0, NULL, 's' }, { "scatter-order", 1, NULL, 'S' }, { "scatter-span", 1, NULL, '$' }, { "align", 0, NULL, 'a' }, { "interval", 0, NULL, 'i' }, { "interval-order", 1, NULL, 'I' }, { "findfat", 0, NULL, 'f' }, { "fat-nr", 1, NULL, 'F' }, { "open-au", 0, NULL, 'O' }, { "open-au-nr", 1, NULL, '0' }, { "offset", 1, NULL, 't' }, { "random", 0, NULL, 'r' }, { "verbose", 0, NULL, 'v' }, { "count", 1, NULL, 'c' }, { "blocksize", 1, NULL, 'b' }, { "erasesize", 1, NULL, 'e' }, { NULL, 0, NULL, 0 }, }; memset(args, 0, sizeof(*args)); args->count = 8; args->scatter_order = 9; args->scatter_span = 1; args->blocksize = 16384; args->offset = -1ull; args->erasesize = 4 * 1024 * 1024; args->fat_nr = 6; args->open_au_nr = 2; while (1) { int c; c = getopt_long(argc, argv, "o:siafF:Ovrc:b:e:p", long_options, &optind); if (c == -1) break; switch (c) { case 'o': args->out = optarg; break; case 's': args->scatter = 1; break; case 'S': args->scatter_order = atoi(optarg); break; case '$': args->scatter_span = atoi(optarg); break; case 'a': args->align = 1; break; case 'i': args->interval = 1; break; case 'I': args->interval_order = atoi(optarg); break; case 'f': args->fat = 1; break; case 'F': args->fat_nr = atoi(optarg); break; case 'O': args->open_au = 1; break; case '0': args->open_au_nr = atoi(optarg); break; case 'r': args->random = 1; break; case 'p': args->program = 1; break; case 'v': verbose++; break; case 'c': args->count = atoi(optarg); break; case 'b': args->blocksize = atoi(optarg); break; case 'e': args->erasesize = atoi(optarg); break; case 't': args->offset = strtoull(optarg, NULL, 0); break; case '?': print_help(argv[0]); return -EINVAL; break; } } if (optind != (argc - 1)) { fprintf(stderr, "%s: invalid arguments\n", argv[0]); return -EINVAL; } args->dev = argv[optind]; if (!(args->scatter || args->interval || args->program || args->fat || args->open_au || args->align)) { fprintf(stderr, "%s: need at least one action\n", argv[0]); return -EINVAL; } if (args->scatter && (args->scatter_order > 16)) { fprintf(stderr, "%s: scatter_order must be at most 16\n", argv[0]); return -EINVAL; } return 0; } static FILE *open_output(const char *filename) { if (!filename || !strcmp(filename, "-")) return fdopen(0, "w"); /* write to stdout */ return fopen(filename, "w+"); } int main(int argc, char **argv) { struct device dev; struct arguments args; FILE *output; int ret; returnif(parse_arguments(argc, argv, &args)); returnif(setup_dev(&dev, args.dev)); output = open_output(args.out); if (!output) { perror(args.out); return -errno; } if (verbose > 1) { printf("filename: \"%s\"\n", argv[1]); printf("filesize: 0x%llx\n", (unsigned long long)dev.size); } if (args.scatter) { ret = try_scatter_io(&dev, args.count, args.scatter_order, args.scatter_span, args.blocksize, output); if (ret < 0) { errno = -ret; perror("try_scatter_io"); return ret; } } if (args.fat) { ret = try_find_fat(&dev, args.erasesize, args.blocksize, args.fat_nr, args.random); if (ret < 0) { errno = -ret; perror("find_fat"); } } if (args.align) { ret = try_read_alignments(&dev, args.count, args.blocksize); if (ret < 0) { errno = -ret; perror("try_align"); return ret; } } if (args.open_au) { ret = try_open_au(&dev, args.erasesize, args.blocksize, args.open_au_nr, args.offset, args.random); if (ret < 0) { errno = -ret; perror("find_fat"); return ret; } } if (args.interval) { ret = try_intervals(&dev, args.count, args.interval_order); if (ret < 0) { errno = -ret; perror("try_intervals"); return ret; } } if (args.program) { try_program(&dev); } return 0; } flashbench-62/vm.c0000644000000000000000000004145411527013640012323 0ustar 00000000000000#define _GNU_SOURCE #define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include "dev.h" #include "vm.h" static inline res_t *res_ptr(res_t r) { return (res_t *)((unsigned long)r._p & ~7); } static inline enum resulttype res_type(res_t r) { return (enum resulttype)((unsigned long)r._p & 7); } static inline res_t to_res(res_t *_p, enum resulttype t) { return (res_t) { ._p = (res_t *)(((unsigned long)_p & ~7) | (t & 7)) }; } static const res_t res_null = { }; struct syntax { enum opcode opcode; const char *name; struct operation *(*function)(struct operation *op, struct device *dev, off_t off, off_t max, size_t len); enum { P_NUM = 1, P_VAL = 2, P_STRING = 4, P_AGGREGATE = 8, P_ATOM = 16, } param; }; static struct syntax syntax[]; int verbose = 0; struct operation *call(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next; if (!op) return_err("internal error: NULL operation\n"); pr_debug("call %s %lld %lld %ld\n", syntax[op->code].name, (long long)off, (long long)max, (long)len); if (op->code > O_MAX) return_err("illegal command code %d\n", op->code); if (!(syntax[op->code].param & P_NUM) != !op->num) return_err("need .num= argument\n"); if (!(syntax[op->code].param & P_VAL) != !op->val) return_err("need .param= argument\n"); if (!(syntax[op->code].param & P_STRING) != !op->string) return_err("need .string= argument\n"); if (!(syntax[op->code].param & P_AGGREGATE) != !op->aggregate) return_err("need .aggregate= argument\n"); if (op->num) { res_t *data; if (res_ptr(op->result)) return_err("%s already has result\n", syntax[op->code].name); data = calloc(sizeof (res_t), op->num); if (!data) return_err("out of memory"); op->result = to_res(data, R_NONE); op->r_type = R_ARRAY; } next = syntax[op->code].function(op, dev, off, max, len); if (!next) return_err("from %s\n", syntax[op->code].name); return next; } static struct operation *call_propagate(struct operation *op, struct device *dev, off_t off, off_t max, size_t len, struct operation *this) { struct operation *next; next = call(op, dev, off, max, len); this->result = op->result; this->size_x = op->size_x; this->size_y = op->size_y; this->r_type = op->r_type; op->result = res_null; op->size_x = op->size_y = 0; op->r_type = R_NONE; return next; } static struct operation *call_aggregate(struct operation *op, struct device *dev, off_t off, off_t max, size_t len, struct operation *this) { struct operation *next; res_t *res = res_ptr(this->result); enum resulttype type = res_type(this->result); next = call(op, dev, off, max, len); if (!next) return NULL; if (this->size_x >= this->num) return_err("array too small for %d entries\n", this->size_x); res[this->size_x] = op->result; /* no result */ if (op->r_type == R_NONE) return next; this->size_x++; /* first data in this aggregation: set type */ if (type == R_NONE) { type = op->r_type; this->result = to_res(res, type); } if (type != op->r_type) { return_err("cannot aggregate return type %d with %d\n", type, op->r_type); } if (op->r_type == R_ARRAY) { if (this->size_y && this->size_y != op->size_x) return_err("cannot aggregate different size arrays (%d, %d)\n", this->size_y, op->size_x); if (op->size_y) return_err("cannot aggregate three-dimensional array\n"); this->size_y = op->size_x; op->size_x = op->size_y = 0; } op->r_type = R_NONE; op->result = res_null; return next; } static struct operation *nop(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { return_err("command not implemented\n"); } static struct operation *do_read(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = time_read(dev, off, len); op->r_type = R_NS; return op+1; } static struct operation *do_write_zero(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = time_write(dev, off, len, WBUF_ZERO); op->r_type = R_NS; return op+1; } static struct operation *do_write_one(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = time_write(dev, off, len, WBUF_ONE); op->r_type = R_NS; return op+1; } static struct operation *do_write_rand(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = time_write(dev, off, len, WBUF_RAND); op->r_type = R_NS; return op+1; } static struct operation *do_erase(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = time_erase(dev, off, len); op->r_type = R_NS; return op+1; } static struct operation *length_or_offs(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { op->result.l = (op->code == O_LENGTH) ? (long long)len : off; op->r_type = R_BYTE; return op+1; } static res_t format_value(res_t val, enum resulttype type, unsigned int size_x, unsigned int size_y) { long long l = val.l; unsigned int x; res_t *res; res_t out; switch (type) { case R_ARRAY: res = res_ptr(val); for (x = 0; x < size_x; x++) { res[x] = format_value(res[x], res_type(val), size_y, 0); if (res[x].s == res_null.s) return res_null; } if (res_type(val) == R_ARRAY) out = val; else out = to_res(res_ptr(val), R_STRING); return out; case R_BYTE: if (l < 1024) snprintf(out.s, 8, "%0lldB", l); else if (l < 1024 * 1024) snprintf(out.s, 8, "%0.3gKiB", l / 1024.0); else if (l < 1024 * 1024 * 1024) snprintf(out.s, 8, "%0.3gMiB", l / (1024.0 * 1024.0)); else snprintf(out.s, 8, "%0.4gGiB", l / (1024.0 * 1024.0 * 1024.0)); break; case R_BPS: if (l < 1000) snprintf(out.s, 8, "%0lldB/s", l); else if (l < 1000 * 1000) snprintf(out.s, 8, "%.03gK/s", l / 1000.0); else if (l < 1000 * 1000 * 1000) snprintf(out.s, 8, "%.03gM/s", l / (1000.0 * 1000.0)); else snprintf(out.s, 8, "%.04gG/s", l / (1000.0 * 1000.0 * 1000.0)); break; case R_NS: if (l < 1000) snprintf(out.s, 8, "%lldns", l); else if (l < 1000 * 1000) snprintf(out.s, 8, "%.3gµs", l / 1000.0); else if (l < 1000 * 1000 * 1000) snprintf(out.s, 8, "%.3gms", l / 1000000.0); else snprintf(out.s, 8, "%.4gs", l / 1000000000.0); break; default: return res_null; } for (x = strlen(out.s); x<7; x++) out.s[x] = ' '; out.s[7] = '\0'; return out; } static struct operation *format(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next; next = call_propagate(op+1, dev, off, max, len, op); op->result = format_value(op->result, op->r_type, op->size_x, op->size_y); if (op->result.s == res_null.s) return NULL; if (op->r_type != R_ARRAY) op->r_type = R_STRING; return next; } static struct operation *print_string(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { printf("%s", op->string); return op+1; } static void *print_value(res_t val, enum resulttype type, unsigned int size_x, unsigned int size_y) { unsigned int x; res_t *res; switch (type) { case R_ARRAY: res = res_ptr(val); for (x=0; x < size_x; x++) { if (!print_value(res[x], res_type(val), size_y, 0)) return_err("cannot print array of type %d\n", res_type(val)); printf(size_y ? "\n" : " "); } break; case R_BYTE: case R_NS: case R_BPS: printf("%lld ", val.l); break; case R_STRING: printf("%s ", val.s); break; default: return NULL; } return (void *)1; } static struct operation *print_val(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next; next = call_propagate(op+1, dev, off, max, len, op); if (!next) return NULL; if (!print_value(op->result, op->r_type, op->size_x, op->size_y)) return_err("cannot print value of type %d\n", op->r_type); return next; } static struct operation *newline(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { printf("\n"); return op+1; } static res_t bytespersec_one(res_t res, size_t bytes, enum resulttype type, unsigned int size_x, unsigned int size_y) { if (type == R_NS) res.l = 1000000000ll * bytes / res.l; else if (type == R_ARRAY) { res_t *array = res_ptr(res); type = res_type(res); unsigned int x; for (x = 0; x < size_x; x++) array[x] = bytespersec_one(array[x], bytes, type, size_y, 0); if (type == R_NS) res = to_res(array, R_BPS); } else { res = res_null; } return res; } static struct operation *bytespersec(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next; next = call_propagate(op+1, dev, off, max, len, op); op->result = bytespersec_one(op->result, len, op->r_type, op->size_x, op->size_y); if (op->result.l == res_null.l) return_err("invalid data, type %d\n", op->r_type); if (op->r_type == R_NS) op->r_type = R_BPS; return next; } static struct operation *sequence(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { unsigned int i; struct operation *next = op+1; for (i=0; inum; i++) { next = call_aggregate(next, dev, off, max, len, op); if (!next) return NULL; } /* immediately fold sequences with a single result */ if (op->size_x == 1) { op->r_type = res_type(op->result); op->result = res_ptr(op->result)[0]; op->size_x = op->size_y; op->size_y = 0; } if (next && next->code != O_END) return_err("sequence needs to end with END command\n"); return next+1; } static struct operation *len_fixed(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { return call_propagate(op+1, dev, off, max, op->val, op); } static struct operation *len_pow2(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { unsigned int i; struct operation *next = op+1; if (!len) len = 1; if (op->val > 0) { for (i = 0; i < op->num && next; i++) next = call_aggregate(op+1, dev, off, max, len * op->val << i, op); } else { for (i = op->num; i>0 && next; i--) next = call_aggregate(op+1, dev, off, max, len * (-op->val/2) << i, op); } return next; } static struct operation *off_fixed(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { return call_propagate(op+1, dev, off + op->val, max, len, op); } static struct operation *off_lin(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next = op+1; unsigned int i; unsigned int num, val; if (op->val == -1) { if (len == 0 || max < (off_t)len) return_err("cannot fill %lld bytes with %ld byte chunks\n", (long long)max, (long)len); num = max/len; val = max/num; } else { val = op->val; num = op->num; } for (i = 0; i < num && next; i++) next = call_aggregate(op+1, dev, off + i * val, max, len, op); return next; } /* * Linear feedback shift register * * We use this to randomize the block positions for random-access * tests. Unlike real random data, we know that within 2^bits * accesses, every possible value up to 2^bits will be seen * exactly once, with the exception of zero, for which we have * a special treatment. */ static int lfsr(unsigned short v, unsigned int bits) { unsigned short bit; if (v >= (1 << bits)) { fprintf(stderr, "lfsr: internal error\n"); exit(-1); } if (v == (((1 << bits) - 1) & 0xace1)) return 0; if (v == 0) v = ((1 << bits) - 1) & 0xace1; switch (bits) { case 8: /* x^8 + x^6 + x^5 + x^4 + 1 */ bit = ((v >> 0) ^ (v >> 2) ^ (v >> 3) ^ (v >> 4)) & 1; break; case 9: /* x9 + x5 + 1 */ bit = ((v >> 0) ^ (v >> 4)) & 1; break; case 10: /* x10 + x7 + 1 */ bit = ((v >> 0) ^ (v >> 3)) & 1; break; case 11: /* x11 + x9 + 1 */ bit = ((v >> 0) ^ (v >> 2)) & 1; break; case 12: bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 8)) & 1; break; case 13: /* x^13 + x^12 + x^11 + x^8 + 1 */ bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 5)) & 1; break; case 14: /* x^14 + x^13 + x^12 + x^2 + 1 */ bit = ((v >> 0) ^ (v >> 1) ^ (v >> 2) ^ (v >> 12)) & 1; break; case 15: /* x^15 + x^14 + 1 */ bit = ((v >> 0) ^ (v >> 1) ) & 1; break; case 16: /* x^16 + x^14 + x^13 + x^11 + 1 */ bit = ((v >> 0) ^ (v >> 2) ^ (v >> 3) ^ (v >> 5) ) & 1; break; default: fprintf(stderr, "lfsr: internal error\n"); exit(-1); } return v >> 1 | bit << (bits - 1); } static struct operation *off_rand(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next = op+1; unsigned int i; unsigned int num, val; unsigned int pos = 0, bits = 0; if (op->val == -1) { if (len == 0 || max < (off_t)len) return_err("cannot fill %lld bytes with %ld byte chunks\n", (long long)max, (long)len); num = max/len; val = max/num; } else { val = op->val; num = op->num; } for (i = num; i > 0; i /= 2) bits++; if (bits < 8) bits = 8; for (i = 0; i < num && next; i++) { do { pos = lfsr(pos, bits); } while (pos >= num); next = call_aggregate(op+1, dev, off + pos * val, max, len, op); } return next; } static struct operation *repeat(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next = op+1; unsigned int i; for (i = 0; i < op->num && next; i++) next = call_aggregate(op+1, dev, off, max, len, op); return next; } static res_t do_reduce_int(int num, res_t *input, int aggregate) { int i; res_t result = { .l = 0 }; for (i = 0; i < num; i++) { switch (aggregate) { case A_MINIMUM: if (!result.l || result.l > input[i].l) result.l = input[i].l; break; case A_MAXIMUM: if (!result.l || result.l < input[i].l) result.l = input[i].l; break; case A_AVERAGE: case A_TOTAL: result.l += input[i].l; break; } } if (aggregate == A_AVERAGE) result.l /= num; return result; } static struct operation *reduce(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next, *child; unsigned int i; enum resulttype type; res_t *in; child = op+1; next = call(child, dev, off, max, len); if (!next) return NULL; /* single value */ if (child->r_type != R_ARRAY || child->size_x == 0) return_err("cannot reduce scalar further, type %d, size %d\n", child->r_type, child->size_y); /* data does not fit */ if (child->size_y > op->num) return_err("target array too short\n"); /* FIXME: is this necessary? */ /* one-dimensional array */ if (child->size_y == 0) { if (res_type(child->result) != R_NS && res_type(child->result) != R_BPS) return_err("cannot reduce type %d\n", res_type(child->result)); op->result = do_reduce_int(child->size_x, res_ptr(child->result), op->aggregate); op->size_x = op->size_y = 0; op->r_type = res_type(child->result); goto clear_child; } /* two-dimensional array */ in = res_ptr(child->result); if (res_type(child->result) != R_ARRAY) return_err("inconsistent array contents\n"); type = res_type(in[0]); for (i=0; isize_x; i++) { if (res_type(in[i]) != type) return_err("cannot combine type %d and %d\n", res_type(in[i]), type); res_ptr(op->result)[i] = do_reduce_int(child->size_y, res_ptr(in[i]), op->aggregate); } op->result = to_res(res_ptr(op->result), type); op->size_x = child->size_y; op->size_y = 0; op->r_type = R_ARRAY; clear_child: child->result = res_null; child->size_x = child->size_y = 0; child->r_type = R_NONE; return next; } static struct operation *drop(struct operation *op, struct device *dev, off_t off, off_t max, size_t len) { struct operation *next, *child; child = op+1; next = call(child, dev, off, max, len); if (!next) return NULL; child->result = res_null; child->r_type = R_NONE; child->size_x = child->size_y = 0; return next; } static struct syntax syntax[] = { { O_END, "END", nop, }, { O_READ, "READ", do_read, }, { O_WRITE_ZERO, "WRITE_ZERO", do_write_zero, }, { O_WRITE_ONE, "WRITE_ONE", do_write_one, }, { O_WRITE_RAND, "WRITE_RAND", do_write_rand, }, { O_ERASE, "ERASE", do_erase, }, { O_LENGTH, "LENGTH", length_or_offs }, { O_OFFSET, "OFFSET", length_or_offs, }, { O_PRINT, "PRINT", print_string, P_STRING }, { O_PRINTF, "PRINTF", print_val, }, { O_FORMAT, "FORMAT", format, }, { O_NEWLINE, "NEWLINE", newline, }, { O_BPS, "BPS", bytespersec, }, { O_SEQUENCE, "SEQUENCE", sequence, P_NUM }, { O_REPEAT, "REPEAT", repeat, P_NUM }, { O_OFF_FIXED, "OFF_FIXED", off_fixed, P_VAL }, { O_OFF_POW2, "OFF_POW2", nop, P_NUM | P_VAL }, { O_OFF_LIN, "OFF_LIN", off_lin, P_NUM | P_VAL }, { O_OFF_RAND, "OFF_RAND", off_rand, P_NUM | P_VAL }, { O_LEN_FIXED, "LEN_FIXED", len_fixed, P_VAL }, { O_LEN_POW2, "LEN_POW2", len_pow2, P_NUM | P_VAL }, { O_MAX_POW2, "MAX_POW2", nop, P_NUM | P_VAL }, { O_MAX_LIN, "MAX_LIN", nop, P_NUM | P_VAL }, { O_REDUCE, "REDUCE", reduce, P_AGGREGATE }, { O_DROP, "DROP", drop, }, }; flashbench-62/vm.h0000644000000000000000000000305011510714004012310 0ustar 00000000000000#ifndef FLASHBENCH_VM_H #define FLASHBENCH_VM_H #include typedef union result res_t; enum resulttype { R_NONE, R_ARRAY, R_NS, R_BYTE, R_BPS, R_STRING, }; union result { res_t *_p; long long l; char s[8]; } __attribute__((aligned(8))); struct device; struct operation { enum opcode { /* end of program marker */ O_END = 0, /* basic operations */ O_READ, O_WRITE_ZERO, O_WRITE_ONE, O_WRITE_RAND, O_ERASE, O_LENGTH, O_OFFSET, /* output */ O_PRINT, O_PRINTF, O_FORMAT, O_NEWLINE, O_BPS, /* group */ O_SEQUENCE, O_REPEAT, /* series */ O_OFF_FIXED, O_OFF_POW2, O_OFF_LIN, O_OFF_RAND, O_LEN_FIXED, O_LEN_POW2, O_MAX_POW2, O_MAX_LIN, /* reduce dimension */ O_REDUCE, /* ignore result */ O_DROP, /* end of list */ O_MAX = O_DROP, } code; /* number of indirect results, if any */ unsigned int num; /* command code specific value */ long long val; /* output string for O_PRINT */ const char *string; /* aggregation of results from children */ enum { A_NONE, A_MINIMUM, A_MAXIMUM, A_AVERAGE, A_TOTAL, A_IGNORE, } aggregate; /* dynamic result contents */ res_t result; unsigned int size_x; unsigned int size_y; enum resulttype r_type; }; extern struct operation *call(struct operation *program, struct device *dev, off_t off, off_t max, size_t len); extern int verbose; #define pr_debug(...) do { if (verbose) printf(__VA_ARGS__); } while(0) #define return_err(...) do { printf(__VA_ARGS__); return NULL; } while(0) #endif /* FLASHBENCH_VM_H */