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

Optimize /proc traversal #450

Merged
merged 5 commits into from
Sep 14, 2015
Merged

Optimize /proc traversal #450

merged 5 commits into from
Sep 14, 2015

Conversation

inercia
Copy link
Contributor

@inercia inercia commented Sep 7, 2015

The objective of this PR is to reduce the number of syscalls when collecting info from /proc. This PR does that by:

  • reading the required files (/proc/*/[cmdline, comm, stat, fd/*], /proc/net/tcp*) once per cycle (spy.interval), parsing and caching the processes/connections information
  • keeping a cache of open files for /proc/*/[cmdline, comm, stat] so we do not open()/close() these files so often.
  • removing unnecessary stat()s when reading /proc directories

Changes:

  • the required code for reading/parsing /proc has been proved from procspy to /probe/proc
  • the Walker has been replaced by a proc.Reader that provides both Processes() and Connections(). Users can walk the list of processes/connections in the same way by providing a function to these functions. A CachingReader can be used for caching and getting this info, updated when Update(). The code for reading and parsing connection and processes has been simplified.
  • a CachingReader is initialized in /probe/main.go and used in both endpoint.Reporter and process.Reporter for reporting about processed and connections, and updated periodically by calling Update() every spy.interval seconds.
  • unit tests have been improved by mocking things like file system operations
  • things like Proc and Process have been unified, and many things from /probe/process have been moved to /probe/proc

This is part of the solution to #284

@2opremio
Copy link
Contributor

2opremio commented Sep 7, 2015

@inercia I don't know if it provides all you need but maybe you could use (or even extend) https://github.com/mitchellh/go-ps

@2opremio
Copy link
Contributor

2opremio commented Sep 7, 2015

Actually, I don't think go-ps provides connection/file information, but https://github.com/c9s/goprocinfo certainly does and it seems more mature.

EDIT: maybe it has the same problems as procspy, feel free to disregard my comment.

@inercia inercia force-pushed the scope-284 branch 3 times, most recently from a2249b2 to 3fae0d7 Compare September 9, 2015 10:20
@inercia
Copy link
Contributor Author

inercia commented Sep 9, 2015

@2opremio thanks with the hint... goprocinfo seems interesting. The only problem I see is that they use Read()s instead of our current mechanism where we buf.ReadFrom(someFile) and the buf is obtained from a Pool, so we do not generate too much garbage... it's worth some investigation, though...

// returns some info like processes and connections
type ProcReader interface {
// Processes walks through the processes
Processes(func(Process)) error

This comment was marked as abuse.

@inercia inercia changed the title [WIP] Optimize /proc traversal Optimize /proc traversal Sep 9, 2015
@inercia
Copy link
Contributor Author

inercia commented Sep 9, 2015

@tomwilkie or @peterbourgon could you please review this?

@peterbourgon peterbourgon self-assigned this Sep 9, 2015
@peterbourgon
Copy link
Contributor

@inercia Sure. Would you mind giving a short overview of what this PR does, and pointers to key types/functions/changes? (I know the basic context, just looking for your perspective.)

@inercia inercia mentioned this pull request Sep 10, 2015
@inercia inercia force-pushed the scope-284 branch 2 times, most recently from 654e5b0 to 62171ca Compare September 10, 2015 15:53
New: func() interface{} {
return bytes.NewBuffer(make([]byte, 0, 5000))
},
}

This comment was marked as abuse.

This comment was marked as abuse.

This comment was marked as abuse.

@inercia
Copy link
Contributor Author

inercia commented Sep 10, 2015

@peterbourgon I have updated the PR description, so please let me know what you think about this...

@inercia
Copy link
Contributor Author

inercia commented Sep 11, 2015

This is the latest profile graph from this branch (using go 1.5). Compared with the original graph in #284, we can see that the number of syscalls has been vastly reduced, but we spend a lot of time in runtime.* now (too much garbage, maybe?). In particular, I think we are allocating too many small objects when generating reports (see the number of allocations, runtime.mallocgc, and garbage collections, runtime.scanobject)

profile

@peterbourgon
Copy link
Contributor

@inercia Thanks, that helps a lot. I'll give it a look.


c.LocalAddress = dupIP(c.LocalAddress)
c.RemoteAddress = dupIP(c.RemoteAddress)
return c

This comment was marked as abuse.

This comment was marked as abuse.

@peterbourgon
Copy link
Contributor

The shape of it is good. Back to @inercia for those few comments (and rebasing, I guess).

@peterbourgon peterbourgon removed their assignment Sep 11, 2015
Some minor renames and cleanups.
@peterbourgon
Copy link
Contributor

👍 pending Coveralls. (Maybe Coveralls is broken?)

@peterbourgon peterbourgon removed their assignment Sep 14, 2015
@inercia
Copy link
Contributor Author

inercia commented Sep 14, 2015

@peterbourgon I think the coveralls hook has failed (the jobs is finisished, but the "Coverage pending" is still here). I'm merging....

inercia added a commit that referenced this pull request Sep 14, 2015
@inercia inercia merged commit 46345e3 into master Sep 14, 2015
@peterbourgon peterbourgon deleted the scope-284 branch September 14, 2015 12:29
tomwilkie pushed a commit that referenced this pull request Sep 15, 2015
This reverts commit 46345e3, reversing
changes made to bf3e9a1.

Conflicts:
	probe/process/walker_darwin.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants