[Lldb-commits] [lldb] r224258 - Replace ioctl with select to reduce processor usage by lldb-mi on OSX.

Hafiz Abid Qadeer hafiz_abid at mentor.com
Mon Dec 15 11:09:40 PST 2014


Author: abidh
Date: Mon Dec 15 13:09:40 2014
New Revision: 224258

URL: http://llvm.org/viewvc/llvm-project?rev=224258&view=rev
Log:
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 <ki.stfu at gmail.com>. A few changes were made by me. 


Modified:
    lldb/trunk/tools/lldb-mi/MICmnStreamStdin.cpp
    lldb/trunk/tools/lldb-mi/MICmnStreamStdin.h
    lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.cpp
    lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.h
    lldb/trunk/tools/lldb-mi/MIDriver.cpp

Modified: lldb/trunk/tools/lldb-mi/MICmnStreamStdin.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnStreamStdin.cpp?rev=224258&r1=224257&r2=224258&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnStreamStdin.cpp (original)
+++ lldb/trunk/tools/lldb-mi/MICmnStreamStdin.cpp Mon Dec 15 13:09:40 2014
@@ -435,3 +435,15 @@ CMICmnStreamStdin::SetOSStdinHandler(IOS
     return MIstatus::success;
 }
 
+//++ ------------------------------------------------------------------------------------
+// Details: Do some actions before exiting.
+// Type:    Method.
+// Args:    None.
+// Return:  None.
+// Throws:  None.
+//--
+void
+CMICmnStreamStdin::OnExitHandler(void)
+{
+    m_pStdinReadHandler->InterruptReadLine();
+}

Modified: lldb/trunk/tools/lldb-mi/MICmnStreamStdin.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnStreamStdin.h?rev=224258&r1=224257&r2=224258&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnStreamStdin.h (original)
+++ lldb/trunk/tools/lldb-mi/MICmnStreamStdin.h Mon Dec 15 13:09:40 2014
@@ -66,6 +66,7 @@ class CMICmnStreamStdin : public CMICmnB
       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 CMICmnB
     void SetCtrlCHit(void);
     bool SetVisitor(IStreamStdin &vrVisitor);
     bool SetOSStdinHandler(IOSStdinHandler &vrHandler);
+    void OnExitHandler(void);
 
     // Overridden:
   public:

Modified: lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.cpp?rev=224258&r1=224257&r2=224258&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.cpp (original)
+++ lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.cpp Mon Dec 15 13:09:40 2014
@@ -20,12 +20,10 @@
 //--
 
 // Third Party Headers:
-#if !defined(_MSC_VER)
+#if defined(__APPLE__)
 #include <sys/select.h>
-#include <unistd.h>
-#include <termios.h>
-#include <sys/ioctl.h>
-#endif              // !defined( _MSC_VER )
+#include <unistd.h> // For STDIN_FILENO
+#endif              // defined( __APPLE__ )
 #include <string.h> // 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(CMIUtil
     return pText;
 }
 
+//++ ------------------------------------------------------------------------------------
+// Details: Interrupt current and prevent new ReadLine operations.
+// Type:    Method.
+// Args:    None.
+// Return:  None.
+// Throws:  None.
+//--
+void
+CMICmnStreamStdinLinux::InterruptReadLine(void)
+{
+    fclose(stdin);
+}

Modified: lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.h?rev=224258&r1=224257&r2=224258&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.h (original)
+++ lldb/trunk/tools/lldb-mi/MICmnStreamStdinLinux.h Mon Dec 15 13:09:40 2014
@@ -51,6 +51,7 @@ class CMICmnStreamStdinLinux : public CM
     // From CMICmnStreamStdin::IOSpecificReadStreamStdin
     virtual bool InputAvailable(bool &vwbAvail);
     virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg);
+    virtual void InterruptReadLine(void);
 
     // Methods:
   private:

Modified: lldb/trunk/tools/lldb-mi/MIDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/tools/lldb-mi/MIDriver.cpp?rev=224258&r1=224257&r2=224258&view=diff
==============================================================================
--- lldb/trunk/tools/lldb-mi/MIDriver.cpp (original)
+++ lldb/trunk/tools/lldb-mi/MIDriver.cpp Mon Dec 15 13:09:40 2014
@@ -1075,6 +1075,7 @@ CMIDriver::SetExitApplicationFlag(const
     {
         CMIUtilThreadLock lock(m_threadMutex);
         m_bExitApp = true;
+        m_rStdin.OnExitHandler();
         return;
     }
 
@@ -1089,6 +1090,7 @@ CMIDriver::SetExitApplicationFlag(const
     }
 
     m_bExitApp = true;
+    m_rStdin.OnExitHandler();
 }
 
 //++ ------------------------------------------------------------------------------------





More information about the lldb-commits mailing list