Profiler.h
Go to the documentation of this file.
1 /****
2  * IProfiler.h - Abstract interface to implement filesystem profiling
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the IFS Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  ****/
19 
20 #pragma once
21 
22 #include <WString.h>
23 #include <Print.h>
24 
25 namespace IFS
26 {
30 class IProfiler
31 {
32 public:
33  virtual ~IProfiler() = default;
34 
39  virtual void read(storage_size_t address, const void* buffer, size_t size) = 0;
46  virtual void write(storage_size_t address, const void* buffer, size_t size) = 0;
52  virtual void erase(storage_size_t address, size_t size) = 0;
53 };
54 
55 class Profiler : public IProfiler
56 {
57 public:
58  struct Stat {
59  size_t count{0};
61 
62  void reset()
63  {
64  size = count = 0;
65  }
66 
68  {
69  size += n;
70  ++count;
71  }
72 
73  String toString() const
74  {
75  String s;
76  s += F("count=");
77  s += count;
78  s += F(", size=");
79  s += (size + 1023) / 1024;
80  s += "KB";
81  return s;
82  }
83 
84  operator String() const
85  {
86  return toString();
87  }
88  };
89 
93 
94  void read(storage_size_t, const void*, size_t size) override
95  {
96  readStat.update(size);
97  }
98 
99  void write(storage_size_t, const void*, size_t size) override
100  {
101  writeStat.update(size);
102  }
103 
104  void erase(storage_size_t, size_t size) override
105  {
106  eraseStat.update(size);
107  }
108 
109  void reset()
110  {
111  readStat.reset();
112  writeStat.reset();
113  eraseStat.reset();
114  }
115 
116  size_t printTo(Print& p) const
117  {
118  size_t n{0};
119  n += p.print(_F("Read: "));
120  n += p.print(readStat);
121  n += p.print(_F(", Write: "));
122  n += p.print(writeStat);
123  n += p.print(_F(", Erase: "));
124  n += p.print(eraseStat);
125  return n;
126  }
127 };
128 
129 } // namespace IFS
uint32_t storage_size_t
Definition: Components/Storage/src/include/Storage/Types.h:19
#define F(string_literal)
Wrap a string literal stored in flash and access it using a String object.
Definition: WString.h:109
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:31
virtual ~IProfiler()=default
virtual void write(storage_size_t address, const void *buffer, size_t size)=0
virtual void erase(storage_size_t address, size_t size)=0
Called BEFORE an erase operation.
virtual void read(storage_size_t address, const void *buffer, size_t size)=0
Definition: Profiler.h:56
Stat eraseStat
Definition: Profiler.h:92
void reset()
Definition: Profiler.h:109
void write(storage_size_t, const void *, size_t size) override
Definition: Profiler.h:99
Stat writeStat
Definition: Profiler.h:91
size_t printTo(Print &p) const
Definition: Profiler.h:116
void erase(storage_size_t, size_t size) override
Called BEFORE an erase operation.
Definition: Profiler.h:104
void read(storage_size_t, const void *, size_t size) override
Definition: Profiler.h:94
Stat readStat
Definition: Profiler.h:90
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:103
The String class.
Definition: WString.h:133
#define _F(str)
Definition: FakePgmSpace.h:97
Definition: DirectoryTemplate.h:37
Definition: Profiler.h:58
void update(storage_size_t n)
Definition: Profiler.h:67
void reset()
Definition: Profiler.h:62
size_t count
Definition: Profiler.h:59
storage_size_t size
Definition: Profiler.h:60
String toString() const
Definition: Profiler.h:73