File: linux-timer.c

package info (click to toggle)
papi 5.7.0+dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 9,856 kB
  • sloc: ansic: 93,265; fortran: 3,338; xml: 2,460; makefile: 815; sh: 290
file content (558 lines) | stat: -rw-r--r-- 13,241 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
 * File:    linux-timer.c
 *
 * @author:  Vince Weaver
 *           vincent.weaver @ maine.edu
 * Mods:     Philip Mucci
 *           mucci @ icl.utk.edu
 */

#include <time.h>
#include <sys/syscall.h>

#include "papi.h"
#include "papi_internal.h"
#include "papi_vector.h"

#include <fcntl.h>
#include <errno.h>
#include <string.h>

#include <sys/time.h>

#include <fcntl.h>
#include "linux-common.h"

#include <sys/time.h>
#include <sys/resource.h>

#include <sys/times.h>
#include <stdint.h>

#ifdef __ia64__
#include "perfmon/pfmlib_itanium2.h"
#include "perfmon/pfmlib_montecito.h"
#endif

#ifdef __powerpc__
#include <sys/platform/ppc.h>
#endif

#if defined(HAVE_MMTIMER)
#include <sys/mman.h>
#include <linux/mmtimer.h>
#include <sys/ioctl.h>
#ifndef MMTIMER_FULLNAME
#define MMTIMER_FULLNAME "/dev/mmtimer"
#endif

static int mmdev_fd;
static unsigned long mmdev_mask;
static unsigned long mmdev_ratio;
static volatile unsigned long *mmdev_timer_addr;

        /* setup mmtimer */
int mmtimer_setup(void) {

	  unsigned long femtosecs_per_tick = 0;
	  unsigned long freq = 0;
	  int result;
	  int offset;

	  SUBDBG( "MMTIMER Opening %s\n", MMTIMER_FULLNAME );
	  if ( ( mmdev_fd = open( MMTIMER_FULLNAME, O_RDONLY ) ) == -1 ) {
	    PAPIERROR( "Failed to open MM timer %s", MMTIMER_FULLNAME );
	    return PAPI_ESYS;
	  }
	  SUBDBG( "MMTIMER checking if we can mmap" );
	  if ( ioctl( mmdev_fd, MMTIMER_MMAPAVAIL, 0 ) != 1 ) {
	    PAPIERROR( "mmap of MM timer unavailable" );
	    return PAPI_ESYS;
	  }
	  SUBDBG( "MMTIMER setting close on EXEC flag\n" );
	  if ( fcntl( mmdev_fd, F_SETFD, FD_CLOEXEC ) == -1 ) {
	    PAPIERROR( "Failed to fcntl(FD_CLOEXEC) on MM timer FD %d: %s",
		       mmdev_fd, strerror( errno ) );
	    return PAPI_ESYS;
	  }
	  SUBDBG( "MMTIMER is on FD %d, getting offset\n", mmdev_fd );
	  if ( ( offset = ioctl( mmdev_fd, MMTIMER_GETOFFSET, 0 ) ) < 0 ) {
	    PAPIERROR( "Failed to get offset of MM timer" );
	    return PAPI_ESYS;
	  }
	  SUBDBG( "MMTIMER has offset of %d, getting frequency\n", offset );
	  if ( ioctl( mmdev_fd, MMTIMER_GETFREQ, &freq ) == -1 ) {
	    PAPIERROR( "Failed to get frequency of MM timer" );
	    return PAPI_ESYS;
	  }
	  SUBDBG( "MMTIMER has frequency %lu Mhz\n", freq / 1000000 );
	  // don't know for sure, but I think this ratio is inverted
	  //     mmdev_ratio = (freq/1000000) / (unsigned long)_papi_hwi_system_info.hw_info.mhz;
          mmdev_ratio =
		  ( unsigned long ) _papi_hwi_system_info.hw_info.cpu_max_mhz / 
	                      ( freq / 1000000 );
          SUBDBG( "MMTIMER has a ratio of %ld to the CPU's clock, getting resolution\n",
		    mmdev_ratio );
          if ( ioctl( mmdev_fd, MMTIMER_GETRES, &femtosecs_per_tick ) == -1 ) {
		  PAPIERROR( "Failed to get femtoseconds per tick" );
		  return PAPI_ESYS;
          }
          SUBDBG( "MMTIMER res is %lu femtosecs/tick (10^-15s) or %f Mhz, getting valid bits\n",
	  femtosecs_per_tick, 1.0e9 / ( double ) femtosecs_per_tick );
          if ( ( result = ioctl( mmdev_fd, MMTIMER_GETBITS, 0 ) ) == -ENOSYS ) {
	     PAPIERROR( "Failed to get number of bits in MMTIMER" );
	     return PAPI_ESYS;
          }
          mmdev_mask = ~( 0xffffffffffffffff << result );
          SUBDBG( "MMTIMER has %d valid bits, mask %#16lx, getting mmaped page\n",
		    result, mmdev_mask );
          if ( ( mmdev_timer_addr =
		       ( unsigned long * ) mmap( 0, getpagesize(  ), PROT_READ,
						 MAP_PRIVATE, mmdev_fd,
						 0 ) ) == NULL ) {
	     PAPIERROR( "Failed to mmap MM timer" );
	     return PAPI_ESYS;
          }
          SUBDBG( "MMTIMER page is at %p, actual address is %p\n",
			mmdev_timer_addr, mmdev_timer_addr + offset );
          mmdev_timer_addr += offset;
          /* mmdev_fd should be closed and page should be unmapped in a global shutdown routine */
	  return PAPI_OK;

}

#else

#if defined(__powerpc__)
static uint64_t multiplier = 1;
#endif

int mmtimer_setup(void) {

#if defined(__powerpc__)
multiplier = ((uint64_t)_papi_hwi_system_info.hw_info.cpu_max_mhz * 1000000ULL) / (__ppc_get_timebase_freq()/(uint64_t)1000);
#endif
return PAPI_OK; }
#endif





/* Hardware clock functions */

/* All architectures should set HAVE_CYCLES in configure if they have these.
   Not all do so for now, we have to guard at the end of the statement,
   instead of the top. When all archs set this, this region will be guarded
   with:
     #if defined(HAVE_CYCLE)
   which is equivalent to
     #if !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_CLOCK_GETTIME)
*/

/************************/
/* MMTIMER get_cycles() */
/************************/

#if defined(HAVE_MMTIMER)

static inline long long
get_cycles( void )
{
	long long tmp = 0;

        tmp = *mmdev_timer_addr & mmdev_mask;
	SUBDBG("MMTIMER is %llu, scaled %llu\n",tmp,tmp*mmdev_ratio);
        tmp *= mmdev_ratio;

	return tmp;
}

/************************/
/* ia64 get_cycles()    */
/************************/

#elif defined(__ia64__)
extern int _perfmon2_pfm_pmu_type;

static inline long long
get_cycles( void )
{
	long long tmp = 0;
#if defined(__INTEL_COMPILER)
	tmp = __getReg( _IA64_REG_AR_ITC );
#else
	__asm__ __volatile__( "mov %0=ar.itc":"=r"( tmp )::"memory" );
#endif
	switch ( _perfmon2_pfm_pmu_type ) {
	case PFMLIB_MONTECITO_PMU:
		tmp = tmp * 4;
		break;
	}
	return tmp;
}

/************************/
/* x86 get_cycles()     */
/************************/

#elif (defined(__i386__)||defined(__x86_64__))
static inline long long
get_cycles( void )
{
	long long ret = 0;
#ifdef __x86_64__
	do {
		unsigned int a, d;
		asm volatile ( "rdtsc":"=a" ( a ), "=d"( d ) );
		( ret ) = ( ( long long ) a ) | ( ( ( long long ) d ) << 32 );
	}
	while ( 0 );
#else
	__asm__ __volatile__( "rdtsc":"=A"( ret ): );
#endif
	return ret;
}

/************************/
/* SPARC get_cycles()   */
/************************/

/* #define get_cycles _rtc ?? */
#elif defined(__sparc__)
static inline long long
get_cycles( void )
{
	register unsigned long ret asm( "g1" );

	__asm__ __volatile__( ".word 0x83410000"	/* rd %tick, %g1 */
						  :"=r"( ret ) );
	return ret;
}

/************************/
/* aarch64 get_cycles() */
/************************/

#elif defined(__aarch64__)
static inline long long
get_cycles( void )
{
	register unsigned long ret;

	__asm__ __volatile__ ("isb; mrs %0, cntvct_el0" : "=r" (ret));

	return ret;
}

/************************/
/* POWER get_cycles()   */
/************************/

#elif defined(__powerpc__)

static inline long long get_cycles()
{
    uint64_t result;
	int64_t retval;
#ifdef _ARCH_PPC64
    /*
        This reads timebase in one 64bit go.  Does *not* include a workaround for the cell (see 
        http://ozlabs.org/pipermail/linuxppc-dev/2006-October/027052.html)
    */
    __asm__ volatile(
        "mftb    %0"
        : "=r" (result));
#else
    /*
        Read the high 32bits of the timer, then the lower, and repeat if high order has changed in the meantime.  See
        http://ozlabs.org/pipermail/linuxppc-dev/1999-October/003889.html
    */
    unsigned long dummy;
    __asm__ volatile(
        "mfspr   %1,269\n\t"  /* mftbu */
        "mfspr   %L0,268\n\t" /* mftb */
        "mfspr   %0,269\n\t"  /* mftbu */
        "cmpw    %0,%1\n\t"   /* check if the high order word has chanegd */
        "bne     $-16"
        : "=r" (result), "=r" (dummy));
#endif
    retval = (result*multiplier)/1000ULL;
    return retval;
}

#elif (defined(__arm__) || defined(__mips__) || defined(__hppa__))
static inline long long
get_cycles( void )
{
  return 0;
}


#elif !defined(HAVE_GETTIMEOFDAY) && !defined(HAVE_CLOCK_GETTIME)
#error "No get_cycles support for this architecture. "
#endif






long long
_linux_get_real_cycles( void )
{
	long long retval;
#if defined(HAVE_GETTIMEOFDAY)||defined(__arm__)||defined(__mips__)

	/* Crude estimate, not accurate in prescence of DVFS */

	retval = _papi_os_vector.get_real_usec(  ) *
		( long long ) _papi_hwi_system_info.hw_info.cpu_max_mhz;
#else
	retval = get_cycles(  );
#endif
	return retval;
}




/********************************************************************
 * microsecond timers                                               *
 ********************************************************************/


/*******************************
 * HAVE_CLOCK_GETTIME          *
 *******************************/

long long
_linux_get_real_usec_gettime( void )
{

   long long retval;

   struct timespec foo;
#ifdef HAVE_CLOCK_GETTIME_REALTIME_HR
   syscall( __NR_clock_gettime, CLOCK_REALTIME_HR, &foo );
#else
   syscall( __NR_clock_gettime, CLOCK_REALTIME, &foo );
#endif
   retval = ( long long ) foo.tv_sec * ( long long ) 1000000;
   retval += ( long long ) ( foo.tv_nsec / 1000 );

   return retval;
}

/**********************
 * HAVE_GETTIMEOFDAY  *
 **********************/

long long
_linux_get_real_usec_gettimeofday( void )
{

   long long retval;

   struct timeval buffer;
   gettimeofday( &buffer, NULL );
   retval = ( long long ) buffer.tv_sec * ( long long ) 1000000;
   retval += ( long long ) ( buffer.tv_usec );

   return retval;
}


long long
_linux_get_real_usec_cycles( void )
{

   long long retval;

   /* Not accurate in the prescence of DVFS */

   retval = get_cycles(  ) /
            ( long long ) _papi_hwi_system_info.hw_info.cpu_max_mhz;

   return retval;
}



/*******************************
 * HAVE_PER_THREAD_GETRUSAGE   *
 *******************************/

long long
_linux_get_virt_usec_rusage( void )
{

    long long retval;

    struct rusage buffer;

    getrusage( RUSAGE_SELF, &buffer );
    SUBDBG( "user %d system %d\n", ( int ) buffer.ru_utime.tv_sec,
				( int ) buffer.ru_stime.tv_sec );
    retval = ( long long ) ( buffer.ru_utime.tv_sec + buffer.ru_stime.tv_sec )
             * ( long long ) 1000000;
    retval += (long long) ( buffer.ru_utime.tv_usec + buffer.ru_stime.tv_usec );

    return retval;
}

/**************************
 * HAVE_PER_THREAD_TIMES  *
 **************************/

long long
_linux_get_virt_usec_times( void )
{

   long long retval;

   struct tms buffer;

   times( &buffer );

   SUBDBG( "user %d system %d\n", ( int ) buffer.tms_utime,
				( int ) buffer.tms_stime );
   retval = ( long long ) ( ( buffer.tms_utime + buffer.tms_stime ) *
			    1000000 / sysconf( _SC_CLK_TCK ));

   /* NOT CLOCKS_PER_SEC as in the headers! */

   return retval;
}

/******************************/
/* HAVE_CLOCK_GETTIME_THREAD  */
/******************************/

long long
_linux_get_virt_usec_gettime( void )
{

    long long retval;

    struct timespec foo;

    syscall( __NR_clock_gettime, CLOCK_THREAD_CPUTIME_ID, &foo );
    retval = ( long long ) foo.tv_sec * ( long long ) 1000000;
    retval += ( long long ) foo.tv_nsec / 1000;

    return retval;
}

/********************/
/* USE_PROC_PTTIMER */
/********************/

long long
_linux_get_virt_usec_pttimer( void )
{

   long long retval;
   char buf[LINE_MAX];
   long long utime, stime;
   int rv, cnt = 0, i = 0;
   int stat_fd;


again:
   sprintf( buf, "/proc/%d/task/%d/stat", getpid(  ), mygettid(  ) );
   stat_fd = open( buf, O_RDONLY );
   if ( stat_fd == -1 ) {
      PAPIERROR( "open(%s)", buf );
      return PAPI_ESYS;
   }

   rv = read( stat_fd, buf, LINE_MAX * sizeof ( char ) );
   if ( rv == -1 ) {
      if ( errno == EBADF ) {
	 close(stat_fd);
	 goto again;
      }
      PAPIERROR( "read()" );
      close(stat_fd);
      return PAPI_ESYS;
   }
   lseek( stat_fd, 0, SEEK_SET );

   if (rv == LINE_MAX) rv--;
   buf[rv] = '\0';
   SUBDBG( "Thread stat file is:%s\n", buf );
   while ( ( cnt != 13 ) && ( i < rv ) ) {
      if ( buf[i] == ' ' ) {
	 cnt++;
      }
      i++;
   }

   if ( cnt != 13 ) {
      PAPIERROR( "utime and stime not in thread stat file?" );
      close(stat_fd);
      return PAPI_ESYS;
   }

   if ( sscanf( buf + i, "%llu %llu", &utime, &stime ) != 2 ) {
      close(stat_fd);
      PAPIERROR("Unable to scan two items from thread stat file at 13th space?");
      return PAPI_ESYS;
   }

   retval = ( utime + stime ) * ( long long ) 1000000 /_papi_os_info.clock_ticks;

   close(stat_fd);

   return retval;
}


/********************************************************************
 * nanosecond timers                                                *
 ********************************************************************/



/*******************************
 * HAVE_CLOCK_GETTIME          *
 *******************************/

long long
_linux_get_real_nsec_gettime( void )
{

   long long retval;

   struct timespec foo;
#ifdef HAVE_CLOCK_GETTIME_REALTIME_HR
   syscall( __NR_clock_gettime, CLOCK_REALTIME_HR, &foo );
#else
   syscall( __NR_clock_gettime, CLOCK_REALTIME, &foo );
#endif
   retval = ( long long ) foo.tv_sec * ( long long ) 1000000000;
   retval += ( long long ) ( foo.tv_nsec );

   return retval;
}


/******************************/
/* HAVE_CLOCK_GETTIME_THREAD  */
/******************************/

long long
_linux_get_virt_nsec_gettime( void )
{

    long long retval;

    struct timespec foo;

    syscall( __NR_clock_gettime, CLOCK_THREAD_CPUTIME_ID, &foo );
    retval = ( long long ) foo.tv_sec * ( long long ) 1000000000;
    retval += ( long long ) foo.tv_nsec ;

    return retval;
}