📄 timestamp.c
字号:
/* Copyright (C) 2004,2005,2006 Bull S.A. * * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#if HAVE_CONFIG_H#include <config.h>#endif#define _GNU_SOURCE#include <string.h>#include <time.h>#include <fcntl.h>#include <sys/types.h>#include <sys/stat.h>#include "timestamp.h"#include "types.h"ptt_timestamp_t get_timestamp() { ptt_timestamp_t timestamp;#if defined(__powerpc__) unsigned long lower, upper, tmp; do { __asm__ __volatile__ ("mftbu %0 #load from TBU" : "=r" (upper) :); __asm__ __volatile__ ("mftb %0 #load from TBL" : "=r" (lower) :); __asm__ __volatile__ ("mftbu %0 #load from TBU" : "=r" (tmp) :); } while (upper != tmp); /* loop if carry occurred */ timestamp = ((hp_timing_t)(upper)<<32)|(lower&0xffffffff);#elif defined(__powerpc64__) __asm__ __volatile__ ( "mftb %0": "=r" (timestamp) :);#elif ((defined(__i386__)) || (defined(__x86_64__))) __asm__ __volatile__ ("rdtsc" : "=A" (timestamp));#elif defined(__ia64__)#define REPEAT_READ(val) __builtin_expect ((long int) val == -1, 0)unsigned long int __itc; do asm volatile ("mov %0=ar.itc" : "=r" (__itc) : : "memory"); while (REPEAT_READ (__itc)); timestamp = __itc;#else#error unsupported arch#endif return timestamp;}/* read the information from /proc filesystem. * contains at least one line like: * cpu MHz : 497.840237 * or also * cpu MHz : 497.841 * search for this line and convert the number in an integer. */int get_clockfreq_khz (void) { long long result = 0; int fd; /* proc filesystem generates "files" up to a size of 4096 bytes. */ char buffer[4096]; ssize_t n; char *mhz, *endp; int seen_decpoint = 0, ndigits = 0; if ((fd = open ("/proc/cpuinfo", O_RDONLY)) == -1) return 0; if ((n = read (fd, buffer, sizeof(buffer))) == -1) return 0;#if defined(__powerpc64__) || defined(__powerpc__) mhz = memmem (buffer, n, "timebase", 8);#elif defined(__ia64) mhz = memmem (buffer, n, "itc MHz", 7);#else mhz = memmem (buffer, n, "cpu MHz", 7);#endif if (mhz == NULL) return 0; endp = buffer + n; while (mhz < endp && (*mhz < '0' || *mhz > '9') && *mhz != '\n') ++mhz; while (mhz < endp && *mhz != '\n') { if (*mhz >= '0' && *mhz <= '9') { result *= 10; result += *mhz - '0'; if (seen_decpoint) ++ndigits; } else if (*mhz == '.') seen_decpoint = 1; ++mhz; }#if !defined(__powerpc64__) || !defined(__powerpc__) /* compensate for missing digits at the end. */ while (ndigits++ < 6) result *= 10;#endif close (fd); return result/1000;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -