Fix the problem of not shutting down the profiler in ART properly

This CL fixes the problem of not saving the last batch of profile data to the
external file when the runtime exits before the end of the current iteration of
profile sampling. This problem was fixed by shutting down the ART profiler
when the runtime exits and allowing the profiler to write the last batch of
data even it was signaled to shut down. This CL increases the precision of
profile data and fixes the bug that no data is saved if the duration of profile
sampling specified by VM arguments (by default 20 seconds) is longer than the
length of a single execution because the profiler was never signaled when the
runtime exits.

Change-Id: I7cc4805b7e2a22a990d04a5b9724ad1d931d7152
Signed-off-by: Wei Jin <wejin@google.com>
diff --git a/runtime/profiler.cc b/runtime/profiler.cc
index 6e33f9d..5459ce3 100644
--- a/runtime/profiler.cc
+++ b/runtime/profiler.cc
@@ -193,7 +193,7 @@
 
       valid_samples += barrier_count;
 
-      ThreadState old_state = self->SetState(kWaitingForCheckPointsToRun);
+      ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun);
 
       // Wait for the barrier to be crossed by all runnable threads.  This wait
       // is done with a timeout so that we can detect problems with the checkpoint
@@ -211,13 +211,11 @@
       // code.  Crash the process in this case.
       CHECK_LT(waitdiff_us, kWaitTimeoutUs);
 
-      self->SetState(old_state);
-
       // Update the current time.
       now_us = MicroTime();
     }
 
-    if (valid_samples > 0 && !ShuttingDown(self)) {
+    if (valid_samples > 0) {
       // After the profile has been taken, write it out.
       ScopedObjectAccess soa(self);   // Acquire the mutator lock.
       uint32_t size = profiler->WriteProfile();
@@ -335,6 +333,7 @@
   pthread_t profiler_pthread = 0U;
   {
     MutexLock trace_mu(Thread::Current(), *Locks::profiler_lock_);
+    CHECK(!shutting_down_);
     profiler = profiler_;
     shutting_down_ = true;
     profiler_pthread = profiler_pthread_;