]> arthur.barton.de Git - netdata.git/commitdiff
added resource usage and timing for tc plugin and script
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Fri, 20 Nov 2015 23:33:34 +0000 (01:33 +0200)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Fri, 20 Nov 2015 23:33:34 +0000 (01:33 +0200)
charts.d/opensips.chart.sh
plugins.d/tc-qos-helper.sh
src/plugin_tc.c

index 60ca728749d991cd18efc9a32ff510b3c3dded9b..31de486cff5ea10c09d5e456863375c857046150 100755 (executable)
@@ -140,7 +140,6 @@ EOF
        return 0
 }
 
-
 opensips_update() {
        # the first argument to this function is the microseconds since last update
        # pass this parameter to the BEGIN statement (see bellow).
index 0c43af737a055d3075907f010fe196bd60ea73b9..eb0529af4797295663107b4407fcc8fa77aa5cca 100755 (executable)
@@ -1,19 +1,33 @@
-#!/bin/sh
+#!/bin/bash
+
+# default time function
+now_ms=
+current_time_ms() {
+       now_ms="$(date +'%s')000"
+}
 
 # default sleep function
+LOOPSLEEPMS_LASTWORK=0
 loopsleepms() {
+       [ "$1" = "tellwork" ] && shift
        sleep $1
 }
+
 # if found and included, this file overwrites loopsleepms()
 # with a high resolution timer function for precise looping.
-. "`dirname $0`/loopsleepms.sh.inc"
+. "$NETDATA_PLUGINS_DIR/loopsleepms.sh.inc"
 
 # check if we have a valid number for interval
 t=$1
-sleep_time=$(( t + 1 - 1 ))
-if [ "$sleep_time" -eq 0 ]
+sleep_time=$((t))
+[ $((sleep_time)) -lt 1 ] && $NETDATA_UPDATE_EVERY
+[ $((sleep_time)) -lt 1 ] && sleep_time=1
+
+tc_cmd="$(which tc)"
+if [ -z "$tc_cmd" ]
        then
-       sleep_time=1
+       echo >&2 "tc: Cannot find a 'tc' command in this system."
+       exit 1
 fi
 
 devices=
@@ -28,19 +42,19 @@ show_tc() {
 
        echo "BEGIN $x"
        
-       /sbin/tc -s class show dev $x
+       $tc_cmd -s class show dev $x
        
        # check FireQOS names for classes
        if [ ! -z "$fix_names" -a -f /var/run/fireqos/ifaces/$x ]
        then
-               name="`cat /var/run/fireqos/ifaces/$x`"
+               name="$(cat /var/run/fireqos/ifaces/$x)"
                echo "SETDEVICENAME $name"
 
                interface_classes=
                . /var/run/fireqos/$name.conf
                for n in $interface_classes_monitor
                do
-                               setclassname `echo $n | tr '|' ' '`
+                               setclassname $(echo $n | tr '|' ' ')
                done
                
                echo "SETDEVICEGROUP $interface_dev"
@@ -51,7 +65,7 @@ show_tc() {
 all_devices() {
        cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
        do
-               l=`/sbin/tc class show dev $dev | wc -l`
+               l=$($tc_cmd class show dev $dev | wc -l)
                [ $l -ne 0 ] && echo $dev
        done
 }
@@ -76,7 +90,7 @@ do
        then
                c=1
                fix_names="YES"
-               devices="`all_devices`"
+               devices="$( all_devices )"
        fi
 
        for d in $devices
@@ -84,7 +98,9 @@ do
                show_tc $d
        done
 
-       loopsleepms $sleep_time
+       echo "WORKTIME $LOOPSLEEPMS_LASTWORK"
+
+       loopsleepms tellwork $sleep_time
 
-       test $gc -gt $exit_after && exit 0
+       [ $gc -gt $exit_after ] && exit 0
 done
index 8f932011dad62f398290e1a6d654a9c2b7d98206..eeeadc95b17575c79bed3361fd9f873da38db11a 100755 (executable)
@@ -5,6 +5,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/types.h>
+#include <sys/resource.h>
 
 #include "avl.h"
 #include "log.h"
@@ -448,6 +449,9 @@ void *tc_main(void *ptr)
        if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
                error("Cannot set pthread cancel state to ENABLE.");
 
+       struct rusage thread;
+       RRDSET *stcpu = NULL, *sttime = NULL;
+
        char buffer[TC_LINE_MAX+1] = "";
        char *words[MAX_WORDS] = { NULL };
 
@@ -458,6 +462,7 @@ void *tc_main(void *ptr)
        uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
        uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
        uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
+       uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
 #ifdef DETACH_PLUGINS_FROM_NETDATA
        uint32_t MYPID_HASH = simple_hash("MYPID");
 #endif
@@ -570,6 +575,33 @@ void *tc_main(void *ptr)
                                char *path  = words[2];
                                if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
                        }
+                       else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
+                               // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
+                               getrusage(RUSAGE_THREAD, &thread);
+
+                               if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
+                               if(!stcpu) {
+                                       stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
+                                       rrddim_add(stcpu, "user",  NULL,  1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
+                                       rrddim_add(stcpu, "system", NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
+                               }
+                               else rrdset_next(stcpu);
+
+                               rrddim_set(stcpu, "user"  , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
+                               rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
+                               rrdset_done(stcpu);
+
+                               if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
+                               if(!sttime) {
+                                       sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
+                                       rrddim_add(sttime, "run_time",  "run time",  1, 1, RRDDIM_ABSOLUTE);
+                               }
+                               else rrdset_next(sttime);
+
+                               rrddim_set(sttime, "run_time", atoll(words[1]));
+                               rrdset_done(sttime);
+
+                       }
 #ifdef DETACH_PLUGINS_FROM_NETDATA
                        else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
                                // debug(D_TC_LOOP, "MYPID line '%s'", words[1]);