forked from petermilne/ACQ420FMC
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Buffer.cpp
232 lines (189 loc) · 6.1 KB
/
Buffer.cpp
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
/* ------------------------------------------------------------------------- */
/* Buffer.cpp D-TACQ ACQ400 FMC DRIVER
* Project: ACQ420_FMC
* Created: 16 Jul 2016 / User: pgm
* ------------------------------------------------------------------------- *
* Copyright (C) 2016 Peter Milne, D-TACQ Solutions Ltd *
* <peter dot milne at D hyphen TACQ dot com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of Version 2 of the GNU General Public License *
* as published by the Free Software Foundation; *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
*
* TODO
* TODO
\* ------------------------------------------------------------------------- */
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/sendfile.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include "popt.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include <vector>
#include <libgen.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <unistd.h>
#include <errno.h>
#include <sched.h>
#define VERID "B1007"
#define NCHAN 4
#include <semaphore.h>
#include <syslog.h>
#include <vector>
#include "local.h" /* chomp() hopefully, not a lot of other garbage */
#include "knobs.h"
using namespace std;
#include "Buffer.h"
#define PAGE_SIZE 0x1000
#define PAGE_MASK (PAGE_SIZE-1)
Buffer::Buffer(const char* _fname, int _buffer_len):
fname(_fname),
ibuf(last_buf++),
buffer_len(_buffer_len)
{
fd = open(fname, O_RDWR, 0777);
if (fd < 0){
perror(fname);
exit(1);
}
}
Buffer::Buffer(Buffer* cpy) :
fd(cpy->fd),
fname(cpy->fname),
ibuf(cpy->ibuf),
buffer_len(cpy->buffer_len),
pdata(cpy->pdata)
{}
int MapBuffer::includes(void *cursor)
/** returns remain length if buffer includes cursor */
{
char* cc = static_cast<char *>(cursor);
char* bp = static_cast<char *>(pdata);
char *be = bp + buffer_len;
int remain = 0;
if (cc >= bp && cc <= be){
remain = buffer_len - (cc-bp);
}
if (verbose){
fprintf(stderr, "%s %s ret %d\n", __func__, fname, remain);
}
return remain;
}
MapBuffer::MapBuffer(const char* _fname, int _buffer_len) :
Buffer(_fname, _buffer_len)
{
if ((_buffer_len&PAGE_MASK) != 0){
_buffer_len = (_buffer_len&~(PAGE_MASK)) + PAGE_SIZE;
contiguous = false;
}
pdata = static_cast<char*>(mmap(static_cast<void*>(ba1), _buffer_len,
PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, fd, 0));
if (pdata != ba1){
fprintf(stderr, "mmap() failed to get the hint:%p actual %p errno:%d\n", ba1, pdata, errno);
exit(1);
}
if (verbose > 2) fprintf(stderr, "MapBuffer[%ld] %s, %p\n",
the_buffers.size()-1, fname, pdata);
ba_hi = ba1 += _buffer_len;
assert(pdata != MAP_FAILED);
}
MapBuffer::~MapBuffer() {
if (verbose) fprintf(stderr, "~MapBuffer() %d munmap %p\n", ibuf, pdata);
munmap(pdata, bufferlen);
}
const char* MapBuffer::listBuffers(char* p0, char* p1, bool show_ba){
char report[512];
report[0] = '\0';
int ibuf1 = -1;
for(char* pn = p0; pn <= p1; pn += bufferlen){
int ibuf = getBuffer(pn);
if (ibuf != ibuf1){
if (!show_ba){
sprintf(report+strlen(report), strlen(report)? ",%d":"%d", ibuf);
}else{
char* ba = reinterpret_cast<MapBuffer*>(the_buffers[ibuf])->pdata;
sprintf(report+strlen(report), strlen(report)? ",%p":"%p", ba);
}
ibuf1 = ibuf;
}
}
char* ret = new char[strlen(report)+1]; // yes, this is a leak
strcpy(ret, report);
return ret;
}
int MapBuffer::writeBuffer(int out_fd, int b_opts, unsigned start_off, unsigned len)
/**< all offsets in bytes */
{
if (start_off > buffer_len) {
return 0;
}else{
if (buffer_len - start_off < len){
len = buffer_len - start_off;
}
}
return write(out_fd, pdata+start_off, len);
}
int MapBuffer::writeBuffer(int out_fd, int b_opts) {
return write(out_fd, pdata, buffer_len);
}
int MapBuffer::copyBuffer(void* dest) {
memcpy(dest, pdata, buffer_len);
return buffer_len;
}
int Buffer::create(const char* root, int _buffer_len)
{
char* fname = new char[128];
sprintf(fname, "%s.hb/%03d", root, Buffer::last_buf);
the_buffers.push_back(new MapBuffer(fname, _buffer_len));
return 0;
}
int Buffer::verbose;
int Buffer::checkiten = 1;
unsigned Buffer::bufferlen;
unsigned Buffer::nbuffers;
unsigned Buffer::sample_size = 1;
unsigned Buffer::last_buf;
vector<Buffer*> Buffer::the_buffers;
#define MAPBUFFER_BA0 (char*)(0x40000000)
char* MapBuffer::ba0 = MAPBUFFER_BA0; /* const */
char* MapBuffer::ba1 = MAPBUFFER_BA0; /* initial value increments to top */
char* MapBuffer::ba_lo = MAPBUFFER_BA0; /* initial value, may move by reserve count */
char* MapBuffer::ba_hi; /* tracks ba1 @@todo may be redundant */
int MapBuffer::buffer_0_reserved;
bool MapBuffer::contiguous = true;
#define MODPRAMS "/sys/module/acq420fmc/parameters/"
#define BUFLEN MODPRAMS "bufferlen"
#define NBUF MODPRAMS "nbuffers"
class BufferInitializer {
public:
BufferInitializer() {
const char* vs = getenv("BufferVerbose");
vs && (Buffer::verbose = atoi(vs));
const char* checkit = getenv("BufferCheckit");
checkit && (Buffer::checkiten = atoi(checkit));
getKnob(-1, NBUF, &Buffer::nbuffers);
getKnob(-1, BUFLEN, &Buffer::bufferlen);
}
};
static BufferInitializer bi;