summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/util
diff options
context:
space:
mode:
authormunjal@chromium.org <munjal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-08 18:20:37 +0000
committermunjal@chromium.org <munjal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-08 18:20:37 +0000
commit01f42660659e45edd7c0a4ad8ecd7a6bb00e370e (patch)
tree3f41d81dce4744debf35d70898d24cbfa19dbbca /chrome/browser/sync/util
parentc9d9b528c1b181f9b4bcf82947b10327b4759b2d (diff)
downloadchromium_src-01f42660659e45edd7c0a4ad8ecd7a6bb00e370e.zip
chromium_src-01f42660659e45edd7c0a4ad8ecd7a6bb00e370e.tar.gz
chromium_src-01f42660659e45edd7c0a4ad8ecd7a6bb00e370e.tar.bz2
Part 1 of making sync work on Mac:
- Add high res timer class for mac (we might eventually replace all timer classes with Chrome ones) - Define POSIX for OS==mac where needed - Add new liens at the end of some files since otherwise they produce errors on Mac - Rearrange initializer list of constructors to match declaration order in header; these become warning as error. - Some chrome.gyp magic At this point the code compiles but doesn't link due to some missing code on Mac like SSL stuff. But I thought this was a good check point. Note that chrome.gyp magic might change in future. BUG=none TEST=none Review URL: http://codereview.chromium.org/255053 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@28414 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/sync/util')
-rw-r--r--chrome/browser/sync/util/highres_timer.h2
-rwxr-xr-xchrome/browser/sync/util/highres_timer_mac.cc53
-rw-r--r--chrome/browser/sync/util/highres_timer_mac.h69
-rw-r--r--chrome/browser/sync/util/path_helpers_posix.cc4
4 files changed, 125 insertions, 3 deletions
diff --git a/chrome/browser/sync/util/highres_timer.h b/chrome/browser/sync/util/highres_timer.h
index 138da04..2c6513d 100644
--- a/chrome/browser/sync/util/highres_timer.h
+++ b/chrome/browser/sync/util/highres_timer.h
@@ -9,7 +9,7 @@
#if defined(OS_WIN)
#include "chrome/browser/sync/util/highres_timer_win.h"
#elif defined(OS_MACOSX)
-#error "Mac timer functions are missing."
+#include "chrome/browser/sync/util/highres_timer_mac.h"
#else
#include "chrome/browser/sync/util/highres_timer_linux.h"
#endif
diff --git a/chrome/browser/sync/util/highres_timer_mac.cc b/chrome/browser/sync/util/highres_timer_mac.cc
new file mode 100755
index 0000000..faad058
--- /dev/null
+++ b/chrome/browser/sync/util/highres_timer_mac.cc
@@ -0,0 +1,53 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// High resolution timer functions for use on Mac OS.
+
+#include "chrome/browser/sync/util/highres_timer.h"
+
+bool HighresTimer::perf_ratio_collected_ = false;
+mach_timebase_info_data_t HighresTimer::perf_ratio_ = {0};
+
+static const uint64 kNanosInMilli = 1000000L;
+static const uint64 kNanosInHalfMilli = 500000L;
+static const uint64 kNanosInSecond = 1000000000L;
+static const uint64 kNanosInHalfSecond = 500000000L;
+
+uint64 HighresTimer::GetElapsedMs() const {
+ uint64 end_time = GetCurrentTicks();
+
+ // Scale to ms and round to nearest ms - rounding is important
+ // because otherwise the truncation error may accumulate e.g. in sums.
+ //
+ GetTimerFrequency();
+ return ((end_time - start_ticks_) * perf_ratio_.numer +
+ kNanosInHalfMilli * perf_ratio_.denom) /
+ (kNanosInMilli * perf_ratio_.denom);
+}
+
+uint64 HighresTimer::GetElapsedSec() const {
+ uint64 end_time = GetCurrentTicks();
+
+ // Scale to ms and round to nearest ms - rounding is important
+ // because otherwise the truncation error may accumulate e.g. in sums.
+ //
+ GetTimerFrequency();
+ return ((end_time - start_ticks_) * perf_ratio_.numer +
+ kNanosInHalfSecond * perf_ratio_.denom) /
+ (kNanosInSecond * perf_ratio_.denom);
+}
+
+void HighresTimer::CollectPerfRatio() {
+ mach_timebase_info(&perf_ratio_);
+ perf_ratio_collected_ = true;
+}
+
+uint64 HighresTimer::GetTimerFrequency() {
+ if (!perf_ratio_collected_)
+ CollectPerfRatio();
+ // we're losing precision by doing the division here, but this value is only
+ // used to estimate tick time by the unit tests, so we're ok.
+ return static_cast<uint64>(
+ perf_ratio_.denom * 1000000000ULL / perf_ratio_.numer);
+}
diff --git a/chrome/browser/sync/util/highres_timer_mac.h b/chrome/browser/sync/util/highres_timer_mac.h
new file mode 100644
index 0000000..3a35622
--- /dev/null
+++ b/chrome/browser/sync/util/highres_timer_mac.h
@@ -0,0 +1,69 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// High resolution timer functions for use on Mac OS.
+
+#ifndef CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_MAC_H_
+#define CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_MAC_H_
+
+#include <mach/mach.h>
+#include <mach/mach_time.h>
+
+#include "base/basictypes.h"
+
+// A handy class for reliably measuring wall-clock time with decent resolution.
+class HighresTimer {
+ public:
+ // Captures the current start time
+ HighresTimer();
+
+ // Captures the current tick, can be used to reset a timer for reuse.
+ void Start();
+
+ // Returns the elapsed ticks with full resolution.
+ uint64 GetElapsedTicks() const;
+
+ // Returns the elapsed time in milliseconds, rounded to the nearest
+ // millisecond.
+ uint64 GetElapsedMs() const;
+
+ // Returns the elapsed time in seconds, rounded to the nearest second.
+ uint64 GetElapsedSec() const;
+
+ uint64 start_ticks() const { return start_ticks_; }
+
+ // Returns timer frequency from cache, should be less overhead than
+ // ::QueryPerformanceFrequency.
+ static uint64 GetTimerFrequency();
+ // Returns current ticks.
+ static uint64 GetCurrentTicks();
+
+ private:
+ static void CollectPerfRatio();
+
+ // Captured start time.
+ uint64 start_ticks_;
+
+ // Captured performance counter frequency.
+ static bool perf_ratio_collected_;
+ static mach_timebase_info_data_t perf_ratio_;
+};
+
+inline HighresTimer::HighresTimer() {
+ Start();
+}
+
+inline void HighresTimer::Start() {
+ start_ticks_ = GetCurrentTicks();
+}
+
+inline uint64 HighresTimer::GetCurrentTicks() {
+ return mach_absolute_time();
+}
+
+inline uint64 HighresTimer::GetElapsedTicks() const {
+ return start_ticks_ - GetCurrentTicks();
+}
+
+#endif // CHROME_BROWSER_SYNC_UTIL_HIGHRES_TIMER_MAC_H_
diff --git a/chrome/browser/sync/util/path_helpers_posix.cc b/chrome/browser/sync/util/path_helpers_posix.cc
index 5c68177..9da1adb 100644
--- a/chrome/browser/sync/util/path_helpers_posix.cc
+++ b/chrome/browser/sync/util/path_helpers_posix.cc
@@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <algorithm>
#include <pwd.h>
#include <string.h>
-#include "chrome/browser/sync/notifier/base/string.h"
#include "chrome/browser/sync/util/path_helpers.h"
#if ((!defined(OS_LINUX)) && (!defined(OS_MACOSX)))
@@ -91,6 +91,6 @@ PathString MakePathComponentOSLegal(const PathString& component) {
if (PathString::npos == component.find("/"))
return PSTR("");
PathString new_name(component);
- notifier::StringReplace(&new_name, "/", ":", true);
+ std::replace(new_name.begin(), new_name.end(), '/', ':');
return new_name;
}