1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
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 <dlfcn.h>
17 #include <sys/time.h>
18 #include "util.h"
19 #include <cstdio>
20 #include <iostream>
21
22 constexpr double THOUSAND = 1000.0;
23
24 class ScopeTime {
25 public:
ScopeTime(const char * name)26 explicit ScopeTime(const char *name) : cost(0), soName(name)
27 {
28 gettimeofday(&timeStart, nullptr);
29 }
30
CurrentTime()31 void CurrentTime()
32 {
33 struct timeval timeCurrent;
34 gettimeofday(&timeCurrent, nullptr);
35 cost = (timeCurrent.tv_sec - timeStart.tv_sec) * THOUSAND +
36 static_cast<double>(timeCurrent.tv_usec - timeStart.tv_usec) / THOUSAND;
37 printf("%s current cost %f ms.\n", soName, cost);
38 }
39
~ScopeTime()40 ~ScopeTime()
41 {
42 gettimeofday(&timeEnd, nullptr);
43 cost = (timeEnd.tv_sec - timeStart.tv_sec) * THOUSAND +
44 static_cast<double>(timeEnd.tv_usec - timeStart.tv_usec) / THOUSAND;
45 printf("dlopen %s cost %f ms.\n", soName, cost);
46 }
47 private:
48 struct timeval timeStart, timeEnd;
49 double cost;
50 const char *soName;
51 };
52
DoDlopen(const char * fileName,int flags)53 static void DoDlopen(const char *fileName, int flags)
54 {
55 ScopeTime st = ScopeTime(fileName);
56 void *handle = dlopen(fileName, flags);
57 if (handle == nullptr) {
58 printf("dlopen error: %s", dlerror());
59 exit(-1);
60 }
61 }
62
main()63 int main()
64 {
65 DoDlopen(LIBACE_PATH, RTLD_LAZY);
66 return 0;
67 }
68