|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | + * of this software and associated documentation files (the "Software"), to deal |
| 6 | + * in the Software without restriction, including without limitation the rights |
| 7 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | + * copies of the Software, and to permit persons to whom the Software is |
| 9 | + * furnished to do so, subject to the following conditions: |
| 10 | + * |
| 11 | + * The above copyright notice and this permission notice shall be included in |
| 12 | + * all copies or substantial portions of the Software. |
| 13 | + * |
| 14 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | + * SOFTWARE. |
| 21 | + */ |
| 22 | +#ifndef MBED_PROFILING_BLOCK_DEVICE_H |
| 23 | +#define MBED_PROFILING_BLOCK_DEVICE_H |
| 24 | + |
| 25 | +#include "BlockDevice.h" |
| 26 | +#include "mbed.h" |
| 27 | + |
| 28 | + |
| 29 | +/** Block device for measuring storage operations of another block device |
| 30 | + * |
| 31 | + * @code |
| 32 | + * #include "mbed.h" |
| 33 | + * #include "HeapBlockDevice.h" |
| 34 | + * #include "ProfilingBlockDevice.h" |
| 35 | + * |
| 36 | + * // Create a heap block device and profiling block device |
| 37 | + * HeapBlockDevice mem(64*512, 512); |
| 38 | + * ProfilingBlockDevice profiler(&mem); |
| 39 | + * |
| 40 | + * // do block device work.... |
| 41 | + * |
| 42 | + * printf("read count: %lld\n", profiler.get_read_count()); |
| 43 | + * printf("program count: %lld\n", profiler.get_program_count()); |
| 44 | + * printf("erase count: %lld\n", profiler.get_erase_count()); |
| 45 | + */ |
| 46 | +class ProfilingBlockDevice : public BlockDevice |
| 47 | +{ |
| 48 | +public: |
| 49 | + /** Lifetime of the memory block device |
| 50 | + * |
| 51 | + * @param bd Block device to back the ProfilingBlockDevice |
| 52 | + */ |
| 53 | + ProfilingBlockDevice(BlockDevice *bd); |
| 54 | + |
| 55 | + /** Lifetime of a block device |
| 56 | + */ |
| 57 | + virtual ~ProfilingBlockDevice() {}; |
| 58 | + |
| 59 | + /** Initialize a block device |
| 60 | + * |
| 61 | + * @return 0 on success or a negative error code on failure |
| 62 | + * @note The init and deinit functions do not effect profile counts |
| 63 | + */ |
| 64 | + virtual int init(); |
| 65 | + |
| 66 | + /** Deinitialize a block device |
| 67 | + * |
| 68 | + * @return 0 on success or a negative error code on failure |
| 69 | + * @note The init and deinit functions do not effect profile counts |
| 70 | + */ |
| 71 | + virtual int deinit(); |
| 72 | + |
| 73 | + /** Read blocks from a block device |
| 74 | + * |
| 75 | + * @param buffer Buffer to read blocks into |
| 76 | + * @param addr Address of block to begin reading from |
| 77 | + * @param size Size to read in bytes, must be a multiple of read block size |
| 78 | + * @return 0 on success, negative error code on failure |
| 79 | + */ |
| 80 | + virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); |
| 81 | + |
| 82 | + /** Program blocks to a block device |
| 83 | + * |
| 84 | + * The blocks must have been erased prior to being programmed |
| 85 | + * |
| 86 | + * @param buffer Buffer of data to write to blocks |
| 87 | + * @param addr Address of block to begin writing to |
| 88 | + * @param size Size to write in bytes, must be a multiple of program block size |
| 89 | + * @return 0 on success, negative error code on failure |
| 90 | + */ |
| 91 | + virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); |
| 92 | + |
| 93 | + /** Erase blocks on a block device |
| 94 | + * |
| 95 | + * The state of an erased block is undefined until it has been programmed |
| 96 | + * |
| 97 | + * @param addr Address of block to begin erasing |
| 98 | + * @param size Size to erase in bytes, must be a multiple of erase block size |
| 99 | + * @return 0 on success, negative error code on failure |
| 100 | + */ |
| 101 | + virtual int erase(bd_addr_t addr, bd_size_t size); |
| 102 | + |
| 103 | + /** Get the size of a readable block |
| 104 | + * |
| 105 | + * @return Size of a readable block in bytes |
| 106 | + */ |
| 107 | + virtual bd_size_t get_read_size() const; |
| 108 | + |
| 109 | + /** Get the size of a programable block |
| 110 | + * |
| 111 | + * @return Size of a programable block in bytes |
| 112 | + * @note Must be a multiple of the read size |
| 113 | + */ |
| 114 | + virtual bd_size_t get_program_size() const; |
| 115 | + |
| 116 | + /** Get the size of a eraseable block |
| 117 | + * |
| 118 | + * @return Size of a eraseable block in bytes |
| 119 | + * @note Must be a multiple of the program size |
| 120 | + */ |
| 121 | + virtual bd_size_t get_erase_size() const; |
| 122 | + |
| 123 | + /** Get the total size of the underlying device |
| 124 | + * |
| 125 | + * @return Size of the underlying device in bytes |
| 126 | + */ |
| 127 | + virtual bd_size_t size() const; |
| 128 | + |
| 129 | + /** Reset the current profile counts to zero |
| 130 | + */ |
| 131 | + void reset(); |
| 132 | + |
| 133 | + /** Get number of bytes that have been read from the block device |
| 134 | + * |
| 135 | + * @return The number of bytes that have been read from the block device |
| 136 | + */ |
| 137 | + bd_size_t get_read_count() const; |
| 138 | + |
| 139 | + /** Get number of bytes that have been programed to the block device |
| 140 | + * |
| 141 | + * @return The number of bytes that have been programed to the block device |
| 142 | + */ |
| 143 | + bd_size_t get_program_count() const; |
| 144 | + |
| 145 | + /** Get number of bytes that have been erased from the block device |
| 146 | + * |
| 147 | + * @return The number of bytes that have been erased from the block device |
| 148 | + */ |
| 149 | + bd_size_t get_erase_count() const; |
| 150 | + |
| 151 | +private: |
| 152 | + BlockDevice *_bd; |
| 153 | + bd_size_t _read_count; |
| 154 | + bd_size_t _program_count; |
| 155 | + bd_size_t _erase_count; |
| 156 | +}; |
| 157 | + |
| 158 | + |
| 159 | +#endif |
0 commit comments