summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Christian <andrew.christian@nokia.com>2012-04-25 09:04:24 -0400
committerChris Craig <ext-chris.craig@nokia.com>2012-04-25 17:45:10 +0200
commit45251292590ef9e3ce0740e5521936a4ad2e3033 (patch)
tree0562e4595e76d7d9a58b3ed425a64458f7337abc
parent874b3b8e96efe2b352b8218ec700e1ac2dd3094d (diff)
Added some threadcount reading for Mac OS
Change-Id: I445d430e6f78475b923fa08001f9bb322f87f854 Reviewed-by: Chris Craig <ext-chris.craig@nokia.com>
-rw-r--r--src/core/procutils.cpp28
-rw-r--r--tests/auto/processmanager/tst_processmanager.cpp8
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 <QRegExp>
#elif defined(Q_OS_MAC)
#include <sys/sysctl.h>
+#include <mach/mach.h>
+#include <mach/task.h>
+#include <mach/mach_vm.h>
+#include <mach/thread_info.h>
#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 <signal.h>
+#include <sys/time.h>
+#include <sys/resource.h>
QT_USE_NAMESPACE_PROCESSMANAGER
@@ -1028,11 +1030,10 @@ void tst_ProcessManager::prelaunchChildAbort()
delete manager;
}
-#include <sys/time.h>
-#include <sys/resource.h>
-
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()