[ZBX-11568] zbx_procstat_get_util() does not round up results correctly Created: 2016 Dec 07  Updated: 2024 Apr 10  Resolved: 2017 Mar 05

Status: Closed
Project: ZABBIX BUGS AND ISSUES
Component/s: Agent (G)
Affects Version/s: 3.0.4, 3.0.5
Fix Version/s: 3.0.8rc1, 3.2.4rc1, 3.4.0alpha1

Type: Incident report Priority: Trivial
Reporter: Constantin Oshmyan Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: agent
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Team: Team C
Sprint: Sprint 1, Sprint 2
Story Points: 1

 Description   

The function zbx_procstat_get_util() in the file src/zabbix_agent/procstat.c calculates the CPU utilization. It gives the result as a percentage with accuracy 0.1% (type "double"). However, most calculations (probably, for optimization purpose) are with the integer types (variables ticks_diff and time_diff have type zbx_uint64_t).

The final part of calculations is the following (lines 1102 – 1105):

	/* 1e9 (nanoseconds) * 1e2 (percent) * 1e1 (one digit decimal place) */
	ticks_diff *= __UINT64_C(1000000000000);
	ticks_diff /= time_diff * sysconf(_SC_CLK_TCK);
	*value = (double)ticks_diff / 10;

The main idea is, probably, used CPU time (in nanoseconds, multiplied to 1000) to divide by the elapsed time (in nanoseconds) to obtain the number of tens portion of percent before casting integer to double.
However, the integer division does not round up the result, remainder is just truncated. So, the real result is, for example (1000 / 6) * 0.1 will be 16.6 instead of 16.7.

If it is the intentional behaviour, this ticket could be closed safely.
Otherwise it could be corrected, for example, by the following way:

	/* 1e9 (nanoseconds) * 1e2 (percent) * 1e2 (two digit decimal places for correct rounding up) */
	ticks_diff *= __UINT64_C(10000000000000);    <------ one zero longer
	ticks_diff /= time_diff * sysconf(_SC_CLK_TCK);
	/*correct rounding up */
	ticks_diff += 5;
	ticks_diff /= 10;
	*value = (double)ticks_diff / 10;

This part of code exists, at least, in versions 3.0.4, 3.0.5 and zabbix-3.0.6rc1.



 Comments   
Comment by Constantin Oshmyan [ 2016 Dec 07 ]

This part of code exists, at least, in versions 3.0.4, 3.0.5 and zabbix-3.0.6rc1.

...and just released v3.0.6 also.

Comment by Andrea Biscuola (Inactive) [ 2017 Feb 16 ]

Fixed in svn://svn.zabbix.com/branches/dev/ZBX-11568

Note: As there is no established rules regarding roundings, please if necessary provide additional info's regarding the test results (if the reported values are satisfactory or not).

Comment by Andris Zeila [ 2017 Feb 17 ]

(1)
The following fix will not work because integer division will not result in floating number without explicit cast

*value = round(ticks_diff / (time_diff * sysconf(_SC_CLK_TCK))) / 10;

To fix it the ticks_diff should be forced to double value first

*value = round((double)ticks_diff / (time_diff * sysconf(_SC_CLK_TCK))) / 10;

abs Yes, completely forgot to add the explicit cast. RESOLVED in r65781

wiper CLOSED

Comment by Andris Zeila [ 2017 Feb 17 ]

Successfully tested

Comment by Andrea Biscuola (Inactive) [ 2017 Feb 17 ]

Fixed in r65790 for:

  • 3.0.8rc1
  • 3.2.4rc1
  • 3.3.0 (trunk)
Comment by Alexander Vladishev [ 2017 Feb 17 ]

(2) [G] math.h already included in sysinc.h

src/zabbix_agent/procstat.c

@@ -17,6 +17,8 @@
 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 **/
 
+#include <math.h>
+
 #include "common.h"
 #include "log.h"
 #include "mutexs.h"

abs RESOLVED in r66032 (all the branches). Also as the issue was already tested, i think it can be safely closed

sasha CLOSED

Comment by richlv [ 2017 Feb 20 ]

(3) the changelog entry currently says "fixed wrong number round for cpu statistics" - maybe something like "fixed incorrect number rounding for CPU statistics" would be slightly better

abs FIXED in 66035

sasha CLOSED

Comment by Martins Valkovskis [ 2017 Mar 02 ]

Briefly mentioned in 'What's new' for 3.0.8 and 3.2.4

Generated at Wed Apr 24 05:29:23 EEST 2024 using Jira 9.12.4#9120004-sha1:625303b708afdb767e17cb2838290c41888e9ff0.