Make use of profiling information for dex2oat

If the profile file exists, the compiler driver will read it
and store the data in an internal map.  Then, when we want to work
out whether to compile a method or not, the map is consulted and if
the method shows up with a high enough percentage of use we compile it.

The profile file itself is created by installd and is writeable by the
app.  The file is in /data/dalvik-cache/profiles and is named by
the package name.

This also modifies the profiler itself to:

1. Only count runnable threads (not suspended threads) in the profile
2. Use system properties to allow tuning of the profile parameters
3. Merge profiles from multiple processes using file locking.

Bug: 12877748
Change-Id: Iab2f3a327a2860db2a80d5724277d6c626227f2b

Conflicts:
	compiler/dex/frontend.cc
	compiler/dex/mir_analysis.cc
	compiler/dex/verification_results.cc
	compiler/driver/compiler_driver.cc
	dex2oat/dex2oat.cc
	runtime/class_linker.cc
	runtime/runtime.cc
	runtime/runtime.h
diff --git a/runtime/profiler.h b/runtime/profiler.h
index 6ea6c84..b03b170 100644
--- a/runtime/profiler.h
+++ b/runtime/profiler.h
@@ -54,10 +54,12 @@
 
   void Put(mirror::ArtMethod* method);
   uint32_t Write(std::ostream &os);
+  void ReadPrevious(int fd);
   void Clear();
   uint32_t GetNumSamples() { return num_samples_; }
   void NullMethod() { ++num_null_methods_; }
   void BootMethod() { ++num_boot_methods_; }
+
  private:
   uint32_t Hash(mirror::ArtMethod* method);
   static constexpr int kHashSize = 17;
@@ -68,6 +70,19 @@
 
   typedef std::map<mirror::ArtMethod*, uint32_t> Map;   // Map of method vs its count.
   Map *table[kHashSize];
+
+  struct PreviousValue {
+    PreviousValue() : count_(0), method_size_(0) {}
+    PreviousValue(uint32_t count, uint32_t method_size) : count_(count), method_size_(method_size) {}
+    uint32_t count_;
+    uint32_t method_size_;
+  };
+
+  typedef std::map<std::string, PreviousValue> PreviousProfile;
+  PreviousProfile previous_;
+  uint32_t previous_num_samples_;
+  uint32_t previous_num_null_methods_;     // Number of samples where can don't know the method.
+  uint32_t previous_num_boot_methods_;     // Number of samples in the boot path.
 };
 
 //
@@ -87,7 +102,8 @@
 
 class BackgroundMethodSamplingProfiler {
  public:
-  static void Start(int period, int duration, std::string profile_filename, int interval_us,
+  static void Start(int period, int duration, const std::string& profile_filename,
+                    const std::string& procName, int interval_us,
                     double backoff_coefficient, bool startImmediately)
   LOCKS_EXCLUDED(Locks::mutator_lock_,
                  Locks::thread_list_lock_,
@@ -104,8 +120,10 @@
   }
 
  private:
-  explicit BackgroundMethodSamplingProfiler(int period, int duration, std::string profile_filename,
-                 double backoff_coefficient, int interval_us, bool startImmediately);
+  explicit BackgroundMethodSamplingProfiler(int period, int duration,
+                                            const std::string& profile_filename,
+                                            const std::string& process_name,
+                                            double backoff_coefficient, int interval_us, bool startImmediately);
 
   // The sampling interval in microseconds is passed as an argument.
   static void* RunProfilerThread(void* arg) LOCKS_EXCLUDED(Locks::profiler_lock_);
@@ -130,6 +148,9 @@
   // File to write profile data out to.  Cannot be empty if we are profiling.
   std::string profile_file_name_;
 
+  // Process name.
+  std::string process_name_;
+
   // Number of seconds between profile runs.
   uint32_t period_s_;