@cryptotaxi247 / netdata-1 / commits / d06308dcb

added command line option to apps.plugin to enable or disable exited childs resource usage; prevented apps.plugin from generating negative values when childs are enabled

Costa Tsaousis committed Jun 30, 2016 at 02:02 UTC d06308dcba6f7884fa028ddead70328c43dc10f9
1 file changed +79 -56
src/apps_plugin.c
+79 -56
@@ -54,7 +54,7 @@ int debug = 0;
54 int update_every = 1;
55 unsigned long long file_counter = 0;
56 int proc_pid_cmdline_is_needed = 0;
57 -
57 +int include_exited_childs = 1;
58 char *host_prefix = "";
59 char *config_dir = CONFIG_DIR;
60
@@ -1334,15 +1334,16 @@ void link_all_processes_to_their_parents(void) {
1334 // the first time we see an entry
1335 // we remove the exited children figures
1336 // to avoid spikes
1337 - p->fix_cminflt += p->cminflt;
1338 - p->fix_cmajflt += p->cmajflt;
1339 - p->fix_cutime += p->cutime;
1340 - p->fix_cstime += p->cstime;
1337 + p->fix_cminflt = p->cminflt;
1338 + p->fix_cmajflt = p->cmajflt;
1339 + p->fix_cutime = p->cutime;
1340 + p->fix_cstime = p->cstime;
1341 }
1342
1343 if(likely(p->ppid > 0 && all_pids[p->ppid])) {
1344 + // valid parent processes
1345 +
1346 struct pid_stat *pp;
1345 - // for valid processes
1347
1348 p->parent = pp = all_pids[p->ppid];
1349 p->parent->children_count++;
@@ -1371,6 +1372,11 @@ void link_all_processes_to_their_parents(void) {
1372 pp->fix_cutime += p->utime + p->cutime + p->fix_cutime;
1373 pp->fix_cstime += p->stime + p->cstime + p->fix_cstime;
1374
1375 + if(unlikely(pp->cminflt < pp->fix_cminflt)) pp->fix_cminflt = pp->cminflt;
1376 + if(unlikely(pp->cmajflt < pp->fix_cmajflt)) pp->fix_cmajflt = pp->cmajflt;
1377 + if(unlikely(pp->cutime < pp->fix_cutime)) pp->fix_cutime = pp->cutime;
1378 + if(unlikely(pp->cstime < pp->fix_cstime)) pp->fix_cstime = pp->cstime;
1379 +
1380 if(unlikely(debug))
1381 fprintf(stderr, "apps.plugin: \tupdating child metrics of %d (%s, %s) to its parent %d (%s, %s). Parent has now: utime=%llu, stime=%llu, minflt=%llu, majflt=%llu, cutime=%llu, cstime=%llu, cminflt=%llu, cmajflt=%llu, fix_cutime=%llu, fix_cstime=%llu, fix_cminflt=%llu, fix_cmajflt=%llu\n", p->pid, p->comm, p->updated?"running":"exited", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cminflt, pp->cmajflt, pp->fix_cutime, pp->fix_cstime, pp->fix_cminflt, pp->fix_cmajflt);
1382 }
@@ -1627,41 +1633,46 @@ void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target
1633 // IMPORTANT
1634 // We add/subtract the last/OLD values we added to the target
1635
1630 - w->fix_cutime -= (p->last_cutime - p->last_fix_cutime);
1631 - w->fix_cstime -= (p->last_cstime - p->last_fix_cstime);
1632 - w->fix_cminflt -= (p->last_cminflt - p->last_fix_cminflt);
1633 - w->fix_cmajflt -= (p->last_cmajflt - p->last_fix_cmajflt);
1636 + unsigned long long cutime = p->last_cutime - p->last_fix_cutime;
1637 + unsigned long long cstime = p->last_cstime - p->last_fix_cstime;
1638 + unsigned long long cminflt = p->last_cminflt - p->last_fix_cminflt;
1639 + unsigned long long cmajflt = p->last_cmajflt - p->last_fix_cmajflt;
1640
1635 - w->fix_utime -= p->last_utime;
1636 - w->fix_stime -= p->last_stime;
1641 + w->fix_cutime -= cutime;
1642 + w->fix_cstime -= cstime;
1643 + w->fix_cminflt -= cminflt;
1644 + w->fix_cmajflt -= cmajflt;
1645 +
1646 + w->fix_utime -= p->last_utime;
1647 + w->fix_stime -= p->last_stime;
1648 w->fix_minflt -= p->last_minflt;
1649 w->fix_majflt -= p->last_majflt;
1650
1640 - w->fix_io_logical_bytes_read -= p->last_io_logical_bytes_read;
1651 + w->fix_io_logical_bytes_read -= p->last_io_logical_bytes_read;
1652 w->fix_io_logical_bytes_written -= p->last_io_logical_bytes_written;
1642 - w->fix_io_read_calls -= p->last_io_read_calls;
1643 - w->fix_io_write_calls -= p->last_io_write_calls;
1644 - w->fix_io_storage_bytes_read -= p->last_io_storage_bytes_read;
1653 + w->fix_io_read_calls -= p->last_io_read_calls;
1654 + w->fix_io_write_calls -= p->last_io_write_calls;
1655 + w->fix_io_storage_bytes_read -= p->last_io_storage_bytes_read;
1656 w->fix_io_storage_bytes_written -= p->last_io_storage_bytes_written;
1657 w->fix_io_cancelled_write_bytes -= p->last_io_cancelled_write_bytes;
1658
1659 // ---
1660
1650 - o->fix_cutime += (p->last_cutime - p->last_fix_cutime);
1651 - o->fix_cstime += (p->last_cstime - p->last_fix_cstime);
1652 - o->fix_cminflt += (p->last_cminflt - p->last_fix_cminflt);
1653 - o->fix_cmajflt += (p->last_cmajflt - p->last_fix_cmajflt);
1661 + o->fix_cutime += cutime;
1662 + o->fix_cstime += cstime;
1663 + o->fix_cminflt += cminflt;
1664 + o->fix_cmajflt += cmajflt;
1665
1655 - o->fix_utime += p->last_utime;
1656 - o->fix_stime += p->last_stime;
1666 + o->fix_utime += p->last_utime;
1667 + o->fix_stime += p->last_stime;
1668 o->fix_minflt += p->last_minflt;
1669 o->fix_majflt += p->last_majflt;
1670
1660 - o->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1671 + o->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1672 o->fix_io_logical_bytes_written += p->last_io_logical_bytes_written;
1662 - o->fix_io_read_calls += p->last_io_read_calls;
1663 - o->fix_io_write_calls += p->last_io_write_calls;
1664 - o->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1673 + o->fix_io_read_calls += p->last_io_read_calls;
1674 + o->fix_io_write_calls += p->last_io_write_calls;
1675 + o->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1676 o->fix_io_storage_bytes_written += p->last_io_storage_bytes_written;
1677 o->fix_io_cancelled_write_bytes += p->last_io_cancelled_write_bytes;
1678 }
@@ -1671,29 +1682,33 @@ void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target
1682
1683 // since the process has exited, the user
1684 // will see a drop in our charts, because the incremental
1674 - // values of this process will not be there
1685 + // values of this process will not be there from now on
1686
1687 // add them to the fix_* values and they will be added to
1688 // the reported values, so that the report goes steady
1678 - w->fix_minflt += p->minflt;
1679 - w->fix_majflt += p->majflt;
1680 - w->fix_utime += p->utime;
1681 - w->fix_stime += p->stime;
1689
1683 - w->fix_cminflt += (p->cminflt - p->fix_cminflt);
1684 - w->fix_cmajflt += (p->cmajflt - p->fix_cmajflt);
1685 - w->fix_cutime += (p->cutime - p->fix_cutime);
1686 - w->fix_cstime += (p->cstime - p->fix_cstime);
1690 + w->fix_minflt += p->last_minflt;
1691 + w->fix_majflt += p->last_majflt;
1692 + w->fix_utime += p->last_utime;
1693 + w->fix_stime += p->last_stime;
1694 +
1695 + w->fix_cminflt += (p->last_cminflt - p->last_fix_cminflt);
1696 + w->fix_cmajflt += (p->last_cmajflt - p->last_fix_cmajflt);
1697 + w->fix_cutime += (p->last_cutime - p->last_fix_cutime);
1698 + w->fix_cstime += (p->last_cstime - p->last_fix_cstime);
1699
1688 - w->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1689 - w->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1690 - w->fix_io_read_calls += p->io_read_calls;
1691 - w->fix_io_write_calls += p->io_write_calls;
1692 - w->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1693 - w->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1694 - w->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1700 + w->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1701 + w->fix_io_logical_bytes_written += p->last_io_logical_bytes_written;
1702 + w->fix_io_read_calls += p->last_io_read_calls;
1703 + w->fix_io_write_calls += p->last_io_write_calls;
1704 + w->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1705 + w->fix_io_storage_bytes_written += p->last_io_storage_bytes_written;
1706 + w->fix_io_cancelled_write_bytes += p->last_io_cancelled_write_bytes;
1707 }
1708
1709 + //if((long long)w->cutime + w->fix_cutime < 0)
1710 + // error("Negative total cutime (%llu - %lld) on target %s after adding process %d (%s, %s) with utime=%llu, stime=%llu, minflt=%llu, majflt=%llu, cutime=%llu, cstime=%llu, cminflt=%llu, cmajflt=%llu, fix_cutime=%llu, fix_cstime=%llu, fix_cminflt=%llu, fix_cmajflt=%llu\n",
1711 + // w->cutime, w->fix_cutime, w->name, p->pid, p->comm, p->updated?"running":"exited", p->utime, p->stime, p->minflt, p->majflt, p->cutime, p->cstime, p->cminflt, p->cmajflt, p->fix_cutime, p->fix_cstime, p->fix_cminflt, p->fix_cmajflt);
1712 }
1713
1714 void count_targets_fds(struct target *root) {
@@ -1886,7 +1901,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, unsig
1901 for (w = root; w ; w = w->next) {
1902 if(w->target || (!w->processes && !w->exposed)) continue;
1903
1889 - fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime + w->cutime + w->cstime + w->fix_cutime + w->fix_cstime);
1904 + fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime + (include_exited_childs?(w->cutime + w->cstime + w->fix_cutime + w->fix_cstime):0));
1905 }
1906 fprintf(stdout, "END\n");
1907
@@ -1894,7 +1909,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, unsig
1909 for (w = root; w ; w = w->next) {
1910 if(w->target || (!w->processes && !w->exposed)) continue;
1911
1897 - fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime + w->cutime + w->fix_cutime);
1912 + fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime + (include_exited_childs?(w->cutime + w->fix_cutime):0));
1913 }
1914 fprintf(stdout, "END\n");
1915
@@ -1902,7 +1917,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, unsig
1917 for (w = root; w ; w = w->next) {
1918 if(w->target || (!w->processes && !w->exposed)) continue;
1919
1905 - fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime + w->cstime + w->fix_cstime);
1920 + fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime + (include_exited_childs?(w->cstime + w->fix_cstime):0));
1921 }
1922 fprintf(stdout, "END\n");
1923
@@ -1934,7 +1949,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, unsig
1949 for (w = root; w ; w = w->next) {
1950 if(w->target || (!w->processes && !w->exposed)) continue;
1951
1937 - fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt + w->cminflt + w->fix_cminflt);
1952 + fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt + (include_exited_childs?(w->cminflt + w->fix_cminflt):0));
1953 }
1954 fprintf(stdout, "END\n");
1955
@@ -1942,7 +1957,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, unsig
1957 for (w = root; w ; w = w->next) {
1958 if(w->target || (!w->processes && !w->exposed)) continue;
1959
1945 - fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt + w->cmajflt + w->fix_cmajflt);
1960 + fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt + (include_exited_childs?(w->cmajflt + w->fix_cmajflt):0));
1961 }
1962 fprintf(stdout, "END\n");
1963
@@ -2156,6 +2171,16 @@ void parse_args(int argc, char **argv)
2171 continue;
2172 }
2173
2174 + if(strcmp("no-childs", argv[i]) == 0) {
2175 + include_exited_childs = 0;
2176 + continue;
2177 + }
2178 +
2179 + if(strcmp("with-childs", argv[i]) == 0) {
2180 + include_exited_childs = 1;
2181 + continue;
2182 + }
2183 +
2184 if(!name) {
2185 name = argv[i];
2186 continue;
@@ -2211,8 +2236,6 @@ int main(int argc, char **argv)
2236 }
2237 #endif /* NETDATA_INTERNAL_CHECKS */
2238
2214 - info("starting...");
2215 -
2239 procfile_adaptive_initial_allocation = 1;
2240
2241 time_t started_t = time(NULL);
@@ -2231,13 +2254,13 @@ int main(int argc, char **argv)
2254 }
2255
2256 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2234 - "DIMENSION user '' incremental 1 1000\n"
2235 - "DIMENSION system '' incremental 1 1000\n"
2236 - "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2237 - "DIMENSION files '' incremental 1 1\n"
2238 - "DIMENSION pids '' absolute 1 1\n"
2239 - "DIMENSION fds '' absolute 1 1\n"
2240 - "DIMENSION targets '' absolute 1 1\n", update_every);
2257 + "DIMENSION user '' incremental 1 1000\n"
2258 + "DIMENSION system '' incremental 1 1000\n"
2259 + "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2260 + "DIMENSION files '' incremental 1 1\n"
2261 + "DIMENSION pids '' absolute 1 1\n"
2262 + "DIMENSION fds '' absolute 1 1\n"
2263 + "DIMENSION targets '' absolute 1 1\n", update_every);
2264
2265 #ifndef PROFILING_MODE
2266 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;