forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.cc
281 lines (253 loc) · 6.26 KB
/
reader.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "libpstack/util.h"
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
#include <cstdint>
#include <iostream>
using std::string;
Reader::Off
FileReader::size() const
{
return fileSize;
}
static int
openFileDirect(const string &name_)
{
auto fd = open(name_.c_str(), O_RDONLY);
if (verbose > 2) {
if (fd != -1)
*debug << "opened " << name_ << ", fd=" << fd << std::endl;
else
*debug << "failed to open " << name_ << ": " << strerror(errno) << std::endl;
}
return fd;
}
static int
openfile(const string &name)
{
int fd;
if (g_openPrefix != "") {
int fd = openFileDirect(g_openPrefix + name);
if (fd != -1)
return fd;
}
fd = openFileDirect(name);
if (fd != -1)
return fd;
throw (Exception() << "cannot open file '" << name << "': " << strerror(errno));
}
FileReader::FileReader(string name_)
: name(std::move(name_))
, file(openfile(name))
{
struct stat buf{};
int rc = fstat(file, &buf);
if (rc == -1)
throw (Exception() << "fstat failed: can't find size of file: " << strerror(errno));
fileSize = buf.st_size;
}
FileReader::~FileReader()
{
::close(file);
}
MemReader::MemReader(const string &descr, size_t len_, const char *data_)
: descr(descr)
, len(len_)
, data(data_)
{
}
size_t
MemReader::read(Off off, size_t count, char *ptr) const
{
if (off > Off(len))
throw (Exception() << "read past end of memory");
size_t rc = std::min(count, len - size_t(off));
memcpy(ptr, data + off, rc);
return rc;
}
void
MemReader::describe(std::ostream &os) const
{
os << descr;
}
string
Reader::readString(Off offset) const
{
string res;
for (Off s = size(); offset < s; ++offset) {
char c;
if (read(offset, 1, &c) != 1)
break;
if (c == 0)
break;
res += c;
}
return res;
}
size_t
FileReader::read(Off off, size_t count, char *ptr) const
{
auto rc = pread(file, ptr, count, off);
if (rc == -1)
throw (Exception()
<< "read " << count
<< " at " << (void *)off
<< " on " << *this
<< " failed: " << strerror(errno));
if (rc == 0)
throw (Exception()
<< "read " << count
<< " at " << (void *)off
<< " on " << *this
<< " hit unexpected EOF");
return rc;
}
void
CacheReader::Page::load(const Reader &r, Off offset_)
{
assert(offset_ % PAGESIZE == 0);
len = r.read(offset_, PAGESIZE, data);
offset = offset_;
}
CacheReader::CacheReader(Reader::csptr upstream_)
: upstream(move(upstream_))
{
}
void
CacheReader::flush() {
std::list<Page *> clearpages;
std::swap(pages, clearpages);
for (auto &i : clearpages)
delete i;
}
CacheReader::~CacheReader()
{
flush();
}
CacheReader::Page *
CacheReader::getPage(Off pageoff) const
{
Page *p;
bool first = true;
for (auto i = pages.begin(); i != pages.end(); ++i) {
p = *i;
if (p->offset == pageoff) {
// move page to front.
if (!first) {
pages.erase(i);
pages.push_front(p);
}
return p;
}
first = false;
}
if (pages.size() == MAXPAGES) {
p = pages.back();
pages.pop_back();
} else {
p = new Page();
}
try {
p->load(*upstream, pageoff);
pages.push_front(p);
return p;
}
catch (...) {
// failed to load page - delete it, and continue with error.
delete p;
throw;
}
}
size_t
CacheReader::read(Off off, size_t count, char *ptr) const
{
if (count >= PAGESIZE)
return upstream->read(off, count, ptr);
Off startoff = off;
for (;;) {
if (count == 0)
break;
size_t offsetOfDataInPage = off % PAGESIZE;
Off offsetOfPageInFile = off - offsetOfDataInPage;
Page *page = getPage(offsetOfPageInFile);
if (page == nullptr)
break;
size_t chunk = std::min(page->len - offsetOfDataInPage, count);
memcpy(ptr, page->data + offsetOfDataInPage, chunk);
off += chunk;
count -= chunk;
ptr += chunk;
if (page->len != PAGESIZE)
break;
}
return off - startoff;
}
string
CacheReader::readString(Off off) const
{
auto &entry = stringCache[off];
if (entry.isNew) {
entry.value = Reader::readString(off);
entry.isNew = false;
}
return entry.value;
}
std::shared_ptr<const Reader>
loadFile(const string &path)
{
return std::make_shared<CacheReader>(
std::make_shared<FileReader>(path));
}
size_t
MmapReader::read(Off off, size_t count, char *ptr) const {
Off size = std::min(count, len - size_t(off));
memcpy(ptr, (char *)base + off, size);
return count;
}
MmapReader::MmapReader(const string &name_)
: name(name_)
{
int fd = openfile(name);
struct stat s;
fstat(fd, &s);
len = s.st_size;
base = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (base == MAP_FAILED)
throw (Exception() << "mmap failed" << strerror(errno));
}
string
MmapReader::readString(Off offset) const {
return string((char *)base + offset);
}
MmapReader::~MmapReader() {
munmap(base, len);
}
OffsetReader::OffsetReader(Reader::csptr upstream_, Off offset_, Off length_)
: upstream(upstream_)
, offset(offset_)
{
for (;;) {
auto orReader = dynamic_cast<const OffsetReader *>(upstream.get());
if (!orReader)
break;
if (verbose > 2)
*debug << "optimize: collapse OR reader : "
<< upstream.get() << "->" << orReader->upstream.get() << "\n";
offset += orReader->offset;
upstream = orReader->upstream;
if (length_ != std::numeric_limits<Off>::max())
length -= orReader->offset;
}
length = length_ == std::numeric_limits<Off>::max() ? upstream->size() - offset : length_;
}
size_t
OffsetReader:: read(Off off, size_t count, char *ptr) const {
if (off > length)
throw Exception() << "read past end of object " << *this;
if (off + Off(count) > length)
count = length - off;
return upstream->read(off + offset, count, ptr);
}