• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include <cmath>
16 #include "include/DDR.h"
17 namespace OHOS {
18     namespace SmartPerf {
19         pthread_mutex_t DDR::mutex;
20         DDR *DDR::instance = nullptr;
getInstance()21         DDR *DDR::getInstance()
22         {
23             if (instance == nullptr) {
24                 pthread_mutex_lock(&mutex);
25                 if (instance == nullptr) {
26                     instance = new DDR();
27                 }
28                 pthread_mutex_unlock(&mutex);
29             }
30             return instance;
31         }
32 
DDR()33         DDR::DDR()
34         {
35             pthread_mutex_init(&mutex, nullptr);
36         }
37 
getDdrFreq()38         long long DDR::getDdrFreq()
39         {
40             long long curFreq;
41 
42             FILE *fp;
43             static char buffer[256];
44             const int defaultUnit = 1000;
45             const int defaultHalf = 2;
46             fp = fopen(ddr_cur_freq_path.c_str(), "r");
47             if (fp == nullptr) {
48                 printf("getDDRInfoFromNode()-fopen %s, err=%s\n", ddr_cur_freq_path.c_str(), strerror(errno));
49                 curFreq = -1;
50             } else {
51                 buffer[0] = '\0';
52                 long long curDDR = -1;
53                 while (fgets(buffer, sizeof(buffer), fp)) {
54                     if (sscanf(buffer, "DDR :%lld", &curDDR) < 0) {
55                         continue;
56                     }
57                 }
58                 if (curDDR != -1) {
59                     curFreq = curDDR * defaultUnit / defaultHalf;
60                 } else {
61                     curFreq = std::atoll(buffer);
62                 }
63                 printf("getDDRInfoFromNode()-cur_freq: %lld\n", curFreq);
64                 fclose(fp);
65             }
66             return curFreq;
67         }
68     }
69 }
70