Uploaded image for project: 'ZABBIX BUGS AND ISSUES'
  1. ZABBIX BUGS AND ISSUES
  2. ZBX-11568

zbx_procstat_get_util() does not round up results correctly

XMLWordPrintable

    • Sprint 1, Sprint 2
    • 1

      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.

            Unassigned Unassigned
            constantin.oshmyan Constantin Oshmyan
            Team C
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: