Refactor profiler code

@airhorns @dklassen This is the profile refactor that should be merged upstream soon, but why wait?