Limit thread name appropriately
authorutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 Jan 2017 02:53:00 +0000 (02:53 +0000)
committerutatane.tea@gmail.com <utatane.tea@gmail.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 5 Jan 2017 02:53:00 +0000 (02:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=166676

Reviewed by Sam Weinig.

Source/WTF:

In some platform, the max length of thread names are limited.
For example, the number of the max length is 32 in Windows and
16 in Linux. But the specified thread name is typically long
in WebKit like "com.apple.CoreIPC.ReceiveQueue"

We port the logic substring the thread name in
generic/WorkQueueGeneric.cpp to Threading. It retrieves the name
"ReceiveQueue" from "com.apple.CoreIPC.ReceiveQueue". And apply
the appropriate the thread name limit and use it on Linux and
Windows environment.

* wtf/Threading.cpp:
(WTF::normalizeThreadName):
(WTF::createThread):
* wtf/Threading.h:
* wtf/ThreadingPthreads.cpp:
(WTF::initializeCurrentThreadInternal):
* wtf/ThreadingWin.cpp:
(WTF::initializeCurrentThreadInternal):
* wtf/generic/WorkQueueGeneric.cpp:
(WorkQueue::platformInitialize):
* wtf/text/StringView.h:
(WTF::StringView::left):
(WTF::StringView::right):
(WTF::StringView::reverseFind):

Tools:

* TestWebKitAPI/Tests/WTF/StringView.cpp:
(TestWebKitAPI::TEST):
* TestWebKitAPI/Tests/WTF/WTFString.cpp:
(TestWebKitAPI::TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@210313 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/Threading.cpp
Source/WTF/wtf/Threading.h
Source/WTF/wtf/ThreadingPthreads.cpp
Source/WTF/wtf/ThreadingWin.cpp
Source/WTF/wtf/generic/WorkQueueGeneric.cpp
Source/WTF/wtf/text/StringView.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WTF/StringView.cpp
Tools/TestWebKitAPI/Tests/WTF/WTFString.cpp

index 49079ad..48f59cc 100644 (file)
@@ -1,3 +1,36 @@
+2017-01-04  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        Limit thread name appropriately
+        https://bugs.webkit.org/show_bug.cgi?id=166676
+
+        Reviewed by Sam Weinig.
+
+        In some platform, the max length of thread names are limited.
+        For example, the number of the max length is 32 in Windows and
+        16 in Linux. But the specified thread name is typically long
+        in WebKit like "com.apple.CoreIPC.ReceiveQueue"
+
+        We port the logic substring the thread name in
+        generic/WorkQueueGeneric.cpp to Threading. It retrieves the name
+        "ReceiveQueue" from "com.apple.CoreIPC.ReceiveQueue". And apply
+        the appropriate the thread name limit and use it on Linux and
+        Windows environment.
+
+        * wtf/Threading.cpp:
+        (WTF::normalizeThreadName):
+        (WTF::createThread):
+        * wtf/Threading.h:
+        * wtf/ThreadingPthreads.cpp:
+        (WTF::initializeCurrentThreadInternal):
+        * wtf/ThreadingWin.cpp:
+        (WTF::initializeCurrentThreadInternal):
+        * wtf/generic/WorkQueueGeneric.cpp:
+        (WorkQueue::platformInitialize):
+        * wtf/text/StringView.h:
+        (WTF::StringView::left):
+        (WTF::StringView::right):
+        (WTF::StringView::reverseFind):
+
 2017-01-04  Sam Weinig  <sam@webkit.org>
 
         REGRESSION (r210257): com.apple.WebKit.WebContent.Development crashed in com.apple.WebCore: WebCore::ExceptionOr<WTF::Ref<WebCore::Database> >::operator= + 14
index 2cc5979..40ce39a 100644 (file)
@@ -29,6 +29,7 @@
 #include <algorithm>
 #include <cmath>
 #include <cstring>
+#include <wtf/text/StringView.h>
 
 namespace WTF {
 
@@ -40,6 +41,33 @@ public:
     Mutex creationMutex;
 };
 
+const char* normalizeThreadName(const char* threadName)
+{
+#if HAVE(PTHREAD_SETNAME_NP)
+    return threadName;
+#else
+    // This name can be com.apple.WebKit.ProcessLauncher or com.apple.CoreIPC.ReceiveQueue.
+    // We are using those names for the thread name, but both are longer than the limit of
+    // the platform thread name length, 32 for Windows and 16 for Linux.
+    StringView result(threadName);
+    size_t size = result.reverseFind('.');
+    if (size != notFound)
+        result = result.substring(size + 1);
+
+#if OS(WINDOWS)
+    constexpr const size_t kVisualStudioThreadNameLimit = 32 - 1;
+    if (result.length() > kVisualStudioThreadNameLimit)
+        result = result.right(kVisualStudioThreadNameLimit);
+#elif OS(LINUX)
+    constexpr const size_t kLinuxThreadNameLimit = 16 - 1;
+    if (result.length() > kLinuxThreadNameLimit)
+        result = result.right(kLinuxThreadNameLimit);
+#endif
+    ASSERT(result.characters8()[result.length()] == '\0');
+    return reinterpret_cast<const char*>(result.characters8());
+#endif
+}
+
 static void threadEntryPoint(void* contextData)
 {
     NewThreadContext* context = static_cast<NewThreadContext*>(contextData);
@@ -62,13 +90,6 @@ static void threadEntryPoint(void* contextData)
 
 ThreadIdentifier createThread(const char* name, std::function<void()> entryPoint)
 {
-    // Visual Studio has a 31-character limit on thread names. Longer names will
-    // be truncated silently, but we'd like callers to know about the limit.
-#if !LOG_DISABLED && PLATFORM(WIN)
-    if (name && strlen(name) > 31)
-        LOG_ERROR("Thread name \"%s\" is longer than 31 characters and will be truncated by Visual Studio", name);
-#endif
-
     NewThreadContext* context = new NewThreadContext { name, WTFMove(entryPoint), { } };
 
     // Prevent the thread body from executing until we've established the thread identifier.
index 31b32ce..3050dd8 100644 (file)
@@ -77,6 +77,8 @@ ThreadIdentifier createThreadInternal(ThreadFunction, void*, const char* threadN
 // Helpful for platforms where the thread name must be set from within the thread.
 void initializeCurrentThreadInternal(const char* threadName);
 
+const char* normalizeThreadName(const char* threadName);
+
 } // namespace WTF
 
 using WTF::ThreadIdentifier;
index be1d826..0f9f2fc 100644 (file)
@@ -195,9 +195,9 @@ ThreadIdentifier createThreadInternal(ThreadFunction entryPoint, void* data, con
 void initializeCurrentThreadInternal(const char* threadName)
 {
 #if HAVE(PTHREAD_SETNAME_NP)
-    pthread_setname_np(threadName);
+    pthread_setname_np(normalizeThreadName(threadName));
 #elif OS(LINUX)
-    prctl(PR_SET_NAME, threadName);
+    prctl(PR_SET_NAME, normalizeThreadName(threadName));
 #else
     UNUSED_PARAM(threadName);
 #endif
index 068646d..54b43ef 100644 (file)
@@ -129,7 +129,7 @@ void initializeCurrentThreadInternal(const char* szThreadName)
 #else
     THREADNAME_INFO info;
     info.dwType = 0x1000;
-    info.szName = szThreadName;
+    info.szName = normalizeThreadName(szThreadName);
     info.dwThreadID = GetCurrentThreadId();
     info.dwFlags = 0;
 
index 4eeb302..f594e24 100644 (file)
 
 #include <wtf/text/WTFString.h>
 
-static const size_t kVisualStudioThreadNameLimit = 31;
-
 void WorkQueue::platformInitialize(const char* name, Type, QOS)
 {
-    // This name can be com.apple.WebKit.ProcessLauncher or com.apple.CoreIPC.ReceiveQueue.
-    // We are using those names for the thread name, but both are longer than 31 characters,
-    // which is the limit of Visual Studio for thread names.
-    // When log is enabled createThread() will assert instead of truncate the name, so we need
-    // to make sure we don't use a name longer than 31 characters.
-    String threadName(name);
-    size_t size = threadName.reverseFind('.');
-    if (size != notFound)
-        threadName = threadName.substring(size + 1);
-    if (threadName.length() > kVisualStudioThreadNameLimit)
-        threadName = threadName.right(kVisualStudioThreadNameLimit);
-
     LockHolder locker(m_initializeRunLoopConditionMutex);
-    m_workQueueThread = createThread(threadName.ascii().data(), [this] {
+    m_workQueueThread = createThread(name, [this] {
         {
             LockHolder locker(m_initializeRunLoopConditionMutex);
             m_runLoop = &RunLoop::current();
index a6945a5..00b521f 100644 (file)
@@ -23,9 +23,9 @@
  * THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef StringView_h
-#define StringView_h
+#pragma once
 
+#include <limits.h>
 #include <unicode/utypes.h>
 #include <wtf/Forward.h>
 #include <wtf/RetainPtr.h>
@@ -114,6 +114,8 @@ public:
     void getCharactersWithUpconvert(UChar*) const;
 
     StringView substring(unsigned start, unsigned length = std::numeric_limits<unsigned>::max()) const;
+    StringView left(unsigned len) const { return substring(0, len); }
+    StringView right(unsigned len) const { return substring(length() - len, len); }
     WTF_EXPORT_STRING_API Vector<StringView> split(UChar);
 
     size_t find(UChar, unsigned start = 0) const;
@@ -121,6 +123,8 @@ public:
 
     WTF_EXPORT_STRING_API size_t find(StringView, unsigned start) const;
 
+    size_t reverseFind(UChar, unsigned index = UINT_MAX) const;
+
     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&) const;
     WTF_EXPORT_STRING_API size_t findIgnoringASCIICase(const StringView&, unsigned startOffset) const;
 
@@ -531,6 +535,13 @@ inline size_t StringView::find(CharacterMatchFunction matchFunction, unsigned st
     return WTF::find(characters16(), m_length, matchFunction, start);
 }
 
+inline size_t StringView::reverseFind(UChar character, unsigned index) const
+{
+    if (is8Bit())
+        return WTF::reverseFind(characters8(), m_length, character, index);
+    return WTF::reverseFind(characters16(), m_length, character, index);
+}
+
 #if !CHECK_STRINGVIEW_LIFETIME
 inline void StringView::invalidate(const StringImpl&)
 {
@@ -837,5 +848,3 @@ template<unsigned length> inline bool equalLettersIgnoringASCIICase(StringView s
 using WTF::append;
 using WTF::equal;
 using WTF::StringView;
-
-#endif // StringView_h
index 2aecae1..3d047a1 100644 (file)
@@ -1,3 +1,15 @@
+2017-01-04  Yusuke Suzuki  <utatane.tea@gmail.com>
+
+        Limit thread name appropriately
+        https://bugs.webkit.org/show_bug.cgi?id=166676
+
+        Reviewed by Sam Weinig.
+
+        * TestWebKitAPI/Tests/WTF/StringView.cpp:
+        (TestWebKitAPI::TEST):
+        * TestWebKitAPI/Tests/WTF/WTFString.cpp:
+        (TestWebKitAPI::TEST):
+
 2017-01-04  Andy Estes  <aestes@apple.com>
 
         [Cocoa] Teach SharedBuffer to return an NSArray of data segments to avoid flattening
index 39ea9be..f7f8188 100644 (file)
@@ -804,4 +804,52 @@ TEST(WTF, StringView8Bit)
     EXPECT_TRUE(StringView(emptyString().impl()).is8Bit());
 }
 
+TEST(WTF, StringViewRightBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_TRUE(reference.right(0) == stringViewFromLiteral(""));
+    EXPECT_TRUE(reference.right(1) == stringViewFromLiteral("o"));
+    EXPECT_TRUE(reference.right(2) == stringViewFromLiteral("no"));
+    EXPECT_TRUE(reference.right(3) == stringViewFromLiteral("ino"));
+    EXPECT_TRUE(reference.right(4) == stringViewFromLiteral("cino"));
+    EXPECT_TRUE(reference.right(5) == stringViewFromLiteral("ccino"));
+    EXPECT_TRUE(reference.right(6) == stringViewFromLiteral("uccino"));
+    EXPECT_TRUE(reference.right(7) == stringViewFromLiteral("puccino"));
+    EXPECT_TRUE(reference.right(8) == stringViewFromLiteral("ppuccino"));
+    EXPECT_TRUE(reference.right(9) == stringViewFromLiteral("appuccino"));
+    EXPECT_TRUE(reference.right(10) == stringViewFromLiteral("Cappuccino"));
+}
+
+TEST(WTF, StringViewLeftBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_TRUE(reference.left(0) == stringViewFromLiteral(""));
+    EXPECT_TRUE(reference.left(1) == stringViewFromLiteral("C"));
+    EXPECT_TRUE(reference.left(2) == stringViewFromLiteral("Ca"));
+    EXPECT_TRUE(reference.left(3) == stringViewFromLiteral("Cap"));
+    EXPECT_TRUE(reference.left(4) == stringViewFromLiteral("Capp"));
+    EXPECT_TRUE(reference.left(5) == stringViewFromLiteral("Cappu"));
+    EXPECT_TRUE(reference.left(6) == stringViewFromLiteral("Cappuc"));
+    EXPECT_TRUE(reference.left(7) == stringViewFromLiteral("Cappucc"));
+    EXPECT_TRUE(reference.left(8) == stringViewFromLiteral("Cappucci"));
+    EXPECT_TRUE(reference.left(9) == stringViewFromLiteral("Cappuccin"));
+    EXPECT_TRUE(reference.left(10) == stringViewFromLiteral("Cappuccino"));
+}
+
+TEST(WTF, StringViewReverseFindBasic)
+{
+    auto reference = stringViewFromLiteral("Cappuccino");
+    EXPECT_EQ(reference.reverseFind('o'), 9);
+    EXPECT_EQ(reference.reverseFind('n'), 8);
+    EXPECT_EQ(reference.reverseFind('c'), 6);
+    EXPECT_EQ(reference.reverseFind('p'), 3);
+    EXPECT_EQ(reference.reverseFind('k'), notFound);
+
+    EXPECT_EQ(reference.reverseFind('o', 8), notFound);
+    EXPECT_EQ(reference.reverseFind('c', 8), 6);
+    EXPECT_EQ(reference.reverseFind('c', 6), 6);
+    EXPECT_EQ(reference.reverseFind('c', 5), 5);
+    EXPECT_EQ(reference.reverseFind('c', 4), notFound);
+}
+
 } // namespace TestWebKitAPI
index 1d4e999..e2222dd 100644 (file)
@@ -313,4 +313,52 @@ TEST(WTF, StringUnicodeEqualUCharArray)
     ASSERT_FALSE(equal(string2, aBc));
 }
 
+TEST(WTF, StringRightBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_TRUE(reference.right(0) == String::fromUTF8(""));
+    EXPECT_TRUE(reference.right(1) == String::fromUTF8("o"));
+    EXPECT_TRUE(reference.right(2) == String::fromUTF8("no"));
+    EXPECT_TRUE(reference.right(3) == String::fromUTF8("ino"));
+    EXPECT_TRUE(reference.right(4) == String::fromUTF8("cino"));
+    EXPECT_TRUE(reference.right(5) == String::fromUTF8("ccino"));
+    EXPECT_TRUE(reference.right(6) == String::fromUTF8("uccino"));
+    EXPECT_TRUE(reference.right(7) == String::fromUTF8("puccino"));
+    EXPECT_TRUE(reference.right(8) == String::fromUTF8("ppuccino"));
+    EXPECT_TRUE(reference.right(9) == String::fromUTF8("appuccino"));
+    EXPECT_TRUE(reference.right(10) == String::fromUTF8("Cappuccino"));
+}
+
+TEST(WTF, StringLeftBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_TRUE(reference.left(0) == String::fromUTF8(""));
+    EXPECT_TRUE(reference.left(1) == String::fromUTF8("C"));
+    EXPECT_TRUE(reference.left(2) == String::fromUTF8("Ca"));
+    EXPECT_TRUE(reference.left(3) == String::fromUTF8("Cap"));
+    EXPECT_TRUE(reference.left(4) == String::fromUTF8("Capp"));
+    EXPECT_TRUE(reference.left(5) == String::fromUTF8("Cappu"));
+    EXPECT_TRUE(reference.left(6) == String::fromUTF8("Cappuc"));
+    EXPECT_TRUE(reference.left(7) == String::fromUTF8("Cappucc"));
+    EXPECT_TRUE(reference.left(8) == String::fromUTF8("Cappucci"));
+    EXPECT_TRUE(reference.left(9) == String::fromUTF8("Cappuccin"));
+    EXPECT_TRUE(reference.left(10) == String::fromUTF8("Cappuccino"));
+}
+
+TEST(WTF, StringReverseFindBasic)
+{
+    auto reference = String::fromUTF8("Cappuccino");
+    EXPECT_EQ(reference.reverseFind('o'), 9);
+    EXPECT_EQ(reference.reverseFind('n'), 8);
+    EXPECT_EQ(reference.reverseFind('c'), 6);
+    EXPECT_EQ(reference.reverseFind('p'), 3);
+    EXPECT_EQ(reference.reverseFind('k'), notFound);
+
+    EXPECT_EQ(reference.reverseFind('o', 8), notFound);
+    EXPECT_EQ(reference.reverseFind('c', 8), 6);
+    EXPECT_EQ(reference.reverseFind('c', 6), 6);
+    EXPECT_EQ(reference.reverseFind('c', 5), 5);
+    EXPECT_EQ(reference.reverseFind('c', 4), notFound);
+}
+
 } // namespace TestWebKitAPI