1 /*
2 * Copyright (C) 2024 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 "image_data_statistics.h"
17
18 #include "securec.h"
19 #include "image_log.h"
20 #include "image_utils.h"
21
22 namespace OHOS {
23 namespace Media {
24 static constexpr int32_t FORMAT_BUF_SIZE = 254;
25 static constexpr uint64_t MEMORY_THRESHOLD_BYTE = 314572800;
26 static constexpr uint64_t TIME_THRESHOLD_MS = 500;
27
ImageDataStatistics(const std::string & title)28 ImageDataStatistics::ImageDataStatistics(const std::string &title) : title_(title), memorySize_(0)
29 {
30 startTime_ = ImageUtils::GetNowTimeMilliSeconds();
31 }
32
ImageDataStatistics(const char * fmt,...)33 ImageDataStatistics::ImageDataStatistics(const char *fmt, ...) : memorySize_(0)
34 {
35 #if !defined(_WIN32) && !defined(_APPLE)
36 if (fmt == nullptr) {
37 title_ = "ImageDataTraceFmt Param invalid";
38 } else {
39 char buf[FORMAT_BUF_SIZE] = { 0 };
40 va_list args;
41 va_start(args, fmt);
42 int32_t ret = vsprintf_s(buf, FORMAT_BUF_SIZE, fmt, args);
43 va_end(args);
44 if (ret != -1) {
45 title_ = buf;
46 } else {
47 title_ = "ImageDataTraceFmt Format Error";
48 }
49 }
50 startTime_ = ImageUtils::GetNowTimeMilliSeconds();
51 #endif
52 }
53
~ImageDataStatistics()54 ImageDataStatistics::~ImageDataStatistics()
55 {
56 #if !defined(_WIN32) && !defined(_APPLE)
57 uint64_t endTime = ImageUtils::GetNowTimeMilliSeconds();
58 uint64_t timeInterval = endTime - startTime_;
59
60 if ((memorySize_ != 0) && (memorySize_ > MEMORY_THRESHOLD_BYTE)) {
61 IMAGE_LOGD("%{public}s The requested memory [%{public}lu]bytes, exceeded the threshold [%{public}lu]bytes\n",
62 title_.c_str(), static_cast<unsigned long>(memorySize_), static_cast<unsigned long>(MEMORY_THRESHOLD_BYTE));
63 }
64
65 if (timeInterval > TIME_THRESHOLD_MS) {
66 IMAGE_LOGD("%{public}s Costtime: [%{public}llu]ms timethreshold: [%{public}llu]ms," \
67 "startTime: [%{public}llu]ms, endTime: [%{public}llu]ms\n", title_.c_str(),
68 static_cast<unsigned long long>(timeInterval), static_cast<unsigned long long>(TIME_THRESHOLD_MS),
69 static_cast<unsigned long long>(startTime_), static_cast<unsigned long long>(endTime));
70 }
71 #endif
72 }
73
SetRequestMemory(uint32_t size)74 void ImageDataStatistics::SetRequestMemory(uint32_t size)
75 {
76 memorySize_ = size;
77 }
78
AddTitle(const std::string title)79 void ImageDataStatistics::AddTitle(const std::string title)
80 {
81 title_ += title;
82 }
83
AddTitle(const char * fmt,...)84 void ImageDataStatistics::AddTitle(const char *fmt, ...)
85 {
86 #if !defined(_WIN32) && !defined(_APPLE)
87 if (fmt == nullptr) {
88 title_ += "AddTitle Param invalid";
89 } else {
90 char buf[FORMAT_BUF_SIZE] = { 0 };
91 va_list args;
92 va_start(args, fmt);
93 int32_t ret = vsprintf_s(buf, FORMAT_BUF_SIZE, fmt, args);
94 va_end(args);
95 if (ret != -1) {
96 title_ += buf;
97 } else {
98 title_ += "AddTitle Format Error";
99 }
100 }
101 #endif
102 }
103 } // namespace Media
104 } // namespace OHOS