Home | History | Annotate | Download | only in vehicle_monitor_service
      1 /*
      2  * Copyright (C) 2016 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 #ifndef CAR_PROCESS_MONITOR_H_
     17 #define CAR_PROCESS_MONITOR_H_
     18 
     19 #include <map>
     20 #include <memory>
     21 #include <set>
     22 #include <string>
     23 #include <shared_mutex>
     24 
     25 #include <inttypes.h>
     26 #include <cutils/compiler.h>
     27 
     28 #include <binder/BinderService.h>
     29 #include <binder/IBinder.h>
     30 #include <utils/String8.h>
     31 
     32 #include <IVehicleMonitor.h>
     33 #include <HandlerThread.h>
     34 
     35 namespace android {
     36 
     37 // ----------------------------------------------------------------------------
     38 struct ProcInfo {
     39     pid_t pid;
     40     uid_t uid;
     41     std::string name;
     42     uint64_t utime;
     43     uint64_t stime;
     44     uint64_t rss;
     45     uint64_t wbytes;
     46     int64_t delta_utime;
     47     int64_t delta_stime;
     48     int64_t delta_time;
     49     int64_t delta_rss;
     50     int64_t delta_wbytes;
     51 };
     52 
     53 // ----------------------------------------------------------------------------
     54 
     55 // This class is used to collect information about running processes.
     56 // It also enforces certain policies to running apps - ex. kills non-system,
     57 // non-foreground applications that use too much memory, CPU, or write too much
     58 // data to disk.
     59 class ProcessMonitor {
     60 public:
     61     ProcessMonitor();
     62     ~ProcessMonitor();
     63 
     64     void dump(String8& msg);
     65     status_t setAppPriority(uint32_t pid, uint32_t uid, uint32_t priority);
     66     status_t process();
     67 
     68 private:
     69     status_t updateProcessInfo();
     70     void populateExistingPids(std::set<pid_t>& pidSet);
     71     void deleteOutdatedPids(std::set<pid_t>& pidSet);
     72     void updateOrAddProcess(pid_t pid);
     73     void readStat(std::shared_ptr<ProcInfo> pidData, pid_t pid);
     74     void readIo(std::shared_ptr<ProcInfo> pidData, pid_t pid);
     75     void readCmdline(std::shared_ptr<ProcInfo> pidData, pid_t pid);
     76     void readStatus(std::shared_ptr<ProcInfo> pidData, pid_t pid);
     77     void updateDiffs(std::shared_ptr<ProcInfo> pidData,
     78                      std::shared_ptr<ProcInfo> oldPidData);
     79     status_t improveSystemHealth();
     80     void dumpTopProcesses(String8& msg,
     81         bool (*procCmpFn) (
     82                 const std::pair<pid_t, const std::shared_ptr<ProcInfo>>,
     83                 const std::pair<pid_t, const std::shared_ptr<ProcInfo>>));
     84 
     85 
     86 private:
     87     std::map<pid_t, std::shared_ptr<ProcInfo>> mProcInfoMap;
     88     bool mIoSupported;
     89     mutable std::shared_timed_mutex mMutex;
     90 };
     91 
     92 }
     93 
     94 #endif /* CAR_PROCESS_MONITOR_H_ */
     95