[PATCH] Implement getloadavg()

Jon Turney jon.turney@dronecode.org.uk
Tue Mar 21 13:56:00 GMT 2017


On 20/03/2017 15:40, Corinna Vinschen wrote:
> On Mar 20 15:04, Jon Turney wrote:
>> On 20/03/2017 10:37, Corinna Vinschen wrote:
>>> On Mar 17 17:50, Jon Turney wrote:
>>> The load average is global, non-critical data.  So what about storing it
>>> in shared_info instead?  This way, only the first call of the first
>>> Cygwin process returns all zero.
>>
>> Ok.
>>
>>>> +static bool load_init (void)
>>>> +{
>>>> +  static bool tried = false;
>>>> +  static bool initialized = false;
>>>> +
>>>> +  if (!tried) {
>>>> +    tried = true;
>>>> +
>>>> +    if ((PdhOpenQueryA (NULL, 0, &query) == ERROR_SUCCESS) &&
>>>> +	(PdhAddEnglishCounterA (query, "\\Processor(_Total)\\% Processor Time",
>>>> +				0, &counter1) == ERROR_SUCCESS) &&
>>>> +	(PdhAddEnglishCounterA (query, "\\System\\Processor Queue Length",
>>>> +				0, &counter2) == ERROR_SUCCESS)) {
>>>> +      initialized = true;
>>>> +    } else {
>>>> +      debug_printf("loadavg PDH initialization failed\n");
>>>> +    }
>>>> +  }
>>>> +
>>>> +  return initialized;
>>>> +}
>>>
>>> How slow is that initialization?  Would it {make sense|hurt} to call it
>>> once in the initalization of Cygwin's shared mem in shared_info::initialize?
>>
>> I don't think that's particularly heavyweight, and I didn't see anything to
>> suggest that PDH query handles can be shared between processes, but I'll
>> look into it.
>
> Oh, right, that might pose a problem.

I can't find anything which documents these handles as shareable.

In practise, they seem to randomly stop working after a while after the 
process which created the handle exits, or something like that. :S

> But even then:
>
> The first process creating shared_info could call this and prime the values
> with a first call to getloadavg.  Each other process would have to init its

We cannot determine an initial value of load when shared_info is 
created, as the %CPU is not measured instantaneously, but over an 
interval.  This means that we can't have a load estimate until the 2nd 
time a process calls PdhCollectQueryData()

I've tweaked things slightly in v2 so the loadavg is initialized to the 
current load, rather than converging on it from 0.0.



More information about the Cygwin-patches mailing list