DonationCoder.com Forum

DonationCoder.com Software => Coding Snacks => Finished Programs => Topic started by: Carol Haynes on December 17, 2006, 12:02 PM

Title: DONE: Replacement Power Options applet
Post by: Carol Haynes on December 17, 2006, 12:02 PM
Maybe not a snack but it would be useful.

Currently the Power Options in Control Panel work on the basis of keyboard and mouse activity.

I would like a replacement that works on a CPU threshold not being exceeded for a period (say 5%) - that way I could set Standby or Hibernate when the computer is truly idle - not just when I don't interact personally. That would allow extended processing applications (such as video recoding) to proceed without Power Options kicking in.

Anyone any ideas how to acheive this ?
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on December 17, 2006, 05:37 PM
 :) How about this one?

(https://www.donationcoder.com/Software/Skrommel/LowToSleep/LowToSleep.gif) LowToSleep  (http://www.1hoursoftware.com) - Stop a PC from falling asleep until the CPU is inactive.

Features:
- Change CPU limit using Settings in the tray menu
- Shows CPU usage in the tray

You'll find the downloads and more info at  1 Hour Software by Skrommel (http://www.1hoursoftware.com).

It just checks the CPU, and if it is above the limit (5 % is default), it sends a keystroke to a hidden control.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 17, 2006, 06:12 PM
Hey great idea - thanks. Nice to see you back  :Thmbsup:

(some pennies on the way)
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 17, 2006, 06:31 PM
Follow up:

I am getting an error at line 42 "Can't load icon" which appears repeatedly. See window ...

[ You are not allowed to view attachments ]

The value of %icon% looks to be negative everytime and the numerical value doesn't seem to match the actual CPU usage (according to task manager)?

I can see that the INI file allows you to set the delay time in minutes but could it also allow the setting of the frequency of checking (in ms) and also the CPU threshold (as a %).
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on December 17, 2006, 07:30 PM
 :) Man, you must really have a slow computer!

But seriously, what CPU is in there? I've never gotten any negative values, and they correspond very well with Task manager's tray icon. The code is lifted from the Autohotkey site, and noone has reported any anomalies...

I've just uploaded a new LowToSleep that checks if the icon exists, and I've changed the settings to show threshold in % and checking interval in seconds. Remember to delete your existing ini file!

Please keep me informed.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 17, 2006, 07:33 PM
By duplicating the icons with a '-' in front of the number it works but I am getting odd CPU usage tooltips (like -88%) when I hover over the icon. The actual CPU usage is <10%. Could this be because I have a dual core processor?

I am using an AMD Athlon64 X2 4600+ ... so pretty slow ;)
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 20, 2006, 06:48 AM
Anyone any further thoughts on this? Unfortunately Skrommel's idea doesn't seem to work for me (I get negative CPU usage and the apparent numerical value calculated int he script is always high - ignoring the sign).
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on December 20, 2006, 07:46 AM
 :( I've posted the question on the AHK forum, but noone has answered...

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on December 26, 2006, 11:57 AM
 :) Carol, try LowToSleep v1.0.

I've found typo in the settings file, causing the CPU load to be checked too fast.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 26, 2006, 05:02 PM
Strange - with <10% CPU activity LowToSleep is still reporting negative CPU activity (of the order of -80%) ???
Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on December 26, 2006, 05:44 PM
Hmm...

dunno if this is at all related, but for AMD dualcore CPUs, each core has it's own "Time Stamp Counter", and they don't update in parallel for a various number of reasons.

So, if an application queries the TSC (with assembly instruction RDTSC), store the number, thread gets scheduled to other CPU, RDTSC is done again and old value is subtracted (ie, timing is being done) you can end up with negative values.

This is why all Unreal Engine games crash on AMD64x2 boxes, unless they're launched with "affinity restriction".
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on December 27, 2006, 04:33 AM
That sounds quite likely.

How do you launch a program with affinity restriction (I know how to change it once it is launched).

Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on December 27, 2006, 06:11 PM
You need to find a launcher - I wrote a little hacky tool with no error checking, but you're probably better off googling for "win2000launcher" :)
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on December 27, 2006, 06:22 PM
 :) Pocess from http://www.beyondlogic.org/solutions/processutil/processutil.htm (http://www.beyondlogic.org/solutions/processutil/processutil.htm) can set the affinity of a process.

Skrommel

Change the affinity of processes . . .

The affinity is a mask which indicates on which processors (CPUs) a process can run. This is only useful on multiprocessor systems. When the -a option is used in conjunction with a process name or PID, the utility will show the System Affinity Mask and the Process Affinity Mask. The System Affinity Mask shows how many configured processors are currently available in a system. The Process Affinity Mask indicates on what processor(s) the specified process can run on.

C:\>process -a wordpad.exe

Command Line Process Viewer/Killer/Suspender for Windows NT/2000/XP V2.01
Copyright(C) 2002-2003 [email protected]
Getting Affinity Mask for PID 1084 'wordpad.exe'
System  : 0x0001 0b00000000000000000000000000000011  [2 Installed Processor(s)]
Process : 0x0001 0b00000000000000000000000000000011

 

To set the affinity mask, simply append the binary mask after the PID/Image Name. Any leading zeros are ignored, so there is no requirement to enter the full 32 bit mask.

C:\>process -a wordpad.exe 01

Command Line Process Viewer/Killer/Suspender for Windows NT/2000/XP V2.01
Copyright(C) 2002-2003 [email protected]
Setting Affinity Mask for PID 1084 'wordpad.exe'
Affinity Mask Successfully Set to 00000000000000000000000000000001
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 11, 2007, 05:18 PM
 :) Try LowToSleep  (http://www.1hoursoftware.com) v1.1!

I've added affinity to show the correct cpu usage on mutliple core/cpu systems. But I haven't got one, so please telle me if it works!

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 12, 2007, 12:09 PM
I still get large negative usage values (eg. -80% even when CPU usage is under 10%)
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 17, 2007, 06:45 PM
 :tellme: How about LowToSleep  (http://www.1HourSoftware.com) v1.2?

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on January 17, 2007, 09:00 PM
Hm, some things... unless I pass parameters for all three variables to GetSystemTimes, it doesn't return anything - the following code would simply print fluff! with SysInternals' dbgview. Adding "dummy1" and "dummy2", I get some garbage output, indicating that there's output.
lala = %idleticks% + "fluff!"
DllCall("kernel32.dll\OutputDebugString", "uint", &lala)
(christ, AutoHotKey is a weird language).

I still get -100% usage, though - and I'm pretty sure affinity isn't the problem, GetSystemTimes shouldn't be affected by that (rdtsc, GetTickCount() and QueryPerformanceCounter() are, though - I originally thought you were using one of those).

What the heck is going on in the loop in GETCPULOAD?

EDIT: oh, A_TickCount probably translates to a call to GetTickCount(), so affinity might be necessary after all. And the Loop seems to be necessary because of AHK's weird "everything is a string, but we don't translate API output" philosophy. Hm.

EDIT2: I've modified the GETCPULOAD() function somewhat...
GETCPULOAD() ;originally made by shimanov
{
  Global
  idletime0 = %idletime% ; Save previous values
  tick0 = %tick%

  DllCall("Kernel32.dll\GetSystemTimes", "*UInt64", idletime, "*Uint64", dummy1, "*Uint64", dummy2)

  tick := A_TickCount
  dIdle := (idletime - idletime0)
  dTick := (tick - tick0)

  load := 100 - 0.01 * dIdle/dTick

  outstr = Spent time: %dIdle%/%dTick%
  DllCall("kernel32.dll\OutputDebugString", "str", outstr)

  Return,load
}
With dbgview, I get values like...
Spent time: 20000000/1000
Spent time: 20000000/1000
Spent time: 19843750/1000
Spent time: 20000000/1000
Spent time: 19843750/1000
Spent time: 20000000/1000
Spent time: 19687500/1000

EDIT 3: Okay, I should have seen this earlier. Quoting MSDN for GetSystemTimes, emphasis is mine: "Retrieves system timing information. On a multiprocessor system, the values returned are the sum of the designated times across all processors.".

So, for NT systems, add a EnvGet, NumCpu, NUMBER_OF_PROCESSORS near the top of the script, and do dIdle := (idletime - idletime0) / NumCpu. Feels somewhat hacky, but works. Probably better using GetSystemInfo() and checking the dwNumberOfProcessors field, but I dunno how AHK handles structs. If you do the environment thing, check for 9x and assign 1 to NumCpu.
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 18, 2007, 03:29 AM
Sounds like progress - but I don't pretend I have followed the argument  :-[
Title: Re: IDEA: Replacement Power Options applet
Post by: CodeTRUCKER on January 18, 2007, 04:26 AM
I'll take a shot at it.  I think what is going on is to use the internal "clock" counts to a certain quantity defined by... I'm not sure, but the countdown/up is what triggers the event.  Clear as mud, right?
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 18, 2007, 06:41 AM
Scratches head .... yes I think I understand that ...  :tellme:
Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on January 18, 2007, 10:16 AM
Okay, let me try... you were in the right direction, Farmsteader :)

the code runs once every second, and measures:

1) how many miliseconds elapsed since it last run (tick-tick0). This might seem superfluous, but it CAN vary a bit, since the code runs at normal priority.

2) how much time the CPU was idle (the value from GetSystemTimes) (again, a relative timing, idletime-idletime0).

Thing is, GetSystemTimes returns totals for all CPUs - so on a 2-core system, although only one second has elapsed, 2 "cpu-seconds" could be spent idle, and this is why we have to divide by NUMBER_OF_PROCESSORS.

The affinity thing is anyother problem, and I'm not sure it applies here (can't remember if it affects GetTickCount(), or only the RDTSC CPU instruction and the win32 api QueryPerformanceCounter()). But that problem happens on AMD dualcore CPUs, because their internal time-stamp counters aren't synchronized (while on intel dualcores it seems like either there's a single TSC, or the TSCs are synchronized).
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 18, 2007, 10:47 AM
 :tellme: Anyone care to test LowToSleep  (http://www.1HourSoftware.com) v1.3?

- 20070118 - v1.3: Retrieves the number of processors for correct calculations on multi core systems.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 18, 2007, 11:18 AM
Ah ! Now that seems to work for me (at least for reporting CPU usage). Thanks that's great.

I'll try it out now for allowing sleeping on sustained low CPU usage.
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 18, 2007, 11:47 AM
 :D Great!

Thanks for finding the solution, f0dder!

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on January 18, 2007, 05:43 PM
np, couldn't sleep anyway - long live insomnia ;)

EDIT: why do you use the loop thing to construct the idletime variable, instead of just doing DllCall("Kernel32.dll\GetSystemTimes", "*UInt64", idletime, "*Uint64", dummy1, "*Uint64", dummy2) and getting it directly?
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 18, 2007, 06:03 PM
Right it allows my computer to go to sleep (and hibernate after a couple of hours) - the proof of the pudding will be to see what happens when I load the CPU for a long period - I'll try that tomorrow.
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 18, 2007, 06:33 PM
 :)
why do you use the loop thing

I missed that information in the excitement, f0dder! So you've been reading the AHK manual, have you?

The code isn't mine, I pretty much borrow everyone else's work.

I've posted your findings on the AHK forum. Maybe you should post your final GetCpuLoad code there?

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 18, 2007, 06:45 PM
 :tellme: f0dder, was the SetAffinity required?

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: f0dder on January 18, 2007, 06:52 PM
I'm not sure the SetAffinity() is required - can't remember if GetTickCount() is affected by the TSC weirdness. Heading off to bed, so don't have time for testing either right now :) - don't think it's necessary in this case, though.
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 19, 2007, 09:11 AM
Right it allows my computer to go to sleep (and hibernate after a couple of hours) - the proof of the pudding will be to see what happens when I load the CPU for a long period - I'll try that tomorrow.

Hmmm ... the pudding just burst.

To keep my system busy I ran a stress tester (the nVidia stress test which stresses most system components in a continuous loop) for 2 hours but LowToSleep didn't manage to keep the computer awake ? CPU usage was continuously high throughout.
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 19, 2007, 02:01 PM
 :tellme: My problem was the oposite... How does LowToSleep (http://www.1HourSoftware.com) v1.5 work?

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 19, 2007, 06:15 PM
I haven't left it long enough to see if it hibernates but it still turns off the screen and hard discs (set up in Power Options Applet), which it shouldn't do, so I suspect it will still hibernate at the appropriate time.
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 20, 2007, 01:14 AM
 :tellme: Hmmm... On my FSC Amilo laptop it behaves like it should. Could there be different power save implementations depending on brand?

Try lowering the idleduration=30 or =1 to test it, and see if that makes a difference.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 20, 2007, 05:59 AM
Nope - changed it to 1 and it made no difference.

Here is my Power Options applet:

[ You are not allowed to view attachments ]

The battery options are because I have a USB UPS which WinXP recognises as a battery.
Title: Re: IDEA: Replacement Power Options applet
Post by: CodeTRUCKER on January 20, 2007, 08:12 AM
Seems like something else is going on.  Have you tried moving your mouse at the right time?
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 20, 2007, 09:59 AM
I think that is pretty much what the script does (see the MAKENOISE subroutine) except that I think it is done by keyboard activity rather than mouse.

Trouble is it doesn't seem to work for me.
Title: Re: IDEA: Replacement Power Options applet
Post by: CodeTRUCKER on January 20, 2007, 10:26 AM
I'm still waiting to see if mine will give up.
Title: Re: IDEA: Replacement Power Options applet
Post by: skrommel on January 20, 2007, 08:18 PM
 :tellme: I've added mouse noise to LowToSleep (http://www.1HourSoftware.com) v1.6.

Please delete your existing ini file before running.

Skrommel
Title: Re: IDEA: Replacement Power Options applet
Post by: CodeTRUCKER on January 20, 2007, 11:08 PM
Works on my end.  Maybe you shoud try it again.  Are you running XP in an emulator, say on a Mac?
Title: Re: IDEA: Replacement Power Options applet
Post by: Carol Haynes on January 21, 2007, 05:36 AM
Thanks I'll give it a go.

No I am just running 32 bit Windows XP Pro natively.
Title: Re: IDEA: Replacement Power Options applet
Post by: MerleOne on December 16, 2010, 08:18 AM
Hi,

I just want to report that LowtoSleep, which I use all the time, is being detected as malware by yesterday's Avast 5 signatures.  I was able to exclude it from detections, and informed Avast, but they didn't acknowledge what I sent them.

Hope this get resolved soon.