Add profiling code.
Contains squashed commits from kanetaka