OpenCSD - CoreSight Trace Decode Library 1.5.1
Loading...
Searching...
No Matches
trc_mem_acc_cache.h
Go to the documentation of this file.
1
8/*
9* Redistribution and use in source and binary forms, with or without modification,
10* are permitted provided that the following conditions are met:
11*
12* 1. Redistributions of source code must retain the above copyright notice,
13* this list of conditions and the following disclaimer.
14*
15* 2. Redistributions in binary form must reproduce the above copyright notice,
16* this list of conditions and the following disclaimer in the documentation
17* and/or other materials provided with the distribution.
18*
19* 3. Neither the name of the copyright holder nor the names of its contributors
20* may be used to endorse or promote products derived from this software without
21* specific prior written permission.
22*
23* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND
24* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*/
34
35#ifndef ARM_TRC_MEM_ACC_CACHE_H_INCLUDED
36#define ARM_TRC_MEM_ACC_CACHE_H_INCLUDED
37
38#include <string>
40
41#define MEM_ACC_CACHE_DEFAULT_PAGE_SIZE 2048
42#define MEM_ACC_CACHE_DEFAULT_MRU_SIZE 16
43#define MEM_ACC_CACHE_PAGE_SIZE_MAX 16384
44#define MEM_ACC_CACHE_MRU_SIZE_MAX 256
45#define MEM_ACC_CACHE_PAGE_SIZE_MIN 64
46#define MEM_ACC_CACHE_MRU_SIZE_MIN 4
47
48#define OCSD_ENV_MEMACC_CACHE_OFF "OPENCSD_MEMACC_CACHE_OFF"
49#define OCSD_ENV_MEMACC_CACHE_PG_SIZE "OPENCSD_MEMACC_CACHE_PAGE_SIZE"
50#define OCSD_ENV_MEMACC_CACHE_PG_NUM "OPENCSD_MEMACC_CACHE_PAGE_NUM"
51
52
54class ITraceErrorLog;
55
56typedef struct cache_block {
58 uint32_t valid_len;
59 uint8_t* data;
60 uint8_t trcID; // trace ID associated with the page
61 uint32_t use_sequence; // number representing the sequence of allocation to evict oldest page.
63
64// enable define to collect stats for debugging / cache performance tests
65// #define LOG_CACHE_STATS
66
67
79{
80public:
83
84 /* cache enabling and usage */
86 ocsd_err_t setCacheSizes(const uint16_t page_size, const int nr_pages);
87
88 const bool enabled() const { return m_bCacheEnabled; };
89 const bool enabled_for_size(const uint32_t reqSize) const
90 {
91 return (m_bCacheEnabled && (reqSize <= m_mru_page_size));
92 }
93
94 /* cache invalidation */
96 void invalidateByTraceID(int8_t trcID);
97 void clearPage(cache_block_t* page);
98
100 ocsd_err_t readBytesFromCache(TrcMemAccessorBase *p_accessor, const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, const uint8_t trcID, uint32_t *numBytes, uint8_t *byteBuffer);
101
104
105 /* look for runtime cache tuning vars */
106 static void getenvMemaccCacheSizes(bool& enable, int& page_size, int& num_pages);
107
108private:
109 bool blockInCache(const ocsd_vaddr_t address, const uint32_t reqBytes, const uint8_t trcID); // run through each page to look for data.
110 bool blockInPage(const ocsd_vaddr_t address, const uint32_t reqBytes, const uint8_t trcID);
111
112 void logMsg(const std::string &szMsg);
113 int findNewPage();
114 void incSequence(); // increment sequence on current block
115
116 ocsd_err_t createCaches(); // create caches according to current sizes
117 void destroyCaches(); // destroy the cache blocks
118
119 cache_block_t *m_mru; // cache pages
120 int m_mru_idx = 0; // in use index - most recently used page
121 uint16_t m_mru_page_size; // page size
122 int m_mru_num_pages; // number of pages
123 uint32_t m_mru_sequence; // allocation & use sequence number
124
125 bool m_bCacheEnabled = false;
126
127#ifdef LOG_CACHE_STATS
128 uint32_t m_hits = 0;
129 uint32_t m_misses = 0;
130 uint32_t m_pages = 0;
131 uint32_t* m_hit_rl = 0;
132 uint32_t* m_hit_rl_max = 0;
133#endif
134
135 ITraceErrorLog *m_err_log = 0;
136};
137
139 m_mru(0), m_mru_sequence(1)
140{
141 /* set default cache sizes */
142 m_mru_page_size = MEM_ACC_CACHE_DEFAULT_PAGE_SIZE;
143 m_mru_num_pages = MEM_ACC_CACHE_DEFAULT_MRU_SIZE;
144}
145
147{
148 destroyCaches();
149}
150
151
152inline bool TrcMemAccCache::blockInPage(const ocsd_vaddr_t address, const uint32_t reqBytes, const uint8_t trcID)
153{
154 /* check has data, trcID and mem space */
155 if ((m_mru[m_mru_idx].trcID != trcID) ||
156 (m_mru[m_mru_idx].valid_len == 0)
157 )
158 return false;
159
160 /* check block is in this page */
161 if ((m_mru[m_mru_idx].st_addr <= address) &&
162 m_mru[m_mru_idx].st_addr + m_mru[m_mru_idx].valid_len >= (address + reqBytes))
163 return true;
164 return false;
165}
166
167inline bool TrcMemAccCache::blockInCache(const ocsd_vaddr_t address, const uint32_t reqBytes, const uint8_t trcID)
168{
169 int tests = m_mru_num_pages;
170 while (tests)
171 {
172 if (blockInPage(address, reqBytes, trcID))
173 return true; // found address in page
174#ifdef LOG_CACHE_STATS
175 // miss counts of current page only - to determine if we hit other page
176 if (tests == m_mru_num_pages)
177 m_misses++;
178#endif
179
180 tests--;
181 m_mru_idx++;
182 if (m_mru_idx == m_mru_num_pages)
183 m_mru_idx = 0;
184 }
185 return false;
186}
187
188// zero out page parameters rendering it empty
190{
191 page->use_sequence = 0;
192 page->st_addr = 0;
193 page->valid_len = 0;
195}
196
197#endif // ARM_TRC_MEM_ACC_CACHE_H_INCLUDED
198
199/* End of File trc_mem_acc_cache.h */
Error logging interface.
static void getenvMemaccCacheSizes(bool &enable, int &page_size, int &num_pages)
void invalidateByTraceID(int8_t trcID)
const bool enabled_for_size(const uint32_t reqSize) const
ocsd_err_t setCacheSizes(const uint16_t page_size, const int nr_pages)
ocsd_err_t readBytesFromCache(TrcMemAccessorBase *p_accessor, const ocsd_vaddr_t address, const ocsd_mem_space_acc_t mem_space, const uint8_t trcID, uint32_t *numBytes, uint8_t *byteBuffer)
const bool enabled() const
void logAndClearCounts()
ocsd_err_t enableCaching(bool bEnable)
void clearPage(cache_block_t *page)
void invalidateAll()
void setErrorLog(ITraceErrorLog *log)
Memory range to access by trace decoder.
#define OCSD_BAD_CS_SRC_ID
enum _ocsd_mem_space_acc_t ocsd_mem_space_acc_t
enum _ocsd_err_t ocsd_err_t
uint64_t ocsd_vaddr_t
OpenCSD : Standard Types used in the library interfaces.
uint32_t use_sequence
ocsd_vaddr_t st_addr
struct cache_block cache_block_t
#define MEM_ACC_CACHE_DEFAULT_MRU_SIZE
#define MEM_ACC_CACHE_DEFAULT_PAGE_SIZE