Replace ioctl with select to reduce processor usage by lldb-mi on OSX.
authorHafiz Abid Qadeer <hafiz_abid@mentor.com>
Mon, 15 Dec 2014 19:09:40 +0000 (19:09 +0000)
committerHafiz Abid Qadeer <hafiz_abid@mentor.com>
Mon, 15 Dec 2014 19:09:40 +0000 (19:09 +0000)
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@gmail.com>. A few changes were made by me.

llvm-svn: 224258

lldb/tools/lldb-mi/MICmnStreamStdin.cpp
lldb/tools/lldb-mi/MICmnStreamStdin.h
lldb/tools/lldb-mi/MICmnStreamStdinLinux.cpp
lldb/tools/lldb-mi/MICmnStreamStdinLinux.h
lldb/tools/lldb-mi/MIDriver.cpp

index 8dccf16..2d54921 100644 (file)
@@ -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();
+}
index 7b31a64..a6779d5 100644 (file)
@@ -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:
index ef9030b..07e652d 100644 (file)
 //--
 
 // 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(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);
+}
index 3471722..edac940 100644 (file)
@@ -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:
index a16c62f..accde1c 100644 (file)
@@ -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();
 }
 
 //++ ------------------------------------------------------------------------------------