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()
34  {
35  }
36 
41  virtual void read(storage_size_t address, const void* buffer, size_t size) = 0;
48  virtual void write(storage_size_t address, const void* buffer, size_t size) = 0;
54  virtual void erase(storage_size_t address, size_t size) = 0;
55 };
56 
57 class Profiler : public IProfiler
58 {
59 public:
60  struct Stat {
61  size_t count{0};
63 
64  void reset()
65  {
66  size = count = 0;
67  }
68 
70  {
71  size += n;
72  ++count;
73  }
74 
75  String toString() const
76  {
77  String s;
78  s += F("count=");
79  s += count;
80  s += F(", size=");
81  s += (size + 1023) / 1024;
82  s += "KB";
83  return s;
84  }
85 
86  operator String() const
87  {
88  return toString();
89  }
90  };
91 
95 
96  void read(storage_size_t address, const void* buffer, size_t size) override
97  {
98  (void)address;
99  (void)buffer;
100  readStat.update(size);
101  }
102 
103  void write(storage_size_t address, const void* buffer, size_t size) override
104  {
105  (void)address;
106  (void)buffer;
107  writeStat.update(size);
108  }
109 
110  void erase(storage_size_t address, size_t size) override
111  {
112  eraseStat.update(size);
113  }
114 
115  void reset()
116  {
117  readStat.reset();
118  writeStat.reset();
119  eraseStat.reset();
120  }
121 
122  size_t printTo(Print& p) const
123  {
124  size_t n{0};
125  n += p.print(_F("Read: "));
126  n += p.print(readStat);
127  n += p.print(_F(", Write: "));
128  n += p.print(writeStat);
129  n += p.print(_F(", Erase: "));
130  n += p.print(eraseStat);
131  return n;
132  }
133 };
134 
135 } // 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:113
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:31
virtual ~IProfiler()
Definition: Profiler.h:33
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:58
Stat eraseStat
Definition: Profiler.h:94
void reset()
Definition: Profiler.h:115
void erase(storage_size_t address, size_t size) override
Called BEFORE an erase operation.
Definition: Profiler.h:110
Stat writeStat
Definition: Profiler.h:93
void write(storage_size_t address, const void *buffer, size_t size) override
Definition: Profiler.h:103
size_t printTo(Print &p) const
Definition: Profiler.h:122
void read(storage_size_t address, const void *buffer, size_t size) override
Definition: Profiler.h:96
Stat readStat
Definition: Profiler.h:92
Provides formatted output to stream.
Definition: Print.h:37
size_t print(char c)
Prints a single character to output stream.
Definition: Print.h:97
The String class.
Definition: WString.h:137
#define _F(str)
Definition: FakePgmSpace.h:97
Definition: DirectoryTemplate.h:37
Definition: Profiler.h:60
void update(storage_size_t n)
Definition: Profiler.h:69
void reset()
Definition: Profiler.h:64
size_t count
Definition: Profiler.h:61
storage_size_t size
Definition: Profiler.h:62
String toString() const
Definition: Profiler.h:75