• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 #include <thread>
17 #include <atomic>
18 #include <sys/types.h>
19 #include <unistd.h>
20 #include <ctime>
21 #include "log.h"
22 #include "hisysevent.h"
23 #include "task_statistics.h"
24 
25 namespace OHOS::Request::Download {
GetInstance()26 TaskStatistics &TaskStatistics::GetInstance()
27 {
28     static TaskStatistics instance;
29     return instance;
30 }
31 
ReportTasksSize(uint64_t totalSize)32 void TaskStatistics::ReportTasksSize(uint64_t totalSize)
33 {
34     dayTasksSize_ += totalSize;
35 }
36 
ReportTasksNumber()37 void TaskStatistics::ReportTasksNumber()
38 {
39     dayTasksNumber_ ++;
40 }
41 
GetNextReportInterval() const42 int32_t TaskStatistics::GetNextReportInterval() const
43 {
44     time_t current = time(nullptr);
45     if (current == -1) {
46         DOWNLOAD_HILOGE("GetNextReportInterval time fail");
47         return -1;
48     }
49     tm localTime = { 0 };
50     tm *result = localtime_r(&current, &localTime);
51     if (result == nullptr) {
52         DOWNLOAD_HILOGE("GetNextReportInterval localTime fail");
53         return -1;
54     }
55     int currentTime = localTime.tm_hour * ONE_HOUR_SEC + localTime.tm_min * ONE_MINUTE_SEC + localTime.tm_sec;
56     return  ONE_DAY_SEC - currentTime;
57 }
58 
ReportStatistics() const59 void TaskStatistics::ReportStatistics() const
60 {
61     OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::REQUEST,
62         REQUEST_TASK_INFO_STATISTICS,
63         OHOS::HiviewDFX::HiSysEvent::EventType::STATISTIC,
64         TASKS_SIZE, &dayTasksSize_,
65         TASKS_NUMBER, &dayTasksNumber_);
66 }
67 
StartTimerThread()68 void TaskStatistics::StartTimerThread()
69 {
70     if (running_) {
71         return;
72     }
73 
74     running_ = true;
75     auto fun = [this]() {
76         while (true) {
77             int32_t nextReportInterval = GetNextReportInterval();
78             if (nextReportInterval < 0) {
79                 nextReportInterval = ONE_DAY_SEC;
80             }
81             DOWNLOAD_HILOGD("taskRun next interval: %{public}d", nextReportInterval);
82             sleep(nextReportInterval);
83             ReportStatistics();
84             this->dayTasksNumber_ = 0;
85             this->dayTasksSize_ = 0;
86         }
87     };
88     std::thread th = std::thread(fun);
89     th.detach();
90 }
91 } // namespace OHOS::Request::Download