From fcbc3cdf3c2cc9593d1ff2ba4823608e51dff74c Mon Sep 17 00:00:00 2001 From: Hafiz Abid Qadeer Date: Mon, 15 Dec 2014 19:09:40 +0000 Subject: [PATCH] Replace ioctl with select to reduce processor usage by lldb-mi on OSX. This saga started with a hang on OSX. 2 solutions were proposed. 1) 'select' based solution works ok on OSX but slows down test completion time on Linux many times. 2) 'ioctl' base solution also works but it causes heavy processor usage on OSX as reported by Ilia K. But as the original hang did not occur on Linux so this commit re-introduces the 'select' in conditional code so that it only runs for OSX. There is no need for this 'fix' to run on Linux. Initial patch by Ilia K . A few changes were made by me. llvm-svn: 224258 --- lldb/tools/lldb-mi/MICmnStreamStdin.cpp | 12 ++++++++ lldb/tools/lldb-mi/MICmnStreamStdin.h | 2 ++ lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp | 43 +++++++++++++++++++--------- lldb/tools/lldb-mi/MICmnStreamStdinLinux.h | 1 + lldb/tools/lldb-mi/MIDriver.cpp | 2 ++ 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/lldb/tools/lldb-mi/MICmnStreamStdin.cpp b/lldb/tools/lldb-mi/MICmnStreamStdin.cpp index 8dccf16..2d54921 100644 --- a/lldb/tools/lldb-mi/MICmnStreamStdin.cpp +++ b/lldb/tools/lldb-mi/MICmnStreamStdin.cpp @@ -435,3 +435,15 @@ CMICmnStreamStdin::SetOSStdinHandler(IOSStdinHandler &vrHandler) return MIstatus::success; } +//++ ------------------------------------------------------------------------------------ +// Details: Do some actions before exiting. +// Type: Method. +// Args: None. +// Return: None. +// Throws: None. +//-- +void +CMICmnStreamStdin::OnExitHandler(void) +{ + m_pStdinReadHandler->InterruptReadLine(); +} diff --git a/lldb/tools/lldb-mi/MICmnStreamStdin.h b/lldb/tools/lldb-mi/MICmnStreamStdin.h index 7b31a64..a6779d5 100644 --- a/lldb/tools/lldb-mi/MICmnStreamStdin.h +++ b/lldb/tools/lldb-mi/MICmnStreamStdin.h @@ -66,6 +66,7 @@ class CMICmnStreamStdin : public CMICmnBase, public CMIUtilThreadActiveObjBase, public: virtual bool InputAvailable(bool &vwbAvail) = 0; virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg) = 0; + virtual void InterruptReadLine(void){}; /* dtor */ virtual ~IOSStdinHandler(void){}; }; @@ -82,6 +83,7 @@ class CMICmnStreamStdin : public CMICmnBase, public CMIUtilThreadActiveObjBase, void SetCtrlCHit(void); bool SetVisitor(IStreamStdin &vrVisitor); bool SetOSStdinHandler(IOSStdinHandler &vrHandler); + void OnExitHandler(void); // Overridden: public: diff --git a/lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp b/lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp index ef9030b..07e652d 100644 --- a/lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp +++ b/lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp @@ -20,12 +20,10 @@ //-- // Third Party Headers: -#if !defined(_MSC_VER) +#if defined(__APPLE__) #include -#include -#include -#include -#endif // !defined( _MSC_VER ) +#include // For STDIN_FILENO +#endif // defined( __APPLE__ ) #include // For std::strerror() // In-house headers: @@ -155,20 +153,27 @@ CMICmnStreamStdinLinux::Shutdown(void) bool CMICmnStreamStdinLinux::InputAvailable(bool &vwbAvail) { -#if !defined(_WIN32) +#if defined(__APPLE__) // The code below is needed on OSX where lldb-mi hangs when doing -exec-run. // The hang seems to come from calling fgets and fileno from different thread. // Although this problem was not observed on Linux. - // A solution based on 'select' was also proposed but it seems to slow things down - // a lot. - int nBytesWaiting; - if (::ioctl(STDIN_FILENO, FIONREAD, &nBytesWaiting) == -1) + // A solution based on 'ioctl' was initially committed but it seems to make + // lldb-mi takes much more processor time. The solution based on 'select' works + // well but it seems to slow the execution of lldb-mi tests a lot on Linux. + // As a result, this code is #defined to run only on OSX. + fd_set setOfStdin; + FD_ZERO(&setOfStdin); + FD_SET(STDIN_FILENO, &setOfStdin); + + // Wait while input would be available + if (::select(STDIN_FILENO + 1, &setOfStdin, nullptr, nullptr, nullptr) == -1) { vwbAvail = false; - return MIstatus::failure;; + return MIstatus::failure; } - vwbAvail = (nBytesWaiting > 0); -#endif // !defined( _WIN32 ) + +#endif // defined( __APPLE__ ) + vwbAvail = true; return MIstatus::success; } @@ -206,3 +211,15 @@ CMICmnStreamStdinLinux::ReadLine(CMIUtilString &vwErrMsg) return pText; } +//++ ------------------------------------------------------------------------------------ +// Details: Interrupt current and prevent new ReadLine operations. +// Type: Method. +// Args: None. +// Return: None. +// Throws: None. +//-- +void +CMICmnStreamStdinLinux::InterruptReadLine(void) +{ + fclose(stdin); +} diff --git a/lldb/tools/lldb-mi/MICmnStreamStdinLinux.h b/lldb/tools/lldb-mi/MICmnStreamStdinLinux.h index 3471722..edac940 100644 --- a/lldb/tools/lldb-mi/MICmnStreamStdinLinux.h +++ b/lldb/tools/lldb-mi/MICmnStreamStdinLinux.h @@ -51,6 +51,7 @@ class CMICmnStreamStdinLinux : public CMICmnBase, public CMICmnStreamStdin::IOSS // From CMICmnStreamStdin::IOSpecificReadStreamStdin virtual bool InputAvailable(bool &vwbAvail); virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg); + virtual void InterruptReadLine(void); // Methods: private: diff --git a/lldb/tools/lldb-mi/MIDriver.cpp b/lldb/tools/lldb-mi/MIDriver.cpp index a16c62f..accde1c 100644 --- a/lldb/tools/lldb-mi/MIDriver.cpp +++ b/lldb/tools/lldb-mi/MIDriver.cpp @@ -1075,6 +1075,7 @@ CMIDriver::SetExitApplicationFlag(const bool vbForceExit) { CMIUtilThreadLock lock(m_threadMutex); m_bExitApp = true; + m_rStdin.OnExitHandler(); return; } @@ -1089,6 +1090,7 @@ CMIDriver::SetExitApplicationFlag(const bool vbForceExit) } m_bExitApp = true; + m_rStdin.OnExitHandler(); } //++ ------------------------------------------------------------------------------------ -- 2.7.4