@cryptotaxi247 / netdata-1 / commits / 0a620ac56

apps.plugin optimization to send all the data to netdata with just one write operation - also avoid using printf

Costa Tsaousis committed Jul 27, 2016 at 03:02 UTC 0a620ac561cd932061bb30f2e0f60a97906d8da8
6 files changed +239 -186
CMakeLists.txt
-2
@@ -96,8 +96,6 @@ set(APPS_PLUGIN_SOURCE_FILES
96 src/log.h
97 src/procfile.c
98 src/procfile.h
99 - src/storage_number.c
100 - src/storage_number.h
99 src/web_buffer.c
100 src/web_buffer.h
101 config.h)
src/Makefile.am
+1
@@ -86,6 +86,7 @@ apps_plugin_SOURCES = \
86 common.c common.h \
87 log.c log.h \
88 procfile.c procfile.h \
89 + web_buffer.c web_buffer.h \
90 $(NULL)
91
92 install-data-hook:
src/apps_plugin.c
+180 -158
@@ -34,6 +34,7 @@
34 #include "log.h"
35 #include "procfile.h"
36 #include "../config.h"
37 +#include "web_buffer.h"
38
39 #ifdef NETDATA_INTERNAL_CHECKS
40 #include <sys/prctl.h>
@@ -66,6 +67,10 @@ char *config_dir = CONFIG_DIR;
67
68 pid_t *all_pids_sortlist = NULL;
69
70 +// will be automatically set to 1, if guest values are collected
71 +int show_guest_time = 0;
72 +int show_guest_time_old = 0;
73 +
74 // ----------------------------------------------------------------------------
75
76 void netdata_cleanup_and_exit(int ret) {
@@ -779,8 +784,11 @@ int read_proc_pid_stat(struct pid_stat *p) {
784 p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
785 p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43+i), NULL, 10);
786
782 - p->utime_raw -= p->gtime_raw;
783 - p->cutime_raw -= p->cgtime_raw;
787 + if(show_guest_time || p->gtime_raw || p->cgtime_raw) {
788 + p->utime_raw -= p->gtime_raw;
789 + p->cutime_raw -= p->cgtime_raw;
790 + show_guest_time = 1;
791 + }
792
793 if(unlikely(debug || (p->target && p->target->debug)))
794 fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, threads=%d\n", host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
@@ -2174,6 +2182,34 @@ void calculate_netdata_statistics(void) {
2182 // ----------------------------------------------------------------------------
2183 // update chart dimensions
2184
2185 +BUFFER *output = NULL;
2186 +int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2187 +
2188 +static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2189 + // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2190 + buffer_strcat(output, "BEGIN ");
2191 + buffer_strcat(output, type);
2192 + buffer_strcat(output, ".");
2193 + buffer_strcat(output, id);
2194 + buffer_strcat(output, " ");
2195 + buffer_print_llu(output, usec);
2196 + buffer_strcat(output, "\n");
2197 +}
2198 +
2199 +static inline void send_SET(const char *name, unsigned long long value) {
2200 + // fprintf(stdout, "SET %s = %llu\n", name, value);
2201 + buffer_strcat(output, "SET ");
2202 + buffer_strcat(output, name);
2203 + buffer_strcat(output, " = ");
2204 + buffer_print_llu(output, value);
2205 + buffer_strcat(output, "\n");
2206 +}
2207 +
2208 +static inline void send_END(void) {
2209 + // fprintf(stdout, "END\n");
2210 + buffer_strcat(output, "END\n");
2211 +}
2212 +
2213 double utime_fix_ratio = 1.0, stime_fix_ratio = 1.0, gtime_fix_ratio = 1.0, cutime_fix_ratio = 1.0, cstime_fix_ratio = 1.0, cgtime_fix_ratio = 1.0;
2214 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2215
@@ -2211,7 +2247,7 @@ unsigned long long send_resource_usage_to_netdata() {
2247 bcopy(&me, &me_last, sizeof(struct rusage));
2248 }
2249
2214 - fprintf(stdout,
2250 + buffer_sprintf(output,
2251 "BEGIN netdata.apps_cpu %llu\n"
2252 "SET user = %llu\n"
2253 "SET system = %llu\n"
@@ -2246,7 +2282,7 @@ unsigned long long send_resource_usage_to_netdata() {
2282 );
2283
2284 if(include_exited_childs)
2249 - fprintf(stdout,
2285 + buffer_sprintf(output,
2286 "BEGIN netdata.apps_children_fix %llu\n"
2287 "SET cutime = %llu\n"
2288 "SET cstime = %llu\n"
@@ -2414,135 +2450,119 @@ void normalize_data(struct target *root) {
2450 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec) {
2451 struct target *w;
2452
2417 - fprintf(stdout, "BEGIN %s.cpu %llu\n", type, usec);
2453 + send_BEGIN(type, "cpu", usec);
2454 for (w = root; w ; w = w->next) {
2419 - if(w->target || (!w->processes && !w->exposed)) continue;
2420 -
2421 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (unsigned long long)(w->stime * stime_fix_ratio) + (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio) + (unsigned long long)(w->cstime * cstime_fix_ratio) + (unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2455 + if(unlikely(w->exposed))
2456 + send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (unsigned long long)(w->stime * stime_fix_ratio) + (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio) + (unsigned long long)(w->cstime * cstime_fix_ratio) + (unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2457 }
2423 - fprintf(stdout, "END\n");
2458 + send_END();
2459
2425 - fprintf(stdout, "BEGIN %s.cpu_user %llu\n", type, usec);
2460 + send_BEGIN(type, "cpu_user", usec);
2461 for (w = root; w ; w = w->next) {
2427 - if(w->target || (!w->processes && !w->exposed)) continue;
2428 -
2429 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2462 + if(unlikely(w->exposed))
2463 + send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2464 }
2431 - fprintf(stdout, "END\n");
2465 + send_END();
2466
2433 - fprintf(stdout, "BEGIN %s.cpu_system %llu\n", type, usec);
2467 + send_BEGIN(type, "cpu_system", usec);
2468 for (w = root; w ; w = w->next) {
2435 - if(w->target || (!w->processes && !w->exposed)) continue;
2436 -
2437 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2469 + if(unlikely(w->exposed))
2470 + send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2471 }
2439 - fprintf(stdout, "END\n");
2440 -
2441 - fprintf(stdout, "BEGIN %s.cpu_guest %llu\n", type, usec);
2442 - for (w = root; w ; w = w->next) {
2443 - if(w->target || (!w->processes && !w->exposed)) continue;
2472 + send_END();
2473
2445 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2474 + if(show_guest_time) {
2475 + send_BEGIN(type, "cpu_guest", usec);
2476 + for (w = root; w ; w = w->next) {
2477 + if(unlikely(w->exposed))
2478 + send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2479 + }
2480 + send_END();
2481 }
2447 - fprintf(stdout, "END\n");
2482
2449 - fprintf(stdout, "BEGIN %s.threads %llu\n", type, usec);
2483 + send_BEGIN(type, "threads", usec);
2484 for (w = root; w ; w = w->next) {
2451 - if(w->target || (!w->processes && !w->exposed)) continue;
2452 -
2453 - fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
2485 + if(unlikely(w->exposed))
2486 + send_SET(w->name, w->num_threads);
2487 }
2455 - fprintf(stdout, "END\n");
2488 + send_END();
2489
2457 - fprintf(stdout, "BEGIN %s.processes %llu\n", type, usec);
2490 + send_BEGIN(type, "processes", usec);
2491 for (w = root; w ; w = w->next) {
2459 - if(w->target || (!w->processes && !w->exposed)) continue;
2460 -
2461 - fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
2492 + if(unlikely(w->exposed))
2493 + send_SET(w->name, w->processes);
2494 }
2463 - fprintf(stdout, "END\n");
2495 + send_END();
2496
2465 - fprintf(stdout, "BEGIN %s.mem %llu\n", type, usec);
2497 + send_BEGIN(type, "mem", usec);
2498 for (w = root; w ; w = w->next) {
2467 - if(w->target || (!w->processes && !w->exposed)) continue;
2468 -
2469 - fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
2499 + if(unlikely(w->exposed))
2500 + send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2501 }
2471 - fprintf(stdout, "END\n");
2502 + send_END();
2503
2473 - fprintf(stdout, "BEGIN %s.minor_faults %llu\n", type, usec);
2504 + send_BEGIN(type, "minor_faults", usec);
2505 for (w = root; w ; w = w->next) {
2475 - if(w->target || (!w->processes && !w->exposed)) continue;
2476 -
2477 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2506 + if(unlikely(w->exposed))
2507 + send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2508 }
2479 - fprintf(stdout, "END\n");
2509 + send_END();
2510
2481 - fprintf(stdout, "BEGIN %s.major_faults %llu\n", type, usec);
2511 + send_BEGIN(type, "major_faults", usec);
2512 for (w = root; w ; w = w->next) {
2483 - if(w->target || (!w->processes && !w->exposed)) continue;
2484 -
2485 - fprintf(stdout, "SET %s = %llu\n", w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2513 + if(unlikely(w->exposed))
2514 + send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2515 }
2487 - fprintf(stdout, "END\n");
2516 + send_END();
2517
2489 - fprintf(stdout, "BEGIN %s.lreads %llu\n", type, usec);
2518 + send_BEGIN(type, "lreads", usec);
2519 for (w = root; w ; w = w->next) {
2491 - if(w->target || (!w->processes && !w->exposed)) continue;
2492 -
2493 - fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
2520 + if(unlikely(w->exposed))
2521 + send_SET(w->name, w->io_logical_bytes_read);
2522 }
2495 - fprintf(stdout, "END\n");
2523 + send_END();
2524
2497 - fprintf(stdout, "BEGIN %s.lwrites %llu\n", type, usec);
2525 + send_BEGIN(type, "lwrites", usec);
2526 for (w = root; w ; w = w->next) {
2499 - if(w->target || (!w->processes && !w->exposed)) continue;
2500 -
2501 - fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
2527 + if(unlikely(w->exposed))
2528 + send_SET(w->name, w->io_logical_bytes_written);
2529 }
2503 - fprintf(stdout, "END\n");
2530 + send_END();
2531
2505 - fprintf(stdout, "BEGIN %s.preads %llu\n", type, usec);
2532 + send_BEGIN(type, "preads", usec);
2533 for (w = root; w ; w = w->next) {
2507 - if(w->target || (!w->processes && !w->exposed)) continue;
2508 -
2509 - fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
2534 + if(unlikely(w->exposed))
2535 + send_SET(w->name, w->io_storage_bytes_read);
2536 }
2511 - fprintf(stdout, "END\n");
2537 + send_END();
2538
2513 - fprintf(stdout, "BEGIN %s.pwrites %llu\n", type, usec);
2539 + send_BEGIN(type, "pwrites", usec);
2540 for (w = root; w ; w = w->next) {
2515 - if(w->target || (!w->processes && !w->exposed)) continue;
2516 -
2517 - fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
2541 + if(unlikely(w->exposed))
2542 + send_SET(w->name, w->io_storage_bytes_written);
2543 }
2519 - fprintf(stdout, "END\n");
2544 + send_END();
2545
2521 - fprintf(stdout, "BEGIN %s.files %llu\n", type, usec);
2546 + send_BEGIN(type, "files", usec);
2547 for (w = root; w ; w = w->next) {
2523 - if(w->target || (!w->processes && !w->exposed)) continue;
2524 -
2525 - fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
2548 + if(unlikely(w->exposed))
2549 + send_SET(w->name, w->openfiles);
2550 }
2527 - fprintf(stdout, "END\n");
2551 + send_END();
2552
2529 - fprintf(stdout, "BEGIN %s.sockets %llu\n", type, usec);
2553 + send_BEGIN(type, "sockets", usec);
2554 for (w = root; w ; w = w->next) {
2531 - if(w->target || (!w->processes && !w->exposed)) continue;
2532 -
2533 - fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
2555 + if(unlikely(w->exposed))
2556 + send_SET(w->name, w->opensockets);
2557 }
2535 - fprintf(stdout, "END\n");
2558 + send_END();
2559
2537 - fprintf(stdout, "BEGIN %s.pipes %llu\n", type, usec);
2560 + send_BEGIN(type, "pipes", usec);
2561 for (w = root; w ; w = w->next) {
2539 - if(w->target || (!w->processes && !w->exposed)) continue;
2540 -
2541 - fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
2562 + if(unlikely(w->exposed))
2563 + send_SET(w->name, w->openpipes);
2564 }
2543 - fprintf(stdout, "END\n");
2544 -
2545 - fflush(stdout);
2565 + send_END();
2566 }
2567
2568
@@ -2554,128 +2574,117 @@ void send_charts_updates_to_netdata(struct target *root, const char *type, const
2574 struct target *w;
2575 int newly_added = 0;
2576
2557 - for(w = root ; w ; w = w->next)
2558 - if(!w->exposed && w->processes) {
2577 + for(w = root ; w ; w = w->next) {
2578 + if (w->target) continue;
2579 +
2580 + if (!w->exposed && w->processes) {
2581 newly_added++;
2582 w->exposed = 1;
2561 - if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2583 + if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2584 }
2585 + }
2586
2587 // nothing more to show
2565 - if(!newly_added) return;
2588 + if(!newly_added && show_guest_time == show_guest_time_old) return;
2589
2590 // we have something new to show
2591 // update the charts
2569 - fprintf(stdout, "CHART %s.cpu '' '%s CPU Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2592 + buffer_sprintf(output, "CHART %s.cpu '' '%s CPU Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2593 for (w = root; w ; w = w->next) {
2571 - if(w->target || (!w->processes && !w->exposed)) continue;
2572 -
2573 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2594 + if(unlikely(w->exposed))
2595 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2596 }
2597
2576 - fprintf(stdout, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2598 + buffer_sprintf(output, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2599 for (w = root; w ; w = w->next) {
2578 - if(w->target || (!w->processes && !w->exposed)) continue;
2579 -
2580 - fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2600 + if(unlikely(w->exposed))
2601 + buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2602 }
2603
2583 - fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2604 + buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2605 for (w = root; w ; w = w->next) {
2585 - if(w->target || (!w->processes && !w->exposed)) continue;
2586 -
2587 - fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2606 + if(unlikely(w->exposed))
2607 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2608 }
2609
2590 - fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2610 + buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2611 for (w = root; w ; w = w->next) {
2592 - if(w->target || (!w->processes && !w->exposed)) continue;
2593 -
2594 - fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2612 + if(unlikely(w->exposed))
2613 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2614 }
2615
2597 - fprintf(stdout, "CHART %s.cpu_user '' '%s CPU User Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2616 + buffer_sprintf(output, "CHART %s.cpu_user '' '%s CPU User Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2617 for (w = root; w ; w = w->next) {
2599 - if(w->target || (!w->processes && !w->exposed)) continue;
2600 -
2601 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2618 + if(unlikely(w->exposed))
2619 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2620 }
2621
2604 - fprintf(stdout, "CHART %s.cpu_system '' '%s CPU System Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2622 + buffer_sprintf(output, "CHART %s.cpu_system '' '%s CPU System Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2623 for (w = root; w ; w = w->next) {
2606 - if(w->target || (!w->processes && !w->exposed)) continue;
2607 -
2608 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2624 + if(unlikely(w->exposed))
2625 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2626 }
2627
2611 - fprintf(stdout, "CHART %s.cpu_guest '' '%s CPU Guest Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20022 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2612 - for (w = root; w ; w = w->next) {
2613 - if(w->target || (!w->processes && !w->exposed)) continue;
2614 -
2615 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2628 + if(show_guest_time) {
2629 + buffer_sprintf(output, "CHART %s.cpu_guest '' '%s CPU Guest Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20022 %d\n", type, title, (processors * 100), processors, (processors > 1) ? "s" : "", type, update_every);
2630 + for (w = root; w; w = w->next) {
2631 + if(unlikely(w->exposed))
2632 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2633 + }
2634 }
2635
2618 - fprintf(stdout, "CHART %s.major_faults '' '%s Major Page Faults (swap read)' 'page faults/s' swap %s.major_faults stacked 20010 %d\n", type, title, type, update_every);
2636 + buffer_sprintf(output, "CHART %s.major_faults '' '%s Major Page Faults (swap read)' 'page faults/s' swap %s.major_faults stacked 20010 %d\n", type, title, type, update_every);
2637 for (w = root; w ; w = w->next) {
2620 - if(w->target || (!w->processes && !w->exposed)) continue;
2621 -
2622 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2638 + if(unlikely(w->exposed))
2639 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2640 }
2641
2625 - fprintf(stdout, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2642 + buffer_sprintf(output, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2643 for (w = root; w ; w = w->next) {
2627 - if(w->target || (!w->processes && !w->exposed)) continue;
2628 -
2629 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2644 + if(unlikely(w->exposed))
2645 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2646 }
2647
2632 - fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2648 + buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2649 for (w = root; w ; w = w->next) {
2634 - if(w->target || (!w->processes && !w->exposed)) continue;
2635 -
2636 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2650 + if(unlikely(w->exposed))
2651 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2652 }
2653
2639 - fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2654 + buffer_sprintf(output, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2655 for (w = root; w ; w = w->next) {
2641 - if(w->target || (!w->processes && !w->exposed)) continue;
2642 -
2643 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2656 + if(unlikely(w->exposed))
2657 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2658 }
2659
2646 - fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2660 + buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2661 for (w = root; w ; w = w->next) {
2648 - if(w->target || (!w->processes && !w->exposed)) continue;
2649 -
2650 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2662 + if(unlikely(w->exposed))
2663 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2664 }
2665
2653 - fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2666 + buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2667 for (w = root; w ; w = w->next) {
2655 - if(w->target || (!w->processes && !w->exposed)) continue;
2656 -
2657 - fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2668 + if(unlikely(w->exposed))
2669 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2670 }
2671
2660 - fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2672 + buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2673 for (w = root; w ; w = w->next) {
2662 - if(w->target || (!w->processes && !w->exposed)) continue;
2663 -
2664 - fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2674 + if(unlikely(w->exposed))
2675 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2676 }
2677
2667 - fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2678 + buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2679 for (w = root; w ; w = w->next) {
2669 - if(w->target || (!w->processes && !w->exposed)) continue;
2670 -
2671 - fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2680 + if(unlikely(w->exposed))
2681 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2682 }
2683
2674 - fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2684 + buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2685 for (w = root; w ; w = w->next) {
2676 - if(w->target || (!w->processes && !w->exposed)) continue;
2677 -
2678 - fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2686 + if(unlikely(w->exposed))
2687 + buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2688 }
2689 }
2690
@@ -2792,7 +2801,11 @@ int main(int argc, char **argv)
2801 exit(1);
2802 }
2803
2795 - fprintf(stdout,
2804 + output = buffer_create(1024);
2805 + if(!output)
2806 + fatal("Cannot create BUFFER.");
2807 +
2808 + buffer_sprintf(output,
2809 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2810 "DIMENSION user '' incremental 1 1000\n"
2811 "DIMENSION system '' incremental 1 1000\n"
@@ -2812,7 +2825,7 @@ int main(int argc, char **argv)
2825 );
2826
2827 if(include_exited_childs)
2815 - fprintf(stdout,
2828 + buffer_sprintf(output,
2829 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2830 "DIMENSION cutime '' absolute 1 %2$llu\n"
2831 "DIMENSION cstime '' absolute 1 %2$llu\n"
@@ -2860,6 +2873,15 @@ int main(int argc, char **argv)
2873 send_collected_data_to_netdata(users_root_target, "users", dt);
2874 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2875
2876 + show_guest_time_old = show_guest_time;
2877 +
2878 + //if(puts(buffer_tostring(output)) == EOF)
2879 + if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2880 + fatal("Cannot send chart values to netdata.");
2881 +
2882 + // fflush(stdout);
2883 + buffer_flush(output);
2884 +
2885 if(unlikely(debug))
2886 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2887
src/storage_number.c
+5 -26
@@ -17,6 +17,9 @@
17 #endif
18 #endif
19
20 +extern char *print_number_lu_r(char *str, unsigned long uvalue);
21 +extern char *print_number_llu_r(char *str, unsigned long long uvalue);
22 +
23 storage_number pack_storage_number(calculated_number value, uint32_t flags)
24 {
25 // bit 32 = sign 0:positive, 1:negative
@@ -119,30 +122,6 @@ calculated_number unpack_storage_number(storage_number value)
122 return n;
123 }
124
122 -#ifdef ENVIRONMENT32
123 -// This trick seems to give an 80% speed increase in 32bit systems
124 -// print_calculated_number_llu_r() will just print the digits up to the
125 -// point the remaining value fits in 32 bits, and then calls
126 -// print_calculated_number_lu_r() to print the rest with 32 bit arithmetic.
127 -
128 -static char *print_calculated_number_lu_r(char *str, unsigned long uvalue) {
129 - char *wstr = str;
130 -
131 - // print each digit
132 - do *wstr++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
133 - return wstr;
134 -}
135 -
136 -static char *print_calculated_number_llu_r(char *str, unsigned long long uvalue) {
137 - char *wstr = str;
138 -
139 - // print each digit
140 - do *wstr++ = (char)('0' + (uvalue % 10)); while((uvalue /= 10) && uvalue > (unsigned long long)0xffffffff);
141 - if(uvalue) return print_calculated_number_lu_r(wstr, uvalue);
142 - return wstr;
143 -}
144 -#endif
145 -
125 int print_calculated_number(char *str, calculated_number value)
126 {
127 char *wstr = str;
@@ -160,9 +139,9 @@ int print_calculated_number(char *str, calculated_number value)
139
140 #ifdef ENVIRONMENT32
141 if(uvalue > (unsigned long long)0xffffffff)
163 - wstr = print_calculated_number_llu_r(str, uvalue);
142 + wstr = print_number_llu_r(str, uvalue);
143 else
165 - wstr = print_calculated_number_lu_r(str, uvalue);
144 + wstr = print_number_lu_r(str, uvalue);
145 #else
146 do *wstr++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
147 #endif
src/web_buffer.c
+48
@@ -73,6 +73,54 @@ void buffer_char_replace(BUFFER *wb, char from, char to)
73 buffer_overflow_check(wb);
74 }
75
76 +// This trick seems to give an 80% speed increase in 32bit systems
77 +// print_calculated_number_llu_r() will just print the digits up to the
78 +// point the remaining value fits in 32 bits, and then calls
79 +// print_calculated_number_lu_r() to print the rest with 32 bit arithmetic.
80 +
81 +inline char *print_number_lu_r(char *str, unsigned long uvalue) {
82 + char *wstr = str;
83 +
84 + // print each digit
85 + do *wstr++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
86 + return wstr;
87 +}
88 +
89 +inline char *print_number_llu_r(char *str, unsigned long long uvalue) {
90 + char *wstr = str;
91 +
92 + // print each digit
93 + do *wstr++ = (char)('0' + (uvalue % 10)); while((uvalue /= 10) && uvalue > (unsigned long long)0xffffffff);
94 + if(uvalue) return print_number_lu_r(wstr, uvalue);
95 + return wstr;
96 +}
97 +
98 +void buffer_print_llu(BUFFER *wb, unsigned long long uvalue)
99 +{
100 + buffer_need_bytes(wb, 50);
101 +
102 + char *str = &wb->buffer[wb->len];
103 + char *wstr = str;
104 +
105 +#ifdef ENVIRONMENT32
106 + if(uvalue > (unsigned long long)0xffffffff)
107 + wstr = print_number_llu_r(wstr, uvalue);
108 + else
109 + wstr = print_number_lu_r(wstr, uvalue);
110 +#else
111 + do *wstr++ = (char)('0' + (uvalue % 10)); while(uvalue /= 10);
112 +#endif
113 +
114 + // terminate it
115 + *wstr = '\0';
116 +
117 + // reverse it
118 + char *begin = str, *end = wstr - 1, aux;
119 + while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
120 +
121 + // return the buffer length
122 + wb->len += wstr - str;
123 +}
124
125 void buffer_strcat(BUFFER *wb, const char *txt)
126 {
src/web_buffer.h
+5
@@ -67,4 +67,9 @@ extern void buffer_sprintf(BUFFER *wb, const char *fmt, ...) __attribute__ (( fo
67
68 extern void buffer_char_replace(BUFFER *wb, char from, char to);
69
70 +extern char *print_number_lu_r(char *str, unsigned long uvalue);
71 +extern char *print_number_llu_r(char *str, unsigned long long uvalue);
72 +
73 +extern void buffer_print_llu(BUFFER *wb, unsigned long long uvalue);
74 +
75 #endif /* NETDATA_WEB_BUFFER_H */