w11 - cpp 0.794
Backend server for Rlink and w11
Loading...
Searching...
No Matches
Rw11VirtDiskOver.cpp
Go to the documentation of this file.
1// $Id: Rw11VirtDiskOver.cpp 1186 2019-07-12 17:49:59Z mueller $
2// SPDX-License-Identifier: GPL-3.0-or-later
3// Copyright 2017-2018 by Walter F.J. Mueller <W.F.J.Mueller@gsi.de>
4//
5// Revision History:
6// Date Rev Version Comment
7// 2018-12-22 1091 1.0.6 Read(): it->it1 (-Wshadow fix)
8// 2017-06-05 907 1.0.5 more detailed stats
9// 2017-06-03 903 1.0.4 Read(): BUGFIX: fix index error in blockwise read
10// 2017-05-07 896 1.0.3 List(): BUGFIX: correct write count accumulation
11// 2017-04-15 875 1.0.2 Open(): use overload with scheme handling
12// 2017-04-07 868 1.0.1 Dump(): add detail arg
13// 2017-03-10 859 1.0 Initial version
14// ---------------------------------------------------------------------------
15
20#include "librtools/RosFill.hpp"
22
23#include "Rw11VirtDiskOver.hpp"
24
25using namespace std;
26
32// all method definitions in namespace Retro
33namespace Retro {
34
35//------------------------------------------+-----------------------------------
37
39 : Rw11VirtDiskFile(punit),
40 fBlkMap()
41{
42 fStats.Define(kStatNVDORead, "NVDORead", "over: Read() calls");
43 fStats.Define(kStatNVDOReadBlkFF, "NVDOReadBlkFF",
44 "over: blocks read file full");
45 fStats.Define(kStatNVDOReadBlkFP, "NVDOReadBlkFP",
46 "over: blocks read file part");
47 fStats.Define(kStatNVDOReadBlkO, "NVDOReadBlkO",
48 "over: blocks read from over");
49 fStats.Define(kStatNVDOWrite, "NVDOWrite", "over: Write() calls");
50 fStats.Define(kStatNVDOWriteBlk, "NVDOWriteBlk", "over: blocks written");
51 fStats.Define(kStatNVDOFlush, "NVDOFlush", "over: Flush() calls");
52}
53
54//------------------------------------------+-----------------------------------
56
58{}
59
60//------------------------------------------+-----------------------------------
62
64{
65 return false; // from unit always writable !!
66}
67
68//------------------------------------------+-----------------------------------
70
71bool Rw11VirtDiskOver::Open(const std::string& url, RerrMsg& emsg)
72{
73 // FIXME_code: do we need to handle wpro ?
74 return Rw11VirtDiskFile::Open(url, "over", emsg);
75}
76
77//------------------------------------------+-----------------------------------
79
80bool Rw11VirtDiskOver::Read(size_t lba, size_t nblk, uint8_t* data,
81 RerrMsg& emsg)
82{
84 auto it = fBlkMap.lower_bound(lba);
85
86 if (it == fBlkMap.end() || it->first >= lba+nblk) { // no match
87 fStats.Inc(kStatNVDOReadBlkFF, double(nblk));
88 return Rw11VirtDiskFile::Read(lba, nblk, data, emsg); // one swoop from disk
89
90 } else { // match
91 for (size_t i=0; i<nblk; i++) { // get it blockwise
92 auto it1 = fBlkMap.find(lba+i);
93 if (it1 == fBlkMap.end()) {
95 bool rc = Rw11VirtDiskFile::Read(lba+i, 1, data+i*fBlkSize, emsg);
96 if (!rc) return rc;
97 } else {
99 (it1->second).Read(data+i*fBlkSize);
100 }
101 }
102 }
103 return true;
104}
105
106//------------------------------------------+-----------------------------------
108
109bool Rw11VirtDiskOver::Write(size_t lba, size_t nblk, const uint8_t* data,
110 RerrMsg& /*emsg*/)
111{
113 fStats.Inc(kStatNVDOWriteBlk, double(nblk));
114 for (size_t i=0; i<nblk; i++) {
115 auto it = fBlkMap.find(lba+i);
116 if (it == fBlkMap.end()) {
117 auto rc = fBlkMap.emplace(lba+i, Rw11VirtDiskBuffer(fBlkSize));
118 it = rc.first;
119 }
120 (it->second).Write(data+i*fBlkSize);
121 }
122 return true;
123}
124
125//------------------------------------------+-----------------------------------
127
129{
130 if (fWProt) {
131 emsg.Init("Rw11VirtDiskOver::Flush()", "file write protected");
132 return false;
133 }
134
136 for (auto& kv: fBlkMap) {
137 bool rc = Rw11VirtDiskFile::Write(kv.first, 1, kv.second.Data(), emsg);
138 if (!rc) return rc;
139 }
140 fBlkMap.clear();
141 return true;
142}
143
144//------------------------------------------+-----------------------------------
146
147void Rw11VirtDiskOver::List(std::ostream& os) const
148{
149 if (fBlkMap.empty()) return;
150
151 uint32_t lbabeg = fBlkMap.begin()->first; // first lba
152 uint32_t nwrite = 0;
153 for (auto it=fBlkMap.begin(); it!=fBlkMap.end(); ) {
154 nwrite += (it->second).NWrite();
155 auto itnext = next(it);
156 if (itnext == fBlkMap.end() || itnext->first != (it->first)+1) {
157 os << RosPrintf(lbabeg,"d",8)
158 << " .. " << RosPrintf(it->first,"d",8)
159 << " : nb=" << RosPrintf(it->first-lbabeg+1,"d",8)
160 << " nw=" << RosPrintf(nwrite,"d",8) << endl;
161 if (itnext != fBlkMap.end()) lbabeg = itnext->first;
162 nwrite = 0;
163 }
164 it = itnext;
165 }
166 return;
167}
168
169//------------------------------------------+-----------------------------------
171
172void Rw11VirtDiskOver::Dump(std::ostream& os, int ind, const char* text,
173 int detail) const
174{
175 RosFill bl(ind);
176 os << bl << (text?text:"--") << "Rw11VirtDiskOver @ " << this << endl;
177
178 os << bl << " fBlkMap.size: " << fBlkMap.size() << endl;
179 Rw11VirtDiskFile::Dump(os, ind, " ^", detail);
180 return;
181}
182
183} // end namespace Retro
FIXME_docs.
Definition: RerrMsg.hpp:25
void Init(const std::string &meth, const std::string &text)
FIXME_docs.
Definition: RerrMsg.cpp:74
I/O appicator to generate fill characters.
Definition: RosFill.hpp:24
void Inc(size_t ind, double val=1.)
FIXME_docs.
Definition: Rstats.ipp:29
void Define(size_t ind, const std::string &name, const std::string &text)
FIXME_docs.
Definition: Rstats.cpp:72
FIXME_docs.
Definition: Rw11Unit.hpp:39
virtual bool Write(size_t lba, size_t nblk, const uint8_t *data, RerrMsg &emsg)
FIXME_docs.
virtual bool Read(size_t lba, size_t nblk, uint8_t *data, RerrMsg &emsg)
FIXME_docs.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
virtual bool Open(const std::string &url, RerrMsg &emsg)
FIXME_docs.
virtual bool WProt() const
FIXME_docs.
virtual bool Write(size_t lba, size_t nblk, const uint8_t *data, RerrMsg &emsg)
FIXME_docs.
void List(std::ostream &os) const
FIXME_docs.
bool Flush(RerrMsg &emsg)
FIXME_docs.
virtual bool Read(size_t lba, size_t nblk, uint8_t *data, RerrMsg &emsg)
FIXME_docs.
Rw11VirtDiskOver(Rw11Unit *punit)
Default constructor.
virtual void Dump(std::ostream &os, int ind=0, const char *text=0, int detail=0) const
FIXME_docs.
size_t fBlkSize
block size in byte
Rstats fStats
statistics
Definition: Rw11Virt.hpp:68
bool fWProt
write protected
Definition: Rw11Virt.hpp:67
RosPrintfS< bool > RosPrintf(bool value, const char *form=0, int width=0, int prec=0)
Creates a print object for the formatted output of a bool value.
Definition: RosPrintf.ipp:38
Declaration of class ReventLoop.
Definition: ReventLoop.cpp:47