Re: [go-nuts] ticker not firing often enough in minimal mac app with run loop

2019-09-04 Thread Gregory Pomerantz



On 9/4/19 2:30 PM, Gregory Pomerantz wrote:
currenly just running the go program from the terminal, though I got 
similar results packaging it into an app.


I soon get very long periods between ticker fires:

Tick slow: 3.501350114s
Tick slow: 5.551485377s


I am seeing the same thing (Late 2012 13" MacBook Pro Retina).

My first guess is that MacOS must be putting your App into a 
background low-power mode and scheduling it infrequently. My ticks go 
up to just about 11 seconds. because clicking on the App in the dock 
seems to (temporarily) fix this. I would check the AppKit docs for how 
to avoid background throttling, or see if the OS is sending you a 
message first that you can respond to and prevent it from happening. 
An alternative is to create a pure Go background task that interacts 
with a MacOS frontend through a socket, grpc or some other mechanism. 
Good luck and let me know if anything works.


Let me know if this works, it seems to fix the issue over here. Not sure 
what impact this has generally on power management, sleep mode etc. but 
you can read the AppKit docs for that.


void StartApp() {

    [NSAutoreleasePool new];
    [NSApplication sharedApplication];
    [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
    id activity = [[NSProcessInfo processInfo] 
beginActivityWithOptions:NSActivityBackground reason:@"Good Reason"];

    [NSApp run];
    [[NSProcessInfo processInfo] endActivity:activity];
}

--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d9ecc46a-d074-a283-94b1-4836866f6bf5%40wow.st.


Re: [go-nuts] ticker not firing often enough in minimal mac app with run loop

2019-09-04 Thread Gregory Pomerantz



currenly just running the go program from the terminal, though I got 
similar results packaging it into an app.


I soon get very long periods between ticker fires:

Tick slow: 3.501350114s
Tick slow: 5.551485377s


I am seeing the same thing (Late 2012 13" MacBook Pro Retina).

My first guess is that MacOS must be putting your App into a background 
low-power mode and scheduling it infrequently. My ticks go up to just 
about 11 seconds. because clicking on the App in the dock seems to 
(temporarily) fix this. I would check the AppKit docs for how to avoid 
background throttling, or see if the OS is sending you a message first 
that you can respond to and prevent it from happening. An alternative is 
to create a pure Go background task that interacts with a MacOS frontend 
through a socket, grpc or some other mechanism. Good luck and let me 
know if anything works.


--
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/71fb6cb6-2f28-b99c-14ab-676b3525e567%40wow.st.


Re: [go-nuts] ticker not firing often enough in minimal mac app with run loop

2019-09-04 Thread Marcin Romaszewicz
I'm not quite sure how to avoid this, but I'm pretty sure that I know the
cause. Read up on Grand Central Dispatch and Centralized Task Scheduling (
https://developer.apple.com/library/archive/documentation/Performance/Conceptual/power_efficiency_guidelines_osx/DiscretionaryTasks.html).
It's been a while since I've written OS X code, but I used to do it quite
heavily (I'm one of the authors of the mac version of Google Earth).

OS X tries very hard to keep the CPU's in their lowest power state, the the
scheduler has all sorts of heuristics for when an application needs to be
woken up. By default, it tries very hard to keep the applications scheduled
as little as possible, and you're supposed to provide hints to the system
about the urgency of the work you're doing.

Now, here's where I'm rusty, but I believe that at very low nice levels,
you can bypass this behavior. Try this:

sudo nice -1 /path/to/your/application

If your timers start working correctly, that's probably the reason.

Generally, when you interact with Cocoa system libraries, there's all sorts
of interaction with GCD and CTS to keep your app running in an
efficiency/performance sweet spot, but when you call out to another
runtime, like Go's, you bypass all those hooks.


On Wed, Sep 4, 2019 at 1:20 AM Tor Langballe  wrote:

> I'm running a mac cocoa event loop, and calling a go ticker:
>
> void StartApp() {
> [NSAutoreleasePool new];
> [NSApplication sharedApplication];
> [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
> [NSApp run];
> }
>
> func test() {
>  last := time.Now()
>  ticker := time.NewTicker(time.Second)
>  for range ticker.C {
>  t := time.Now()
>  diff := t.Sub(last)
>  if diff > time.Second*2 {
>  fmt.Println("Tick slow:", diff)
>  }
>  last = t
>   }
> }
>
>
> func main() {
>  go test()
>  C.StartApp()
> }
>
> currenly just running the go program from the terminal, though I got
> similar results packaging it into an app.
>
> I soon get very long periods between ticker fires:
>
> Tick slow: 3.501350114s
> Tick slow: 5.551485377s
>
> This seems to only happen with
>
> [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
>
> NSApplicationActivationPolicyProhibited and
> NSApplicationActivationPolicyAccessory don't cause this.
>
> I assume it's the event loop in [NSApp run] that is causing this, but
> don't know how to start making a mac appliction with goroutines and tickers
> that don't get blocked.
>
> Does anybody know how to avoid this and why it is happening?
>
> tor
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/62325cef-7441-4a7c-abf9-ff58454ff5f4%40googlegroups.com
> 
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CA%2Bv29LvAMukbgVPV_ARiOsm7X7zpUcdnOA-BLQ9%3DEdX3yeZYFg%40mail.gmail.com.


[go-nuts] ticker not firing often enough in minimal mac app with run loop

2019-09-04 Thread Tor Langballe
I'm running a mac cocoa event loop, and calling a go ticker:

void StartApp() {
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp run];
}

func test() {
 last := time.Now()
 ticker := time.NewTicker(time.Second)
 for range ticker.C {
 t := time.Now()
 diff := t.Sub(last)
 if diff > time.Second*2 {
 fmt.Println("Tick slow:", diff)
 }
 last = t
  }
}


func main() {
 go test()
 C.StartApp()
}

currenly just running the go program from the terminal, though I got 
similar results packaging it into an app.

I soon get very long periods between ticker fires:

Tick slow: 3.501350114s
Tick slow: 5.551485377s

This seems to only happen with 

[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];

NSApplicationActivationPolicyProhibited and 
NSApplicationActivationPolicyAccessory don't cause this.

I assume it's the event loop in [NSApp run] that is causing this, but don't 
know how to start making a mac appliction with goroutines and tickers that 
don't get blocked.

Does anybody know how to avoid this and why it is happening?

tor



-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/62325cef-7441-4a7c-abf9-ff58454ff5f4%40googlegroups.com.