1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_PROCESS_PROCESS_STATUS_H 16 #define HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_PROCESS_PROCESS_STATUS_H 17 18 #include <mutex> 19 #include <unordered_map> 20 21 #include "singleton.h" 22 23 namespace OHOS { 24 namespace HiviewDFX { 25 namespace UCollectUtil { 26 enum ProcessState { 27 INVALID = -1, 28 BACKGROUND = 0, 29 FOREGROUND, 30 }; 31 32 struct ProcessInfo { 33 std::string name; 34 ProcessState state; 35 uint64_t lastForegroundTime; 36 }; 37 38 class ProcessStatus : public OHOS::DelayedRefSingleton<ProcessStatus> { 39 public: 40 std::string GetProcessName(int32_t pid); 41 ProcessState GetProcessState(int32_t pid); 42 uint64_t GetProcessLastForegroundTime(int32_t pid); 43 void NotifyProcessState(int32_t pid, ProcessState procState); 44 45 private: 46 bool UpdateProcessName(int32_t pid, const std::string& procName); 47 void UpdateProcessState(int32_t pid, ProcessState procState); 48 void UpdateProcessForegroundState(int32_t pid); 49 void UpdateProcessBackgroundState(int32_t pid); 50 bool NeedClearProcessInfos(); 51 void ClearProcessInfos(); 52 53 private: 54 std::mutex mutex_; 55 /* map<pid, ProcessInfo> */ 56 std::unordered_map<int32_t, ProcessInfo> processInfos_; 57 }; 58 } // namespace UCollectUtil 59 } // namespace HiviewDFX 60 } // namespace OHOS 61 #endif // HIVIEW_FRAMEWORK_NATIVE_UNIFIED_COLLECTION_PROCESS_PROCESS_STATUS_H 62