xref: /netbsd-src/external/gpl3/binutils.old/dist/gprofng/src/gethrtime.c (revision c42dbd0ed2e61fe6eda8590caa852ccf34719964)
1*c42dbd0eSchristos /* Copyright (C) 2021 Free Software Foundation, Inc.
2*c42dbd0eSchristos    Contributed by Oracle.
3*c42dbd0eSchristos 
4*c42dbd0eSchristos    This file is part of GNU Binutils.
5*c42dbd0eSchristos 
6*c42dbd0eSchristos    This program is free software; you can redistribute it and/or modify
7*c42dbd0eSchristos    it under the terms of the GNU General Public License as published by
8*c42dbd0eSchristos    the Free Software Foundation; either version 3, or (at your option)
9*c42dbd0eSchristos    any later version.
10*c42dbd0eSchristos 
11*c42dbd0eSchristos    This program is distributed in the hope that it will be useful,
12*c42dbd0eSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c42dbd0eSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c42dbd0eSchristos    GNU General Public License for more details.
15*c42dbd0eSchristos 
16*c42dbd0eSchristos    You should have received a copy of the GNU General Public License
17*c42dbd0eSchristos    along with this program; if not, write to the Free Software
18*c42dbd0eSchristos    Foundation, 51 Franklin Street - Fifth Floor, Boston,
19*c42dbd0eSchristos    MA 02110-1301, USA.  */
20*c42dbd0eSchristos 
21*c42dbd0eSchristos #include "config.h"
22*c42dbd0eSchristos #include <time.h>
23*c42dbd0eSchristos #include <unistd.h>
24*c42dbd0eSchristos #include <string.h>
25*c42dbd0eSchristos #include <stdio.h>
26*c42dbd0eSchristos #include <stdlib.h>
27*c42dbd0eSchristos #include <sys/resource.h>
28*c42dbd0eSchristos 
29*c42dbd0eSchristos #include "gp-defs.h"
30*c42dbd0eSchristos #include "gp-time.h"
31*c42dbd0eSchristos 
32*c42dbd0eSchristos /* =============================================================== */
33*c42dbd0eSchristos /*
34*c42dbd0eSchristos  * Below this are the get_clock_rate() and get_ncpus() for all architectures
35*c42dbd0eSchristos  */
36*c42dbd0eSchristos 
37*c42dbd0eSchristos static int clock_rate = 0;
38*c42dbd0eSchristos static int ncpus = 0;
39*c42dbd0eSchristos static char msgbuf[1024];
40*c42dbd0eSchristos 
41*c42dbd0eSchristos int
get_clock_rate(void)42*c42dbd0eSchristos get_clock_rate (void)
43*c42dbd0eSchristos {
44*c42dbd0eSchristos   /* Linux version -- read /proc/cpuinfo
45*c42dbd0eSchristos    *	Note the parsing is different on intel-Linux and sparc-Linux
46*c42dbd0eSchristos    */
47*c42dbd0eSchristos   FILE *fp = fopen ("/proc/cpuinfo", "r");
48*c42dbd0eSchristos   if (fp != NULL)
49*c42dbd0eSchristos     {
50*c42dbd0eSchristos 
51*c42dbd0eSchristos       char temp[1024];
52*c42dbd0eSchristos       while (fgets (temp, sizeof (temp), fp) != NULL)
53*c42dbd0eSchristos 	{
54*c42dbd0eSchristos #if ARCH(SPARC)
55*c42dbd0eSchristos 	  /* cpu count for SPARC linux -- read from /proc/cpuinfo */
56*c42dbd0eSchristos 	  if (strncmp (temp, "ncpus active", 12) == 0)
57*c42dbd0eSchristos 	    {
58*c42dbd0eSchristos 	      char *val = strchr (temp, ':');
59*c42dbd0eSchristos 	      ncpus = val ? atol (val + 1) : 0;
60*c42dbd0eSchristos 	    }
61*c42dbd0eSchristos #endif /* ARCH(SPARC) */
62*c42dbd0eSchristos 
63*c42dbd0eSchristos 	  if (clock_rate == 0)
64*c42dbd0eSchristos 	    {
65*c42dbd0eSchristos 	      /* pick the first line that gives a CPU clock rate */
66*c42dbd0eSchristos #if ARCH(SPARC)
67*c42dbd0eSchristos 	      long long clk;
68*c42dbd0eSchristos 	      if (strncmp (temp, "Cpu0ClkTck", 10) == 0)
69*c42dbd0eSchristos 		{
70*c42dbd0eSchristos 		  char *val = strchr (temp, ':');
71*c42dbd0eSchristos 		  clk = val ? strtoll (val + 1, NULL, 16) : 0;
72*c42dbd0eSchristos 		  clock_rate = (int) (clk / 1000000);
73*c42dbd0eSchristos 		}
74*c42dbd0eSchristos #else
75*c42dbd0eSchristos 	      if (strncmp (temp, "cpu MHz", 7) == 0)
76*c42dbd0eSchristos 		{
77*c42dbd0eSchristos 		  char *val = strchr (temp, ':');
78*c42dbd0eSchristos 		  clock_rate = val ? atoi (val + 1) : 0;
79*c42dbd0eSchristos 		}
80*c42dbd0eSchristos #endif /* ARCH() */
81*c42dbd0eSchristos 	    }
82*c42dbd0eSchristos 
83*c42dbd0eSchristos 	  /* did we get a clock rate? */
84*c42dbd0eSchristos 	  if (clock_rate != 0)
85*c42dbd0eSchristos 	    {
86*c42dbd0eSchristos #if ARCH(SPARC)
87*c42dbd0eSchristos 	      /* since we got a cpu count, we can break from the look */
88*c42dbd0eSchristos 	      break;
89*c42dbd0eSchristos #endif /* ARCH(SPARC) */
90*c42dbd0eSchristos 	    }
91*c42dbd0eSchristos #if ARCH(Intel)
92*c42dbd0eSchristos 	  /* On intel-Linux, count cpus based on "cpu MHz" lines */
93*c42dbd0eSchristos 	  if (strncmp (temp, "cpu MHz", 7) == 0)
94*c42dbd0eSchristos 	    ncpus++;
95*c42dbd0eSchristos #endif /* ARCH(Intel) */
96*c42dbd0eSchristos 	}
97*c42dbd0eSchristos       fclose (fp);
98*c42dbd0eSchristos     }
99*c42dbd0eSchristos 
100*c42dbd0eSchristos   if (clock_rate != 0)
101*c42dbd0eSchristos     sprintf (msgbuf,
102*c42dbd0eSchristos 	     "Clock rate = %d MHz (from reading /proc/cpuinfo) %d CPUs\n",
103*c42dbd0eSchristos 	     clock_rate, ncpus);
104*c42dbd0eSchristos 
105*c42dbd0eSchristos   /* did we get a clock rate? */
106*c42dbd0eSchristos   if (clock_rate == 0)
107*c42dbd0eSchristos     {
108*c42dbd0eSchristos       clock_rate = 1000;
109*c42dbd0eSchristos       sprintf (msgbuf, "Clock rate = %d MHz (set by default) %d CPUs\n",
110*c42dbd0eSchristos 	       clock_rate, ncpus);
111*c42dbd0eSchristos     }
112*c42dbd0eSchristos   return clock_rate;
113*c42dbd0eSchristos }
114*c42dbd0eSchristos 
115*c42dbd0eSchristos int
get_ncpus(void)116*c42dbd0eSchristos get_ncpus (void)
117*c42dbd0eSchristos {
118*c42dbd0eSchristos   if (clock_rate == 0)
119*c42dbd0eSchristos     get_clock_rate ();
120*c42dbd0eSchristos   return ncpus;
121*c42dbd0eSchristos }
122*c42dbd0eSchristos 
123*c42dbd0eSchristos /* gethrvtime -- generic solution, getting user time from
124*c42dbd0eSchristos  * clock_gettime(CLOCK_THREAD_CPUTIME_ID,..), and reformatting.
125*c42dbd0eSchristos  * need -lrt to compile.*/
126*c42dbd0eSchristos hrtime_t
gethrvtime()127*c42dbd0eSchristos gethrvtime ()
128*c42dbd0eSchristos {
129*c42dbd0eSchristos   struct timespec tp;
130*c42dbd0eSchristos   hrtime_t rc = 0;
131*c42dbd0eSchristos   int r = clock_gettime (CLOCK_THREAD_CPUTIME_ID, &tp);
132*c42dbd0eSchristos   if (r == 0)
133*c42dbd0eSchristos     rc = ((hrtime_t) tp.tv_sec) * 1000000000 + (hrtime_t) tp.tv_nsec;
134*c42dbd0eSchristos   return rc;
135*c42dbd0eSchristos }
136*c42dbd0eSchristos 
137*c42dbd0eSchristos /*
138*c42dbd0eSchristos  *  CLOCK_MONOTONIC
139*c42dbd0eSchristos  *  Clock that cannot be set and represents monotonic time since some
140*c42dbd0eSchristos  *           unspecified starting point.
141*c42dbd0eSchristos  */
142*c42dbd0eSchristos hrtime_t
gethrtime(void)143*c42dbd0eSchristos gethrtime (void)
144*c42dbd0eSchristos {
145*c42dbd0eSchristos   struct timespec tp;
146*c42dbd0eSchristos   hrtime_t rc = 0;
147*c42dbd0eSchristos 
148*c42dbd0eSchristos   /*
149*c42dbd0eSchristos    * For er_kernel on Linux, we want to match how DTrace gets its timestamps.
150*c42dbd0eSchristos    * This is CLOCK_MONOTONIC_RAW.  It might be changing to CLOCK_MONOTONIC.
151*c42dbd0eSchristos    * For now, we change to "RAW" and can change back if DTrace changes.
152*c42dbd0eSchristos    *
153*c42dbd0eSchristos    * The two can be different.  Check the clock_gettime() man page.
154*c42dbd0eSchristos    * CLOCK_MONOTONIC_RAW is Linux-specific and introduced in 2.6.28.
155*c42dbd0eSchristos    * It is impervious to NTP or adjtime adjustments.
156*c42dbd0eSchristos    *
157*c42dbd0eSchristos    * We must match the timer used in perfan/libcollector/src/gethrtime.c.
158*c42dbd0eSchristos    *
159*c42dbd0eSchristos    * There is no issue on Solaris, where gethrtime() is provided by the kernel
160*c42dbd0eSchristos    * and used by DTrace.
161*c42dbd0eSchristos    */
162*c42dbd0eSchristos #ifdef CLOCK_MONOTONIC_RAW
163*c42dbd0eSchristos   int r = clock_gettime (CLOCK_MONOTONIC_RAW, &tp);
164*c42dbd0eSchristos #else
165*c42dbd0eSchristos   int r = clock_gettime (CLOCK_MONOTONIC, &tp);
166*c42dbd0eSchristos #endif
167*c42dbd0eSchristos   if (r == 0)
168*c42dbd0eSchristos     rc = ((hrtime_t) tp.tv_sec) * 1000000000 + (hrtime_t) tp.tv_nsec;
169*c42dbd0eSchristos   return rc;
170*c42dbd0eSchristos }
171