YoushouldhavereceivedcopiesoftheGNUGeneralPublicLicenseandthe GNULesserGeneralPublicLicensealongwiththeGNUMPLibrary.Ifnot,
see https://www.gnu.org/licenses/. */
/* Currently we don't get a CPU frequency on the following systems,
#if HAVE_SYS_IOGRAPH_H #include <sys/iograph.h> /* for IRIX INFO_LBL_DETAIL_INVENT */ #endif
#if HAVE_SYS_PARAM_H /* for constants needed by NetBSD <sys/sysctl.h> */ #include <sys/param.h> /* and needed by HPUX <sys/pstat.h> */ #endif
#if HAVE_SYS_PSTAT_H #include <sys/pstat.h> /* for HPUX pstat_getprocessor() */ #endif
#if HAVE_SYS_SYSCTL_H #include <sys/sysctl.h> /* for sysctlbyname() */ #endif
#if TIME_WITH_SYS_TIME # include <sys/time.h> /* for struct timeval */ # include <time.h> #else # if HAVE_SYS_TIME_H # include <sys/time.h> # else # include <time.h> # endif #endif
#if HAVE_SYS_RESOURCE_H #include <sys/resource.h> /* for struct rusage */ #endif
#if HAVE_SYS_PROCESSOR_H #include <sys/processor.h> /* for solaris processor_info_t */ #endif
/* On AIX 5.1 with gcc 2.9-aix51-020209 in -maix64 mode, <sys/sysinfo.h> getsanerrorabout"fill"in"structcpuinfo"havinganegativesize, apparentlydueto__64BIT_KERNELnotbeingdefinedbecause_KERNELisnot defined.Avoidthisfileifwedon'tactuallyneedit,whichwedon'ton
AIX since there's no getsysinfo there. */ #if HAVE_SYS_SYSINFO_H && HAVE_GETSYSINFO #include <sys/sysinfo.h> /* for OSF getsysinfo */ #endif
/* GMP_CPU_FREQUENCY environment variable. Should be in Hertz and can be
floating point, for example "450e6". */ staticint
freq_environment (int help)
{ char *e;
HELP ("environment variable GMP_CPU_FREQUENCY (in Hertz)");
e = getenv ("GMP_CPU_FREQUENCY"); if (e == NULL) return0;
speed_cycletime = 1.0 / atof (e);
if (speed_option_verbose)
printf ("Using GMP_CPU_FREQUENCY %.2f for cycle time %.3g\n",
atof (e), speed_cycletime);
return1;
}
/* getsysinfo is available on OSF, or 4.0 and up at least. Themanpage(on4.0)suggestsa0returnindicatesinformationnot
available, but that seems to be the normal return for GSI_CPU_INFO. */ staticint
freq_getsysinfo (int help)
{ #if HAVE_GETSYSINFO struct cpu_info c; int start;
HELP ("getsysinfo() GSI_CPU_INFO");
start = 0; if (getsysinfo (GSI_CPU_INFO, (caddr_t) &c, sizeof (c),
&start, NULL, NULL) != -1)
{
speed_cycletime = 1e-6 / (double) c.mhz; if (speed_option_verbose)
printf ("Using getsysinfo() GSI_CPU_INFO %u for cycle time %.3g\n",
c.mhz, speed_cycletime); return1;
} #endif return0;
}
/* In HPUX 10 and up, pstat_getprocessor() psp_iticksperclktick is the numberofCPUcycles(ie.theCR16register)perCLK_TCK.HPUX9doesn't havethatfieldinpst_processorthough,andhasnoapparent
equivalent. */
HELP ("pstat_getprocessor() psp_iticksperclktick");
if (pstat_getprocessor (&p, sizeof(p), 1, 0) != -1)
{ long c = clk_tck();
speed_cycletime = 1.0 / (c * p.psp_iticksperclktick); if (speed_option_verbose)
printf ("Using pstat_getprocessor() psp_iticksperclktick %lu and clk_tck %ld for cycle time %.3g\n",
(unsignedlong) p.psp_iticksperclktick, c,
speed_cycletime); return1;
} #endif return0;
}
/* i386 FreeBSD 2.2.8 sysctlbyname machdep.i586_freq is in Hertz.
There's no obvious defines available to get this from plain sysctl. */ staticint
freq_sysctlbyname_i586_freq (int help)
{ #if HAVE_SYSCTLBYNAME unsigned val;
size_t size;
staticint
freq_proc_cpuinfo (int help)
{
FILE *fp; char buf[128]; double val; int ret = 0; int end;
HELP ("linux kernel /proc/cpuinfo file, cpu MHz or bogomips");
if ((fp = fopen ("/proc/cpuinfo", "r")) != NULL)
{ while (fgets (buf, sizeof (buf), fp) != NULL)
{ if (sscanf (buf, "cycle frequency [Hz] : %lf", &val) == 1
&& val != 0.0)
{
speed_cycletime = 1.0 / val; if (speed_option_verbose)
printf ("Using /proc/cpuinfo \"cycle frequency\" %.2f for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
} if (sscanf (buf, "cpu MHz : %lf\n", &val) == 1)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /proc/cpuinfo \"cpu MHz\" %.2f for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
}
end = 0; if (sscanf (buf, "clock : %lfMHz\n%n", &val, &end) == 1 && end != 0)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /proc/cpuinfo \"clock\" %.2f for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
} if (sscanf (buf, "bogomips : %lf\n", &val) == 1
|| sscanf (buf, "BogoMIPS : %lf\n", &val) == 1)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /proc/cpuinfo \"bogomips\" %.2f for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
}
}
fclose (fp);
} return ret;
}
/* /bin/sysinfo for SunOS 4.
Prints a line like: cpu0 is a "75 MHz TI,TMS390Z55" CPU */ staticint
freq_sunos_sysinfo (int help)
{ int ret = 0; #if HAVE_POPEN
FILE *fp; char buf[128]; double val; int end;
HELP ("SunOS /bin/sysinfo program output, cpu0");
/* Error messages are sent to /dev/null in case /bin/sysinfo doesn't
exist. The brackets are necessary for some shells. */ if ((fp = popen ("(/bin/sysinfo) 2>/dev/null", "r")) != NULL)
{ while (fgets (buf, sizeof (buf), fp) != NULL)
{
end = 0; if (sscanf (buf, " cpu0 is a \"%lf MHz%n", &val, &end) == 1
&& end != 0)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /bin/sysinfo \"cpu0 MHz\" %.2f for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
}
}
pclose (fp);
} #endif return ret;
}
/* "/etc/hw -r cpu" for SCO OpenUnix 8, printing a line like ThespeedoftheCPUisapproximately450MHz
*/ staticint
freq_sco_etchw (int help)
{ int ret = 0; #if HAVE_POPEN
FILE *fp; char buf[128]; double val; int end;
HELP ("SCO /etc/hw program output");
/* Error messages are sent to /dev/null in case /etc/hw doesn't exist.
The brackets are necessary for some shells. */ if ((fp = popen ("(/etc/hw -r cpu) 2>/dev/null", "r")) != NULL)
{ while (fgets (buf, sizeof (buf), fp) != NULL)
{
end = 0; if (sscanf (buf, " The speed of the CPU is approximately %lfMHz%n",
&val, &end) == 1 && end != 0)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /etc/hw %.2f MHz, for cycle time %.3g\n",
val, speed_cycletime);
ret = 1; break;
}
}
pclose (fp);
} #endif return ret;
}
/* attr_get("/hw/cpunum/0",INFO_LBL_DETAIL_INVENT) ic_cpu_info.cpufq for IRIX6.5.Pastversionsdon'thaveINFO_LBL_DETAIL_INVENT, invent_cpuinfo_t,or/hw/cpunum/0.
Thesameinformationisavailablefromthe"hinv-cprocessor"command,
but it seems better to make a system call where possible. */
staticint
freq_attr_get_invent (int help)
{ int ret = 0; #if HAVE_ATTR_GET && HAVE_INVENT_H && defined (INFO_LBL_DETAIL_INVENT)
invent_cpuinfo_t inv; int len, val;
HELP ("attr_get(\"/hw/cpunum/0\") ic_cpu_info.cpufq");
len = sizeof (inv); if (attr_get ("/hw/cpunum/0", INFO_LBL_DETAIL_INVENT,
(char *) &inv, &len, 0) == 0
&& len == sizeof (inv)
&& inv.ic_gen.ig_invclass == INV_PROCESSOR)
{
val = inv.ic_cpu_info.cpufq;
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using attr_get(\"/hw/cpunum/0\") ic_cpu_info.cpufq %d MHz for cycle time %.3g\n", val, speed_cycletime);
ret = 1;
} #endif return ret;
}
/* FreeBSD on i386 gives a line like the following at bootup, and which can bereadbackfrom/var/run/dmesg.boot.
It'sbettertouse/var/run/dmesg.bootthantorun/sbin/dmesg,sincethe latterprintsthecurrentsystemmessagebuffer,whichisalimitedsize
and can wrap around if the system is up for a long time. */
staticint
freq_bsd_dmesg (int help)
{
FILE *fp; char buf[256], *p; double val; int ret = 0; int end;
HELP ("BSD /var/run/dmesg.boot file");
if ((fp = fopen ("/var/run/dmesg.boot", "r")) != NULL)
{ while (fgets (buf, sizeof (buf), fp) != NULL)
{ if (memcmp (buf, "CPU:", 4) == 0)
{ for (p = buf; *p != '\0'; p++)
{
end = 0; if (sscanf (p, "(%lf-MHz%n", &val, &end) == 1 && end != 0)
{
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using /var/run/dmesg.boot CPU: %.2f MHz for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
}
}
}
}
fclose (fp);
} return ret;
}
/* "hinv -c processor" for IRIX. The following lines have been seen,
staticint
freq_irix_hinv (int help)
{ int ret = 0; #if HAVE_POPEN
FILE *fp; char buf[128]; double val; int nproc, end;
HELP ("IRIX \"hinv -c processor\" output");
/* Error messages are sent to /dev/null in case hinv doesn't exist. The
brackets are necessary for some shells. */ if ((fp = popen ("(hinv -c processor) 2>/dev/null", "r")) != NULL)
{ while (fgets (buf, sizeof (buf), fp) != NULL)
{
end = 0; if (sscanf (buf, "Processor 0: %lf MHZ%n", &val, &end) == 1
&& end != 0)
{
found:
speed_cycletime = 1e-6 / val; if (speed_option_verbose)
printf ("Using hinv -c processor \"%.2f MHZ\" for cycle time %.3g\n", val, speed_cycletime);
ret = 1; break;
}
end = 0; if (sscanf (buf, "%d %lf MHZ%n", &nproc, &val, &end) == 2
&& end != 0) goto found;
}
pclose (fp);
} #endif return ret;
}
/* processor_info() for Solaris. "psrinfo" is the command-line interface to this."prtconf-vp"givessimilarinformation.
AppleDarwinhasaprocessor_info,butinanincompatiblestyle.It
doesn't have <sys/processor.h>, so test for that. */
staticint
freq_processor_info (int help)
{ #if HAVE_PROCESSOR_INFO && HAVE_SYS_PROCESSOR_H
processor_info_t p; int i, n, mhz = 0;
HELP ("processor_info() pi_clock");
n = sysconf (_SC_NPROCESSORS_CONF); for (i = 0; i < n; i++)
{ if (processor_info (i, &p) != 0) continue; if (p.pi_state != P_ONLINE) continue;
if (mhz != 0 && p.pi_clock != mhz)
{
fprintf (stderr, "freq_processor_info(): There's more than one CPU and they have different clock speeds\n"); return0;
}
mhz = p.pi_clock;
}
speed_cycletime = 1.0e-6 / (double) mhz;
if (speed_option_verbose)
printf ("Using processor_info() %d mhz for cycle time %.3g\n",
mhz, speed_cycletime); return1;
/* MEASURE_MATCH is how many readings within MEASURE_TOLERANCE of each other
are required. This must be at least 2. */ #define MEASURE_MAX_ATTEMPTS 20 #define MEASURE_TOLERANCE 1.005/* 0.5% */ #define MEASURE_MATCH 3
double
freq_measure (constchar *name, double (*one) (void))
{ double t[MEASURE_MAX_ATTEMPTS]; int i, j;
for (i = 0; i < numberof (t); i++)
{
t[i] = (*one) ();
qsort (t, i+1, sizeof(t[0]), (qsort_function_t) double_cmp_ptr); if (speed_option_verbose >= 3) for (j = 0; j <= i; j++)
printf (" t[%d] is %.6g\n", j, t[j]);
for (j = 0; j+MEASURE_MATCH-1 <= i; j++)
{ if (t[j+MEASURE_MATCH-1] <= t[j] * MEASURE_TOLERANCE)
{ /* use the average of the range found */ return (t[j+MEASURE_MATCH-1] + t[j]) / 2.0;
}
}
} return -1.0;
}
/* Each function returns 1 if it succeeds in setting speed_cycletime, or 0 ifnot.
Ingeneralsystemcalltestsarefirstsincethey'refast,thenfile tests,thentestsrunningprograms.Necessaryexceptionstothisrule arenoted.Themeasuringislastsinceit'stimeconsuming,andrather
wasteful of cpu. */
staticint
freq_all (int help)
{ return /* This should be first, so an environment variable can override
anything the system gives. */
freq_environment (help)
if (speed_option_verbose)
printf ("CPU frequency couldn't be determined\n");
}
void
speed_cycletime_fail (constchar *str)
{
fprintf (stderr, "Measuring with: %s\n", speed_time_string);
fprintf (stderr, "%s,\n", str);
fprintf (stderr, "but none of the following are available,\n");
freq_all (1);
abort ();
}
/* speed_time_init leaves speed_cycletime set to either 0.0 or 1.0 when the CPUfrequencyisunknown.0.0iswhenthetimebaseisinseconds,so that'snogoodifcyclesarewanted.1.0iswhenthetimebaseisin
cycles, which conversely is no good if seconds are wanted. */ void
speed_cycletime_need_cycles (void)
{
speed_time_init (); if (speed_cycletime == 0.0)
speed_cycletime_fail
("Need to know CPU frequency to give times in cycles");
} void
speed_cycletime_need_seconds (void)
{
speed_time_init (); if (speed_cycletime == 1.0)
speed_cycletime_fail
("Need to know CPU frequency to convert cycles to seconds");
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.