From d8e5445dc4d7ca8267dcf97004f6baaead668eb7 Mon Sep 17 00:00:00 2001 From: Aleksandar Donchev Date: Thu, 12 Oct 2017 13:02:09 +0200 Subject: Real time scheduler added, capi wrapper timeout return value considered in registerTimeout, only requested revent passed from within the camsockethandler instead of all Signed-off-by: Christian Linke Change-Id: I2d9d2c424ac3fac62c76a66545a531c518edb2e8 --- AudioManagerDaemon/src/main.cpp | 8 ++++ AudioManagerUtilities/include/CAmSocketHandler.h | 7 --- AudioManagerUtilities/src/CAmCommonAPIWrapper.cpp | 43 +++++++++++++----- AudioManagerUtilities/src/CAmSocketHandler.cpp | 51 +++++++++------------- .../AmSocketHandlerTest/CAmSocketHandlerTest.cpp | 48 ++++++++++++-------- 5 files changed, 90 insertions(+), 67 deletions(-) diff --git a/AudioManagerDaemon/src/main.cpp b/AudioManagerDaemon/src/main.cpp index 8271db4..8838ceb 100755 --- a/AudioManagerDaemon/src/main.cpp +++ b/AudioManagerDaemon/src/main.cpp @@ -387,6 +387,14 @@ iControlSender.setControllerReady(); */ int main(int argc, char *argv[], char** envp) { + struct sched_param param; + param.sched_priority = 50;//mid rt proprity + if (sched_setscheduler(0, SCHED_FIFO, & param) != 0) + { + std::cerr <<"sched_setscheduler:"<getTimeoutInterval(); - - pollTimeout.tv_sec = localTimeout / 1000; - pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000; + + if(CommonAPI::TIMEOUT_INFINITE==localTimeout)//dispatch never + { + pollTimeout.tv_sec = localTimeout; + pollTimeout.tv_nsec = 0; + } + else if(CommonAPI::TIMEOUT_NONE==localTimeout)//dispatch immediately + { + pollTimeout.tv_sec = 0; + pollTimeout.tv_nsec = 1000000; + } + else + { + pollTimeout.tv_sec = localTimeout / 1000; + pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000; + } //prepare handle and callback. new is eval, but there is no other choice because we need the pointer! sh_timerHandle_t handle; //add the timer to the pollLoop - mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout); - - timerHandles myHandle({handle,timeout}); - mpListTimerhandles.push_back(myHandle); - - return; + am_Error_e error = mpSocketHandler->addTimer(pollTimeout, &pCommonTimerCallback, handle, timeout); + if (error != am_Error_e::E_OK || handle == 0) + { + logError(__func__,"adding timer failed"); + } + else + { + timerHandles myHandle({handle,timeout}); + mpListTimerhandles.push_back(myHandle); + } } void CAmCommonAPIWrapper::deregisterTimeout(CommonAPI::Timeout* timeout) @@ -243,10 +260,12 @@ void CAmCommonAPIWrapper::registerWatch(CommonAPI::Watch* watch, const CommonAPI am_Error_e error = mpSocketHandler->addFDPoll(pollfd_.fd, pollfd_.events, &pCommonPrepareCallback, &pCommonFireCallback, &pCommonCheckCallback, &pCommonDispatchCallback, watch, handle); //if everything is alright, add the watch and the handle to our map so we know this relationship - if (error == !am_Error_e::E_OK || handle == 0) + if (error != am_Error_e::E_OK || handle == 0) + { logError(__func__,"entering watch failed"); - - mMapWatches.insert(std::make_pair(pollfd_.fd,watch)); + } + else + mMapWatches.insert(std::make_pair(pollfd_.fd,watch)); } void CAmCommonAPIWrapper::commonTimerCallback(sh_timerHandle_t handle, void *) diff --git a/AudioManagerUtilities/src/CAmSocketHandler.cpp b/AudioManagerUtilities/src/CAmSocketHandler.cpp index 6809902..250d731 100644 --- a/AudioManagerUtilities/src/CAmSocketHandler.cpp +++ b/AudioManagerUtilities/src/CAmSocketHandler.cpp @@ -31,7 +31,7 @@ #include #include #include - +#include #include "CAmDltWrapper.h" #include "CAmSocketHandler.h" @@ -180,7 +180,8 @@ void CAmSocketHandler::start_listenting() //stage 0+1, call firedCB for (itMfdPollingArray = fdPollingArray.begin(); itMfdPollingArray != fdPollingArray.end(); itMfdPollingArray++) { - if (CAmSocketHandler::eventFired(*itMfdPollingArray)) + itMfdPollingArray->revents &= itMfdPollingArray->events | POLLERR | POLLHUP; + if ( itMfdPollingArray->revents!=0 ) { listmPollIt = mListActivePolls.begin(); std::advance(listmPollIt, std::distance(fdPollingArray.begin(), itMfdPollingArray)); @@ -189,7 +190,6 @@ void CAmSocketHandler::start_listenting() listPoll.push_back(&pollObj); CAmSocketHandler::fire(&pollObj); - itMfdPollingArray->revents = 0; } } @@ -312,31 +312,19 @@ am_Error_e CAmSocketHandler::listenToSignals(const std::vector & listSi return (E_NOT_POSSIBLE); } - int signalHandlerFd; + sh_poll_s sgPollData; if(mSignalFdHandle) { - sh_poll_s sgPollData; if(E_OK!=getFDPollData(mSignalFdHandle, sgPollData)) { - removeFDPoll(mSignalFdHandle); - mSignalFdHandle = 0; - } - else - { - int signalHandlerFd = signalfd(sgPollData.pollfdValue.fd, &sigset, 0); - if (signalHandlerFd == -1) - { - logError("Could not update signal fd!"); - return (E_NOT_POSSIBLE); - } - return E_OK; + mSignalFdHandle = 0; } } if(0==mSignalFdHandle) { /* Create the signalfd */ - signalHandlerFd = signalfd(-1, &sigset, 0); + int signalHandlerFd = signalfd(-1, &sigset, 0); if (signalHandlerFd == -1) { logError("Could not open signal fd!"); @@ -356,10 +344,19 @@ am_Error_e CAmSocketHandler::listenToSignals(const std::vector & listSi it.callback(it.handle, info, it.userData); }; /* We're going to add the signal fd through addFDPoll. At this point we don't have any signal listeners. */ - am_Error_e shFdError = addFDPoll(signalHandlerFd, POLLIN | POLLERR | POLLHUP, NULL, actionPoll, [](const sh_pollHandle_t, void*) + return addFDPoll(signalHandlerFd, POLLIN | POLLERR | POLLHUP, NULL, actionPoll, [](const sh_pollHandle_t, void*) { return (false);}, NULL, NULL, mSignalFdHandle); - return shFdError; } + else + { + int signalHandlerFd = signalfd(sgPollData.pollfdValue.fd, &sigset, 0); + if (signalHandlerFd == -1) + { + logError("Could not update signal fd!", strerror(errno)); + return (E_NOT_POSSIBLE); + } + return E_OK; + } } /** @@ -611,7 +608,7 @@ am_Error_e CAmSocketHandler::addTimer(const timespec & timeouts, std::functiondispatchCB(a->handle, a->userData)); } -/** - * event triggered - */ -bool CAmSocketHandler::eventFired(const pollfd& a) -{ - return (a.revents == 0 ? false : true); -} - /** * is used to set the pointer for the ppoll command * @param buffertime @@ -1088,8 +1077,8 @@ inline timespec* CAmSocketHandler::insertTime(timespec& buffertime) buffertime = mListActiveTimer.front().countdown; return (&buffertime); } - else -#endif + else +#endif { return (NULL); } diff --git a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp index 7e03f6e..b0c00d4 100644 --- a/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp +++ b/AudioManagerUtilities/test/AmSocketHandlerTest/CAmSocketHandlerTest.cpp @@ -42,7 +42,7 @@ #define SOCK_PATH "/tmp/mysock" #define SOCKET_TEST_LOOPS_COUNT 50 -#define TIMERS_TO_TEST 500 +#define TIMERS_TO_TEST 100 using namespace testing; using namespace am; @@ -194,10 +194,16 @@ void am::CAmTimerStressTest2::timerCallback(sh_timerHandle_t handle, void* pUser CAmTimerMeasurment::CAmTimerMeasurment(CAmSocketHandler *myHandler, const timespec &timeout, const std::string & label, const int32_t repeats, void * userData) : - MockIAmTimerCb(), pTimerCallback(this, &CAmTimerMeasurment::timerCallback), // - mSocketHandler(myHandler), mUpdateTimeout(timeout), mUpdateTimePoint(std::chrono::seconds - { mUpdateTimeout.tv_sec } + std::chrono::nanoseconds - { mUpdateTimeout.tv_nsec }), mLastInvocationTime(), mExpected(mUpdateTimePoint - TP_ZERO), mRepeats(repeats), mpUserData(userData), mDebugText(label) + MockIAmTimerCb() + , pTimerCallback(this, &CAmTimerMeasurment::timerCallback) + , mSocketHandler(myHandler) + , mUpdateTimeout(timeout) + , mUpdateTimePoint(std::chrono::seconds{ mUpdateTimeout.tv_sec } + std::chrono::nanoseconds{ mUpdateTimeout.tv_nsec }) + , mLastInvocationTime() + , mExpected(mUpdateTimePoint - TP_ZERO) + , mRepeats(repeats) + , mpUserData(userData) + , mDebugText(label) { } @@ -245,7 +251,7 @@ void am::CAmTimerMeasurment::timerCallback(sh_timerHandle_t handle, void* userDa std::cout << mDebugText << " Init measurment " << std::endl; #endif mLastInvocationTime = t_end; - mSocketHandler->updateTimer(handle, mUpdateTimeout); + mSocketHandler->updateTimer( handle, mUpdateTimeout); } } @@ -531,17 +537,17 @@ TEST(CAmSocketHandlerTest, timersStressTest) timespec timeoutTime11, timeout12, timeout13; timeoutTime11.tv_sec = 1; timeoutTime11.tv_nsec = 34000000; - CAmTimerMeasurment testCallback11(&myHandler, timeoutTime11, "repeatedCallback 1", std::numeric_limits::max()); + CAmTimerMeasurment testCallback11(&myHandler, timeoutTime11, "repeated 1", std::numeric_limits::max()); - timeout12.tv_nsec = 2000000; + timeout12.tv_nsec = 100000000; timeout12.tv_sec = 0; - CAmTimerMeasurment testCallback12(&myHandler, timeout12, "repeatedCallback 2", std::numeric_limits::max()); + CAmTimerMeasurment testCallback12(&myHandler, timeout12, "repeated 2", std::numeric_limits::max()); timeout13.tv_nsec = 333000000; timeout13.tv_sec = 3; - CAmTimerMeasurment testCallback13(&myHandler, timeout13, "oneshotCallback 3"); + CAmTimerMeasurment testCallback13(&myHandler, timeout13, "oneshot 3"); - myHandler.addTimer(timeoutTime, &testCallback11.pTimerCallback, handle, NULL, true); + myHandler.addTimer(timeoutTime11, &testCallback11.pTimerCallback, handle, NULL, true); EXPECT_CALL(testCallback11,timerCallback(_,NULL)).Times(AnyNumber()); myHandler.addTimer(timeout12, &testCallback12.pTimerCallback, handle, NULL, true); @@ -557,8 +563,8 @@ TEST(CAmSocketHandlerTest, timersStressTest) EXPECT_CALL(testCallback4,timerCallback(_,NULL)).Times(1); myHandler.start_listenting(); - - for(int i=0;i::max()); + CAmTimerMeasurment testCallback1(&myHandler, timeoutTime, "repeated 1", std::numeric_limits::max()); timeout2.tv_nsec = 2000000; timeout2.tv_sec = 0; - CAmTimerMeasurment testCallback2(&myHandler, timeout2, "repeatedCallback 2", std::numeric_limits::max()); + CAmTimerMeasurment testCallback2(&myHandler, timeout2, "repeated 2", std::numeric_limits::max()); timeout3.tv_nsec = 333000000; timeout3.tv_sec = 3; - CAmTimerMeasurment testCallback3(&myHandler, timeout3, "oneshotCallback 3"); + CAmTimerMeasurment testCallback3(&myHandler, timeout3, "oneshot 3"); timeout4.tv_nsec = 0; timeout4.tv_sec = 8; CAmTimerSockethandlerController testCallback4(&myHandler, timeout4); @@ -609,7 +615,7 @@ TEST(CAmSocketHandlerTest,playWithTimers) #else ASSERT_EQ(handle, 4); #endif - EXPECT_CALL(testCallback3,timerCallback(handle,NULL)).Times(2); //+1 because of measurment + EXPECT_CALL(testCallback3,timerCallback(handle,NULL)).Times(2); myHandler.addTimer(timeout4, &testCallback4.pTimerCallback, handle, NULL); #ifndef WITH_TIMERFD @@ -753,6 +759,14 @@ TEST(CAmSocketHandlerTest,playWithSockets) int main(int argc, char **argv) { + struct sched_param param; + param.sched_priority = 50;//mid rt proprity + if (sched_setscheduler(0, SCHED_FIFO, & param) != 0) + { + std::cerr <<"sched_setscheduler:"<