CPU usage with ssh.blocking = true - WeOnlyDo Discussion board

CPU usage with ssh.blocking = true (wodSSH / wodSSH.NET)

by Marc M, Saturday, April 12, 2014, 00:22 (3640 days ago)

Hello,

I am running multiple threads of the ssh class so I can run commands on several network devices at once. They are all set to 'blocking = true' and I am using the 'client.waitfor()' method to pause for the responses. While the regex I am using does work and will process correctly, when my code is 'waitingfor' the cpu core spikes to 100% usage while it waits for the data to return.

Is this normal? Is there a workaround without having to switch from blocking mode to using events?

Thanks

CPU usage with ssh.blocking = true

by wodSupport, Saturday, April 12, 2014, 01:07 (3640 days ago) @ Marc M

Marc,

hi. Not sure why you get high CPU utilization, but you can always make your own WatiFor which is quite simple actually. Define one global variable:

Global String InData

and then in DataReceived event add this:

InData = InData + SSh1.Receive
if (instr(InData, "your_prompt")) then
"prompt found", remove all up to found data....
else
do nothing.. wait for more data
endif

so basically you append to your own string until your string is found among all received data. When it's found - do with it whatever you want, clear InData buffer, continue with coding, etc..

This way you can have multiple InStr calls, and have much more control than with our WaitFor.

Would that help?

Kreso

CPU usage with ssh.blocking = true

by Marc M, Monday, April 14, 2014, 19:34 (3637 days ago) @ wodSupport

I think I can try and make this logic work but I am going to need to rewrite a bunch of the functions. Right now I am finding and declaring the prompt, I am just using a regex string. I am also declaring WeOnlyDo.Client.SSH as a local variable since I spawn a new one based upon a queue list of devices to connect to, which means I can't create it with events.

I am trying this now though by setting up a new inherited class but I was hoping for fix that wouldn't rely on events.

CPU usage with ssh.blocking = true

by wodSupport, Monday, April 14, 2014, 19:40 (3637 days ago) @ Marc M

But this doesn't rely on events. You can use blocking Receive.

So, I am suggesting to make blocking function MyWaitfor that does the suggested in previous post. I thought this would end up as search/replace in your code from WaitFor to MyWaitfor.

Kreso

CPU usage with ssh.blocking = true

by Marc M, Monday, April 14, 2014, 20:28 (3637 days ago) @ wodSupport

Ok I see where you were getting at and it looks to be working now. I can probably optimize this a lot better but using this methodology I added a 500ms thread sleep to slow down the indata loop checking for my regex and my first few tests didn't spike anymore.

CPU usage with ssh.blocking = true

by wodSupport, Monday, April 14, 2014, 20:30 (3637 days ago) @ Marc M

Marc,

I wouldn't suggest to use Thread.Sleep since you're blocking wodSSH as well. But if it works ok for you, I have no objections :)

Kreso