Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom plugin consume too much CPU #1495

Closed
aj-fernando opened this issue Jan 2, 2017 · 4 comments
Closed

Custom plugin consume too much CPU #1495

aj-fernando opened this issue Jan 2, 2017 · 4 comments
Labels

Comments

@aj-fernando
Copy link

aj-fernando commented Jan 2, 2017

Hello,

Using some inputs from peeps at netdata here, I have written a custom plugin using bash. But for some reason this plugin consume too much CPU. I think perhaps there's something wrong with the timing that I use. I also referred to the python sample plugin according to here

Any help would be appreciated! Can anyone explain the proper usage of update_every and the setting timing gaps between data collection? Here's my sample bash plugin code.

#!/bin/bash

update_every=1
#echo $((update_every / 1000))
last_run=0

now="$(date +'%s')"
next_run=$now
last_run=$now
#update_every=$(($update_every*1000))
#echo $update_every

echo "CHART sample.battery '' 'Battery Utilization' 'percentage' battery sample.battery line 100 $((update_every))"
echo "DIMENSION value1 'remaining battery' absolute 1 1"

count=0
while true
do
	#bashCommand = "tail -f ~/tegraStats.log | grep -oP '(?<=GR3D 0%@)[^ ]+' "  
  	if [ $next_run -le $now ]; then
        	count=$(($count + 1))
	fi

	#DATA COLLECTION 

	#Random Integer between 1 -100
	#value1=$(( ( RANDOM % 100 )  + 1 ))

        # get the current time again
        # data collection may be too slow
        now="$(date +'%s')"

        # find the time for the next run
        while [ "$next_run" -le "$now" ]
        do
            next_run=$(($next_run + $update_every))
        done

        # calculate dt = the time we took
        # since the last run
        dt=$(($now - $last_run))
        last_run=$now

        # on the first iteration, don't set dt
        # allowing netdata to align itself
        if [ "$count" -eq 1 ]; then
            dt=0
	fi

	# send the values to netdata
	echo "BEGIN sample.battery $((dt))"
	echo "SET value1 = $value1"
	echo "END"

	# sleep 
	sleep $update_every
	now="$(date +'%s')"
done
@ktsaou
Copy link
Member

ktsaou commented Jan 2, 2017

Hi,

where is data collection?

When I run it, I see this:

CHART sample.battery '' 'Battery Utilization' 'percentage' battery sample.battery line 100 1
DIMENSION value1 'remaining battery' absolute 1 1
BEGIN sample.battery 0
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END
BEGIN sample.battery 1
SET value1 = 
END

This does not consume much CPU, but it does nothing too...

@ktsaou ktsaou added the question label Jan 2, 2017
@aj-fernando
Copy link
Author

Ah sory, that's because data collection raw ( printing random integer is commented) which was my first trial. You can uncomment the line and run it.
value1=$(( ( RANDOM % 100 ) + 1 ))

@ktsaou
Copy link
Member

ktsaou commented Jan 3, 2017

Well, this does not take much CPU.

The only CPU intensive things in the plugin are running date and sleep. They are CPU intensive because bash forks itself to execute them, which are then linked against shared libraries.

If you write a module for charts.d.plugin, it will be more efficient. charts.d.plugin uses /proc/uptime to read the clock and read to sleep. Both are implemented using bash internal commands, so no forking and linking.

Check the charts.d directory. There are many examples there.

@ktsaou
Copy link
Member

ktsaou commented Jan 19, 2017

I am closing this due to inactivity.
If you need help, just post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants