-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbzip-table-lines-threaded.c
309 lines (269 loc) · 7.46 KB
/
bzip-table-lines-threaded.c
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bzlib.h>
#include <stdio.h>
#include <stdlib.h>
#include "fcntl.h"
#include "errno.h"
#include <thread>
#include <queue>
#include <mutex>
#include <condition_variable>
#include "filepreindexer.hpp"
const int threadcount=10;
#define BUF_SIZE 4096 * 100
std::mutex globalsystem;
std::mutex queuelock;
std::condition_variable cond;
bool data_ready=0;
std::mutex globalsystem_finished;
bool data_finished=0;
#include <dirent.h>
#include <errno.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <sys/stat.h>
int runmkdir(const char *path)
{
struct stat st;
int status=0;
if (stat(path, &st) != 0)
{
if (mkdir(path, 0777) != 0)
status = -1;
}
else if (!S_ISDIR(0777))
{
errno = ENOTDIR;
status = -1;
}
return(status);
}
int debug() {return 0;}
int prescanner(OSMWorldPreindex & world,const char *s);
class Datablock {
public:
long long seen;
int blockcount;
char buf[BUF_SIZE+1];
Datablock() {
blockcount=0;
buf[0]=0;
buf[BUF_SIZE]=0;
buf[BUF_SIZE+1]=0;
}
Datablock(int blockcount, const char * pbuf) :
blockcount(blockcount) {
// cerr << "Created block " << blockcount << endl;
strncpy(buf,pbuf,BUF_SIZE);
// cerr << "Check data:\"" << buf[BUF_SIZE -1] << "\""<< endl;
buf[BUF_SIZE]=0;
buf[BUF_SIZE+1]=0;
}
Datablock(const Datablock & r) {
blockcount=r.blockcount;
strncpy(buf,r.buf,BUF_SIZE);
buf[BUF_SIZE]=0;
buf[BUF_SIZE+1]=0;
}
int process() {
// skip over everything until we get a <node/<way/<relation and send the stuff before to the previous block
// skip all unfinished data
runmkdir("datafiles");
char dirbuf[255];
sprintf(dirbuf,"datafiles/%06d/",blockcount);
runmkdir(dirbuf);
OSMWorldPreindex iworld2(dirbuf,blockcount);
seen += BUF_SIZE;
int ret=0;
ret=prescanner(iworld2,buf);
if (ret != 1) {
cerr << "prescanner returned ret " << ret << " for len :" << BUF_SIZE << endl;
} else {
//cerr << "OK scanner returned ret " << ret << " for len :" << strlen(buffer) << endl;
}
if (iworld2.scannerstatus(ret,buf)!=0) {
cerr << "ERROR: status returned ret " << ret << endl;
// exit(233);
return -1;
}
return 0;
}
int merge_previous() {
// merge the data from the previous block
}
int merge_next() {
// merge the leftover data with the next block
}
};
std::queue<Datablock> dataqueue;
static void threadprocess ()
{
std::thread::id id; // Refers to no thread
id = std::this_thread::get_id(); // get id for this thread
{
std::unique_lock<std::mutex> lk(globalsystem);
while(!data_ready)
{
// printf("thread process %llu waiting \n",id);
cond.wait(lk);
}
// printf("thread process finished waiting \n",id);
} // finish the lock
//
while (1)
{
// printf("begin of while thread process %llu\n",id);
{
if (dataqueue.size()>0) {
Datablock b;
{
std::unique_lock<std::mutex> lk(queuelock);
// now we have the lock, check the size again
if (dataqueue.size()>0) {
printf("queue size %d\n",dataqueue.size());
Datablock b2 = dataqueue.front();
b=b2;
dataqueue.pop();
}
}
if (b.blockcount>=0)
{
printf("block %d, size %d\n",b.blockcount,strlen(b.buf));
//std::this_thread::sleep_for(std::chrono::milliseconds(400)); // simulate processing, dont do this inside the
b.process();
//connect this to the previous one
}
}
else
{
// printf("before going to get the lock for finished\n");
{
// printf("going to get the lock for finished\n");
std::unique_lock<std::mutex> lk(globalsystem_finished);
if (data_finished)
{
// printf("thread process finished no data left \n",id);
return;
}
}
// printf("thread process %llu no data\n",id);
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
// printf("end of while thread process %llu\n",id);
}
}
}
class MyThread : public std::thread {
public :
MyThread() : std::thread(threadprocess) {
//printf("my thread created\n");
}
};
std::vector<MyThread> threads(threadcount);
void jointhreads()
{
// printf("joining threads\n");
// a short lock
{
// printf("locking global\n");
std::unique_lock<std::mutex> lk(globalsystem_finished);
data_finished=1;
}
int i;
for (i=0;i<threadcount; i++) { // wait for all to finishe?
std::thread::id id = threads[i].get_id();
// printf("looking at %d with id %llu\n",i,id);
if(threads[i].joinable()){
// printf("going to join %d with id %llu\n",i,id);
threads[i].join();
// printf("joined %d with id %llu\n",i,id);
}
else
{
// printf("i %d is not joinable",i);
}
}
// printf("done waiting for joining\n");
}
int
bunzip_one(FILE *f) {
int bzError=0;
BZFILE *bzf=0;
bzf = BZ2_bzReadOpen(&bzError, f, 0, 0, NULL, 0);
if (bzError != BZ_OK) {
fprintf(stderr, "E: BZ2_bzReadOpen: %d\n", bzError);
return -1;
}
int blockcount =0;
char buf[BUF_SIZE];
while (bzError == BZ_OK) {
int nread = BZ2_bzRead(&bzError, bzf, buf, sizeof(buf));
if (bzError == BZ_OK || bzError == BZ_STREAM_END) {
if (blockcount ==0) {
//printf("got count %d data:%s\n",nread,buf);
}
// each block depends on the previous block, but can skip the first items until the previoud block is finished.
Datablock data(blockcount,buf);
{
//data.process(); // process them directly
std::unique_lock<std::mutex> lk(queuelock);
dataqueue.push(data);
if (dataqueue.size() > threadcount) {
printf("queue size %d\n",dataqueue.size());
int i=0;
{
printf("queue size %d\n",dataqueue.size());
for (i=0;i<threadcount; i++) { // wait for all to finishe?
std::thread::id id = threads[i].get_id();
// printf("looking at %d with id %llu\n",i,id);
}
}
}
}
if (blockcount == 0)
{
printf("going to start threads\n");
std::unique_lock<std::mutex> lk(globalsystem);
data_ready=true;
cond.notify_all();
}
blockcount++;
}
}
if (bzError != BZ_STREAM_END) {
jointhreads();
fprintf(stderr, "E: bzip error after read: %d\n", bzError);
return -1;
}
BZ2_bzReadClose(&bzError, bzf);
jointhreads();
return 0;
}
int main(int argc, const char ** argv) {
FILE *f;
printf("argc %d\n",argc);
if (argc>=2) {
printf("argv %s\n",argv[1]);
} else {
printf("no file\n");
return 0;
}
printf("going to open %s\n",argv[1]);
f=fopen(argv[1], "rb");
if (f == NULL)
{
printf("error %d\n",errno);
perror(argv[1]);
return 233;
}
else {
// printf("file opened with fd %d\n",fd);
}
if (bunzip_one(f) == -1)
return -1;
fclose(f);
return 0;
}