Only try to use the membarrier on Linux
[folly.git] / folly / stats / Histogram-defs.h
index 7253d5c9a0b52735c601befafc315d9bcc4ebf9d..8d4af44fd48062b689dab41fa54c14ec3b4eb38d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2013 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * limitations under the License.
  */
 
-#ifndef FOLLY_HISTOGRAM_DEFS_H_
-#define FOLLY_HISTOGRAM_DEFS_H_
+#pragma once
 
-#include "folly/Conv.h"
+#include <folly/Conv.h>
 
 #include <glog/logging.h>
 
@@ -36,7 +35,9 @@ HistogramBuckets<T, BucketT>::HistogramBuckets(ValueType bucketSize,
   CHECK_GT(bucketSize_, ValueType(0));
   CHECK_LT(min_, max_);
 
-  unsigned int numBuckets = (max - min) / bucketSize;
+  // Deliberately make this a signed type, because we're about
+  // to compare it against max-min, which is nominally signed, too.
+  int numBuckets = (max - min) / bucketSize;
   // Round up if the bucket size does not fit evenly
   if (numBuckets * bucketSize < max - min) {
     ++numBuckets;
@@ -59,6 +60,17 @@ unsigned int HistogramBuckets<T, BucketType>::getBucketIdx(
   }
 }
 
+template <typename T, typename BucketType>
+template <typename CountFn>
+uint64_t HistogramBuckets<T, BucketType>::computeTotalCount(
+    CountFn countFromBucket) const {
+  uint64_t count = 0;
+  for (unsigned int n = 0; n < buckets_.size(); ++n) {
+    count += countFromBucket(const_cast<const BucketType&>(buckets_[n]));
+  }
+  return count;
+}
+
 template <typename T, typename BucketType>
 template <typename CountFn>
 unsigned int HistogramBuckets<T, BucketType>::getPercentileBucketIdx(
@@ -265,5 +277,3 @@ void Histogram<T>::toTSV(std::ostream& out, bool skipEmptyBuckets) const {
 }
 
 } // folly
-
-#endif // FOLLY_HISTOGRAM_DEFS_H_