/*vectorPerformanceMain.cpp */ /** * Copyright - See the COPYRIGHT that is included with this distribution. * EPICS pvData is distributed subject to a Software License Agreement found * in file LICENSE that is included with this distribution. */ /** * @author mrk * @date 2013.09.02 */ /* Author: Marty Kraimer */ #include #include #include #include #include #include #include #include #include #include using namespace std; using std::tr1::static_pointer_cast; using namespace epics::pvData; class VectorPerformanceThread; typedef std::tr1::shared_ptr VectorPerformanceThreadPtr; class VectorPerformanceThread : public epicsThreadRunable { public: VectorPerformanceThread(int threadNumber,size_t size,double delay); virtual ~VectorPerformanceThread(){}; void init(); void start(); virtual void run(); void destroy(); private: bool isDestroyed; bool runReturned; int threadNumber; size_t size; epics::pvData::int64 value; double delay; Mutex mutex; std::vector vector; std::auto_ptr thread; }; VectorPerformanceThread::VectorPerformanceThread( int threadNumber,size_t size,double delay) : isDestroyed(false), runReturned(false), threadNumber(threadNumber), size(size), value(0), delay(delay) {} void VectorPerformanceThread::init() { vector.resize(size); thread = std::auto_ptr(new epicsThread( *this, "vectorPerform", epicsThreadGetStackSize(epicsThreadStackSmall), epicsThreadPriorityHigh)); } void VectorPerformanceThread::start() { thread->start(); } void VectorPerformanceThread::destroy() { Lock lock(mutex); if(isDestroyed) return; isDestroyed = true; while(true) { if(runReturned) break; lock.unlock(); epicsThreadSleep(.01); lock.lock(); } thread->exitWait(); thread.reset(); } void VectorPerformanceThread::run() { TimeStamp timeStamp; TimeStamp timeStampLast; timeStampLast.getCurrent(); int nSinceLastReport = 0; while(true) { if(delay>0.0) epicsThreadSleep(delay); { Lock lock(mutex); if(isDestroyed) { cout << " found isDestroyed " << threadNumber << endl; runReturned = true; return; } } timeStamp.getCurrent(); double diff = TimeStamp::diff(timeStamp,timeStampLast); if(diff>=1.0) { cout << "thread" << threadNumber; cout << " value " << value; cout << " time " << diff; double iterations = nSinceLastReport; iterations /= diff; cout << " iterations/sec " << iterations; double elementSize = size; double elementsPerSecond = elementSize*nSinceLastReport; elementsPerSecond /= diff; elementsPerSecond /= 1e6; cout << " elements/sec " << elementsPerSecond << "million" << endl; cout.flush(); timeStampLast = timeStamp; nSinceLastReport = 0; } ++nSinceLastReport; ++value; for(size_t i=0; i1) size = strtoul(argv[1],0,0); if(argc>2) delay = atof(argv[2]); if(argc>3) nThread = strtoul(argv[3],0,0); cout << "vectorPerformance "; cout << size << " "; cout << delay << " "; cout << nThread << " "; cout << endl; cout << "vectorPerformance\n"; std::vector threads; for(size_t i=0; iinit(); } for(size_t i=0; istart(); } string str; while(true) { cout << "Type exit to stop: \n"; getline(cin,str); if(str.compare("exit")==0) break; } for(size_t i=0; idestroy(); threads[i].reset(); } return 0; }