1 /* Copyright (C) 2021 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include <sys/resource.h>
32 /* =============================================================== */
34 * Below this are the get_clock_rate() and get_ncpus() for all architectures
37 static int clock_rate
= 0;
39 static char msgbuf
[1024];
44 /* Linux version -- read /proc/cpuinfo
45 * Note the parsing is different on intel-Linux and sparc-Linux
47 FILE *fp
= fopen ("/proc/cpuinfo", "r");
52 while (fgets (temp
, sizeof (temp
), fp
) != NULL
)
55 /* cpu count for SPARC linux -- read from /proc/cpuinfo */
56 if (strncmp (temp
, "ncpus active", 12) == 0)
58 char *val
= strchr (temp
, ':');
59 ncpus
= val
? atol (val
+ 1) : 0;
61 #endif /* ARCH(SPARC) */
65 /* pick the first line that gives a CPU clock rate */
68 if (strncmp (temp
, "Cpu0ClkTck", 10) == 0)
70 char *val
= strchr (temp
, ':');
71 clk
= val
? strtoll (val
+ 1, NULL
, 16) : 0;
72 clock_rate
= (int) (clk
/ 1000000);
75 if (strncmp (temp
, "cpu MHz", 7) == 0)
77 char *val
= strchr (temp
, ':');
78 clock_rate
= val
? atoi (val
+ 1) : 0;
83 /* did we get a clock rate? */
87 /* since we got a cpu count, we can break from the look */
89 #endif /* ARCH(SPARC) */
92 /* On intel-Linux, count cpus based on "cpu MHz" lines */
93 if (strncmp (temp
, "cpu MHz", 7) == 0)
95 #endif /* ARCH(Intel) */
102 "Clock rate = %d MHz (from reading /proc/cpuinfo) %d CPUs\n",
105 /* did we get a clock rate? */
109 sprintf (msgbuf
, "Clock rate = %d MHz (set by default) %d CPUs\n",
123 /* gethrvtime -- generic solution, getting user time from
124 * clock_gettime(CLOCK_THREAD_CPUTIME_ID,..), and reformatting.
125 * need -lrt to compile.*/
131 int r
= clock_gettime (CLOCK_THREAD_CPUTIME_ID
, &tp
);
133 rc
= ((hrtime_t
) tp
.tv_sec
) * 1000000000 + (hrtime_t
) tp
.tv_nsec
;
139 * Clock that cannot be set and represents monotonic time since some
140 * unspecified starting point.
149 * For er_kernel on Linux, we want to match how DTrace gets its timestamps.
150 * This is CLOCK_MONOTONIC_RAW. It might be changing to CLOCK_MONOTONIC.
151 * For now, we change to "RAW" and can change back if DTrace changes.
153 * The two can be different. Check the clock_gettime() man page.
154 * CLOCK_MONOTONIC_RAW is Linux-specific and introduced in 2.6.28.
155 * It is impervious to NTP or adjtime adjustments.
157 * We must match the timer used in perfan/libcollector/src/gethrtime.c.
159 * There is no issue on Solaris, where gethrtime() is provided by the kernel
160 * and used by DTrace.
162 #ifdef CLOCK_MONOTONIC_RAW
163 int r
= clock_gettime (CLOCK_MONOTONIC_RAW
, &tp
);
165 int r
= clock_gettime (CLOCK_MONOTONIC
, &tp
);
168 rc
= ((hrtime_t
) tp
.tv_sec
) * 1000000000 + (hrtime_t
) tp
.tv_nsec
;