Commit 4ce184d8 by Dominic Hamon

Code reformat

parent 80093519
...@@ -414,8 +414,8 @@ class Benchmark { ...@@ -414,8 +414,8 @@ class Benchmark {
static void RunInstance(const Instance& b, BenchmarkReporter* br); static void RunInstance(const Instance& b, BenchmarkReporter* br);
friend class ::benchmark::State; friend class ::benchmark::State;
friend struct ::benchmark::internal::Benchmark::Instance; friend struct ::benchmark::internal::Benchmark::Instance;
friend void ::benchmark::internal::RunMatchingBenchmarks( friend void ::benchmark::internal::RunMatchingBenchmarks(const std::string&,
const std::string&, BenchmarkReporter*); BenchmarkReporter*);
DISALLOW_COPY_AND_ASSIGN(Benchmark); DISALLOW_COPY_AND_ASSIGN(Benchmark);
}; };
...@@ -425,7 +425,7 @@ class Benchmark { ...@@ -425,7 +425,7 @@ class Benchmark {
struct BenchmarkContextData { struct BenchmarkContextData {
int num_cpus; int num_cpus;
double mhz_per_cpu; double mhz_per_cpu;
//std::string cpu_info; // std::string cpu_info;
bool cpu_scaling_enabled; bool cpu_scaling_enabled;
// The number of chars in the longest benchmark name. // The number of chars in the longest benchmark name.
...@@ -433,8 +433,8 @@ struct BenchmarkContextData { ...@@ -433,8 +433,8 @@ struct BenchmarkContextData {
}; };
struct BenchmarkRunData { struct BenchmarkRunData {
BenchmarkRunData() : BenchmarkRunData()
thread_index(-1), : thread_index(-1),
iterations(1), iterations(1),
real_accumulated_time(0), real_accumulated_time(0),
cpu_accumulated_time(0), cpu_accumulated_time(0),
...@@ -481,15 +481,13 @@ class BenchmarkReporter { ...@@ -481,15 +481,13 @@ class BenchmarkReporter {
virtual ~BenchmarkReporter(); virtual ~BenchmarkReporter();
}; };
// ------------------------------------------------------ // ------------------------------------------------------
// Internal implementation details follow; please ignore // Internal implementation details follow; please ignore
// Given a collection of reports, computes their mean and stddev. // Given a collection of reports, computes their mean and stddev.
// REQUIRES: all runs in "reports" must be from the same benchmark. // REQUIRES: all runs in "reports" must be from the same benchmark.
void ComputeStats(const std::vector<BenchmarkRunData>& reports, void ComputeStats(const std::vector<BenchmarkRunData>& reports,
BenchmarkRunData* mean_data, BenchmarkRunData* mean_data, BenchmarkRunData* stddev_data);
BenchmarkRunData* stddev_data);
// Simple reporter that outputs benchmark data to the console. This is the // Simple reporter that outputs benchmark data to the console. This is the
// default reporter used by RunSpecifiedBenchmarks(). // default reporter used by RunSpecifiedBenchmarks().
...@@ -497,6 +495,7 @@ class ConsoleReporter : public BenchmarkReporter { ...@@ -497,6 +495,7 @@ class ConsoleReporter : public BenchmarkReporter {
public: public:
virtual bool ReportContext(const BenchmarkContextData& context); virtual bool ReportContext(const BenchmarkContextData& context);
virtual void ReportRuns(const std::vector<BenchmarkRunData>& reports); virtual void ReportRuns(const std::vector<BenchmarkRunData>& reports);
private: private:
std::string PrintMemoryUsage(double bytes); std::string PrintMemoryUsage(double bytes);
virtual void PrintRunData(const BenchmarkRunData& report); virtual void PrintRunData(const BenchmarkRunData& report);
...@@ -513,11 +512,11 @@ void Initialize(int* argc, const char** argv); ...@@ -513,11 +512,11 @@ void Initialize(int* argc, const char** argv);
// Helpers for generating unique variable names // Helpers for generating unique variable names
#define BENCHMARK_CONCAT(a, b, c) BENCHMARK_CONCAT2(a, b, c) #define BENCHMARK_CONCAT(a, b, c) BENCHMARK_CONCAT2(a, b, c)
#define BENCHMARK_CONCAT2(a, b, c) a ## b ## c #define BENCHMARK_CONCAT2(a, b, c) a##b##c
#define BENCHMARK(n) \ #define BENCHMARK(n) \
static ::benchmark::internal::Benchmark* \ static ::benchmark::internal::Benchmark* BENCHMARK_CONCAT( \
BENCHMARK_CONCAT(__benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \ __benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \
(new ::benchmark::internal::Benchmark(#n, n)) (new ::benchmark::internal::Benchmark(#n, n))
// Old-style macros // Old-style macros
...@@ -536,13 +535,13 @@ void Initialize(int* argc, const char** argv); ...@@ -536,13 +535,13 @@ void Initialize(int* argc, const char** argv);
// //
// will register BM_Foo<1> as a benchmark. // will register BM_Foo<1> as a benchmark.
#define BENCHMARK_TEMPLATE(n, a) \ #define BENCHMARK_TEMPLATE(n, a) \
static ::benchmark::internal::Benchmark* \ static ::benchmark::internal::Benchmark* BENCHMARK_CONCAT( \
BENCHMARK_CONCAT(__benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \ __benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \
(new ::benchmark::internal::Benchmark(#n "<" #a ">", n<a>)) (new ::benchmark::internal::Benchmark(#n "<" #a ">", n<a>))
#define BENCHMARK_TEMPLATE2(n, a, b) \ #define BENCHMARK_TEMPLATE2(n, a, b) \
static ::benchmark::internal::Benchmark* \ static ::benchmark::internal::Benchmark* BENCHMARK_CONCAT( \
BENCHMARK_CONCAT(__benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \ __benchmark_, n, __LINE__) ATTRIBUTE_UNUSED = \
(new ::benchmark::internal::Benchmark(#n "<" #a "," #b ">", n<a, b>)) (new ::benchmark::internal::Benchmark(#n "<" #a "," #b ">", n<a, b>))
#endif // BENCHMARK_BENCHMARK_H_ #endif // BENCHMARK_BENCHMARK_H_
......
...@@ -34,7 +34,10 @@ char (&ArraySizeHelper(const T (&array)[N]))[N]; ...@@ -34,7 +34,10 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];
#define arraysize(array) (sizeof(ArraySizeHelper(array))) #define arraysize(array) (sizeof(ArraySizeHelper(array)))
#define CHECK(b) do { if (!(b)) assert(false); } while(0) #define CHECK(b) \
do { \
if (!(b)) assert(false); \
} while (0)
#define CHECK_EQ(a, b) CHECK((a) == (b)) #define CHECK_EQ(a, b) CHECK((a) == (b))
#define CHECK_NE(a, b) CHECK((a) != (b)) #define CHECK_NE(a, b) CHECK((a) != (b))
#define CHECK_GE(a, b) CHECK((a) >= (b)) #define CHECK_GE(a, b) CHECK((a) >= (b))
...@@ -45,14 +48,14 @@ char (&ArraySizeHelper(const T (&array)[N]))[N]; ...@@ -45,14 +48,14 @@ char (&ArraySizeHelper(const T (&array)[N]))[N];
// //
// Prevent the compiler from complaining about or optimizing away variables // Prevent the compiler from complaining about or optimizing away variables
// that appear unused. // that appear unused.
#define ATTRIBUTE_UNUSED __attribute__ ((unused)) #define ATTRIBUTE_UNUSED __attribute__((unused))
// //
// For functions we want to force inline or not inline. // For functions we want to force inline or not inline.
// Introduced in gcc 3.1. // Introduced in gcc 3.1.
#define ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline)) #define ATTRIBUTE_ALWAYS_INLINE __attribute__((always_inline))
#define HAVE_ATTRIBUTE_ALWAYS_INLINE 1 #define HAVE_ATTRIBUTE_ALWAYS_INLINE 1
#define ATTRIBUTE_NOINLINE __attribute__ ((noinline)) #define ATTRIBUTE_NOINLINE __attribute__((noinline))
#define HAVE_ATTRIBUTE_NOINLINE 1 #define HAVE_ATTRIBUTE_NOINLINE 1
#endif // BENCHMARK_MACROS_H_ #endif // BENCHMARK_MACROS_H_
...@@ -17,25 +17,40 @@ typedef const char* PlatformColorCode; ...@@ -17,25 +17,40 @@ typedef const char* PlatformColorCode;
PlatformColorCode GetPlatformColorCode(LogColor color) { PlatformColorCode GetPlatformColorCode(LogColor color) {
#ifdef OS_WINDOWS #ifdef OS_WINDOWS
switch (color) { switch (color) {
case COLOR_RED: return FOREGROUND_RED; case COLOR_RED:
case COLOR_GREEN: return FOREGROUND_GREEN; return FOREGROUND_RED;
case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN; case COLOR_GREEN:
case COLOR_BLUE: return FOREGROUND_BLUE; return FOREGROUND_GREEN;
case COLOR_MAGENTA: return FOREGROUND_BLUE | FOREGROUND_RED; case COLOR_YELLOW:
case COLOR_CYAN: return FOREGROUND_BLUE | FOREGROUND_GREEN; return FOREGROUND_RED | FOREGROUND_GREEN;
case COLOR_BLUE:
return FOREGROUND_BLUE;
case COLOR_MAGENTA:
return FOREGROUND_BLUE | FOREGROUND_RED;
case COLOR_CYAN:
return FOREGROUND_BLUE | FOREGROUND_GREEN;
case COLOR_WHITE: // fall through to default case COLOR_WHITE: // fall through to default
default: return 0; default:
return 0;
} }
#else #else
switch (color) { switch (color) {
case COLOR_RED: return "1"; case COLOR_RED:
case COLOR_GREEN: return "2"; return "1";
case COLOR_YELLOW: return "3"; case COLOR_GREEN:
case COLOR_BLUE: return "4"; return "2";
case COLOR_MAGENTA: return "5"; case COLOR_YELLOW:
case COLOR_CYAN: return "6"; return "3";
case COLOR_WHITE: return "7"; case COLOR_BLUE:
default: return NULL; return "4";
case COLOR_MAGENTA:
return "5";
case COLOR_CYAN:
return "6";
case COLOR_WHITE:
return "7";
default:
return NULL;
}; };
#endif #endif
} }
...@@ -72,8 +87,7 @@ void ColorPrintf(LogColor color, const char* fmt, ...) { ...@@ -72,8 +87,7 @@ void ColorPrintf(LogColor color, const char* fmt, ...) {
SetConsoleTextAttribute(stdout_handle, old_color_attrs); SetConsoleTextAttribute(stdout_handle, old_color_attrs);
#else #else
const char* color_code = GetPlatformColorCode(color); const char* color_code = GetPlatformColorCode(color);
if (color_code) if (color_code) fprintf(stdout, "\033[0;3%sm", color_code);
fprintf(stdout, "\033[0;3%sm", color_code);
vprintf(fmt, args); vprintf(fmt, args);
printf("\033[m"); // Resets the terminal to default. printf("\033[m"); // Resets the terminal to default.
#endif #endif
......
...@@ -60,7 +60,6 @@ bool ParseDouble(const std::string& src_text, const char* str, double* value) { ...@@ -60,7 +60,6 @@ bool ParseDouble(const std::string& src_text, const char* str, double* value) {
return true; return true;
} }
inline const char* GetEnv(const char* name) { inline const char* GetEnv(const char* name) {
#if GTEST_OS_WINDOWS_MOBILE #if GTEST_OS_WINDOWS_MOBILE
// We are on Windows CE, which has no environment variables. // We are on Windows CE, which has no environment variables.
...@@ -95,8 +94,7 @@ static std::string FlagToEnvVar(const char* flag) { ...@@ -95,8 +94,7 @@ static std::string FlagToEnvVar(const char* flag) {
bool BoolFromEnv(const char* flag, bool default_value) { bool BoolFromEnv(const char* flag, bool default_value) {
const std::string env_var = FlagToEnvVar(flag); const std::string env_var = FlagToEnvVar(flag);
const char* const string_value = GetEnv(env_var.c_str()); const char* const string_value = GetEnv(env_var.c_str());
return string_value == NULL ? return string_value == NULL ? default_value : strcmp(string_value, "0") != 0;
default_value : strcmp(string_value, "0") != 0;
} }
// Reads and returns a 32-bit integer stored in the environment // Reads and returns a 32-bit integer stored in the environment
...@@ -111,8 +109,8 @@ int32_t Int32FromEnv(const char* flag, int32_t default_value) { ...@@ -111,8 +109,8 @@ int32_t Int32FromEnv(const char* flag, int32_t default_value) {
} }
int32_t result = default_value; int32_t result = default_value;
if (!ParseInt32(std::string("Environment variable ") + env_var, if (!ParseInt32(std::string("Environment variable ") + env_var, string_value,
string_value, &result)) { &result)) {
std::cout << "The default value " << default_value << " is used.\n"; std::cout << "The default value " << default_value << " is used.\n";
return default_value; return default_value;
} }
...@@ -133,8 +131,7 @@ const char* StringFromEnv(const char* flag, const char* default_value) { ...@@ -133,8 +131,7 @@ const char* StringFromEnv(const char* flag, const char* default_value) {
// part can be omitted. // part can be omitted.
// //
// Returns the value of the flag, or NULL if the parsing failed. // Returns the value of the flag, or NULL if the parsing failed.
const char* ParseFlagValue(const char* str, const char* ParseFlagValue(const char* str, const char* flag,
const char* flag,
bool def_optional) { bool def_optional) {
// str and flag must not be NULL. // str and flag must not be NULL.
if (str == NULL || flag == NULL) return NULL; if (str == NULL || flag == NULL) return NULL;
...@@ -148,8 +145,7 @@ const char* ParseFlagValue(const char* str, ...@@ -148,8 +145,7 @@ const char* ParseFlagValue(const char* str,
const char* flag_end = str + flag_len; const char* flag_end = str + flag_len;
// When def_optional is true, it's OK to not have a "=value" part. // When def_optional is true, it's OK to not have a "=value" part.
if (def_optional && (flag_end[0] == '\0')) if (def_optional && (flag_end[0] == '\0')) return flag_end;
return flag_end;
// If def_optional is true and there are more characters after the // If def_optional is true and there are more characters after the
// flag name, or if def_optional is false, there must be a '=' after // flag name, or if def_optional is false, there must be a '=' after
...@@ -180,8 +176,8 @@ bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) { ...@@ -180,8 +176,8 @@ bool ParseInt32Flag(const char* str, const char* flag, int32_t* value) {
if (value_str == NULL) return false; if (value_str == NULL) return false;
// Sets *value to the value of the flag. // Sets *value to the value of the flag.
return ParseInt32(std::string("The value of flag --") + flag, return ParseInt32(std::string("The value of flag --") + flag, value_str,
value_str, value); value);
} }
bool ParseDoubleFlag(const char* str, const char* flag, double* value) { bool ParseDoubleFlag(const char* str, const char* flag, double* value) {
...@@ -192,8 +188,8 @@ bool ParseDoubleFlag(const char* str, const char* flag, double* value) { ...@@ -192,8 +188,8 @@ bool ParseDoubleFlag(const char* str, const char* flag, double* value) {
if (value_str == NULL) return false; if (value_str == NULL) return false;
// Sets *value to the value of the flag. // Sets *value to the value of the flag.
return ParseDouble(std::string("The value of flag --") + flag, return ParseDouble(std::string("The value of flag --") + flag, value_str,
value_str, value); value);
} }
bool ParseStringFlag(const char* str, const char* flag, std::string* value) { bool ParseStringFlag(const char* str, const char* flag, std::string* value) {
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include <stdint.h> #include <stdint.h>
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
# include <mach/mach_time.h> #include <mach/mach_time.h>
#endif #endif
// For MSVC, we want to use '_asm rdtsc' when possible (since it works // For MSVC, we want to use '_asm rdtsc' when possible (since it works
// with even ancient MSVC compilers), and when not possible the // with even ancient MSVC compilers), and when not possible the
...@@ -48,8 +48,8 @@ namespace benchmark { ...@@ -48,8 +48,8 @@ namespace benchmark {
// with modifications by m3b. See also // with modifications by m3b. See also
// https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h // https://setisvn.ssl.berkeley.edu/svn/lib/fftw-3.0.1/kernel/cycle.h
namespace cycleclock { namespace cycleclock {
// This should return the number of cycles since power-on. Thread-safe. // This should return the number of cycles since power-on. Thread-safe.
inline ATTRIBUTE_ALWAYS_INLINE int64_t Now() { inline ATTRIBUTE_ALWAYS_INLINE int64_t Now() {
#if defined(OS_MACOSX) #if defined(OS_MACOSX)
// this goes at the top because we need ALL Macs, regardless of // this goes at the top because we need ALL Macs, regardless of
// architecture, to return the number of "mach time units" that // architecture, to return the number of "mach time units" that
...@@ -63,29 +63,29 @@ namespace cycleclock { ...@@ -63,29 +63,29 @@ namespace cycleclock {
return mach_absolute_time(); return mach_absolute_time();
#elif defined(__i386__) #elif defined(__i386__)
int64_t ret; int64_t ret;
__asm__ volatile ("rdtsc" : "=A" (ret) ); __asm__ volatile("rdtsc" : "=A"(ret));
return ret; return ret;
#elif defined(__x86_64__) || defined(__amd64__) #elif defined(__x86_64__) || defined(__amd64__)
uint64_t low, high; uint64_t low, high;
__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high)); __asm__ volatile("rdtsc" : "=a"(low), "=d"(high));
return (high << 32) | low; return (high << 32) | low;
#elif defined(__powerpc__) || defined(__ppc__) #elif defined(__powerpc__) || defined(__ppc__)
// This returns a time-base, which is not always precisely a cycle-count. // This returns a time-base, which is not always precisely a cycle-count.
int64_t tbl, tbu0, tbu1; int64_t tbl, tbu0, tbu1;
asm("mftbu %0" : "=r" (tbu0)); asm("mftbu %0" : "=r"(tbu0));
asm("mftb %0" : "=r" (tbl)); asm("mftb %0" : "=r"(tbl));
asm("mftbu %0" : "=r" (tbu1)); asm("mftbu %0" : "=r"(tbu1));
tbl &= -static_cast<int64>(tbu0 == tbu1); tbl &= -static_cast<int64>(tbu0 == tbu1);
// high 32 bits in tbu1; low 32 bits in tbl (tbu0 is garbage) // high 32 bits in tbu1; low 32 bits in tbl (tbu0 is garbage)
return (tbu1 << 32) | tbl; return (tbu1 << 32) | tbl;
#elif defined(__sparc__) #elif defined(__sparc__)
int64_t tick; int64_t tick;
asm(".byte 0x83, 0x41, 0x00, 0x00"); asm(".byte 0x83, 0x41, 0x00, 0x00");
asm("mov %%g1, %0" : "=r" (tick)); asm("mov %%g1, %0" : "=r"(tick));
return tick; return tick;
#elif defined(__ia64__) #elif defined(__ia64__)
int64_t itc; int64_t itc;
asm("mov %0 = ar.itc" : "=r" (itc)); asm("mov %0 = ar.itc" : "=r"(itc));
return itc; return itc;
#elif defined(COMPILER_MSVC) && defined(_M_IX86) #elif defined(COMPILER_MSVC) && defined(_M_IX86)
// Older MSVC compilers (like 7.x) don't seem to support the // Older MSVC compilers (like 7.x) don't seem to support the
...@@ -101,11 +101,11 @@ namespace cycleclock { ...@@ -101,11 +101,11 @@ namespace cycleclock {
uint32_t pmuseren; uint32_t pmuseren;
uint32_t pmcntenset; uint32_t pmcntenset;
// Read the user mode perf monitor counter access permissions. // Read the user mode perf monitor counter access permissions.
asm("mrc p15, 0, %0, c9, c14, 0" : "=r" (pmuseren)); asm("mrc p15, 0, %0, c9, c14, 0" : "=r"(pmuseren));
if (pmuseren & 1) { // Allows reading perfmon counters for user mode code. if (pmuseren & 1) { // Allows reading perfmon counters for user mode code.
asm("mrc p15, 0, %0, c9, c12, 1" : "=r" (pmcntenset)); asm("mrc p15, 0, %0, c9, c12, 1" : "=r"(pmcntenset));
if (pmcntenset & 0x80000000ul) { // Is it counting? if (pmcntenset & 0x80000000ul) { // Is it counting?
asm("mrc p15, 0, %0, c9, c13, 0" : "=r" (pmccntr)); asm("mrc p15, 0, %0, c9, c13, 0" : "=r"(pmccntr));
// The counter is set up to count every 64th cycle // The counter is set up to count every 64th cycle
return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6 return static_cast<int64>(pmccntr) * 64; // Should optimize to << 6
} }
...@@ -126,7 +126,7 @@ namespace cycleclock { ...@@ -126,7 +126,7 @@ namespace cycleclock {
// a fast implementation and use generic version if nothing better is available. // a fast implementation and use generic version if nothing better is available.
#error You need to define CycleTimer for your OS and CPU #error You need to define CycleTimer for your OS and CPU
#endif #endif
} }
} // end namespace cycleclock } // end namespace cycleclock
} // end namespace benchmark } // end namespace benchmark
......
...@@ -10,9 +10,7 @@ class mutex_lock { ...@@ -10,9 +10,7 @@ class mutex_lock {
pthread_mutex_lock(mu_); pthread_mutex_lock(mu_);
} }
~mutex_lock() { ~mutex_lock() { pthread_mutex_unlock(mu_); }
pthread_mutex_unlock(mu_);
}
private: private:
pthread_mutex_t* mu_; pthread_mutex_t* mu_;
......
...@@ -6,9 +6,7 @@ ...@@ -6,9 +6,7 @@
namespace benchmark { namespace benchmark {
#ifdef OS_WINDOWS #ifdef OS_WINDOWS
// Window's _sleep takes milliseconds argument. // Window's _sleep takes milliseconds argument.
void SleepForMilliseconds(int milliseconds) { void SleepForMilliseconds(int milliseconds) { _sleep(milliseconds); }
_sleep(milliseconds);
}
void SleepForSeconds(double seconds) { void SleepForSeconds(double seconds) {
SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds)); SleepForMilliseconds(static_cast<int>(kNumMillisPerSecond * seconds));
} }
......
...@@ -39,7 +39,7 @@ int64_t EstimateCyclesPerSecond(const int estimate_time_ms) { ...@@ -39,7 +39,7 @@ int64_t EstimateCyclesPerSecond(const int estimate_time_ms) {
// Helper function for reading an int from a file. Returns true if successful // Helper function for reading an int from a file. Returns true if successful
// and the memory location pointed to by value is set to the value read. // and the memory location pointed to by value is set to the value read.
bool ReadIntFromFile(const char *file, int *value) { bool ReadIntFromFile(const char* file, int* value) {
bool ret = false; bool ret = false;
int fd = open(file, O_RDONLY); int fd = open(file, O_RDONLY);
if (fd != -1) { if (fd != -1) {
...@@ -116,42 +116,41 @@ void InitializeSystemInfo() { ...@@ -116,42 +116,41 @@ void InitializeSystemInfo() {
if (sizeof(line) == oldlinelen + 1) // oldlinelen took up entire line if (sizeof(line) == oldlinelen + 1) // oldlinelen took up entire line
line[0] = '\0'; line[0] = '\0';
else // still other lines left to save else // still other lines left to save
memmove(line, line + oldlinelen+1, sizeof(line) - (oldlinelen+1)); memmove(line, line + oldlinelen + 1, sizeof(line) - (oldlinelen + 1));
// Terminate the new line, reading more if we can't find the newline // Terminate the new line, reading more if we can't find the newline
char* newline = strchr(line, '\n'); char* newline = strchr(line, '\n');
if (newline == NULL) { if (newline == NULL) {
const int linelen = strlen(line); const int linelen = strlen(line);
const int bytes_to_read = sizeof(line)-1 - linelen; const int bytes_to_read = sizeof(line) - 1 - linelen;
CHECK(bytes_to_read > 0); // because the memmove recovered >=1 bytes CHECK(bytes_to_read > 0); // because the memmove recovered >=1 bytes
chars_read = read(fd, line + linelen, bytes_to_read); chars_read = read(fd, line + linelen, bytes_to_read);
line[linelen + chars_read] = '\0'; line[linelen + chars_read] = '\0';
newline = strchr(line, '\n'); newline = strchr(line, '\n');
} }
if (newline != NULL) if (newline != NULL) *newline = '\0';
*newline = '\0';
// When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only // When parsing the "cpu MHz" and "bogomips" (fallback) entries, we only
// accept postive values. Some environments (virtual machines) report zero, // accept postive values. Some environments (virtual machines) report zero,
// which would cause infinite looping in WallTime_Init. // which would cause infinite looping in WallTime_Init.
if (!saw_mhz && strncasecmp(line, "cpu MHz", sizeof("cpu MHz")-1) == 0) { if (!saw_mhz && strncasecmp(line, "cpu MHz", sizeof("cpu MHz") - 1) == 0) {
const char* freqstr = strchr(line, ':'); const char* freqstr = strchr(line, ':');
if (freqstr) { if (freqstr) {
cpuinfo_cycles_per_second = strtod(freqstr+1, &err) * 1000000.0; cpuinfo_cycles_per_second = strtod(freqstr + 1, &err) * 1000000.0;
if (freqstr[1] != '\0' && *err == '\0' && cpuinfo_cycles_per_second > 0) if (freqstr[1] != '\0' && *err == '\0' && cpuinfo_cycles_per_second > 0)
saw_mhz = true; saw_mhz = true;
} }
} else if (strncasecmp(line, "bogomips", sizeof("bogomips")-1) == 0) { } else if (strncasecmp(line, "bogomips", sizeof("bogomips") - 1) == 0) {
const char* freqstr = strchr(line, ':'); const char* freqstr = strchr(line, ':');
if (freqstr) { if (freqstr) {
bogo_clock = strtod(freqstr+1, &err) * 1000000.0; bogo_clock = strtod(freqstr + 1, &err) * 1000000.0;
if (freqstr[1] != '\0' && *err == '\0' && bogo_clock > 0) if (freqstr[1] != '\0' && *err == '\0' && bogo_clock > 0)
saw_bogo = true; saw_bogo = true;
} }
} else if (strncasecmp(line, "processor", sizeof("processor")-1) == 0) { } else if (strncasecmp(line, "processor", sizeof("processor") - 1) == 0) {
num_cpus++; // count up every time we see an "processor :" entry num_cpus++; // count up every time we see an "processor :" entry
const char* freqstr = strchr(line, ':'); const char* freqstr = strchr(line, ':');
if (freqstr) { if (freqstr) {
const int cpu_id = strtol(freqstr+1, &err, 10); const int cpu_id = strtol(freqstr + 1, &err, 10);
if (freqstr[1] != '\0' && *err == '\0' && max_cpu_id < cpu_id) if (freqstr[1] != '\0' && *err == '\0' && max_cpu_id < cpu_id)
max_cpu_id = cpu_id; max_cpu_id = cpu_id;
} }
...@@ -181,17 +180,17 @@ void InitializeSystemInfo() { ...@@ -181,17 +180,17 @@ void InitializeSystemInfo() {
} }
#elif defined OS_FREEBSD #elif defined OS_FREEBSD
// For this sysctl to work, the machine must be configured without // For this sysctl to work, the machine must be configured without
// SMP, APIC, or APM support. hz should be 64-bit in freebsd 7.0 // SMP, APIC, or APM support. hz should be 64-bit in freebsd 7.0
// and later. Before that, it's a 32-bit quantity (and gives the // and later. Before that, it's a 32-bit quantity (and gives the
// wrong answer on machines faster than 2^32 Hz). See // wrong answer on machines faster than 2^32 Hz). See
// http://lists.freebsd.org/pipermail/freebsd-i386/2004-November/001846.html // http://lists.freebsd.org/pipermail/freebsd-i386/2004-November/001846.html
// But also compare FreeBSD 7.0: // But also compare FreeBSD 7.0:
// http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG70#L223 // http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG70#L223
// 231 error = sysctl_handle_quad(oidp, &freq, 0, req); // 231 error = sysctl_handle_quad(oidp, &freq, 0, req);
// To FreeBSD 6.3 (it's the same in 6-STABLE): // To FreeBSD 6.3 (it's the same in 6-STABLE):
// http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG6#L131 // http://fxr.watson.org/fxr/source/i386/i386/tsc.c?v=RELENG6#L131
// 139 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req); // 139 error = sysctl_handle_int(oidp, &freq, sizeof(freq), req);
#if __FreeBSD__ >= 7 #if __FreeBSD__ >= 7
uint64_t hz = 0; uint64_t hz = 0;
#else #else
...@@ -199,25 +198,25 @@ void InitializeSystemInfo() { ...@@ -199,25 +198,25 @@ void InitializeSystemInfo() {
#endif #endif
size_t sz = sizeof(hz); size_t sz = sizeof(hz);
const char *sysctl_path = "machdep.tsc_freq"; const char *sysctl_path = "machdep.tsc_freq";
if ( sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0 ) { if (sysctlbyname(sysctl_path, &hz, &sz, NULL, 0) != 0) {
fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n", fprintf(stderr, "Unable to determine clock rate from sysctl: %s: %s\n",
sysctl_path, strerror(errno)); sysctl_path, strerror(errno));
cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000); cpuinfo_cycles_per_second = EstimateCyclesPerSecond(1000);
} else { } else {
cpuinfo_cycles_per_second = hz; cpuinfo_cycles_per_second = hz;
} }
// TODO: also figure out cpuinfo_num_cpus // TODO: also figure out cpuinfo_num_cpus
#elif defined OS_WINDOWS #elif defined OS_WINDOWS
# pragma comment(lib, "shlwapi.lib") // for SHGetValue() #pragma comment(lib, "shlwapi.lib") // for SHGetValue()
// In NT, read MHz from the registry. If we fail to do so or we're in win9x // In NT, read MHz from the registry. If we fail to do so or we're in win9x
// then make a crude estimate. // then make a crude estimate.
OSVERSIONINFO os; OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(os); os.dwOSVersionInfoSize = sizeof(os);
DWORD data, data_size = sizeof(data); DWORD data, data_size = sizeof(data);
if (GetVersionEx(&os) && if (GetVersionEx(&os) && os.dwPlatformId == VER_PLATFORM_WIN32_NT &&
os.dwPlatformId == VER_PLATFORM_WIN32_NT && SUCCEEDED(
SUCCEEDED(SHGetValueA(HKEY_LOCAL_MACHINE, SHGetValueA(HKEY_LOCAL_MACHINE,
"HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
"~MHz", NULL, &data, &data_size))) "~MHz", NULL, &data, &data_size)))
cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz cpuinfo_cycles_per_second = (int64)data * (int64)(1000 * 1000); // was mhz
...@@ -243,10 +242,10 @@ void InitializeSystemInfo() { ...@@ -243,10 +242,10 @@ void InitializeSystemInfo() {
int num_cpus = 0; int num_cpus = 0;
size_t size = sizeof(num_cpus); size_t size = sizeof(num_cpus);
int numcpus_name[] = { CTL_HW, HW_NCPU }; int numcpus_name[] = {CTL_HW, HW_NCPU};
if (::sysctl(numcpus_name, arraysize(numcpus_name), &num_cpus, &size, 0, 0) if (::sysctl(numcpus_name, arraysize(numcpus_name), &num_cpus, &size, 0, 0) ==
== 0 0 &&
&& (size == sizeof(num_cpus))) (size == sizeof(num_cpus)))
cpuinfo_num_cpus = num_cpus; cpuinfo_num_cpus = num_cpus;
#else #else
...@@ -262,15 +261,15 @@ static double MyCPUUsageRUsage() { ...@@ -262,15 +261,15 @@ static double MyCPUUsageRUsage() {
struct rusage ru; struct rusage ru;
if (getrusage(RUSAGE_SELF, &ru) == 0) { if (getrusage(RUSAGE_SELF, &ru) == 0) {
return (static_cast<double>(ru.ru_utime.tv_sec) + return (static_cast<double>(ru.ru_utime.tv_sec) +
static_cast<double>(ru.ru_utime.tv_usec)*1e-6 + static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 +
static_cast<double>(ru.ru_stime.tv_sec) + static_cast<double>(ru.ru_stime.tv_sec) +
static_cast<double>(ru.ru_stime.tv_usec)*1e-6); static_cast<double>(ru.ru_stime.tv_usec) * 1e-6);
} else { } else {
return 0.0; return 0.0;
} }
} }
static bool MyCPUUsageCPUTimeNsLocked(double *cputime) { static bool MyCPUUsageCPUTimeNsLocked(double* cputime) {
static int cputime_fd = -1; static int cputime_fd = -1;
if (cputime_fd == -1) { if (cputime_fd == -1) {
cputime_fd = open("/proc/self/cputime_ns", O_RDONLY); cputime_fd = open("/proc/self/cputime_ns", O_RDONLY);
...@@ -281,7 +280,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double *cputime) { ...@@ -281,7 +280,7 @@ static bool MyCPUUsageCPUTimeNsLocked(double *cputime) {
} }
char buff[64]; char buff[64];
memset(buff, 0, sizeof(buff)); memset(buff, 0, sizeof(buff));
if (pread(cputime_fd, buff, sizeof(buff)-1, 0) <= 0) { if (pread(cputime_fd, buff, sizeof(buff) - 1, 0) <= 0) {
close(cputime_fd); close(cputime_fd);
cputime_fd = -1; cputime_fd = -1;
return false; return false;
...@@ -317,9 +316,9 @@ double ChildrenCPUUsage() { ...@@ -317,9 +316,9 @@ double ChildrenCPUUsage() {
struct rusage ru; struct rusage ru;
if (getrusage(RUSAGE_CHILDREN, &ru) == 0) { if (getrusage(RUSAGE_CHILDREN, &ru) == 0) {
return (static_cast<double>(ru.ru_utime.tv_sec) + return (static_cast<double>(ru.ru_utime.tv_sec) +
static_cast<double>(ru.ru_utime.tv_usec)*1e-6 + static_cast<double>(ru.ru_utime.tv_usec) * 1e-6 +
static_cast<double>(ru.ru_stime.tv_sec) + static_cast<double>(ru.ru_stime.tv_sec) +
static_cast<double>(ru.ru_stime.tv_usec)*1e-6); static_cast<double>(ru.ru_stime.tv_usec) * 1e-6);
} else { } else {
return 0.0; return 0.0;
} }
......
...@@ -75,8 +75,8 @@ void Initialize() { ...@@ -75,8 +75,8 @@ void Initialize() {
cycles_per_second = static_cast<int64_t>(CyclesPerSecond()); cycles_per_second = static_cast<int64_t>(CyclesPerSecond());
CHECK(cycles_per_second != 0); CHECK(cycles_per_second != 0);
seconds_per_cycle = 1.0 / cycles_per_second; seconds_per_cycle = 1.0 / cycles_per_second;
max_interval_cycles = static_cast<int64_t>( max_interval_cycles =
cycles_per_second * kMaxErrorInterval); static_cast<int64_t>(cycles_per_second * kMaxErrorInterval);
do { do {
base_cycletime = cycleclock::Now(); base_cycletime = cycleclock::Now();
base_walltime = Slow(); base_walltime = Slow();
...@@ -90,8 +90,7 @@ void Initialize() { ...@@ -90,8 +90,7 @@ void Initialize() {
} }
WallTime Now() { WallTime Now() {
if (!std::atomic_load(&initialized)) if (!std::atomic_load(&initialized)) return Slow();
return Slow();
WallTime now = 0.0; WallTime now = 0.0;
WallTime result = 0.0; WallTime result = 0.0;
...@@ -119,8 +118,8 @@ WallTime Now() { ...@@ -119,8 +118,8 @@ WallTime Now() {
return now; return now;
} }
std::string Print(WallTime time, const char *format, bool local, std::string Print(WallTime time, const char* format, bool local,
int *remainder_us) { int* remainder_us) {
char storage[32]; char storage[32];
struct tm split; struct tm split;
double subsecond; double subsecond;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment