discovering if a process is busy

  • So is there any way to figure out if a process is busy or idle?
    I understand there was a way in OS9 to establish the total time a
    process has been active but it apparently returns 0 in OSX.

    I am basically trying to establish that a process is idle before
    sending it keystrokes.
  • On Dec 19, 2007 12:23 PM, justin webster <mail...> wrote:
    > So is there any way to figure out if a process is busy or idle?
    > I understand there was a way in OS9 to establish the total time a
    > process has been active but it apparently returns 0 in OSX.
    >
    > I am basically trying to establish that a process is idle before
    > sending it keystrokes.

    "idle" by whose definition?

    -Shawn
  • The Window Server knows whether a process is idle or not based on
    whether it is pulling events from its event loop. Unfortunately, I don't
    believe it shares this information—I think it's all hidden behind
    private SPIs. Unless they added this in Leopard…?

    Shawn Erickson wrote:
    > On Dec 19, 2007 12:23 PM, justin webster <mail...> wrote:
    >
    >> So is there any way to figure out if a process is busy or idle?
    >> I understand there was a way in OS9 to establish the total time a
    >> process has been active but it apparently returns 0 in OSX.
    >>
    >> I am basically trying to establish that a process is idle before
    >> sending it keystrokes.
    >>
    >
    > "idle" by whose definition?
    >
    > -Shawn
    >
  • on 2007-12-19 3:23 PM, justin webster at <mail...> wrote:

    > So is there any way to figure out if a process is busy or idle?
    > I understand there was a way in OS9 to establish the total time a
    > process has been active but it apparently returns 0 in OSX.
    >
    > I am basically trying to establish that a process is idle before
    > sending it keystrokes.

    There are functions in Quartz Event Taps -- a C API -- that return the
    elapsed time since the last hardware input event of any kind, and also since
    the last hardware input event of specified kinds. If I recall correctly --
    it's been a long day -- you can limit the functions to specific applications
    as well as getting the information globally. The event taps API is available
    in Tiger and Leopard. Look at the "Quartz Event Services" documentation.

    I would give you a more precise documentation reference, but Xcode now
    always crashes when I try to search the documentation -- since uploading the
    December docset updates.

    Just because the user isn't hitting the keyboard or moving the mouse doesn't
    mean the application is idle, of course. It might be running a lengthy
    operation, or doing something on a timer, or being manipulated via Apple
    events or other means, locally or over a network.

    --

    Bill Cheeseman - <bill...>
    Quechee Software, Quechee, Vermont, USA
    www.quecheesoftware.com

    PreFab Software - www.prefabsoftware.com
  • On Dec 19, 2007 1:30 PM, John Stiles <JStiles...> wrote:
    >
    > The Window Server knows whether a process is idle or not based on whether
    > it is pulling events from its event loop. Unfortunately, I don't believe it
    > shares this information—I think it's all hidden behind private SPIs. Unless
    > they added this in Leopard…?

    It isn't clear what problem he is trying to solve. Why does "idle"
    matter? What does "idle" mean to the problem he is trying to solve?
    etc.

    -Shawn
  • I'm sending keyboard events to an app which sometimes ignores a key
    or two if it is slow to process a previous keystroke or is busy with
    some other task.
    I want to know if the process is busy with a task (pulling events
    from event loop?) before sending the next key event.

    I would still have a polling issue to work around of course but even
    slow polling would be better than the situation I have at the moment.

    On 20/12/2007, at 10:41 AM, Shawn Erickson wrote:

    > On Dec 19, 2007 1:30 PM, John Stiles <JStiles...> wrote:
    >>
    >> The Window Server knows whether a process is idle or not based on
    >> whether
    >> it is pulling events from its event loop. Unfortunately, I don't
    >> believe it
    >> shares this information—I think it's all hidden behind private
    >> SPIs. Unless
    >> they added this in Leopard…?
    >
    > It isn't clear what problem he is trying to solve. Why does "idle"
    > matter? What does "idle" mean to the problem he is trying to solve?
    > etc.
    >
    > -Shawn
    >
  • On Dec 19, 2007, at 3:25 PM, justin webster wrote:

    > I'm sending keyboard events to an app which sometimes ignores a key
    > or two if it is slow to process a previous keystroke or is busy
    > with some other task.

    That seems wrong. Are you sure this is what's going on?

    Mac apps normally remove key events from a queue. So if an app is
    busy, the events stay in the queue until the app can get them. If
    this app is missing events because it's busy, it means that it is
    purposely throwing some events away, or that it's not using events at
    all but merely polling the keyboard state. Either way, it sounds like
    a poorly written app. What app is it?

    _murat
  • > it sounds like a poorly written app. What app is it?
    well you're right there. it's a bit of a mess.
    Pro Tools.

    Perhaps I've mis-diagnosed the problem it. it certainly seems like
    one or two key events get missed while the app is busy reading stuff
    into memory.
    I am using CGPostKeyboardEvent.
    is there a better way of achieving this with a different API?

    justin

    On 20/12/2007, at 1:09 PM, Murat Konar wrote:

    >
    > On Dec 19, 2007, at 3:25 PM, justin webster wrote:
    >
    >> I'm sending keyboard events to an app which sometimes ignores a
    >> key or two if it is slow to process a previous keystroke or is
    >> busy with some other task.
    >
    > That seems wrong. Are you sure this is what's going on?
    >
    > Mac apps normally remove key events from a queue. So if an app is
    > busy, the events stay in the queue until the app can get them. If
    > this app is missing events because it's busy, it means that it is
    > purposely throwing some events away, or that it's not using events
    > at all but merely polling the keyboard state. Either way, it sounds
    > like a poorly written app. What app is it?
    >
    > _murat
    >
  • The docs do say that CGPostKeyboardEvent is deprecated in Leopard. You
    might check to see if the modern version works better.
    However, I wouldn't be shocked to find out that Pro Tools is dropping
    events if it receives them at inopportune times…

    justin webster wrote:
    >> it sounds like a poorly written app. What app is it?
    > well you're right there. it's a bit of a mess.
    > Pro Tools.
    >
    > Perhaps I've mis-diagnosed the problem it. it certainly seems like one
    > or two key events get missed while the app is busy reading stuff into
    > memory.
    > I am using CGPostKeyboardEvent.
    > is there a better way of achieving this with a different API?
    >
    > justin
    >
    > On 20/12/2007, at 1:09 PM, Murat Konar wrote:
    >
    >>
    >> On Dec 19, 2007, at 3:25 PM, justin webster wrote:
    >>
    >>> I'm sending keyboard events to an app which sometimes ignores a key
    >>> or two if it is slow to process a previous keystroke or is busy with
    >>> some other task.
    >>
    >> That seems wrong. Are you sure this is what's going on?
    >>
    >> Mac apps normally remove key events from a queue. So if an app is
    >> busy, the events stay in the queue until the app can get them. If
    >> this app is missing events because it's busy, it means that it is
    >> purposely throwing some events away, or that it's not using events at
    >> all but merely polling the keyboard state. Either way, it sounds like
    >> a poorly written app. What app is it?
    >>
    >> _murat
    >>

  • On Dec 19, 2007, at 4:16 PM, justin webster wrote:

    >> it sounds like a poorly written app. What app is it?
    > well you're right there. it's a bit of a mess.
    > Pro Tools.

    Yow.

    I don't know how "up to date" ProTools is under the hood, but the
    code base is old enough that it wouldn't surprise me (well, actually
    it would) if ProTools is indeed just polling the keyboard map at times.

    Sorry, if this is the case, I don't have any suggestions for you. I
    don't think posting an event modifies the keyboard map though I could
    be wrong. Even if it did, you'd have to have some way of knowing when
    it was safe to post the keyUp event. Alternatives like diving down
    into the HID layer to "fake" key presses are too depressing to
    contemplate.

    I would investigate whether your synthesized key events are being
    properly posted. You can write an app to "listen" and log key press
    events (essentially, a key logger). Maybe you'll find that the
    problem is on your end. That would be a happy thing since you are at
    least in a position to fix it.

    _murat
  • > I would investigate whether your synthesized key events are being
    > properly posted. You can write an app to "listen" and log key press
    > events (essentially, a key logger). Maybe you'll find that the
    > problem is on your end. That would be a happy thing since you are
    > at least in a position to fix it.

    I'm pretty sure what I'm seeing is Pro Tools not handling the keys I
    send.
    If I add pauses between key events I get the desired result so the
    key events themselves are fine.
    it is also the case that the more work Pro Tools has to do between
    key events the more likely an error.

    is there some way to get the kind of stats I see in Activity Monitor?
    eg. CPU usage per process, memory usage per process.
    I could theoretically take baseline readings for the "idle" app
    before I run my routine, then check the values before deciding if
    it's safe to send a key event.
    pretty shonky but might be an option.

    justin
  • Activity Monitor is running a helper process as root to do its magic,
    and this helper process is using parts of the OS which can change from
    release to release. In other words, it's not easy to write your own
    Activity Monitor :(

    justin webster wrote:
    >
    >
    >> I would investigate whether your synthesized key events are being
    >> properly posted. You can write an app to "listen" and log key press
    >> events (essentially, a key logger). Maybe you'll find that the
    >> problem is on your end. That would be a happy thing since you are at
    >> least in a position to fix it.
    >
    > I'm pretty sure what I'm seeing is Pro Tools not handling the keys I
    > send.
    > If I add pauses between key events I get the desired result so the key
    > events themselves are fine.
    > it is also the case that the more work Pro Tools has to do between key
    > events the more likely an error.
    >
    > is there some way to get the kind of stats I see in Activity Monitor?
    > eg. CPU usage per process, memory usage per process.
    > I could theoretically take baseline readings for the "idle" app before
    > I run my routine, then check the values before deciding if it's safe
    > to send a key event.
    > pretty shonky but might be an option.
    >
    > justin
  • I think Pro Tools needs a bit of a break.  After all, priority number one
    for PT is not paying attention to the UI, but capturing, manipulating, and
    rendering the audio.  And it's one of the best in the business at doing
    that.  For me, as a user of PT, a lag or missing response to a keystroke is
    far preferable to glitches or errors in the sound.
  • You're saying that dropping events is the reason they can play back
    audio smoothly?
    Color me skeptical…

    Chris Williams wrote:
    > I think Pro Tools needs a bit of a break.  After all, priority number one
    > for PT is not paying attention to the UI, but capturing, manipulating, and
    > rendering the audio.  And it's one of the best in the business at doing
    > that.  For me, as a user of PT, a lag or missing response to a keystroke is
    > far preferable to glitches or errors in the sound.
    >
  • On Dec 19, 2007, at 5:44 PM, justin webster wrote:

    >> I would investigate whether your synthesized key events are being
    >> properly posted. You can write an app to "listen" and log key
    >> press events (essentially, a key logger). Maybe you'll find that
    >> the problem is on your end. That would be a happy thing since you
    >> are at least in a position to fix it.
    >
    > I'm pretty sure what I'm seeing is Pro Tools not handling the keys
    > I send.

    Does it miss keys that you type at the keyboard? Can you post your
    key sending code?

    _murat
  • On Dec 19, 2007 4:16 PM, justin webster <mail...> wrote:
    >> it sounds like a poorly written app. What app is it?
    > well you're right there. it's a bit of a mess.
    > Pro Tools.
    >
    > Perhaps I've mis-diagnosed the problem it. it certainly seems like
    > one or two key events get missed while the app is busy reading stuff
    > into memory.
    > I am using CGPostKeyboardEvent.
    > is there a better way of achieving this with a different API?

    You should use an CGEventCreateKeyboardEvent and post it using
    CGEventPost or in this case likely CGEventPostToPSN.

    <http://developer.apple.com/documentation/Carbon/Reference/QuartzEventServic
    esRef/Reference/reference.html
    >

    -Shawn
  • possible solution found.
    so for the next person who needs this:

    in terminal
    man ps

    for CPU usage only try
    ps -p330 -o pcpu

    it's an average but seems pretty responsive.
    is there any reason I shouldn't use this?
  • Le 20 déc. 07 à 04:00, Shawn Erickson a écrit :

    > On Dec 19, 2007 4:16 PM, justin webster <mail...>
    > wrote:
    >>> it sounds like a poorly written app. What app is it?
    >> well you're right there. it's a bit of a mess.
    >> Pro Tools.
    >>
    >> Perhaps I've mis-diagnosed the problem it. it certainly seems like
    >> one or two key events get missed while the app is busy reading stuff
    >> into memory.
    >> I am using CGPostKeyboardEvent.
    >> is there a better way of achieving this with a different API?

    That's not a Pro Tool issus. All Application will miss keyboard events
    if they are synthetized to fast. I think this is a Window Server Issus.
    This problem occurs whatever API you use (deprecated or recent) and
    with any applications (old carbon, or new Cocoa code).
previous month december 2007 next month
MTWTFSS
          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            
Go to today