Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV

Before Posting...

Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


Soft Affinity of Thread

OSR_Community_UserOSR_Community_User Member Posts: 110,217
Hello,

How can one set the soft-affinity (and hard-affinity) of a Driver-created
thread to a particular processor ?

Thank you for your help!
Puja

Comments

  • Mark_RoddyMark_Roddy Member - All Emails Posts: 4,712
    Use ZwSetInformationThread with THREADINFOCLASS set to ThreadAffinityMask.
    The affinity mask ought to be a ULONG, but it appears to be undocumented in
    the DDK. Each bit corresponds to the processor to which you want this thread
    bound. You can set more than one bit. Who knows what happens if you set only
    bits for processors that don't exist. There is most likely some Nt
    equivalent that you could look up in Undocumented Windows NT to confirm the
    Zw parameters. Thereis probably a Win32 equivalent too.

    That's hard affinity. Soft affinity as far as I know is not programmable. It
    is a function of the run time behavior of the thread and some screwball
    algorithm in the dispatcher. It should be ignored.

    Mark Roddy
    Windows 2000/NT Consultant
    Hollis Technology Solutions
    www.hollistech.com
  • Eliyas_YakubEliyas_Yakub Member Posts: 229
    The function ZwSetInformationThread is partially documented. You can use the
    following lines of code in a device driver to set the thread affinity:

    KAFFINITY threadAffinityMask;
    threadAffinityMask = KeNumberProcessors & ( 1<< TargetProcessorNumber)
    ; // TargetProcessorNumber is a zero based number
    status = ZwSetInformationThread((HANDLE)-2, //current thread handle
    ThreadAffinityMask,
    &threadAffinityMask,
    sizeof(KAFFINITY));

    -Eliyas

    -----Original Message-----
    From: [email protected] [mailto:[email protected]]
    Sent: Wednesday, April 19, 2000 7:00 PM
    To: NT Developers Interest List
    Subject: [ntdev] Soft Affinity of Thread


    Hello,

    How can one set the soft-affinity (and hard-affinity) of a Driver-created
    thread to a particular processor ?

    Thank you for your help!
    Puja

    ---
    You are currently subscribed to ntdev as: [email protected]
    To unsubscribe send a blank email to $subst('Email.Unsub')
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    Hello,

    Maybe I'm missing something really big here, but I donot understand the
    code snippet:

    1. Shouldn't threadAffinityMask = (1 << TargetProcessorNumber) instead of
    threadAffinityMask = KeNumberProcessors & (1 << TargetProcessorNumber) ??

    [By my calculation, threadAffinityMask is always 0 if I use the code
    snippet given here]

    2. Even if we were to use KeNumberProcessors, isn't it a pointer (we will
    need to dereference it, won't we) ?

    Appreciate the clarification.

    Thanks
    Puja
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    Sorry, somehow the original post didn't get included ...
    ==

    Hello,

    Maybe I'm missing something really big here, but I donot understand the
    code snippet in the post below:

    1. Shouldn't threadAffinityMask = (1 << TargetProcessorNumber) instead of
    threadAffinityMask = KeNumberProcessors & (1 << TargetProcessorNumber) ??

    [By my calculation, threadAffinityMask is always 0 if I use the code
    snippet given here]

    2. Even if we were to use KeNumberProcessors, isn't it a pointer (we will
    need to dereference it, won't we) ?

    Appreciate the clarification.

    Thanks
    Puja

    On 04/21/00, "Eliyas Yakub <[email protected]>" wrote:
    > The function ZwSetInformationThread is partially documented. You can use the
    > following lines of code in a device driver to set the thread affinity:
    >
    > KAFFINITY threadAffinityMask;
    > threadAffinityMask = KeNumberProcessors & ( 1<< TargetProcessorNumber)
    > ; // TargetProcessorNumber is a zero based number
    > status = ZwSetInformationThread((HANDLE)-2, //current thread handle
    > ThreadAffinityMask,
    > &threadAffinityMask,
    > sizeof(KAFFINITY));
    >
    > -Eliyas
    >
    > -----Original Message-----
    > From: [email protected] [mailto:[email protected]]
    > Sent: Wednesday, April 19, 2000 7:00 PM
    > To: NT Developers Interest List
    > Subject: [ntdev] Soft Affinity of Thread
    >
    >
    > Hello,
    >
    > How can one set the soft-affinity (and hard-affinity) of a Driver-created
    > thread to a particular processor ?
    >
    > Thank you for your help!
    > Puja
    >
    > ---
    > You are currently subscribed to ntdev as: [email protected]
    > To unsubscribe send a blank email to $subst('Email.Unsub')
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    KAFFINITY
    KeSetAffinityThread (
    IN PKTHREAD Thread,
    IN KAFFINITY Affinity
    )

    > That's hard affinity. Soft affinity as far as I know is not programmable.
    It

    CCHAR
    KeSetIdealProcessorThread (
    IN PKTHREAD Thread,
    IN CCHAR Processor
    )

    - this is the soft affinity.

    Max
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 13-17 May 2024 Live, Online
Developing Minifilters 1-5 Apr 2024 Live, Online
Internals & Software Drivers 11-15 Mar 2024 Live, Online
Writing WDF Drivers 26 Feb - 1 Mar 2024 Live, Online