Home | History | Annotate | Download | only in memory
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SRC_PROFILING_MEMORY_SYSTEM_PROPERTY_H_
     18 #define SRC_PROFILING_MEMORY_SYSTEM_PROPERTY_H_
     19 
     20 #include <map>
     21 #include <string>
     22 
     23 namespace perfetto {
     24 namespace profiling {
     25 
     26 // SystemProperties allows to set properties in a reference counted fashion.
     27 // SetAll() is used to enable startup profiling for all programs, SetProperty
     28 // can be used to enable startup profiling for a specific program name.
     29 // Both of those return opaque Handles that need to be held on to as long as
     30 // startup profiling should be enabled.
     31 //
     32 // This automatically manages the heappprofd.enable flag, which is first
     33 // checked to determine whether to check the program name specific flag.
     34 // Once the last Handle for a given program name goes away, the flag for the
     35 // program name is unset. Once the last of all Handles goes away, the
     36 // heapprofd.enable flag is unset.
     37 // See
     38 // https://android.googlesource.com/platform/bionic/+/0dbe6d1aec12d2f30f0331dcfea6dc8e8c55cf97/libc/bionic/malloc_common.cpp#473
     39 class SystemProperties {
     40  public:
     41   class Handle {
     42    public:
     43     friend void swap(SystemProperties::Handle&, SystemProperties::Handle&);
     44 
     45     Handle(const Handle&) = delete;
     46     Handle& operator=(const Handle&) = delete;
     47 
     48     Handle(Handle&&);
     49     Handle& operator=(Handle&&);
     50 
     51     friend class SystemProperties;
     52     ~Handle();
     53     operator bool();
     54 
     55    private:
     56     explicit Handle(SystemProperties* system_properties, std::string property);
     57     explicit Handle(SystemProperties* system_properties);
     58 
     59     SystemProperties* system_properties_;
     60     std::string property_;
     61     bool all_ = false;
     62   };
     63 
     64   Handle SetProperty(std::string name);
     65   Handle SetAll();
     66 
     67   static void ResetHeapprofdProperties();
     68 
     69   virtual ~SystemProperties();
     70 
     71  protected:
     72   // virtual for testing.
     73   virtual bool SetAndroidProperty(const std::string& name,
     74                                   const std::string& value);
     75 
     76  private:
     77   void UnsetProperty(const std::string& name);
     78   void UnsetAll();
     79 
     80   size_t alls_ = 0;
     81   std::map<std::string, size_t> properties_;
     82 };
     83 
     84 void swap(SystemProperties::Handle& a, SystemProperties::Handle& b);
     85 
     86 }  // namespace profiling
     87 }  // namespace perfetto
     88 
     89 #endif  // SRC_PROFILING_MEMORY_SYSTEM_PROPERTY_H_
     90