MxN Threads on HP-UX 11i:

lightnewsΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

106 εμφανίσεις


MxN Threads on HP-UX 11i:
Consequences and Implications

Introduction
Starting with the release of 11i v1.6, HP-UX contains new thread functionality: the possibility
for multiple threads of a process to map to a smaller number of kernel threads. This feature is
commonly known as MxN threading and differs from the previous model for threading on HP-
UX 11 where every thread created in a process corresponds directly to a single thread in the
kernel (1x1 threads).
1

The most significant aspect to this change in functionality is that not only are MxN threads now
available on HP-UX, they are also now the default for C/C++ programs. It is possible that this
change from 1x1 to MxN will impact the performance of your application. It is also likely that
the first time developers encounter these potential impacts will be when migrating from PA-
RISC based systems to Itanium systems, since the versions of HP-UX that support MxN threads
are currently only available on Itanium. Therefore it is important to be informed about the
implications of the change and how to control which threading model is used by your program.
This document offers:
• A brief explanation of the various threading models.
• Information on changing the thread model for a C/C++ program.
• Information on thread models for Java programs.
Thread Model Overview
Some basic concepts are important to understanding the difference between the various threading
models. Primary is the idea that the word “thread” is actually used to refer to two different
things. There are threads from the user-space program’s perspective; a programmer writes code
to create and control these multiple paths of execution known as threads. And then there are
threads from the kernel’s perspective; it must manage the scheduling of execution entities
(threads) to run on the processors available on the system. It is the mapping of the programmatic


1
HP-UX 11 also allows user space threading through the use of libcma (DCE Threads). Use of this library for
multithreading is generally discouraged.
- 1 –
MxN Threads on HP-UX 11i

abstraction of threads to the entities scheduled by the kernel that is determined by the particular
thread model being used.
There are three basic thread models: Mx1, 1x1, and MxN.
In the Mx1 model (also known as “green threads” or “user threads”), all threads of a process are
mapped to a single execution entity to be scheduled by the kernel. Scheduling of a process’
threads takes place in user space; the kernel is not aware that the process in question is multi-
threaded. For operating systems that do not support kernel threads (e.g. HP-UX releases prior to
11.0), this is the only option. The primary disadvantage to this model is that even on
multiprocessor systems, a process can only ever be executing one thread, so there is no real
execution parallelism.
In the 1x1 model (sometimes referred to as NxN), each user thread of a process corresponds to
exactly one thread in the kernel. All scheduling of threads is done by the kernel. This model is
simple to understand and transparent to the programmer because there is tight coupling between
the programmatic abstraction (user threads) and what is actually going on in the kernel. One
drawback is the overhead for each kernel thread (memory, slots in the scheduling algorithm) that
may be excessive for programs that create a large number of threads. The 1x1 thread model is
supported by HP-UX releases 11.0 and later.
The last model of interest – MxN – is a hybrid of the two other models. In the MxN model,
threads created by a process do not directly correspond to a scheduled entity in the kernel, but a
single process may have multiple scheduled entities. The task of scheduling threads ends up
being split between user space and the kernel. The pthreads library (in user space) assigns
program threads onto kernel-scheduled entities and the kernel schedules these entities onto
available processors.
MxN threads were implemented in response to the perception that enterprise applications
creating large numbers of threads perform better with MxN threads than with 1x1. The only real
way to know how using the MxN model affects the performance of your particular application is
to benchmark the two models and compare the results. In order to do that, you need to know how
to control which thread model your program uses.
- 2 –
MxN Threads on HP-UX 11i

Changing the Thread Model for a C/C++ Program
The most direct way to control the threading model used by your program is to explicitly specify
the thread scope in the thread attributes structure passed to pthread_create.
pthread_t thread;
pthread_attr_t ptattr;
int rtnval;
pthread_attr_init(&ptattr);
pthread_attr_setscope(&ptattr,PTHREAD_SCOPE_PROCESS);
rtnval = pthread_create(&thread,&ptattr,start_func,NULL);
The value of the scope attribute can be either PTHREAD_SCOPE_PROCESS or
PTHREAD_SCOPE_SYSTEM. PTHREAD_SCOPE_PROCESS specifies MxN and
PTHREAD_SCOPE_SYSTEM specifies 1x1.
HP-UX releases prior to 11i v1.6 do not support MxN. On those releases, setting the thread
scope is a no-op; threads created by pthread_create always have a scope of
PTHREAD_SCOPE_SYSTEM. An obvious corollary is that threads created with no scope
specified also have a scope of PTHREAD_SCOPE_SYSTEM. However, on the HP-UX releases
that do support MxN (11i v1.6 and later), the explicitly specified scope of a thread is honored
and threads created with no scope specified have a scope of PTHREAD_SCOPE_PROCESS.
Obviously, if you care about your program using a particular threading model, the best way to
get what you want is to specify it programmatically as shown above. However, there are a couple
alternatives for forcing a program to run with 1x1 threads by default instead of MxN.
If you are running on HP-UX 11i v1.6, the easiest way to change the default behavior is to
recompile your application with the additional compiler flag DPTHREAD_COMPAT_MODE.
The default scope for threads created by a program compiled in this manner is
PTHREAD_SCOPE_SYSTEM (1x1). Scope explicitly set by the program will be honored.
This compile-time technique also works on HP-UX 11i v2, but there is an additional method that
can be used at runtime. If you set the environment variable PTHREAD_COMPAT_MODE to 1,
then programs run from that shell will have a default thread scope of
PTHREAD_SCOPE_SYSTEM.
Mixing Models (Bound and Unbound)
The explanation above may motivate you to ask “What if my program creates some threads with
PTHREAD_SCOPE_PROCESS and some other threads with PTHREAD_SCOPE_SYSTEM?”
This is permitted, though not necessarily recommended. Threads created with
PTHREAD_SCOPE_SYSTEM (bound threads) will be tightly associated with one and only one
- 3 –
MxN Threads on HP-UX 11i

kernel thread throughout their lifetime and will bypass the user-space scheduler in the pthreads
library, while threads created with PTHREAD_SCOPE_PROCESS (unbound) will not have a
dedicated kernel thread, but will be scheduled onto a kernel thread as necessary by the user-space
scheduler.
Thread Models for Java Programs
At this writing (September 2003) Java on HP-UX does not support MxN threads; all threads
created by the JVM are PTHREAD_SCOPE_SYSTEM (1x1). A future Java release will support a
command line option to enable MxN threads within the JVM, with the default remaining 1x1.
Resources
White Paper on Java Threads and Threading Models.

Software Transition Kit (STK) Overview of Thread Impacts

STK Description of MxN Impact

- 4 –
MxN Threads on HP-UX 11i