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

Netstat input is consuming a lot of CPU on server with 120K established connections #6559

Open
jimis opened this issue Oct 22, 2019 · 13 comments · May be fixed by #6772
Open

Netstat input is consuming a lot of CPU on server with 120K established connections #6559

jimis opened this issue Oct 22, 2019 · 13 comments · May be fixed by #6772

Comments

@jimis
Copy link

@jimis jimis commented Oct 22, 2019

telegraf-1.12.3 with data collection interval 10s, sending data to influxDB
OS: Linux x86_64

  • Telegraf was consuming constantly around 70% CPU (on average, as measured by telegraf itself with the procstat plugin).
  • Server has around 120K established incoming connections because of other services running
  • The CPU usage went back low when the connections were gone
  • I am suspecting the culprit is "inputs.netstat" plugin which is enabled on this specific server in order to count the number of connections
@danielnelson
Copy link
Contributor

@danielnelson danielnelson commented Oct 24, 2019

@jimis Would you be able to help us collect a few profiles? Our documentation for profiling needs a bit of work first though, we will get back to you with an update that has more precise instructions.

@jimis
Copy link
Author

@jimis jimis commented Oct 28, 2019

Yes I can do some profiling. I already tried to grab some gdb backtraces to see where the threads are kept busy, but all I got was question marks instead of function names. Are you binaries shipping with debug info?

FYI, on this server it is InfluxDB that has thousands of connections in ESTABLISHED state. There is an issue there, because I see a slow but constant upwards trend in the number of connections (as logged by telegraf), and the number gets reset only when I restart influxd. I hope I'll fix this issue soon, which will significantly reduce the number of connections. Until then I can definitely profile telegraf.

@jimis
Copy link
Author

@jimis jimis commented Oct 29, 2019

I don't think profiling is necessary, I have verified that the slowness is in the inputs.netstat plugin.

What I did to verify this:

  • disabled netstat plugin and restarted telegraf
  • CPU utilisation went very low
  • re-enable netstat plugin and restart telegraf
  • CPU utilisation goes to 70% on average

I believe it should be easy for you to replicate the scenario, by providing a fake /proc/net/tcp6 file with 120K lines. I can provide you with one such file if needed.

EDIT: I might be wrong about /proc/net/tcp6 file. I had a look at gopsutil net_linux.go and I believe it goes through and parses all of /proc/*/fd/*, which would explain why it is horrendously slow. :-)

@jimis
Copy link
Author

@jimis jimis commented Oct 29, 2019

Opened shirou/gopsutil#784 for that, feel free to comment.

In my opinion, telegraf shouldn't use gopsutil for something so simple, but better open directly the relevant file.

@glinton glinton added the upstream label Nov 4, 2019
@danielnelson danielnelson removed the ready label Nov 6, 2019
@jimis
Copy link
Author

@jimis jimis commented Nov 27, 2019

@glinton I see you added the upstream label, but I have to disagree, after reading a bunch of comments in issues in gopsutil. gopsutil is having the job to parse and return information from every single process in the system. While telegraf can do much better by opening only one generic file.

@danielnelson I see you removed the ready label, what more information would you need from me? I don't think profiling will be useful, the culprit was easy to identify.

@danielnelson
Copy link
Contributor

@danielnelson danielnelson commented Nov 27, 2019

I'll answer over on the gopsutil issue too, it may make sense for us to have a custom linux implementation because gopsutil is reporting the pid, and we don't need it in this plugin. I don't think the pid can be obtained from /proc/net/tcp and it appears from strace that when given the -p flag netstat follows a similar strategy to gopsutil.

@danielnelson
Copy link
Contributor

@danielnelson danielnelson commented Nov 27, 2019

If you can work on an implementation that uses the /proc/net/tcp* files, we can either add it to Telegraf or into gopsutil depending on what is decided.

@JoveYu
Copy link

@JoveYu JoveYu commented Dec 2, 2019

I can get tcp netstat from /proc/net/tcp*, but I don't know what the metrics udp_socket means, and where is it from?

@jimis
Copy link
Author

@jimis jimis commented Dec 6, 2019

I can get tcp netstat from /proc/net/tcp*, but I don't know what the metrics udp_socket means, and where is it from?

@JoveYu I would /guess/ it is the number of udp sockets the system keeps track of. Probably the number of lines of some of the files /proc/net/udp* ?

JoveYu added a commit to JoveYu/telegraf that referenced this issue Dec 9, 2019
@JoveYu
Copy link

@JoveYu JoveYu commented Dec 9, 2019

i hava rewrite netstat plugin in linux, it can help fix this issue

JoveYu added a commit to JoveYu/telegraf that referenced this issue Dec 9, 2019
JoveYu added a commit to JoveYu/telegraf that referenced this issue Dec 9, 2019
JoveYu added a commit to JoveYu/telegraf that referenced this issue Dec 9, 2019
@JoveYu JoveYu linked a pull request that will close this issue Dec 9, 2019
2 of 3 tasks complete
@danielnelson danielnelson changed the title Telegraf is consuming a lot of CPU on server with 120K established connections Netstat input is consuming a lot of CPU on server with 120K established connections Mar 25, 2020
@Kedesai
Copy link

@Kedesai Kedesai commented Mar 25, 2020

@danielnelson thanks for pointing me to this open ticket.

@Kedesai
Copy link

@Kedesai Kedesai commented Mar 25, 2020

What version of telegraf client has the fix @JoveYu and @danielnelson

@danielnelson
Copy link
Contributor

@danielnelson danielnelson commented Mar 26, 2020

Still working on the fix in #6772.

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

Successfully merging a pull request may close this issue.

6 participants
You can’t perform that action at this time.