File: pentium.c

package info (click to toggle)
swi-prolog 6.6.6-1~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 82,312 kB
  • sloc: ansic: 322,250; perl: 245,822; sh: 6,651; java: 5,254; makefile: 4,423; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (158 lines) | stat: -rw-r--r-- 3,824 bytes parent folder | download | duplicates (3)
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
/*  $Id$

    Part of SWI-Prolog

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        J.Wielemaker@uva.nl
    WWW:           http://www.swi-prolog.org
    Copyright (C): 1985-2008, University of Amsterdam

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/


#include <pentium.h>
#include <stdio.h>

prof_record prof_data[MAXPROF];
prof_record *prof_current;
ticks prof_ticks;

static ticks overhead;

/* See http://www.technovelty.org/code/c/reading-rdtsc.html */

ticks
pentium_clock()
{ unsigned int iax;
  unsigned int idx;

  __asm__ __volatile__ ("rdtsc"
			: "=a" (iax), "=d" (idx)
		       );

  return (ticks)iax | ((ticks)idx<<32);
}


static double
CPU_MHz()
{ static double mhz = 1400.00;
  static int done = 0;

  if ( !done )
  { FILE *fp = fopen("/proc/cpuinfo", "r");
    char buf[256];

    if ( fp )
    { while(fgets(buf, sizeof(buf), fp))
      { if ( sscanf(buf, "cpu MHz : %lf", &mhz) == 1 )
	{ /*printf("%f MHz CPU\n", mhz);*/
	  break;
	}
      }

      fclose(fp);
    }

    done++;
  }

  return mhz;
}


static int
prof_compare(const void *p1, const void *p2)
{ const prof_record *pr1 = p1;
  const prof_record *pr2 = p2;
  ticks t1 = pr1->ticks - pr1->calls*overhead;
  ticks t2 = pr2->ticks - pr2->calls*overhead;

  return t1  > t2 ? -1 :
         t1 == t2 ?  0 :
		     1;
}


static void
prof_report_field(prof_record *pr)
{ if ( pr->name && pr->calls > 0 )
  { double f   = (double)(pr->fastest-overhead)/CPU_MHz();
    double av  = (double)(pr->ticks/pr->calls-overhead)/CPU_MHz();
    double tot = av*(double)pr->calls;

    printf("%9d %10.3f %10.3f %20.3f %s\n",
	   pr->calls, f, av, tot, pr->name);
  }
  memset(pr, 0, sizeof(*pr));
}

#define PROF_NOOP

void
prof_report()
{ int i;
  ticks totalticks = 0, totalfast = 0, totalav = 0;
  long long totalcalls = 0;
  prof_record *pr = &prof_data[0];

  for(i=0; i<100; i++)
    PROF(0, PROF_NOOP);

  printf("------------------------------------------------------\n");
  printf("Pentium rdtsc timing in micro-seconds (%.0f MHz CPU)\n", CPU_MHz());
  printf("------------------------------------------------------\n");
  printf("%9s %10s %10s %20s %s\n",
	 "calls", "fastest", "av", "total", "statement");
  printf("------------------------------------------------------\n");
  overhead = 0;
  prof_report_field(pr);
  overhead = pr->fastest;
  pr++;
  printf("------------------------------------------------------\n");

  qsort(pr, MAXPROF-1, sizeof(*pr), prof_compare);
  for(i=1; i<MAXPROF; i++, pr++)
  { if ( pr->name && pr->calls > 0 )
    { totalfast  += pr->fastest - overhead;
      totalav    += pr->ticks/pr->calls - overhead;
      totalticks += pr->ticks - pr->calls*overhead;
      totalcalls += pr->calls;
    }

    prof_report_field(pr);
  }

  printf("------------------------------------------------------\n");

  { double tot = (double)totalticks/CPU_MHz();

    printf("%9lld %10s %10s %20.3f %s\n",
	   totalcalls, "", "", tot, "Totals");
  }
}


void
prof_reset()
{ prof_record *pr = &prof_data[0];
  int i;

  END_PROF();

  for(i=0; i<MAXPROF; i++, pr++)
    memset(pr, 0, sizeof(*pr));
}