Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: Massive CPU Drain (Two different bugs)  (Read 3515 times)

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Massive CPU Drain (Two different bugs)
« on: February 18, 2014, 08:22:38 PM »

Two situations cause pretty extreme drain atm.

1.  When two large fleets engage, the moment that either can see each other on LOS, framerate drops extremely sharply.  Not sure what's up with that- it's not the AIs and it's not graphics, though, everything is peachy until the two fleets can see each other. 

LOS display, maybe?

2.  Viewing the player's Fleet screen.  Not the Fleet screen in the Station purchase menu; that's just fine.  But a large fleet in the player's Fleet display causes noticeable stuttering.  Had a player note this was happening, checked it and it's definitely a real issue, but I haven't the foggiest what would cause that, especially since the Station purchase menu shows, for all intents and purposes, the same data, but is apparently running a lot faster.  Really weird.

I think both of these can be duplicated in Vanilla, but I haven't run a test there.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24118
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #1 on: February 18, 2014, 09:46:14 PM »

What exactly do you mean by "large", in each case?

Re: 1 - I'm not sure I quite understand what you mean. Is this in the campaign? Hmm, I think you mean combat, right?

If it's in combat, are you *sure* it's not your custom AI? :) I mean, if we rule out AI and graphics, it doesn't leave much, and what's left doesn't care about whether stuff is visible or not.

If by "LOS display" you mean the fog of war rendering, hmm. That shouldn't matter since that's being done before you see the enemy ships also. It also doesn't happen unless the command UI is up.

Really, stuff triggered by seeing ships sounds makes me suspect the AI.



Re: 2 - Just tried buying every ship in the Hegemony station, and the Diktat one for good measure. With the fleet screen open, the FPS stayed at 60, with about 70% idle (90% idle being normal in the campaign, w/o the fleet screen being open. If you do find a way to replicate this in vanilla, please let me know.

(Might it be something related to custom weapon graphics? The ships in the stations generally don't have anything mounted. Just tried a huge fleet - 50+ fully fitted Odysseys, and lots of other ships - and that seems more or less fine, too. Only hiccuping a bit when picking ships up and putting them back down, and that's an unreasonably huge fleet.)
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #2 on: February 18, 2014, 10:14:21 PM »

Hmm.  By "large", in case 1, it's near-crippling when the scales go up enough; in case 2, it's very noticeable lag.

In case 1, we're talking about battle, and I thought it was my AI, too, except that everything is fine (in the sense that the framerate is very reasonable) until the enemy is visible on LOS. 

The second the LOS symbols start showing up, instant lag.

But the AI's not doing much at that point, other than doing (very occasional) searches for valid targets. 

Maybe it's auto-aim AI doing the complex quadratic stuff with targets that are out of range or something equally silly; there are probably more searches going on there than anywhere else.  I'll look into that.  Whatever it is, it's pretty big and it's definitely related to the scale of the battle.  Let's call that one User Error on my part until I have eliminated those possibilities.

Case 2... about the only thing I think might be different there is that there are Decorative weapons scripts and a bunch of ships that don't have Engines any more.  But neither of these things are causing the lag on the Purchase screen, and both things are there on the ships, whether armed or not.  I can't think what could be doing it under the hood; I presume stuff like Auto-Aim AIs aren't operating, same with ship AIs.  Pretty confused by this one.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24118
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #3 on: February 18, 2014, 10:21:38 PM »

(What I meant was to ask what you mean by "large" fleets - no idea whether that means 10 ships to a side or 100.)

Case 2... about the only thing I think might be different there is that there are Decorative weapons scripts and a bunch of ships that don't have Engines any more.  But neither of these things are causing the lag on the Purchase screen, and both things are there on the ships, whether armed or not.  I can't think what could be doing it under the hood; I presume stuff like Auto-Aim AIs aren't operating, same with ship AIs.  Pretty confused by this one.

Hmm, yeah, what you're saying makes sense. But, I'm unable to reproduce it in vanilla - can you?

What about any hullmod-related scripts that might be running? They shouldn't be running every frame, though, so never mind... Yeah, very odd, that.


As far as #1 - sounds pretty reasonable, yeah. Especially if you have a large number of fighters, which, given your work on optimizing those, it sounds like you might.
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #4 on: February 18, 2014, 10:37:51 PM »

Quote
I meant was to ask what you mean by "large" fleets
Somewhere north of 50 actors, if we count Drones (which are still using Vanilla AI).  Pretty big fights by Vanilla standards. 

I'll look at that in detail and see what's really causing that.  It's really weird that it doesn't happen until the ship symbols start showing up on the edge of the screen, though; makes me think something else may be there.

Quote
What about any hullmod-related scripts that might be running?
That's a distinct possibility, given the new Armor Regeneration script, which, as I'm trying to get it vaguely balanced, I tend to put on everything... but its AdvanceInCombat() is supposed to return if CombatEngine.isPaused(), it doesn't even update the intervals... and there's nothing in the AdvanceInCampaign().

If CombatEngine.isPaused() isn't returning true in that context, though... that could certainly explain it.  It'd also effect the Auto-Aim AIs, although they'd get null for targets, so they wouldn't attempt to move.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #5 on: February 18, 2014, 10:53:16 PM »

OK, there's one thing in the Auto-Aim code that may be causing an issue; I see that the LazyLib AIUtils checks against Fog of War when it's evaluating certain things via the getEnemiesOnMap() function, which I'm using. 

That may point out why there's a slowdown there.

I'm going to try eliminating that stuff by just using a simpler version that doesn't test vs. FOW and see what happens to speeds; it'll tell me a lot about what's really causing the slowdowns.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #6 on: February 18, 2014, 11:57:16 PM »

Not even close, lol.  It sped things up a little bit, but only a little bit. 

I haven't tried getting rid of same from the AIs themselves yet or limiting auto-aim re-targeting from automatically dropping anything that leaves the range; that probably causes a lot of unnecessary churn when the two fleets aren't in range (which is when the slowdown really hits hardest), but it's small stuff in the bigger picture.

I'm wondering what the n^2 impact is on FoW calculations when huge numbers of fighters are impacting the FoW system, though... I might want to do some tests on just how much FoW radius checks impact performance; IIRC I did test with huge radii at one point and the sim really wasn't happy about that.

Oh well, bedtime :)
Logged
Please check out my SS projects :)
Xeno's Mod Pack

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #7 on: February 19, 2014, 11:49:25 PM »

Tracked this one down and in terms of the Battle situation, it's definitely my fault, although there's still a bit of mystery.

It's in the Auto-Fire AIs (rather than the Ship AIs, the System AIs, the AI AIs... sigh). 

I had a rather badly designed bit of logic that was calling up a routine for target sorting, which caused huge numbers of searches through CombatEngine.getShips() and many sub-searches through it, which, erm, was not fast when called a thousand times a frame or more because of a logic bug... go figure ::)

IDK whether that also fixes that issue with the Fleet screen, but it's a prime candidate.  Will test that out again after getting some sleep and finishing the polish stuff that needs to be done post-fix on the Auto-Fire AIs to get them all up to snuff; I fixed the "biggie" on the rest of 'em right away but I found a lot of minor stuff that I should probably prune away.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

Alex

  • Administrator
  • Admiral
  • *****
  • Posts: 24118
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #8 on: February 20, 2014, 09:22:16 AM »

Cool, glad you figured out what it was. Yeah, sorting... not so hot if you're doing it every frame. Don't know if you're using the term sorting literally, but if you are - generally, since you're only looking for one target, a search (rather than a sort) would achieve the same result. Unless you're doing something fancy with target picking. But, you're probably aware of this - just thought I'd bring it up since the word "sort" threw up a red flag for me :)

About the fleet screen - not sure this could be causing it; the fleet screen shouldn't run any AI except possibly for one frame when it's opened. If it's running the AI every frame, that would be a surprise to me. Doesn't make it impossible, though.


(Moving this to Modding, btw, just so I don't think there's a new bug to fix every time there's a new post in this thread.)
Logged

xenoargh

  • Admiral
  • *****
  • Posts: 5078
  • naively breaking things!
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #9 on: February 20, 2014, 09:49:21 AM »

Both literally and, er, not literally.  LazyWizard profiled it and noted that practically all of the CPU time drain is in findBestTarget(), which was pretty badly optimized (for example, iterating through the lists multiple times when one time was efficient, not returning immediately when it found a target in range) and most importantly was calling practically every frame. 

Oh well, growing pains and all that, sure glad to have had so much help looking at the issues, though :)

It makes me think I'll have to go back to the beginning and also flush out the missile AIs and re-optimize that function in the ship AIs (although there it's getting called very infrequently, it's still sub-optimal in design), too :)

On the Fleet screen... we'll just have to see.  I know that the lag occurs most noticeably when dragging the menu up and down.  Past that, I'm not sure just yet.
Logged
Please check out my SS projects :)
Xeno's Mod Pack

Garmine

  • Lieutenant
  • **
  • Posts: 97
  • We're The Borg. Every resistance is futile.
    • View Profile
Re: Massive CPU Drain (Two different bugs)
« Reply #10 on: February 23, 2014, 09:08:14 AM »

Do you still have infinite fighters?

I recall that on my computer having several squadrons visible with any replacement fighters equals a whole lot of sprites drawn / squadron. AFAIK Starsector uses old OpenGL, so I'd imagine rendering those sprites is simply a cycle that draws ~200 quads / squadron one by one. Which could be something that slows things down, depending on implementation... :)
Logged
I went outside one day, the graphics were amazing, but the gameplay sucked...
Prepare yourselves for miscapitalization errors! *evil laugh*