File indexing completed on 2024-05-12 05:43:25

0001 /*
0002     Copyright (C) 2015 Volker Krause <vkrause@kde.org>
0003 
0004     This program is free software; you can redistribute it and/or modify it
0005     under the terms of the GNU Library General Public License as published by
0006     the Free Software Foundation; either version 2 of the License, or (at your
0007     option) any later version.
0008 
0009     This program is distributed in the hope that it will be useful, but WITHOUT
0010     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0011     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
0012     License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program.  If not, see <https://www.gnu.org/licenses/>.
0016 */
0017 
0018 #include <dlfcn.h>
0019 #include <stdio.h>
0020 #include <string.h>
0021 #include <time.h>
0022 
0023 int usage()
0024 {
0025     fprintf(stderr, "Usage: ldbenchark-runner [RTLD_LAZY|RTLD_NOW] <files>\n");
0026     return 1;
0027 }
0028 
0029 int main(int argc, char **argv)
0030 {
0031     if (argc < 3)
0032         return usage();
0033 
0034     int flags = 0;
0035     if (strcmp(argv[1], "RTLD_NOW") == 0)
0036         flags = RTLD_NOW;
0037     else if (strcmp(argv[1], "RTLD_LAZY") == 0)
0038         flags = RTLD_LAZY;
0039     else
0040         return usage();
0041 
0042     for (int i = 2; i < argc; ++i) {
0043         if (dlopen(argv[i], flags | RTLD_NOLOAD) != NULL) {
0044             fprintf(stderr, "%s is already loaded, check argument order!\n", argv[i]);
0045             continue;
0046         }
0047 
0048         struct timespec start, end;
0049         clock_gettime(CLOCK_REALTIME, &start);
0050         void* result = dlopen(argv[i], flags);
0051         clock_gettime(CLOCK_REALTIME, &end);
0052 
0053         if (!result) {
0054             fprintf(stderr, "Loading %s failed: %s\n", argv[i], dlerror());
0055             return 1;
0056         }
0057 
0058         long long diff = (end.tv_sec - start.tv_sec) * 1000000000;
0059         if (diff == 0)
0060             diff = end.tv_nsec - start.tv_nsec;
0061         else {
0062             diff += end.tv_nsec;
0063             diff += 1000000000 - start.tv_nsec;
0064         }
0065         fprintf(stdout, "LDBENCHMARKRUNNER\t%s\t%.2f\n", argv[i], diff/1000.0);
0066     }
0067 
0068     return 0;
0069 }