1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 
17 #include <time.h>
18 #include <cstring>
19 #include <iostream>
20 #include <optional>
21 #include <sstream>
22 #include <unordered_map>
23 
s2ns(uint64_t s)24 uint64_t s2ns(uint64_t s) {
25     return s * 1000000000ull;
26 }
27 
PrintHelpAndExit(const std::string & error_msg="")28 void PrintHelpAndExit(const std::string& error_msg = "") {
29     int exit_error = 0;
30     if (!error_msg.empty()) {
31         std::cout << error_msg << "\n";
32         exit_error = 1;
33     }
34 
35     std::cout << "Usage: ClockTime [CLOCK_ID]\n"
36               << "CLOCK_ID can be  CLOCK_REALTIME or CLOCK_MONOTONIC \n"
37               << "if omitted, it will obtain the processors's time-stamp counter \n"
38               << "on x86 it will use RDTSC, on arm64 it will use MRS CNTCVT. \n"
39               << "-h, --help      Print this help message\n";
40 
41     exit(exit_error);
42 }
43 
GetTime(int type,uint64_t * ts_ns)44 int GetTime(int type, uint64_t* ts_ns) {
45     struct timespec ts;
46     int res = clock_gettime(type, &ts);
47     if (!res) {
48         *ts_ns = s2ns(ts.tv_sec) + ts.tv_nsec;
49     }
50     return res;
51 }
52 
GetCPUTicks()53 uint64_t GetCPUTicks() {
54 #if defined(__x86_64__) || defined(__amd64__)
55     uint32_t hi, lo;
56     asm volatile("rdtsc" : "=a"(lo), "=d"(hi));
57     return ((uint64_t)lo) | (((uint64_t)hi) << 32);
58 #elif defined(__aarch64__)
59     uint64_t vct;
60     asm volatile("mrs %0, cntvct_el0" : "=r"(vct));
61     return vct;
62 #else
63     PrintHelpAndExit("GetCPUTicks() is not supported");
64     return 0;
65 #endif
66 }
67 
main(int argc,char * argv[])68 int main(int argc, char* argv[]) {
69     std::unordered_map<std::string, clockid_t> clock_map = {
70             std::make_pair("CLOCK_REALTIME", CLOCK_REALTIME),
71             std::make_pair("CLOCK_MONOTONIC", CLOCK_MONOTONIC)};
72 
73     if (argc == 1) {
74         std::cout << GetCPUTicks() << "\n";
75     } else if (argc == 2) {
76         if (!(strcmp(argv[1], "-h") && strcmp(argv[1], "--help"))) {
77             PrintHelpAndExit();
78         }
79 
80         uint64_t ts_ns;
81         auto it = clock_map.find(argv[1]);
82         if (it == clock_map.end()) {
83             PrintHelpAndExit("Wrong CLOCK_ID");
84         }
85 
86         int res = GetTime(it->second, &ts_ns);
87         if (res) {
88             std::stringstream err_msg("GetTime() got error");
89             err_msg << res;
90             PrintHelpAndExit(err_msg.str());
91         }
92 
93         std::cout << ts_ns << "\n";
94     } else {
95         PrintHelpAndExit("Wrong number of arguments");
96     }
97 
98     return EXIT_SUCCESS;
99 }
100