Skip to content

Commit d610737

Browse files
committed
bd: Added ProfilingBlockDevice for measuring higher-level applications
1 parent 2305a8c commit d610737

File tree

3 files changed

+327
-0
lines changed

3 files changed

+327
-0
lines changed

features/TESTS/filesystem/util_block_device/main.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "HeapBlockDevice.h"
2222
#include "SlicingBlockDevice.h"
2323
#include "ChainingBlockDevice.h"
24+
#include "ProfilingBlockDevice.h"
2425
#include <stdlib.h>
2526

2627
using namespace utest::v1;
@@ -176,6 +177,67 @@ void test_chaining() {
176177
TEST_ASSERT_EQUAL(0, err);
177178
}
178179

180+
// Simple test which read/writes blocks on a chain of block devices
181+
void test_profiling() {
182+
HeapBlockDevice bd(BLOCK_COUNT*BLOCK_SIZE, BLOCK_SIZE);
183+
uint8_t *write_block = new uint8_t[BLOCK_SIZE];
184+
uint8_t *read_block = new uint8_t[BLOCK_SIZE];
185+
186+
// Test under profiling
187+
ProfilingBlockDevice profiler(&bd);
188+
189+
int err = profiler.init();
190+
TEST_ASSERT_EQUAL(0, err);
191+
192+
TEST_ASSERT_EQUAL(BLOCK_SIZE, profiler.get_erase_size());
193+
TEST_ASSERT_EQUAL(BLOCK_COUNT*BLOCK_SIZE, profiler.size());
194+
195+
// Fill with random sequence
196+
srand(1);
197+
for (int i = 0; i < BLOCK_SIZE; i++) {
198+
write_block[i] = 0xff & rand();
199+
}
200+
201+
// Write, sync, and read the block
202+
err = profiler.erase(0, BLOCK_SIZE);
203+
TEST_ASSERT_EQUAL(0, err);
204+
205+
err = profiler.program(write_block, 0, BLOCK_SIZE);
206+
TEST_ASSERT_EQUAL(0, err);
207+
208+
err = profiler.read(read_block, 0, BLOCK_SIZE);
209+
TEST_ASSERT_EQUAL(0, err);
210+
211+
// Check that the data was unmodified
212+
srand(1);
213+
for (int i = 0; i < BLOCK_SIZE; i++) {
214+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
215+
}
216+
217+
// Check with original block device
218+
err = bd.read(read_block, 0, BLOCK_SIZE);
219+
TEST_ASSERT_EQUAL(0, err);
220+
221+
// Check that the data was unmodified
222+
srand(1);
223+
for (int i = 0; i < BLOCK_SIZE; i++) {
224+
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
225+
}
226+
227+
delete[] write_block;
228+
delete[] read_block;
229+
err = profiler.deinit();
230+
TEST_ASSERT_EQUAL(0, err);
231+
232+
// Check that profiled operations match expectations
233+
bd_size_t read_count = profiler.get_read_count();
234+
TEST_ASSERT_EQUAL(BLOCK_SIZE, read_count);
235+
bd_size_t program_count = profiler.get_program_count();
236+
TEST_ASSERT_EQUAL(BLOCK_SIZE, program_count);
237+
bd_size_t erase_count = profiler.get_erase_count();
238+
TEST_ASSERT_EQUAL(BLOCK_SIZE, erase_count);
239+
}
240+
179241

180242
// Test setup
181243
utest::v1::status_t test_setup(const size_t number_of_cases) {
@@ -186,6 +248,7 @@ utest::v1::status_t test_setup(const size_t number_of_cases) {
186248
Case cases[] = {
187249
Case("Testing slicing of a block device", test_slicing),
188250
Case("Testing chaining of block devices", test_chaining),
251+
Case("Testing profiling of block devices", test_profiling),
189252
};
190253

191254
Specification specification(test_setup, cases);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "ProfilingBlockDevice.h"
18+
19+
20+
ProfilingBlockDevice::ProfilingBlockDevice(BlockDevice *bd)
21+
: _bd(bd)
22+
, _read_count(0)
23+
, _program_count(0)
24+
, _erase_count(0)
25+
{
26+
}
27+
28+
int ProfilingBlockDevice::init()
29+
{
30+
return _bd->init();
31+
}
32+
33+
int ProfilingBlockDevice::deinit()
34+
{
35+
return _bd->deinit();
36+
}
37+
38+
int ProfilingBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size)
39+
{
40+
int err = _bd->read(b, addr, size);
41+
if (!err) {
42+
_read_count += size;
43+
}
44+
return err;
45+
}
46+
47+
int ProfilingBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size)
48+
{
49+
int err = _bd->program(b, addr, size);
50+
if (!err) {
51+
_program_count += size;
52+
}
53+
return err;
54+
}
55+
56+
int ProfilingBlockDevice::erase(bd_addr_t addr, bd_size_t size)
57+
{
58+
int err = _bd->erase(addr, size);
59+
if (!err) {
60+
_erase_count += size;
61+
}
62+
return err;
63+
}
64+
65+
bd_size_t ProfilingBlockDevice::get_read_size() const
66+
{
67+
return _bd->get_read_size();
68+
}
69+
70+
bd_size_t ProfilingBlockDevice::get_program_size() const
71+
{
72+
return _bd->get_program_size();
73+
}
74+
75+
bd_size_t ProfilingBlockDevice::get_erase_size() const
76+
{
77+
return _bd->get_erase_size();
78+
}
79+
80+
bd_size_t ProfilingBlockDevice::size() const
81+
{
82+
return _bd->size();
83+
}
84+
85+
void ProfilingBlockDevice::reset()
86+
{
87+
_read_count = 0;
88+
_program_count = 0;
89+
_erase_count = 0;
90+
}
91+
92+
bd_size_t ProfilingBlockDevice::get_read_count() const
93+
{
94+
return _read_count;
95+
}
96+
97+
bd_size_t ProfilingBlockDevice::get_program_count() const
98+
{
99+
return _program_count;
100+
}
101+
102+
bd_size_t ProfilingBlockDevice::get_erase_count() const
103+
{
104+
return _erase_count;
105+
}
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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

Comments
 (0)