added resource usage and timing for tc plugin and script
Costa Tsaousis (ktsaou) committed
Nov 21, 2015 at 01:33 UTC
eb062ba543c505984beb81b82578bc0235f0236c
3 files changed
+60
-13
charts.d/opensips.chart.sh
-1
@@ -140,7 +140,6 @@ EOF
140
return 0
141
}
142
143
-
143
opensips_update() {
144
# the first argument to this function is the microseconds since last update
145
# pass this parameter to the BEGIN statement (see bellow).
plugins.d/tc-qos-helper.sh
+28
-12
@@ -1,19 +1,33 @@
1
-#!/bin/sh
1
+#!/bin/bash
2
+
3
+# default time function
4
+now_ms=
5
+current_time_ms() {
6
+ now_ms="$(date +'%s')000"
7
+}
8
9
# default sleep function
10
+LOOPSLEEPMS_LASTWORK=0
11
loopsleepms() {
12
+ [ "$1" = "tellwork" ] && shift
13
sleep $1
14
}
15
+
16
# if found and included, this file overwrites loopsleepms()
17
# with a high resolution timer function for precise looping.
9
-. "`dirname $0`/loopsleepms.sh.inc"
18
+. "$NETDATA_PLUGINS_DIR/loopsleepms.sh.inc"
19
20
# check if we have a valid number for interval
21
t=$1
13
-sleep_time=$(( t + 1 - 1 ))
14
-if [ "$sleep_time" -eq 0 ]
22
+sleep_time=$((t))
23
+[ $((sleep_time)) -lt 1 ] && $NETDATA_UPDATE_EVERY
24
+[ $((sleep_time)) -lt 1 ] && sleep_time=1
25
+
26
+tc_cmd="$(which tc)"
27
+if [ -z "$tc_cmd" ]
28
then
16
- sleep_time=1
29
+ echo >&2 "tc: Cannot find a 'tc' command in this system."
30
+ exit 1
31
fi
32
33
devices=
@@ -28,19 +42,19 @@ show_tc() {
42
43
echo "BEGIN $x"
44
31
- /sbin/tc -s class show dev $x
45
+ $tc_cmd -s class show dev $x
46
47
# check FireQOS names for classes
48
if [ ! -z "$fix_names" -a -f /var/run/fireqos/ifaces/$x ]
49
then
36
- name="`cat /var/run/fireqos/ifaces/$x`"
50
+ name="$(cat /var/run/fireqos/ifaces/$x)"
51
echo "SETDEVICENAME $name"
52
53
interface_classes=
54
. /var/run/fireqos/$name.conf
55
for n in $interface_classes_monitor
56
do
43
- setclassname `echo $n | tr '|' ' '`
57
+ setclassname $(echo $n | tr '|' ' ')
58
done
59
60
echo "SETDEVICEGROUP $interface_dev"
@@ -51,7 +65,7 @@ show_tc() {
65
all_devices() {
66
cat /proc/net/dev | grep ":" | cut -d ':' -f 1 | while read dev
67
do
54
- l=`/sbin/tc class show dev $dev | wc -l`
68
+ l=$($tc_cmd class show dev $dev | wc -l)
69
[ $l -ne 0 ] && echo $dev
70
done
71
}
@@ -76,7 +90,7 @@ do
90
then
91
c=1
92
fix_names="YES"
79
- devices="`all_devices`"
93
+ devices="$( all_devices )"
94
fi
95
96
for d in $devices
@@ -84,7 +98,9 @@ do
98
show_tc $d
99
done
100
87
- loopsleepms $sleep_time
101
+ echo "WORKTIME $LOOPSLEEPMS_LASTWORK"
102
+
103
+ loopsleepms tellwork $sleep_time
104
89
- test $gc -gt $exit_after && exit 0
105
+ [ $gc -gt $exit_after ] && exit 0
106
done
src/plugin_tc.c
+32
@@ -5,6 +5,7 @@
5
#include <stdlib.h>
6
#include <string.h>
7
#include <sys/types.h>
8
+#include <sys/resource.h>
9
10
#include "avl.h"
11
#include "log.h"
@@ -448,6 +449,9 @@ void *tc_main(void *ptr)
449
if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
450
error("Cannot set pthread cancel state to ENABLE.");
451
452
+ struct rusage thread;
453
+ RRDSET *stcpu = NULL, *sttime = NULL;
454
+
455
char buffer[TC_LINE_MAX+1] = "";
456
char *words[MAX_WORDS] = { NULL };
457
@@ -458,6 +462,7 @@ void *tc_main(void *ptr)
462
uint32_t SETDEVICENAME_HASH = simple_hash("SETDEVICENAME");
463
uint32_t SETDEVICEGROUP_HASH = simple_hash("SETDEVICEGROUP");
464
uint32_t SETCLASSNAME_HASH = simple_hash("SETCLASSNAME");
465
+ uint32_t WORKTIME_HASH = simple_hash("WORKTIME");
466
#ifdef DETACH_PLUGINS_FROM_NETDATA
467
uint32_t MYPID_HASH = simple_hash("MYPID");
468
#endif
@@ -570,6 +575,33 @@ void *tc_main(void *ptr)
575
char *path = words[2];
576
if(id && *id && path && *path) tc_device_set_class_name(device, id, path);
577
}
578
+ else if(first_hash == WORKTIME_HASH && strcmp(words[0], "WORKTIME") == 0) {
579
+ // debug(D_TC_LOOP, "WORKTIME line '%s' '%s'", words[1], words[2]);
580
+ getrusage(RUSAGE_THREAD, &thread);
581
+
582
+ if(!stcpu) stcpu = rrdset_find("netdata.plugin_tc_cpu");
583
+ if(!stcpu) {
584
+ stcpu = rrdset_create("netdata", "plugin_tc_cpu", NULL, "netdata", "NetData TC CPU usage", "milliseconds/s", 10000, rrd_update_every, RRDSET_TYPE_STACKED);
585
+ rrddim_add(stcpu, "user", NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
586
+ rrddim_add(stcpu, "system", NULL, 1, 1000 * rrd_update_every, RRDDIM_INCREMENTAL);
587
+ }
588
+ else rrdset_next(stcpu);
589
+
590
+ rrddim_set(stcpu, "user" , thread.ru_utime.tv_sec * 1000000ULL + thread.ru_utime.tv_usec);
591
+ rrddim_set(stcpu, "system", thread.ru_stime.tv_sec * 1000000ULL + thread.ru_stime.tv_usec);
592
+ rrdset_done(stcpu);
593
+
594
+ if(!sttime) stcpu = rrdset_find("netdata.plugin_tc_time");
595
+ if(!sttime) {
596
+ sttime = rrdset_create("netdata", "plugin_tc_time", NULL, "netdata", "NetData TC script execution", "milliseconds/run", 10001, rrd_update_every, RRDSET_TYPE_AREA);
597
+ rrddim_add(sttime, "run_time", "run time", 1, 1, RRDDIM_ABSOLUTE);
598
+ }
599
+ else rrdset_next(sttime);
600
+
601
+ rrddim_set(sttime, "run_time", atoll(words[1]));
602
+ rrdset_done(sttime);
603
+
604
+ }
605
#ifdef DETACH_PLUGINS_FROM_NETDATA
606
else if(first_hash == MYPID_HASH && (strcmp(words[0], "MYPID") == 0)) {
607
// debug(D_TC_LOOP, "MYPID line '%s'", words[1]);