isaumya / wp-server-stats

Monitor your WordPress site the right way with most important server stats like memory usage, CPU load, uptime & more.
https://wordpress.org/plugins/wp-server-stats/
MIT License
15 stars 7 forks source link

Memory reporting used + cached instead of used #7

Closed razvanfatu closed 5 years ago

razvanfatu commented 5 years ago

Thank you very much for the plugin, it's quite useful. I've tried it on a few installations so far and it looks like the "Real Time RAM Usage" reports the sum of Used and Cached RAM instead of the Used RAM.

From my understanding, Linux tends to use up available RAM for disk caching, so this value would always be around 90% of all available RAM no matter what; the cached RAM is available to applications to use when requested, so what is relevant is the Used RAM (see screen capture).

RAM

This is from an AWS Lightsail instance, but I get the same on various setups (Synology Web Station, local VM with Ubuntu 16 LTS etc).

Is there anything that can be done about it ?

Thanks, Razvan.

isaumya commented 5 years ago

@razvanfatu Trust me I know about this problem and honestly, I don't think I can do much to fix it, let me explain you why so that if you have any idea you can share that.

See, the first and most important thing is that this is a WordPress plugin, which means it runs on a million different server configuration and OS (I mean separate distros). So, I had to carefully choose commands to ensure they run on any UNIX system no matter what distro is being used.

Also, it was very important that to run this plugin the customers don't have to install any special package or anything like that as not everyone has root access nor everyone likes to do sysadmin things. Also, many managed hosts are rude and they won't help the users.

So, finally /proc/meminfo has been used in the plugin to get the RAM data. Now in some distros /proc/meminfo do provide the actual memory available number i.e. free + cache like in Ubuntu and as per a user's request I did push an update to use that data for more accuracy.

But after that update, I quickly realized what a havoc mess I was in. You see the plugin being used by thousands and thousands of people around the world. Some using a distro that doesn't have those fields in /proc/meminfo. In some cases some hosting company was using super optimized distros which didn't have those data and very quickly the plugin started crashing in many places.

Hence, I had to roll back to the old check to ensure it works everywhere.

Here is how I get the data.

Total RAM: grep -w 'MemTotal' /proc/meminfo | grep -o -E '[0-9]+'

Free RAM: grep -w 'MemFree' /proc/meminfo | grep -o -E '[0-9]+'

Hope this helps. Let me know if you have any other queries.

razvanfatu commented 5 years ago

Thanks for the reply and detailed information. I understand now, it's not really a bug but a way to display that adds consistency across all platforms.

I just checked on Ubuntu 16.04 LTE, Raspbian 10 and Synology DSM 6.2.2, adjusting your queries I can get the cached and buffered memory:

Cached: grep -w 'Cached' /proc/meminfo | grep -o -E '[0-9]+' Buffered: grep -w 'Buffers' /proc/meminfo | grep -o -E '[0-9]+'

On newer releases there is also a way to get the available memory directly, but we can assume this is not as common as the previous two:

Avialable: grep -w 'MemAvailable' /proc/meminfo | grep -o -E '[0-9]+'

If the data is not available in proc/meminfo it returns nothing.

The only solution I see is to check if the first two commands above return anything and if so, also display a new line with (let's call it) Real Time Used Memory as [MemTotal - MemFree - Cached - Buffers] and add an overlay on the percentage bar with (Real Time Used Memory / Total Memory)*100 like below.

mem

Not sure how easy (or possible) that is to implement.

Cheers, Razvan.

isaumya commented 5 years ago

@razvanfatu Yes I can do check if buffer and cache data exists and if it does then I can add those numbers with the MemFree and then do the math otherwise just takes the data of memfree. But from your experience in how many systems the Buffer and Cache data exists? Also yes earlier I tried getting the data from MemAvailable which caused the entire fiasco.

Just to be clear (Memfree + buffer + cache) is the actual RAM free correct?

razvanfatu commented 5 years ago

You won't find Buffer / Cache on shared hosting plans or on many cloud /managed VPS. It should be available on AWS/Digitalocean instances or dedicated machines, which is where people care more about these metrics anyway.

That is correct, the memory occupied by Buffers & Cache is not free but is available to applications if needed. This is how it looks visually (disregard Reserved as that's particular to that machine):

mem-usage

"Used" is what applications use (including Apache, MySQL, PHP etc) and uses as much as it needs. Whatever is left is temporarily used by the operating system to buffer/cache disk data so it's easier to access; whenever the applications require more memory, buffer/cache data is discarded.

Cheers, Razvan.

isaumya commented 5 years ago

@razvanfatu Thank you very much for all your help in order to resolve this matter. I've just now pushed v1.6.8 which is supposed to provide you with the accurate RAM usage details across all systems (distros). If possible please test v1.6.8 and let me know if now it provides accurate numbers or not. Looking forward to your reply.

razvanfatu commented 5 years ago

Excellent, thank you very much. Tried on 3 installs:

AWS Lightsail host (1vCPU, 1GB RAM, custom Ubuntu 16.04 LTE): looks OK, works fine. The "Total RAM Usage" is the sum of "used" and "shared" memory (from the free command), which is correct as the shared memory is also in use by tmpfs / GEM graphics buffers and not available.

Synology NAS (2vCPU, 2GB RAM, their custom GNU/Linux 'DSM 6.2.2'): works fine.

Siteground cloud server (8vCPU, 8GB RAM, CentOS '3.12.18-clouder0'): proc/meminfo is missing, the plugin doesn't report memory / CPU load but works fine otherwise.

Cheers, Razvan.

isaumya commented 5 years ago

@razvanfatu That's great! 😃 Thanks for reporting and testing. I'm closing this issue now. If possible, please provide a review for WP Server Stats plugin.