Help with load issues

Hi All,

I have posted about this in the past but every time I hit a road block and gave up. I want to try to really focus this time on getting the issue solved. I have a system that is used to play recordings on the phone. All calls come from the a SIP providre with PJSIP. Every call that comes in launches a php agi for under a half second. The system then uses ODBC to get a list of files to play. Once the user selects a file to play Asterisk uses ControlPlayBack to play the file from CloudFlare R2. Once we go over 50-75 calls the load on the system will randomly go up for a few moments and then go down. I have written a lot of scripts to try to get stats on the system. The problem is the load jumps for such a small period that by the time I see anything the load is on the way back down and it’s impossible to see what is causing it. For instance right now here is what my top looks like.

top - 17:53:08 up 407 days, 34 min, 1 user, load average: 0.83, 2.76, 4.58
Tasks: 718 total, 1 running, 717 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.3 us, 0.4 sy, 0.0 ni, 99.0 id, 0.0 wa, 0.1 hi, 0.2 si, 0.0 st
MiB Mem : 7681.6 total, 322.0 free, 6318.9 used, 1040.7 buff/cache
MiB Swap: 0.0 total, 0.0 free, 0.0 used. 581.3 avail Mem

PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                  

1380869 user 20 0 19.8g 4.9g 9588 S 29.4 65.6 23227:40 asterisk
3447012 root 20 0 264768 5260 3780 R 11.8 0.1 0:00.03 top
1 root 20 0 241192 8216 3076 S 0.0 0.1 100:30.63 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:37.57 kthreadd

You can see a short while ago the load went up but now it’s stable. Whenever the load goes up for that short period, it takes time for it to come down. During that entire time period the quality goes bad and it seems packets are dropped.

I also tried doing
watch -n 0.2 'top -b -n 1 | head -n 10'
and watching in realtime if I see any process maxing out the CPU for a moment (I know load is not just CPU but memory and disk too). I am not able to see any correlation with the processes in top and what would cause the load to randomly jump. I don’t think it’s the AGI’s as for instance now I just did channel hangup all which killed 90 calls all that call an agi on hangup and it didn’t jump. I am running 22.10.0 on a VPS whose host I control. I also noticed that it seemed like there is some sort of memory leak as asterisk was using 65% of the memory (the VM has 8GB of ram).

Any pointers what could be causing the spikes and were I should start looking?

regular agi ? agi()?
or with node ?

regular has the above issues

Two things stand out in your top output. Asterisk is using about 4.9 GB of an 8 GB VM with no swap and only ~320 MB free, so any burst (page cache pressure, a big allocation) can stall everything and cause the choppy audio. A growing RES over a 400-day uptime is worth chasing: check “core show channels” for leaked channels, and if you built it yourself, run with MALLOC_DEBUG or use valgrind/heap profiling on a test box.

For catching short spikes, sample from a service rather than by eye: run “sar -u -q -r 1” or atop with 1s logging, then read it back after a spike. Also check iowait and “dmesg” for steal time, since on a VPS the host can cause load jumps. Enable “pjsip set logger” briefly around a spike, and check the ODBC connection pool for slow queries.

(Drafted with AI help.)

Thanks. What’s strange is I restarted Asterisk last night, I logged the load and over night the load randomly spiked. I can’t enable pjsip logger since spotting the jump is very hard, it comes then right away goes away. I am rebuilding asterisk 22.11.0 with MALLOC_DEBUG to see if I can find anything.

I am using regular AGI but I will be switching soon to a swool daemon to see if that helps. I also believe I tried fastagi in the past and it didnt help though from what I recall phpfastagi still invokes a new php script every time so I am not sure testing this way was/is valid.

The only 320 MB free is normal, or even represents undercommitment. Both Linux and Windows deliberately run with only enough free RAM to cover for short term peaks. It would be quite difficult to work out how much memory is actually needed to avoid unnecessary disk reads. Amongst other things, you would need to decide which reads were unnecessary; there are some things were one would assume that something written to file would be read back from the file, rather than from the saved write buffer.

Also you say 8 GB VM. If that VM virtualises the RAM, all bets are off. Virtualised OSes assume that the virtual physical memory is real physical memory, with just the addresses changed.

I think this represents memory mapped files. In any case, it is the Virtual, not the resident that matters. Normal heap and stack allocation would have to be fully resident, so the most likely reason for the large excess of virtual over resident, is either the executable code, or memory mapped files, given that swap isn’t being used.

Fair point, david551. Low free memory on its own isn’t a red flag, since Linux uses spare RAM for cache. I overstated that. Dovid, moving to a persistent AGI daemon like Swoole should help, because each plain AGI call starts a new PHP process, and at 50 to 75 calls those startups can stack up. Since the spikes are too short to catch live, running atop with 1-second logging overnight lets you replay the exact minute afterward and see which process or resource jumped. (Drafted with AI help.)

I restarted Asterisk, free’d up the memory and within hours I had the problems again. I had codex connect to the box and analyze the system in real time. It did things way above my understanding. It said the issue was with AGI’s and how Asterisk forks the process. I am going to try a new agis system and see how it works.

Have you considered writing AGIs in a compiled language like C?

Also, is there anything in your usage that conflicts with FastAGI. Combining that with compiled code could further reduce the startup cost.

Also you said the AI was saying the problem was with your use of AGI, but you didn’t understand the details. It’s OK to quote AI, as long as you make it clear that that is what you are doing.