2 * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
17 * @file ckm_so_loader.cpp
18 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
26 #include <sys/types.h>
44 ofstream of("/proc/sys/vm/drop_caches");
46 cerr << "Cache clearing failed: " << strerror(errno) << endl;
52 void test(int flags, const string& library, const string& symbol)
54 bool lazy = (flags & LAZY);
55 if (flags & CLEAR_CACHE)
58 chrono::time_point<chrono::high_resolution_clock> tp[4];
60 tp[0] = chrono::high_resolution_clock::now();
61 void* handle = dlopen(library.c_str(), (lazy?RTLD_LAZY:RTLD_NOW));
62 tp[1] = chrono::high_resolution_clock::now();
64 cerr << "dlopen failed: " << dlerror() << endl;
70 tp[2] = chrono::high_resolution_clock::now();
71 void* sym = dlsym(handle, symbol.c_str());
72 tp[3] = chrono::high_resolution_clock::now();
74 cerr << "dlsym failed: " << dlerror() << endl;
80 cout << (tp[1] - tp[0]).count() << ";" << (tp[3] - tp[2]).count() << endl;
83 int main(int argc, char* argv[])
86 cerr << "Usage: ckm_so_loader [flags] [repeats] [library] [symbol]" << endl;
87 cerr << " flags: 1-clear cache, 2-lazy binding" << endl;
88 cerr << "Example: ckm_so_loader 3 100 /usr/lib/libkey-manager-client.so ckmc_save_key" << endl;
92 int flags = stoi(argv[1]); // let it throw
93 int repeats = stoi(argv[2]); // let it throw
94 string so_path(argv[3]);
95 string symbol(argv[4]);
97 cout << "dlopen[us];dlsym[us]" << endl;
98 for (int cnt = 0 ; cnt < repeats; cnt++)
101 * It has to be a different process each time. Glibc somehow caches the library information
102 * and consecutive calls are faster
106 cerr << "fork failed: " << strerror(errno) << endl;
110 test(flags, so_path, symbol);
116 pid_t ret = waitpid(pid,&status, 0);
118 cerr << "waitpid failed: " << strerror(errno) << endl;