From 45251292590ef9e3ce0740e5521936a4ad2e3033 Mon Sep 17 00:00:00 2001 From: Andrew Christian Date: Wed, 25 Apr 2012 09:04:24 -0400 Subject: Added some threadcount reading for Mac OS Change-Id: I445d430e6f78475b923fa08001f9bb322f87f854 Reviewed-by: Chris Craig --- src/core/procutils.cpp | 28 ++++++++++++++++++++++++ tests/auto/processmanager/tst_processmanager.cpp | 8 ++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/core/procutils.cpp b/src/core/procutils.cpp index 3e57b30..6c1d766 100644 --- a/src/core/procutils.cpp +++ b/src/core/procutils.cpp @@ -57,6 +57,10 @@ #include #elif defined(Q_OS_MAC) #include +#include +#include +#include +#include #endif @@ -428,6 +432,9 @@ void ProcUtils::setPriority(pid_t pid, qint32 priority) /*! Return a count of the number of threads in a process + + Under Mach this requires some pretty strong permissions. Under Linux, anyone + can read the number of threads. */ int ProcUtils::getThreadCount(pid_t pid) @@ -435,6 +442,27 @@ int ProcUtils::getThreadCount(pid_t pid) #if defined(Q_OS_LINUX) QDir pdir(QString::fromLatin1("/proc/%1/task").arg(pid)); return pdir.entryList(QDir::Dirs | QDir::NoDotAndDotDot).size(); +#elif defined(Q_OS_MAC) + mach_port_t task; + thread_act_array_t threads; + mach_msg_type_number_t thread_count; + kern_return_t err = task_for_pid(mach_task_self(), pid, &task); + if (err != KERN_SUCCESS) { + qWarning("Unable to convert pid to task: %d", err); + return 1; + } + err = task_threads(task, &threads, &thread_count); + if (err != KERN_SUCCESS) { + qWarning("Call to task_threads failed: %d", err); + mach_port_deallocate(mach_task_self(), task); + } + qDebug("Found task with %d threads", thread_count); + mach_port_deallocate(mach_task_self(), task); + err = mach_vm_deallocate(mach_task_self(), (mach_vm_address_t) threads, + sizeof(*threads) * thread_count); + if (err != KERN_SUCCESS) + qWarning("Troubling freeing thread list"); + return thread_count; #endif return 1; } diff --git a/tests/auto/processmanager/tst_processmanager.cpp b/tests/auto/processmanager/tst_processmanager.cpp index b586ff6..7ef60db 100644 --- a/tests/auto/processmanager/tst_processmanager.cpp +++ b/tests/auto/processmanager/tst_processmanager.cpp @@ -57,6 +57,8 @@ #include "procutils.h" #include +#include +#include QT_USE_NAMESPACE_PROCESSMANAGER @@ -1028,11 +1030,10 @@ void tst_ProcessManager::prelaunchChildAbort() delete manager; } -#include -#include - void tst_ProcessManager::prelaunchThreadPriority() { + // Running this under Mac OSX has issues with permissions to read thread information +#if defined(Q_OS_LINUX) ProcessBackendManager *manager = new ProcessBackendManager; TimeoutIdleDelegate *delegate = new TimeoutIdleDelegate; delegate->setIdleInterval(250); @@ -1071,6 +1072,7 @@ void tst_ProcessManager::prelaunchThreadPriority() cleanupProcess(process); delete manager; +#endif } void tst_ProcessManager::prelaunchWaitIdleTest() -- cgit v1.2.3