Add profiler state checks

This also fixes a bug where script's statistics were written to disk
regardless of whether or not it had been profiled.