Port WTF to OS(FUCHSIA)
authorabarth@webkit.org <abarth@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 30 Jun 2018 20:53:56 +0000 (20:53 +0000)
committerabarth@webkit.org <abarth@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 30 Jun 2018 20:53:56 +0000 (20:53 +0000)
https://bugs.webkit.org/show_bug.cgi?id=187221

Reviewed by Yusuke Suzuki.

* wtf/FastMalloc.cpp: Fuchsia does not have resource.h
(WTF::fastMallocStatistics): Skip collecting stats without resource.h
* wtf/InlineASM.h: Fuchsia uses ELF
* wtf/Platform.h: Define OS(FUCHSIA) as an OS(UNIX) variant
* wtf/RandomDevice.cpp: Call zx_cprng_draw for crypographic randomness
(WTF::RandomDevice::cryptographicallyRandomValues):
* wtf/ThreadingPthreads.cpp: Fuchsia does not have pthread_setschedparam
(WTF::Thread::changePriority):
* wtf/unix/CPUTimeFuchsia.cpp: Added.

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

Source/WTF/ChangeLog
Source/WTF/wtf/CurrentTime.cpp
Source/WTF/wtf/FastMalloc.cpp
Source/WTF/wtf/InlineASM.h
Source/WTF/wtf/Platform.h
Source/WTF/wtf/PlatformJSCOnly.cmake
Source/WTF/wtf/RandomDevice.cpp
Source/WTF/wtf/RandomDevice.h
Source/WTF/wtf/ThreadingPthreads.cpp
Source/WTF/wtf/fuchsia/CPUTimeFuchsia.cpp [new file with mode: 0644]

index bba3f3b..0298cb4 100644 (file)
@@ -1,3 +1,20 @@
+2018-06-30  Adam Barth  <abarth@webkit.org>
+
+        Port WTF to OS(FUCHSIA)
+        https://bugs.webkit.org/show_bug.cgi?id=187221
+
+        Reviewed by Yusuke Suzuki.
+
+        * wtf/FastMalloc.cpp: Fuchsia does not have resource.h
+        (WTF::fastMallocStatistics): Skip collecting stats without resource.h
+        * wtf/InlineASM.h: Fuchsia uses ELF
+        * wtf/Platform.h: Define OS(FUCHSIA) as an OS(UNIX) variant
+        * wtf/RandomDevice.cpp: Call zx_cprng_draw for crypographic randomness
+        (WTF::RandomDevice::cryptographicallyRandomValues):
+        * wtf/ThreadingPthreads.cpp: Fuchsia does not have pthread_setschedparam
+        (WTF::Thread::changePriority):
+        * wtf/unix/CPUTimeFuchsia.cpp: Added.
+
 2018-06-30  Commit Queue  <commit-queue@webkit.org>
 
         Unreviewed, rolling out r233391.
index 7932dbf..e32a64b 100644 (file)
 #include <sys/time.h>
 #endif
 
+#if OS(FUCHSIA)
+#include <zircon/syscalls.h>
+#endif
+
 #if USE(GLIB)
 #include <glib.h>
 #endif
@@ -261,6 +265,8 @@ MonotonicTime MonotonicTime::now()
     });
 
     return fromRawSeconds((mach_absolute_time() * timebaseInfo.numer) / (1.0e9 * timebaseInfo.denom));
+#elif OS(FUCHSIA)
+    return fromRawSeconds(zx_clock_get_monotonic() / static_cast<double>(ZX_SEC(1)));
 #elif OS(LINUX) || OS(FREEBSD) || OS(OPENBSD) || OS(NETBSD)
     struct timespec ts { };
     clock_gettime(CLOCK_MONOTONIC, &ts);
index 74d1b56..b5b8157 100644 (file)
@@ -35,7 +35,9 @@
 #include <windows.h>
 #else
 #include <pthread.h>
+#if HAVE(RESOURCE_H)
 #include <sys/resource.h>
+#endif // HAVE(RESOURCE_H)
 #endif
 
 #if OS(DARWIN)
@@ -361,7 +363,7 @@ FastMallocStatistics fastMallocStatistics()
     PROCESS_MEMORY_COUNTERS resourceUsage;
     GetProcessMemoryInfo(GetCurrentProcess(), &resourceUsage, sizeof(resourceUsage));
     statistics.committedVMBytes = resourceUsage.PeakWorkingSetSize;
-#else
+#elif HAVE(RESOURCE_H)
     struct rusage resourceUsage;
     getrusage(RUSAGE_SELF, &resourceUsage);
 
index 603119b..4666a05 100644 (file)
 #elif OS(AIX)
     // IBM's own file format
 #define HIDE_SYMBOL(name) ".lglobl " #name
-#elif   OS(LINUX)               \
-     || OS(FREEBSD)             \
-     || OS(OPENBSD)             \
-     || OS(HPUX)                \
-     || OS(NETBSD)
+#elif  OS(LINUX)               \
+    || OS(FREEBSD)             \
+    || OS(FUCHSIA)             \
+    || OS(OPENBSD)             \
+    || OS(HPUX)                \
+    || OS(NETBSD)
     // ELF platform
 #define HIDE_SYMBOL(name) ".hidden " #name
 #else
@@ -80,8 +81,9 @@
 // Don't know about any of the others.
 #if OS(DARWIN)
 #define LOCAL_LABEL_STRING(name) "L" #name
-#elif   OS(LINUX)               \
+#elif  OS(LINUX)               \
     || OS(FREEBSD)             \
+    || OS(FUCHSIA)             \
     || OS(OPENBSD)             \
     || OS(HURD)                \
     || OS(NETBSD)              \
index 0281771..eedc2a5 100644 (file)
 #define WTF_OS_FREEBSD 1
 #endif
 
+/* OS(FUCHSIA) - Fuchsia */
+#ifdef __Fuchsia__
+#define WTF_OS_FUCHSIA 1
+#endif
+
 /* OS(HURD) - GNU/Hurd */
 #ifdef __GNU__
 #define WTF_OS_HURD 1
 #if    OS(AIX)              \
     || OS(DARWIN)           \
     || OS(FREEBSD)          \
+    || OS(FUCHSIA)          \
     || OS(HURD)             \
     || OS(LINUX)            \
     || OS(NETBSD)           \
 #define USE_PTHREADS 1
 #endif /* OS(UNIX) */
 
+#if OS(UNIX) && !OS(FUCHSIA)
+#define HAVE_RESOURCE_H 1
+#define HAVE_PTHREAD_SETSCHEDPARAM 1
+#endif
+
 #if OS(DARWIN)
 #define HAVE_DISPATCH_H 1
 #define HAVE_MADV_FREE 1
index f610c6e..525d9ee 100644 (file)
@@ -19,9 +19,18 @@ else ()
 
         text/unix/TextBreakIteratorInternalICUUnix.cpp
 
-        unix/CPUTimeUnix.cpp
         unix/LanguageUnix.cpp
     )
+    if (WTF_OS_FUCHSIA)
+        list(APPEND WTF_SOURCES
+            fuchsia/CPUTimeFuchsia.cpp
+        )
+    else ()
+        list(APPEND WTF_SOURCES
+            unix/CPUTimeUnix.cpp
+        )
+    endif ()
+
 endif ()
 
 if (WIN32)
index 61d6057..5ccb554 100644 (file)
@@ -30,7 +30,7 @@
 #include <stdint.h>
 #include <stdlib.h>
 
-#if !OS(DARWIN) && OS(UNIX)
+#if !OS(DARWIN) && !OS(FUCHSIA) && OS(UNIX)
 #include <errno.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include "CommonCryptoSPI.h"
 #endif
 
+#if OS(FUCHSIA)
+#include <zircon/syscalls.h>
+#endif
+
 namespace WTF {
 
-#if !OS(DARWIN) && OS(UNIX)
+#if !OS(DARWIN) && !OS(FUCHSIA) && OS(UNIX)
 NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToOpenURandom()
 {
     CRASH();
@@ -59,7 +63,7 @@ NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToReadFromURandom()
 }
 #endif
 
-#if !OS(DARWIN) && !OS(WINDOWS)
+#if !OS(DARWIN) && !OS(FUCHSIA) && !OS(WINDOWS)
 RandomDevice::RandomDevice()
 {
     int ret = 0;
@@ -72,7 +76,7 @@ RandomDevice::RandomDevice()
 }
 #endif
 
-#if !OS(DARWIN) && !OS(WINDOWS)
+#if !OS(DARWIN) && !OS(FUCHSIA) && !OS(WINDOWS)
 RandomDevice::~RandomDevice()
 {
     close(m_fd);
@@ -85,6 +89,8 @@ void RandomDevice::cryptographicallyRandomValues(unsigned char* buffer, size_t l
 {
 #if OS(DARWIN)
     RELEASE_ASSERT(!CCRandomCopyBytes(kCCRandomDefault, buffer, length));
+#elif OS(FUCHSIA)
+    zx_cprng_draw(buffer, length);
 #elif OS(UNIX)
     ssize_t amountRead = 0;
     while (static_cast<size_t>(amountRead) < length) {
index 86636c9..a593cac 100644 (file)
@@ -47,7 +47,7 @@ public:
     void cryptographicallyRandomValues(unsigned char* buffer, size_t length);
 
 private:
-#if OS(DARWIN) || OS(WINDOWS)
+#if OS(DARWIN) || OS(FUCHSIA) || OS(WINDOWS)
 #elif OS(UNIX)
     int m_fd { -1 };
 #else
index a3f428c..6aa83ff 100644 (file)
@@ -256,6 +256,7 @@ void Thread::initializeCurrentThreadInternal(const char* threadName)
 
 void Thread::changePriority(int delta)
 {
+#if HAVE(PTHREAD_SETSCHEDPARAM)
     auto locker = holdLock(m_mutex);
 
     int policy;
@@ -267,6 +268,7 @@ void Thread::changePriority(int delta)
     param.sched_priority += delta;
 
     pthread_setschedparam(m_handle, policy, &param);
+#endif
 }
 
 int Thread::waitForCompletion()
diff --git a/Source/WTF/wtf/fuchsia/CPUTimeFuchsia.cpp b/Source/WTF/wtf/fuchsia/CPUTimeFuchsia.cpp
new file mode 100644 (file)
index 0000000..6dd2f8e
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2018 Google, Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "CPUTime.h"
+
+#include <zircon/syscalls.h>
+
+namespace WTF {
+
+static Seconds timeToSeconds(zx_time_t t)
+{
+    return Seconds(t / static_cast<double>(ZX_SEC(1)));
+}
+
+std::optional<CPUTime> CPUTime::get()
+{
+    // Fuchsia issue ZX-2318 tracks being able to get the monotonic and thread
+    // times atomically and being able to separate ZX_CLOCK_THREAD into user and
+    // kernel time.
+    zx_time_t thread = zx_clock_get(ZX_CLOCK_THREAD);
+
+    return CPUTime { MonotonicTime::now(), timeToSeconds(thread), Seconds() };
+}
+
+Seconds CPUTime::forCurrentThread()
+{
+    return timeToSeconds(zx_clock_get(ZX_CLOCK_THREAD));
+}
+
+}