Mercurial > hg > beaglert
comparison core/WriteFile.cpp @ 159:1e7db6610600
Increased buffer size, improves performances (but increases memory usage) for binary mode
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sun, 18 Oct 2015 01:13:40 +0100 |
parents | 45fc760622c9 |
children | 9bfe04d184fb e4392164b458 |
comparison
equal
deleted
inserted
replaced
158:45fc760622c9 | 159:1e7db6610600 |
---|---|
33 | 33 |
34 void WriteFile::init(const char* filename){ //if you do not call this before using the object, results are undefined | 34 void WriteFile::init(const char* filename){ //if you do not call this before using the object, results are undefined |
35 file = fopen(filename, "w"); | 35 file = fopen(filename, "w"); |
36 variableOpen = false; | 36 variableOpen = false; |
37 lineLength = 0; | 37 lineLength = 0; |
38 echo = false; | 38 setEcho(false); |
39 bufferLength = 0; | 39 bufferLength = 0; |
40 textReadPointer = 0; | 40 textReadPointer = 0; |
41 binaryReadPointer = 0; | 41 binaryReadPointer = 0; |
42 writePointer = 0; | 42 writePointer = 0; |
43 sleepTimeMs = 1; | 43 sleepTimeMs = 1; |
51 echoPeriod = 1; | 51 echoPeriod = 1; |
52 } | 52 } |
53 | 53 |
54 void WriteFile::setFileType(WriteFileType newFileType){ | 54 void WriteFile::setFileType(WriteFileType newFileType){ |
55 fileType = newFileType; | 55 fileType = newFileType; |
56 if(fileType == kBinary) | |
57 setLineLength(1); | |
56 } | 58 } |
57 void WriteFile::setEcho(bool newEcho){ | 59 void WriteFile::setEcho(bool newEcho){ |
58 echo=newEcho; | 60 echo=newEcho; |
59 } | 61 } |
60 void WriteFile::setEchoInterval(int newEchoPeriod){ | 62 void WriteFile::setEchoInterval(int newEchoPeriod){ |
83 for(unsigned int n = 0; n < formatTokens.size(); n++){ | 85 for(unsigned int n = 0; n < formatTokens.size(); n++){ |
84 int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer, | 86 int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer, |
85 formatTokens[n], buffer[textReadPointer]); | 87 formatTokens[n], buffer[textReadPointer]); |
86 stringBufferPointer += numOfCharsWritten; | 88 stringBufferPointer += numOfCharsWritten; |
87 textReadPointer++; | 89 textReadPointer++; |
88 if (textReadPointer >= bufferLength){ | 90 if(textReadPointer >= bufferLength){ |
89 textReadPointer -= bufferLength; | 91 textReadPointer -= bufferLength; |
90 } | 92 } |
91 } | 93 } |
92 print(stringBuffer); | 94 print(stringBuffer); |
93 } | 95 } |
94 } | 96 } |
95 | 97 |
96 void WriteFile::setLineLength(int newLineLength){ | 98 void WriteFile::setLineLength(int newLineLength){ |
97 lineLength=newLineLength; | 99 lineLength=newLineLength; |
98 free(buffer); | 100 free(buffer); |
99 bufferLength = lineLength * 10000; // circular buffer of length 10000 lineLenghts | 101 bufferLength = lineLength * (int)1e7; // circular buffer of length 1e7 lineLenghts |
100 buffer = (float*)malloc(sizeof(float) * bufferLength); | 102 buffer = (float*)malloc(sizeof(float) * bufferLength); |
101 } | 103 } |
102 | 104 |
103 void WriteFile::log(float value){ | 105 void WriteFile::log(float value){ |
104 if(fileType != kBinary && (format == NULL || buffer == NULL)) | 106 if(fileType != kBinary && (format == NULL || buffer == NULL)) |
200 else{ | 202 else{ |
201 return getOffsetFromPointer(textReadPointer); | 203 return getOffsetFromPointer(textReadPointer); |
202 } | 204 } |
203 } | 205 } |
204 | 206 |
205 void WriteFile::writeOutput(bool writeAll){ | 207 void WriteFile::writeOutput(bool flush){ |
206 while((echo == true || fileType == kText) && getOffsetFromPointer(textReadPointer) >= lineLength){ //if there is less than one line worth of data to write, skip over. | 208 while((echo == true || fileType == kText) && getOffsetFromPointer(textReadPointer) >= lineLength){ //if there is less than one line worth of data to write, skip over. |
207 // So we make sure we only write full lines | 209 // So we make sure we only write full lines |
208 writeLine(); | 210 writeLine(); |
209 } | 211 } |
210 if(fileType == kBinary){ | 212 if(fileType == kBinary){ |
211 int numBinaryElementsToWriteAtOnce = 100; | 213 int numBinaryElementsToWriteAtOnce = 3*(int)1e5; |
212 while(getOffsetFromPointer(binaryReadPointer) > numBinaryElementsToWriteAtOnce){ | 214 while(getOffsetFromPointer(binaryReadPointer) > numBinaryElementsToWriteAtOnce){ |
213 int elementsToEndOfBuffer = bufferLength - binaryReadPointer; | 215 int elementsToEndOfBuffer = bufferLength - binaryReadPointer; |
214 int numberElementsToWrite = numBinaryElementsToWriteAtOnce < elementsToEndOfBuffer ? | 216 int numberElementsToWrite = numBinaryElementsToWriteAtOnce < elementsToEndOfBuffer ? |
215 numBinaryElementsToWriteAtOnce : elementsToEndOfBuffer; | 217 numBinaryElementsToWriteAtOnce : elementsToEndOfBuffer; |
216 numberElementsToWrite = fwrite(&(buffer[binaryReadPointer]), sizeof(float), numberElementsToWrite, file); | 218 numberElementsToWrite = fwrite(&(buffer[binaryReadPointer]), sizeof(float), numberElementsToWrite, file); |
217 binaryReadPointer += numberElementsToWrite; | 219 binaryReadPointer += numberElementsToWrite; |
218 if(binaryReadPointer >= bufferLength){ | 220 if(binaryReadPointer >= bufferLength){ |
219 binaryReadPointer = 0; | 221 binaryReadPointer = 0; |
220 } | 222 } |
221 } | 223 } |
222 if(writeAll == true){ // flush all the buffer to the file | 224 if(flush == true){ // flush all the buffer to the file |
223 while(getOffsetFromPointer(binaryReadPointer) != 0){ | 225 while(getOffsetFromPointer(binaryReadPointer) != 0){ |
224 binaryReadPointer += fwrite(&(buffer[binaryReadPointer]), sizeof(float), 1, file); | 226 binaryReadPointer += fwrite(&(buffer[binaryReadPointer]), sizeof(float), 1, file); |
225 if(binaryReadPointer >= bufferLength){ | 227 if(binaryReadPointer >= bufferLength){ |
226 binaryReadPointer = 0; | 228 binaryReadPointer = 0; |
227 } | 229 } |
228 } | 230 } |
229 } | 231 } |
230 } | 232 } |
231 } | 233 } |
232 | 234 |
233 void WriteFile::writeAllOutputs(){ | 235 void WriteFile::writeAllOutputs(bool flush){ |
234 for(unsigned int n = 0; n < objAddrs.size(); n++){ | 236 for(unsigned int n = 0; n < objAddrs.size(); n++){ |
235 objAddrs[n] -> writeOutput(false); | 237 objAddrs[n] -> writeOutput(flush); |
236 } | 238 } |
237 } | 239 } |
238 | 240 |
239 void WriteFile::writeAllHeaders(){ | 241 void WriteFile::writeAllHeaders(){ |
240 for(unsigned int n = 0; n < objAddrs.size(); n++){ | 242 for(unsigned int n = 0; n < objAddrs.size(); n++){ |
277 | 279 |
278 void WriteFile::run(){ | 280 void WriteFile::run(){ |
279 threadRunning = true; | 281 threadRunning = true; |
280 writeAllHeaders(); | 282 writeAllHeaders(); |
281 while(threadShouldExit()==false){ | 283 while(threadShouldExit()==false){ |
282 writeAllOutputs(); | 284 writeAllOutputs(false); |
283 usleep(sleepTimeMs*1000); | 285 usleep(sleepTimeMs*1000); |
284 } | 286 } |
287 writeAllOutputs(true); | |
285 writeAllFooters(); // when ctrl-c is pressed, the last line is closed and the file is closed | 288 writeAllFooters(); // when ctrl-c is pressed, the last line is closed and the file is closed |
286 threadRunning = false; | 289 threadRunning = false; |
287 } | 290 } |
288 | 291 |
289 void WriteFile::allocateAndCopyString(const char* source, char** destination){ | 292 void WriteFile::allocateAndCopyString(const char* source, char** destination){ |