This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Zelos's profile picture

CC 1.5 massive Lag 30 Computers 50% CPU Usage

Started by Zelos, 14 August 2014 - 08:25 AM
Zelos #1
Posted 14 August 2014 - 10:25 AM
Hello,

i have build 24 new Computers, all Computers have a sleep(1) or sleep(5) Command at the Main Loop BUT:
Now i have >50% CPU Usage of all my 8 Cores (3,4Ghz) .

The mainlooop contains some Print Commands a Sleep and a filesave (fs.open("statTL", "w"))

The VisualVM (Java Debugging Tool) says i spend:
90% of CPU Time at org.luaj.vm2.LuaThread$State.lua_yield()
5% of CPU Time at org.luaj.vm2.LuaThread$State.lua_resume()


Does anyone hav a Idea why this happend?


The Programm Problem Function is the function TurtelLoader

Code: http://mc.sun-world.at:8123/computers/TLoader2.lua
Neywiny #2
Posted 14 August 2014 - 08:52 PM
yeah so computercraft has small issues with resource usage when you use 30 computers all executing code at the same time. which is why people used redpower computers, so the only thing I can think of would be to remove some computers. Also as for the sleep commands, that doesn't help as much as you think. And final note, if you're accessing files thus reading from your computer's disk and writing it to the ram and sending to the cpu, that's going to be effected speed wise by disk speed and sata speed, but that's not CC related. And looking into your code, you cold just use item ducts or hoppers or pipes or what have you to transfer items.
Edited on 14 August 2014 - 06:53 PM
Cranium #3
Posted 14 August 2014 - 11:47 PM
I do want to point out that Minecraft is not capable of using multiple cores, and as such, means that it will only use one core at a time. So your 8 core processor isn't too useful in this instance. I would like to point out that ComputerCraft computers are all run in sequence, not simultaneously. This means that the more computers you have in your world, the more threads are devoted to resuming and yielding. It's always better to have one computer do hundreds of different things, than to have hundreds of computers do one thing each.
However, I will also point out that whatever resources your server does have, is most likely being used up by other things. I'd recommend installing Opis, which can tell you exactly which processes are using up the most processing power on your server and client.

My server has lots of mods, and ComputerCraft doesn't even come close to using what you're saying it does.
Bomb Bloke #4
Posted 15 August 2014 - 06:38 AM
I do want to point out that Minecraft is not capable of using multiple cores, and as such, means that it will only use one core at a time.

I've been curious about that for some time now (not enough to actually check, but still). Wasn't much of the point of that blasted 1.3 update to change all that?

Certainly I wouldn't expect it to hold true for modded MineCraft. If ComputerCraft is creating multiple threads, wouldn't they be automatically spread across multiple cores by the host OS?
Cranium #5
Posted 15 August 2014 - 06:15 PM
Well not really. I believe someone told me a while back that the threads that CC creates isn't true multithreading. Since Minecraft is on one core, the threads all share the same processing, which explains why computers and all code is executed sequentially, instead of simultaneously.
Lyqyd #6
Posted 15 August 2014 - 07:42 PM
Oh, it's definitely got multiples threads in existence. LuaJ uses a system thread for each coroutine. It's not what one usually thinks of as multithreading though, because only one of those threads is ever being executed at a time, no matter how many cores you have. That's because their execution matches exactly with ComputerCraft's coroutine model.
Yevano #7
Posted 16 August 2014 - 03:40 PM
Well not really. I believe someone told me a while back that the threads that CC creates isn't true multithreading. Since Minecraft is on one core, the threads all share the same processing, which explains why computers and all code is executed sequentially, instead of simultaneously.

IIRC, each computer runs in its own thread, but the threads are basically just for running separate Lua states and never run simultaneously. Interesting thing is you can see this yourself by running a busy loop on computer A and then quickly switching to computer B. Computer B will be entirely unresponsive for a few seconds until computer A is forced to yield due to executing for too long. Pretty sure this is another reason the computers are all in separate threads, so that one computer can be killed without killing the others.

And Bomb Bloke you're correct in thinking that modded Minecraft has access to parallelism even if Minecraft itself doesn't do much of that on its own. In theory, one could mod ComputerCraft to allow all computers to run simultaneously, so long as you correctly synchronized the code. The only problem with this might be that you could create enough computers running busy loops that the server would basically come to a halt.
Bomb Bloke #8
Posted 17 August 2014 - 04:08 AM
Ok, so I got around to giving this a quick test:

Rather than bothering with CC 1.5, I worked under 1.63. In a new creative world I placed a bank of 64 computers connected via wired modems to a single controller computer.

I had the controller generate a basic "while true do sleep(0) end" startup script for each other system, and also gave it "off" and "on" scripts to control them via their peripheral functions as well.

On average, Minecraft was using about 5% of my CPU (quad-core, for what it's worth, though using the usual virtualisation features of the day to pretend it's got eight). Turning on the bank and waiting half a minute or so saw it stabilise at around 10%. I flipped it off/on a few times to make sure.

I then distributed a new script to each system using sleep(1000) instead of sleep(0). Turning the bank on with this in place had next to no effect on my CPU usage.

So if there were any bugs with the way ComputerCraft's threads were idling, it looks like they've since been fixed. The only oddity I noticed was that typically one or two computers in the bank would still have a blank screen when turned on (changing each time I enabled/disabled the group), even though they were on.