Change std::ifStream to FileStream of using mmap.
This commit is contained in:
103
src/FileStream/FileStream.cpp
Normal file
103
src/FileStream/FileStream.cpp
Normal file
@@ -0,0 +1,103 @@
|
||||
#include "FileStream.h"
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#ifdef WIN32
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
namespace
|
||||
{
|
||||
const unsigned long long SKIP_UNIT_SIZE = 2147483647;//maxValue of int
|
||||
}
|
||||
|
||||
FileStream::FileStream()
|
||||
#ifdef WIN32
|
||||
: mStream()
|
||||
#else
|
||||
: mPosition(0)
|
||||
, mData(nullptr)
|
||||
, mFd()
|
||||
, mStSize()
|
||||
#endif
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
FileStream::~FileStream()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool FileStream::open(const std::string& aFileName)
|
||||
{
|
||||
#ifdef WIN32
|
||||
mStream.open(aFileName.c_str(), std::ios_base::binary | std::ios::in);
|
||||
return true;
|
||||
#else
|
||||
if ((mFd = ::open(aFileName.c_str(), O_RDONLY)) < 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
struct stat sb;
|
||||
if (fstat(mFd, &sb) == -1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
mStSize = sb.st_size;
|
||||
mData = (int16_t*)mmap(NULL, mStSize, PROT_READ, MAP_SHARED, mFd, 0);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileStream::seekg(unsigned long long aSkipSize, StreamPosition aFlag)
|
||||
{
|
||||
#ifdef WIN32
|
||||
if(aFlag == StreamPosition::cur)
|
||||
{
|
||||
unsigned long long skipData = aSkipSize % SKIP_UNIT_SIZE;
|
||||
int skipLoop = static_cast<int>((aSkipSize - skipData) / SKIP_UNIT_SIZE);
|
||||
for(int i = 0;i < skipLoop ; ++i)
|
||||
{
|
||||
mStream.seekg(SKIP_UNIT_SIZE,std::ios::cur);
|
||||
}
|
||||
mStream.seekg(static_cast<int>(skipData),std::ios::cur);
|
||||
}
|
||||
else if(aFlag == StreamPosition::begin)
|
||||
{
|
||||
mStream.seekg(static_cast<int>(aSkipSize),std::ios::beg);
|
||||
}
|
||||
#else
|
||||
if(aFlag == StreamPosition::cur)
|
||||
{
|
||||
mPosition += aSkipSize;
|
||||
}
|
||||
else if(aFlag == StreamPosition::begin)
|
||||
{
|
||||
mPosition = aSkipSize;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileStream::read(int16_t& aValue)
|
||||
{
|
||||
#ifdef WIN32
|
||||
mStream.read((char*)&aValue,sizeof(int16_t));
|
||||
#else
|
||||
aValue = mData[mPosition/2];
|
||||
mPosition += 2;
|
||||
#endif
|
||||
}
|
||||
|
||||
void FileStream::close()
|
||||
{
|
||||
#ifdef WIN32
|
||||
mStream.close();
|
||||
#else
|
||||
munmap(mData, mStSize);
|
||||
::close(mFd);
|
||||
#endif
|
||||
}
|
||||
|
||||
38
src/FileStream/FileStream.h
Normal file
38
src/FileStream/FileStream.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#ifndef FILESTREAM_H
|
||||
#define FILESTREAM_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
class FileStream
|
||||
{
|
||||
|
||||
public:
|
||||
enum StreamPosition
|
||||
{
|
||||
cur = 0,
|
||||
begin
|
||||
};
|
||||
|
||||
public:
|
||||
FileStream();
|
||||
~FileStream();
|
||||
bool open(const std::string& aFileName);
|
||||
void seekg(unsigned long long aSkipSize, StreamPosition aFlag);
|
||||
void read(int16_t& aValue);
|
||||
void close();
|
||||
|
||||
private:
|
||||
#ifdef WIN32
|
||||
std::ifstream mStream;
|
||||
#else
|
||||
unsigned long long mPosition;
|
||||
int16_t* mData;
|
||||
int mFd;
|
||||
__off_t mStSize;
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
#endif // FILESTREAM_H
|
||||
@@ -56,7 +56,8 @@ SOURCES += \
|
||||
DataReader/PatientDataReader.cpp \
|
||||
Helper/MatlabHelper.cpp \
|
||||
Data/ConfigHeader.cpp \
|
||||
DataReader/ConfigHeaderReader.cpp
|
||||
DataReader/ConfigHeaderReader.cpp \
|
||||
FileStream/FileStream.cpp
|
||||
|
||||
HEADERS += \
|
||||
Parser.h \
|
||||
@@ -97,7 +98,8 @@ HEADERS += \
|
||||
Data/ConfigHeader.h \
|
||||
DataReader/ConfigHeaderReader.h \
|
||||
Json/json.hpp \
|
||||
DataReader/TemperatureDataReader.h
|
||||
DataReader/TemperatureDataReader.h \
|
||||
FileStream/FileStream.h
|
||||
|
||||
INCLUDEPATH += $$PWD/../thirdParty/include
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
#include "DataReader/PatientDataReader.h"
|
||||
#include "DataReader/TemperatureDataReader.h"
|
||||
#include "DataReader/ConfigHeaderReader.h"
|
||||
#include "FileStream/FileStream.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <thread>
|
||||
#include <math.h>
|
||||
|
||||
@@ -67,15 +67,16 @@ namespace
|
||||
return 0;
|
||||
}
|
||||
|
||||
void skipStreamDataSize(std::ifstream& aStream,unsigned long long aSkipSize)
|
||||
void skipStreamDataSize(FileStream& aStream,unsigned long long aSkipSize)
|
||||
{
|
||||
unsigned long long skipData = aSkipSize % SKIP_UNIT_SIZE;
|
||||
int skipLoop = static_cast<int>((aSkipSize - skipData) / SKIP_UNIT_SIZE);
|
||||
for(int i = 0;i < skipLoop ; ++i)
|
||||
{
|
||||
aStream.seekg(SKIP_UNIT_SIZE,std::ios::cur);
|
||||
}
|
||||
aStream.seekg(static_cast<int>(skipData),std::ios::cur);
|
||||
// unsigned long long skipData = aSkipSize % SKIP_UNIT_SIZE;
|
||||
// int skipLoop = static_cast<int>((aSkipSize - skipData) / SKIP_UNIT_SIZE);
|
||||
// for(int i = 0;i < skipLoop ; ++i)
|
||||
// {
|
||||
// aStream.seekg(SKIP_UNIT_SIZE,std::ios::cur);
|
||||
// }
|
||||
// aStream.seekg(static_cast<int>(skipData),std::ios::cur);
|
||||
aStream.seekg(aSkipSize, FileStream::cur);
|
||||
}
|
||||
|
||||
ParserMode initParserMode(const std::string& aPath)
|
||||
@@ -325,8 +326,8 @@ AScanData ParserPrivate::getAscanDataOfMotorPosition(const TasElementIndex& aSen
|
||||
aShotList->restAllCurrentLoopIndex();
|
||||
unsigned long long skipSize = blockSizeIndex * mBlockSize
|
||||
+ static_cast<unsigned long long>(AcmChannelNum - 1) * mDataSize;
|
||||
std::ifstream stream;
|
||||
stream.open(mFileList.at(static_cast<size_t>(AcmNum - 1)).c_str(), std::ios_base::binary | std::ios::in);
|
||||
FileStream stream;
|
||||
stream.open(mFileList.at(static_cast<size_t>(AcmNum - 1)).c_str());
|
||||
if (aMotorPosition == MotorPosition::MotorPosition2 && aShotList->hasMotorPosition(2))
|
||||
{
|
||||
unsigned long long motorPositionSize = mBlockSize * aShotList->getElementSize() * aShotList->getTasSize() * aShotList->getMuxSize();
|
||||
@@ -382,8 +383,8 @@ void ParserPrivate::getAscanDataOfMotorPositionInThread(std::shared_ptr<ShotList
|
||||
unsigned short* mapperArray = mapperArrayPointer.get();
|
||||
for(int i = 0; i < aLoopNum; ++i)
|
||||
{
|
||||
std::ifstream stream;
|
||||
stream.open(mFileList.at(aFileNum).c_str(), std::ios_base::binary | std::ios::in);
|
||||
FileStream stream;
|
||||
stream.open(mFileList.at(aFileNum).c_str());
|
||||
unsigned long long jumpIndex[CHANNEL_SIZE] = {0};
|
||||
unsigned long long jumpMotorPosition = 0;
|
||||
unsigned long long jumpElement = 0;
|
||||
@@ -398,7 +399,7 @@ void ParserPrivate::getAscanDataOfMotorPositionInThread(std::shared_ptr<ShotList
|
||||
{
|
||||
if(aShotList->getCurrentTasValue() != aSenderTasNum)
|
||||
{
|
||||
stream.seekg(static_cast<int>(mBlockSize), std::ios::cur);
|
||||
stream.seekg(static_cast<int>(mBlockSize), FileStream::cur);
|
||||
continue;
|
||||
}
|
||||
jumpElement = RECEIVE_ELEMENT_COUNT * (ElementMapper::mapping(ElectricIndex(aShotList->getCurrentElementValue())).getIndex() - 1);
|
||||
@@ -442,8 +443,8 @@ AScanData ParserPrivate::getAscanDataOfCEMeasured(unsigned short aTasNum,const T
|
||||
{
|
||||
return AScanData();
|
||||
}
|
||||
std::ifstream stream;
|
||||
stream.open(mFileList.at(static_cast<size_t>(AcmNum - 1)).c_str(), std::ios_base::binary | std::ios::in);
|
||||
FileStream stream;
|
||||
stream.open(mFileList.at(static_cast<size_t>(AcmNum - 1)).c_str());
|
||||
unsigned long long motorPositionSize = mBlockSize * aShotList->getElementSize() * aShotList->getTasSize() * aShotList->getMuxSize();
|
||||
skipStreamDataSize(stream,motorPositionSize * aShotList->getMotorPositionSize());
|
||||
int muxIndex = getMuxIndex(aReceiverIndex) + 1;
|
||||
@@ -522,8 +523,8 @@ void ParserPrivate::getAscanDataOfCEMeasuredInThread(std::shared_ptr<ShotList> a
|
||||
unsigned short* mapperArray = mapperArrayPointer.get();
|
||||
for(int i = 0; i < aLoopNum; ++i)
|
||||
{
|
||||
std::ifstream stream;
|
||||
stream.open(mFileList.at(aFileNum).c_str(), std::ios_base::binary | std::ios::in);
|
||||
FileStream stream;
|
||||
stream.open(mFileList.at(aFileNum).c_str());
|
||||
unsigned long long motorPositionSize = mBlockSize * aShotList->getElementSize() * aShotList->getTasSize() * aShotList->getMuxSize();
|
||||
skipStreamDataSize(stream,motorPositionSize * aShotList->getMotorPositionSize());
|
||||
unsigned long long jumpIndex[32] = {0};
|
||||
@@ -539,7 +540,7 @@ void ParserPrivate::getAscanDataOfCEMeasuredInThread(std::shared_ptr<ShotList> a
|
||||
}
|
||||
if(aShotList->getCurrentTasValue() != aSenderTasNum)
|
||||
{
|
||||
stream.seekg(static_cast<int>(mBlockSize), std::ios::cur);
|
||||
stream.seekg(static_cast<int>(mBlockSize), FileStream::cur);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -705,14 +706,14 @@ AScanData ParserPrivate::searchAscanDataFromOneTasAscanDataOfCE(OneTasAScanData
|
||||
return result;
|
||||
}
|
||||
|
||||
void ParserPrivate::readAscanData(std::ifstream* aStream, AScanData aAScanData)
|
||||
void ParserPrivate::readAscanData(FileStream* aStream, AScanData aAScanData)
|
||||
{
|
||||
//Read Header Info
|
||||
for(unsigned short i=0; i<HEADER_SIZE; ++i)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), std::ios::cur);
|
||||
aStream->read(data);
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), FileStream::cur);
|
||||
aAScanData.getHeader()[i] = data;
|
||||
}
|
||||
|
||||
@@ -723,8 +724,8 @@ void ParserPrivate::readAscanData(std::ifstream* aStream, AScanData aAScanData)
|
||||
for(unsigned int i=0;i < mDataLength;++i)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), std::ios::cur);
|
||||
aStream->read(data);
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), FileStream::cur);
|
||||
aAScanData.get()[i] = data;
|
||||
}
|
||||
|
||||
@@ -732,8 +733,8 @@ void ParserPrivate::readAscanData(std::ifstream* aStream, AScanData aAScanData)
|
||||
for(unsigned int i=0;i < CRC_SIZE;++i)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), std::ios::cur);
|
||||
aStream->read(data);
|
||||
aStream->seekg(static_cast<int>((CHANNEL_SIZE-1)*mDataSize), FileStream::cur);
|
||||
aAScanData.getCrc()[i] = data;
|
||||
}
|
||||
|
||||
@@ -744,7 +745,7 @@ void ParserPrivate::readAscanData(std::ifstream* aStream, AScanData aAScanData)
|
||||
}
|
||||
}
|
||||
|
||||
void ParserPrivate::readOneTasAscanData(std::ifstream* aStream, unsigned long long* aIndex, OneTasAScanData aOneTasAscan)
|
||||
void ParserPrivate::readOneTasAscanData(FileStream* aStream, unsigned long long* aIndex, OneTasAScanData aOneTasAscan)
|
||||
{
|
||||
//Read Header Info
|
||||
for(unsigned long long header = 0; header < HEADER_SIZE; ++header)
|
||||
@@ -752,7 +753,7 @@ void ParserPrivate::readOneTasAscanData(std::ifstream* aStream, unsigned long lo
|
||||
for(unsigned int channel = 0; channel < CHANNEL_SIZE; ++channel)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->read(data);
|
||||
aOneTasAscan.getHeader()[aIndex[channel] * HEADER_SIZE + header] = data;
|
||||
}
|
||||
}
|
||||
@@ -765,7 +766,7 @@ void ParserPrivate::readOneTasAscanData(std::ifstream* aStream, unsigned long lo
|
||||
for(unsigned int channel = 0; channel < CHANNEL_SIZE; ++channel)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->read(data);
|
||||
aOneTasAscan.get()[aIndex[channel] * mDataLength + ascanIndex] = data;
|
||||
}
|
||||
}
|
||||
@@ -776,7 +777,7 @@ void ParserPrivate::readOneTasAscanData(std::ifstream* aStream, unsigned long lo
|
||||
for(unsigned int channel = 0; channel < CHANNEL_SIZE; ++channel)
|
||||
{
|
||||
int16_t data;
|
||||
aStream->read((char*)&data,sizeof(int16_t));
|
||||
aStream->read(data);
|
||||
aOneTasAscan.getCrc()[aIndex[channel] * CRC_SIZE + crc] = data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,8 @@ enum class ParserMode : unsigned short
|
||||
KITMode = 1
|
||||
};
|
||||
|
||||
class FileStream;
|
||||
|
||||
class ParserPrivate
|
||||
{
|
||||
public:
|
||||
@@ -52,8 +54,8 @@ private:
|
||||
void getAscanDataOfMotorPositionInThread(std::shared_ptr<ShotList> aShotList, OneTasAScanData aOneTasAscanData, unsigned short aSenderTasNum, unsigned short aFileNum, unsigned short aLoopNum);
|
||||
void getAscanDataOfCEMeasuredInThread(std::shared_ptr<ShotList> aShotList, OneTasAScanData aOneTasAscanData, unsigned short aSenderTasNum, unsigned short aFileNum, unsigned short aLoopNum);
|
||||
void getAllCEMeasuredDataInThread(CEMeasuredData aCEMeasuredData, unsigned short aTasNum, unsigned short aLoopNum);
|
||||
void readAscanData(std::ifstream* aStream, AScanData aAScanData);
|
||||
void readOneTasAscanData(std::ifstream* aStream, unsigned long long* aIndex, OneTasAScanData aOneTasAscan);
|
||||
void readAscanData(FileStream* aStream, AScanData aAScanData);
|
||||
void readOneTasAscanData(FileStream* aStream, unsigned long long* aIndex, OneTasAScanData aOneTasAscan);
|
||||
double getAmplification(short aValue);
|
||||
void initParser();
|
||||
void initShotList();
|
||||
|
||||
Reference in New Issue
Block a user