1 /* 2 * Copyright (c) 2025 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 16 #ifndef CLOUD_FILE_DAEMON_IO_MESSAGE_STATUS_LISTENER_H 17 #define CLOUD_FILE_DAEMON_IO_MESSAGE_STATUS_LISTENER_H 18 19 #include <chrono> 20 #include <condition_variable> 21 #include <fstream> 22 #include <thread> 23 #include <mutex> 24 25 #include "application_state_observer_stub.h" 26 #include "common_event_subscriber.h" 27 28 namespace OHOS { 29 namespace FileManagement { 30 namespace CloudDisk { 31 32 struct IoData { 33 int64_t rchar = 0; 34 int64_t syscr = 0; 35 int64_t readBytes = 0; 36 int64_t syscopen = 0; 37 int64_t syscstat = 0; 38 }; 39 40 struct IoDataToWrite { 41 std::string bundleName; 42 int64_t rcharDiff = 0; 43 int64_t syscrDiff = 0; 44 int64_t readBytesDiff = 0; 45 int64_t syscopenDiff = 0; 46 int64_t syscstatDiff = 0; 47 double result = 0; 48 }; 49 50 class IoMessageManager { 51 private: 52 std::string currentBundleName = ""; 53 std::atomic<bool> isThreadRunning{false}; 54 std::thread ioThread; 55 std::mutex sleepMutex; 56 std::mutex cvMute; 57 std::condition_variable sleepCv; 58 std::map<std::string, int32_t> bundleTimeMap; 59 IoData currentData; 60 IoData preData; 61 IoDataToWrite dataToWrite; 62 AppExecFwk::AppStateData lastestAppStateData; 63 64 bool ReadIoDataFromFile(const std::string &path); 65 void RecordDataToFile(const std::string &path); 66 bool IsFirstLineHeader(const std::string &path); 67 void RecordIoData(); 68 void ProcessIoData(const std::string &path); 69 70 public: 71 static IoMessageManager &GetInstance(); 72 void OnReceiveEvent(const AppExecFwk::AppStateData &appStateData); 73 }; 74 } // namespace CloudDisk 75 } // namespace FileManagement 76 } // namespace OHOS 77 #endif // CLOUD_FILE_DAEMON_IO_MESSAGE_LISTENER_H