Professional Web Applications Themes

OS-X Real-Time Scheduling Difficulties - Mac Programming

Our problem is the following: We found that if our primary application thread does not use the "real-time" thread scheduling API offered by the Mach kernel, that is, if a competing process lauches, for example another application is launched, our thread is stopped entirely. The scheduler just doesn't let us run until the competing process completes and/or releases resources. Using the real-time API for the Mach kernel (set_thread_priority(...)), we are able to guarantee our application thread enough of the CPU to run fairly smoothly even when another process is competing. However, now the behavior we observe is that when our ...

  1. #1

    Default OS-X Real-Time Scheduling Difficulties

    Our problem is the following:

    We found that if our primary application thread does not use the
    "real-time" thread scheduling API offered by the Mach kernel, that is,
    if a competing process lauches, for example another application is
    launched, our thread is stopped entirely. The scheduler just doesn't
    let us run until the competing process completes and/or releases
    resources.

    Using the real-time API for the Mach kernel
    (set_thread_priority(...)), we are able to guarantee our application
    thread enough of the CPU to run fairly smoothly even when another
    process is competing. However, now the behavior we observe is that
    when our process has run smoothly for a while at high CPU usage
    (approx 70-80% for example), it will be suddenly "demoted" by the
    scheduler to a very low CPU%. Piecing together background from the
    kernel programming doentation on Apple's developer site, I believe
    that this "demotion" effect happens because the scheduler
    automatically demotes processes deemed to be taking too much of the
    CPU.

    What's surprising is that we are using "reasonable"
    thread_time_constraint_policy settings. We have experimented with a
    range of values based on examples from Apple and other "real-time"
    applications such as audio and video players. We are taking a high but
    still reasonable percentage of the CPU.

    So the basic question is, is there a way that we can guarantee that
    our process will not be completely stopped when competing processes
    launch other than using the real-time API? And if we need to use the
    real-time API, is there a way we can avoid the demotion effect, for
    example, yield to other processes when necessary?

    I have researched all of the Mac OS X programming texts and the Apple
    doentation but have found no additional information on how to use
    the real-time APIs beyond basic examples, and would appreciate any
    advice.

    Below is a snippet of the code we are using to set our main
    application thread policy.

    Many thanks in advance.

    --TW

    ---------------------------8<-------------------------------------


    void thread_priority_init() {
    struct thread_time_constraint_policy ttcpolicy;
    int ret, bus_speed, mib [2] = { CTL_HW, HW_BUS_FREQ };
    size_t len;

    len = sizeof( bus_speed);
    ret = sysctl (mib, 2, &bus_speed, &len, NULL, 0);
    if (ret < 0) {
    err("sysctl query bus speed failed, errno=%d", errno);
    return;
    }

    /*
    The period is the number of cycles
    over which we are guaranteed a number of clock cycles equal
    to the second argument (the computation).
    The third argument (the constraint) is the maximum number of
    cycles to complete.
    */
    // from esound example: (160, 3300, 2200) cdaudio example: (120,
    // 1440, 720)
    ttcpolicy.period=bus_speed/160;
    ttcpolicy.computation=bus_speed/3300;
    ttcpolicy.constraint=bus_speed/2200;
    ttcpolicy.preemptible=1;

    if ((ret=thread_policy_set(mach_thread_self(),
    THREAD_TIME_CONSTRAINT_POLICY,
    (int *)&ttcpolicy,
    THREAD_TIME_CONSTRAINT_POLICY_COUNT)
    ) != KERN_SUCCESS)
    {
    err("Apple real time policy setting failure, errno=%d",
    errno);
    } else {
    dbg("Apple real time: %d of %d cycles, %d constraint, bus
    speed=%d(Hz)",
    ttcpolicy.computation, ttcpolicy.period,
    ttcpolicy.constraint, bus_speed);
    }
    }
    TW Guest

  2. #2

    Default Re: OS-X Real-Time Scheduling Difficulties

    In article <google.com>,
    com (TW) wrote:
     

    Ask this question on Apple's darwin-development mailing list. Real-time
    threads have been discussed there a number of times.

    Hope this helps,
    Eric

    --
    Eric Albert stanford.edu
    http://rescomp.stanford.edu/~ejalbert/
    Eric Guest

  3. #3

    Default Re: OS-X Real-Time Scheduling Difficulties

    On 29/07/2004, TW wrote in message
    <google.com>:
     

    Out of interest, how long is it until this happens ?
     

    I suspect that you are grabbing so much CPU for so long that
    Unix simply has to grab some for itself. Do you need that
    much CPU for all that time or does your thread need CPU in
    bursts so you could demote it between the bursts ? Both the
    DVD player and CD audio decoders use the burst technique and
    let the CPU go before they get demoted.

    Simon.
    --
    Using pre-release version of newsreader.
    Please tell me if it does weird things.
    Simon Guest

Similar Threads

  1. 3D real Time
    By gusilvapires in forum Macromedia Director 3D
    Replies: 1
    Last Post: May 22nd, 03:53 PM
  2. Real Time 3D for TV
    By gjab13 in forum Macromedia Director 3D
    Replies: 4
    Last Post: December 22nd, 02:25 PM
  3. Scheduling hourly tasks at certain time of the hour
    By MiNapier in forum Coldfusion Server Administration
    Replies: 0
    Last Post: April 6th, 04:25 PM
  4. Stupid "Time & Date" scheduling question
    By H. in forum Mac Applications & Software
    Replies: 19
    Last Post: September 16th, 09:46 AM
  5. Real time converted to Unix time
    By Brian in forum PERL Miscellaneous
    Replies: 1
    Last Post: August 29th, 04:36 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139