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

Regexp tidy up #978

Merged
merged 1 commit into from
Nov 26, 2015
Merged

Regexp tidy up #978

merged 1 commit into from
Nov 26, 2015

Conversation

jimmidyson
Copy link
Collaborator

Cleaned up some areas where regexps were either messy or unnecessary.

@@ -172,9 +172,12 @@ func (self *dockerFactory) DebugInfo() map[string][]string {
return map[string][]string{}
}

var (
version_regexp_string = "(\\d+)\\.(\\d+)\\.(\\d+)"
version_re = regexp.MustCompile(version_regexp_string)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why move this out of the function? Personally I find it more readable if I can see the regexp right where it's used (and I don't think this should be used outside that function).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting it in the function means the RE gets wastefully compiled every time the function is called. In this case probably not called more than once, but normally best to move it outside so trying to keep it consistent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, makes sense. Maybe move the declaration to just above the function in that case? Also, consider making the string const.

@timstclair timstclair self-assigned this Nov 25, 2015
tcpStateMap[state[3]]++

state := strings.Fields(line)
//#file header tcp state is the 4th field:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) space after //

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this comment reads a little weird. Maybe rephrase to:

// TCP state is the 4th field.
// Format: sl local_address rem_address st tx_queue rx_queue tr tm->when retrnsmt  uid timeout inode

@jimmidyson
Copy link
Collaborator Author

@timstclair Thanks for a thorough code review - this is making it a much more worthwhile endeavour than it would have otherwise been :)

disk_info.Scheduler = "none"
blkSched, err := sysfs.GetBlockDeviceScheduler(name)
if err == nil {
matches := schedulerRegExp.FindSubmatch([]byte(blkSched))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regex should use a raw string too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@timstclair
Copy link
Contributor

@timstclair Thanks for a thorough code review - this is making it a much more worthwhile endeavour than it would have otherwise been :)

No problem! Thanks for the cleanup!

@jimmidyson
Copy link
Collaborator Author

All comments addressed. Good review!

@jimmidyson
Copy link
Collaborator Author

As an observation there is a lot we could do to lower resource utilisation. Regexps are hungry & need to be used judiciously. They're overused here where we could probably do some cheaper string parsing.

@jimmidyson
Copy link
Collaborator Author

Integration tests pass, so it must be good right? 😜

@@ -157,6 +160,7 @@ func GetTopology(sysFs sysfs.SysFs, cpuinfo string) ([]info.Node, int, error) {
lastNode = -1
}
lastThread = thread
continue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole loop could use some cleanup. I think it would be much more efficient and readable to switch on strings.TrimSpace(line[:strings.Index(line, ":")-1])

That's sort of a separate change, so up to you where / whether you want to address it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Not going to do it here, luckily AFAICT this is only called once on start of cadvisor so efficiency isn't paramount. Would like to improve it though, for vanity more than anything else :-b

@timstclair
Copy link
Contributor

LGTM (please squash)

Integration tests pass, so it must be good right?

Of course! I always like to have some simple unit tests for regexes, but I won't force you to add them if you aren't up for it...

As an observation there is a lot we could do to lower resource utilisation. Regexps are hungry & need to be used judiciously. They're overused here where we could probably do some cheaper string parsing.

Agreed. Although I don't think it's worth spending too much time on this without first doing some profiling to identify the places it really matters.

@jimmidyson
Copy link
Collaborator Author

Yeah we need to do more profiling of cadvisor in general. There have been quite a few performance issues when embedded in the kubelet & we need to identify areas for improvement. Regexes were a recurrent theme in those perf problems, hence me looking at them here. I think with the work we've done recently in cadvisor this is improved significantly, but further work needs to be done to keep cadvisor's footprint as low as possible.

@jimmidyson
Copy link
Collaborator Author

retest this please

@jimmidyson
Copy link
Collaborator Author

Self-merging after LGTM.

jimmidyson added a commit that referenced this pull request Nov 26, 2015
@jimmidyson jimmidyson merged commit cec96eb into google:master Nov 26, 2015
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.

2 participants