[netcore] Implement missing ThreadPool methods (for metrics) (mono/mono#16076)
authorEgor Bogatov <egorbo@gmail.com>
Mon, 26 Aug 2019 15:35:38 +0000 (18:35 +0300)
committerRyan Lucia <rylucia@microsoft.com>
Mon, 26 Aug 2019 15:35:38 +0000 (11:35 -0400)
* Implement missing ThreadPool methods

* Address feedback

* ifdef for mono_threadpool_worker_get_completed_threads_count

* remove GetPendingUnmanagedWorkItemCount

Commit migrated from https://github.com/mono/mono/commit/85265d884cf31f8779daba2d639eb795fbc9bd5e

src/mono/mono/metadata/icall-def-netcore.h
src/mono/mono/metadata/threadpool-worker-default.c
src/mono/mono/metadata/threadpool-worker-wasm.c
src/mono/mono/metadata/threadpool-worker.h
src/mono/mono/metadata/threadpool.c
src/mono/netcore/CoreFX.issues.rsp
src/mono/netcore/System.Private.CoreLib/src/System.Threading/ThreadPool.cs

index 9b5ecff..d3c2570 100644 (file)
@@ -519,8 +519,10 @@ NOHANDLES(ICALL(THREAD_14, "YieldInternal", ves_icall_System_Threading_Thread_Yi
 
 ICALL_TYPE(THREADP, "System.Threading.ThreadPool", THREADP_2)
 HANDLES(THREADP_2, "GetAvailableThreadsNative", ves_icall_System_Threading_ThreadPool_GetAvailableThreadsNative, void, 2, (gint32_ref, gint32_ref))
+HANDLES(THREADP_2a, "GetCompletedWorkItemCount", ves_icall_System_Threading_ThreadPool_GetCompletedWorkItemCount, gint64, 0, ())
 HANDLES(THREADP_3, "GetMaxThreadsNative", ves_icall_System_Threading_ThreadPool_GetMaxThreadsNative, void, 2, (gint32_ref, gint32_ref))
 HANDLES(THREADP_4, "GetMinThreadsNative", ves_icall_System_Threading_ThreadPool_GetMinThreadsNative, void, 2, (gint32_ref, gint32_ref))
+HANDLES(THREADP_4a, "GetThreadCount", ves_icall_System_Threading_ThreadPool_GetThreadCount, gint32, 0, ())
 HANDLES(THREADP_5, "InitializeVMTp", ves_icall_System_Threading_ThreadPool_InitializeVMTp, void, 1, (MonoBoolean_ref))
 HANDLES(THREADP_7, "NotifyWorkItemComplete", ves_icall_System_Threading_ThreadPool_NotifyWorkItemComplete, MonoBoolean, 0, ())
 HANDLES(THREADP_8, "NotifyWorkItemProgressNative", ves_icall_System_Threading_ThreadPool_NotifyWorkItemProgressNative, void, 0, ())
index 0638125..aa9513e 100644 (file)
@@ -349,6 +349,19 @@ mono_threadpool_worker_request (void)
        mono_refcount_dec (&worker);
 }
 
+#ifdef ENABLE_NETCORE
+gint64 mono_threadpool_worker_get_completed_threads_count (void)
+{
+       return worker.heuristic_completions;
+}
+
+gint32 mono_threadpool_worker_get_threads_count (void)
+{
+       ThreadPoolWorkerCounter const counter = COUNTER_READ ();
+       return counter._.working;
+}
+#endif
+
 /* return TRUE if timeout, FALSE otherwise (worker unpark or interrupt) */
 static gboolean
 worker_park (void)
index 8877df9..9eaa348 100644 (file)
@@ -75,3 +75,15 @@ mono_threadpool_worker_notify_completed (void)
 {
        return FALSE;
 }
+
+#ifdef ENABLE_NETCORE
+gint64 mono_threadpool_worker_get_completed_threads_count (void)
+{
+       return 0;
+}
+
+gint32 mono_threadpool_worker_get_threads_count (void)
+{
+       return 0;
+}
+#endif
index 9b2c51d..99bf52e 100644 (file)
@@ -21,6 +21,14 @@ mono_threadpool_worker_request (void);
 gboolean
 mono_threadpool_worker_notify_completed (void);
 
+#ifdef ENABLE_NETCORE
+gint64
+mono_threadpool_worker_get_completed_threads_count (void);
+
+gint32
+mono_threadpool_worker_get_threads_count (void);
+#endif
+
 gint32
 mono_threadpool_worker_get_min (void);
 gboolean
index c9931a2..47a8dae 100644 (file)
@@ -725,6 +725,20 @@ ves_icall_System_Threading_ThreadPool_SetMaxThreadsNative (gint32 worker_threads
        return TRUE;
 }
 
+#ifdef ENABLE_NETCORE
+gint32
+ves_icall_System_Threading_ThreadPool_GetThreadCount (MonoError *error)
+{
+       return mono_threadpool_worker_get_threads_count ();
+}
+
+gint64
+ves_icall_System_Threading_ThreadPool_GetCompletedWorkItemCount (MonoError *error)
+{
+       return mono_threadpool_worker_get_completed_threads_count ();
+}
+#endif
+
 void
 ves_icall_System_Threading_ThreadPool_InitializeVMTp (MonoBoolean *enable_worker_tracking, MonoError *error)
 {
index 3dc9a0c..4321cce 100644 (file)
 # Explicitly skipped if it's MonoVM (SkipOnTargetFramework)
 -nomethod System.Threading.ThreadPools.Tests.ThreadPoolTests.SetMinMaxThreadsTest_ChangedInDotNetCore
 
-# ThreadPool.CompletedWorkItemCount is not implemented
-# https://github.com/mono/mono/issues/14829
--nomethod System.Threading.ThreadPools.Tests.ThreadPoolTests.MetricsTest
-
 ####################################################################
 ##  System.ComponentModel.Composition.Tests
 ####################################################################
index 7e411bc..96e0460 100644 (file)
@@ -3,6 +3,7 @@
 // See the LICENSE file in the project root for more information.
 
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
 
 namespace System.Threading
 {
@@ -73,6 +74,13 @@ namespace System.Threading
                [MethodImplAttribute (MethodImplOptions.InternalCall)]
                static extern void GetAvailableThreadsNative (out int workerThreads, out int completionPortThreads);
 
+               [MethodImpl(MethodImplOptions.InternalCall)]
+               static extern long GetCompletedWorkItemCount ();
+
+               [MethodImpl(MethodImplOptions.InternalCall)]
+               static extern int GetThreadCount ();
+               
+
                public static bool SetMaxThreads (int workerThreads, int completionPortThreads)
                {
                        return SetMaxThreadsNative (workerThreads, completionPortThreads);
@@ -106,9 +114,9 @@ namespace System.Threading
 
                static long PendingUnmanagedWorkItemCount => 0;
                
-               public static long CompletedWorkItemCount => throw new PlatformNotSupportedException ();
+               public static long CompletedWorkItemCount => GetCompletedWorkItemCount ();
 
-               public static int ThreadCount => throw new NotImplementedException ();
+               public static int ThreadCount => GetThreadCount ();
        }
 
        internal static class _ThreadPoolWaitCallback