CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
algorithm.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_ALGORITHM_H_
6 #define CVMFS_UTIL_ALGORITHM_H_
7 
8 #include <sys/time.h>
9 
10 #include <algorithm>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
15 #include "util/atomic.h"
16 #include "util/export.h"
17 #include "util/murmur.hxx"
18 #include "util/platform.h"
19 #include "util/prng.h"
20 #include "util/single_copy.h"
21 
22 #ifdef CVMFS_NAMESPACE_GUARD
23 namespace CVMFS_NAMESPACE_GUARD {
24 #endif
25 
26 
27 CVMFS_EXPORT double DiffTimeSeconds(struct timeval start, struct timeval end);
28 
29 // Bitfield manipulation for different integer types T
30 template <typename T>
31 inline void SetBit(unsigned int bit, T *field) {
32  *field |= static_cast<T>(1) << bit;
33 }
34 
35 template <typename T>
36 inline void ClearBit(unsigned int bit, T *field) {
37  *field &= ~(static_cast<T>(1) << bit);
38 }
39 
40 template <typename T>
41 inline bool TestBit(unsigned int bit, const T field) {
42  return field & (static_cast<T>(1) << bit);
43 }
44 
45 
49 template <typename T>
50 std::vector<T> Shuffle(const std::vector<T> &input, Prng *prng) {
51  std::vector<T> shuffled(input);
52  unsigned N = shuffled.size();
53  // No shuffling for the last element
54  for (unsigned i = 0; i < N; ++i) {
55  const unsigned swap_idx = i + prng->Next(N - i);
56  std::swap(shuffled[i], shuffled[swap_idx]);
57  }
58  return shuffled;
59 }
60 
61 
67 template <typename T, typename U>
68 void SortTeam(std::vector<T> *tractor, std::vector<U> *towed) {
69  assert(tractor);
70  assert(towed);
71  assert(tractor->size() == towed->size());
72  int N = tractor->size();
73 
74  // Insertion sort on both, tractor and towed
75  for (int i = 1; i < N; ++i) {
76  T val_tractor = (*tractor)[i];
77  U val_towed = (*towed)[i];
78  int pos;
79  for (pos = i-1; (pos >= 0) && ((*tractor)[pos] > val_tractor); --pos) {
80  (*tractor)[pos+1] = (*tractor)[pos];
81  (*towed)[pos+1] = (*towed)[pos];
82  }
83  (*tractor)[pos+1] = val_tractor;
84  (*towed)[pos+1] = val_towed;
85  }
86 }
87 
88 
89 template <typename hashed_type>
90 struct hash_murmur {
91  size_t operator() (const hashed_type key) const {
92 #ifdef __x86_64__
93  return MurmurHash64A(&key, sizeof(key), 0x9ce603115bba659bLLU);
94 #else
95  return MurmurHash2(&key, sizeof(key), 0x07387a4f);
96 #endif
97  }
98 };
99 
100 
113  public:
114  StopWatch() : running_(false) {}
115 
116  void Start();
117  void Stop();
118  void Reset();
119 
120  double GetTime() const;
121 
122  private:
123  bool running_;
124  timeval start_, end_;
125 };
126 
127 
142 friend class UTLog2Histogram;
143 
144  public:
145  explicit Log2Histogram(unsigned int nbins);
146 
147  void Add(uint64_t value) {
148  unsigned int i;
149  const unsigned int n = this->bins_.size() - 1;
150 
151  for (i = 1; i <= n; i++) {
152  if (value < this->boundary_values_[i]) {
153  atomic_inc32(&(this->bins_[i]));
154  return;
155  }
156  }
157 
158  atomic_inc32(&(this->bins_[0])); // add to overflow bin.
159  }
160 
164  inline uint64_t N() {
165  uint64_t n = 0;
166  unsigned int i;
167  for (i = 0; i <= this->bins_.size() - 1; i++) {
168  n += static_cast<unsigned int>(atomic_read32(&(this->bins_[i])));
169  }
170  return n;
171  }
172 
176  unsigned int GetQuantile(float n);
177 
178  std::string ToString();
179 
180  void PrintLog2Histogram();
181 
182  private:
183  std::vector<atomic_int32> bins_;
184  // boundary_values_ handle the largest value a certain
185  // bin can store in itself.
186  std::vector<unsigned int> boundary_values_;
187 };
188 
194  public:
195  std::vector<atomic_int32> GetBins(const Log2Histogram &h);
196 };
197 
198 
200  public:
201  static bool g_is_enabled; // false by default
202 
203  explicit HighPrecisionTimer(Log2Histogram *recorder)
204  : timestamp_start_(g_is_enabled ? platform_monotonic_time_ns() : 0)
205  , recorder_(recorder)
206  { }
207 
209  if (g_is_enabled)
210  recorder_->Add(platform_monotonic_time_ns() - timestamp_start_);
211  }
212 
213  private:
216 };
217 
218 #ifdef CVMFS_NAMESPACE_GUARD
219 } // namespace CVMFS_NAMESPACE_GUARD
220 #endif
221 
222 #endif // CVMFS_UTIL_ALGORITHM_H_
Definition: prng.h:28
std::vector< T > Shuffle(const std::vector< T > &input, Prng *prng)
Definition: algorithm.h:50
uint64_t timestamp_start_
Definition: algorithm.h:214
std::vector< unsigned int > boundary_values_
Definition: algorithm.h:186
std::vector< atomic_int32 > bins_
Definition: algorithm.h:183
double DiffTimeSeconds(struct timeval start, struct timeval end)
Definition: algorithm.cc:31
Log2Histogram * recorder_
Definition: algorithm.h:215
uint64_t N()
Definition: algorithm.h:164
#define CVMFS_EXPORT
Definition: export.h:11
assert((mem||(size==0))&&"Out Of Memory")
uint64_t platform_monotonic_time_ns()
bool running_
Definition: algorithm.h:123
void SetBit(unsigned int bit, T *field)
Definition: algorithm.h:31
void ClearBit(unsigned int bit, T *field)
Definition: algorithm.h:36
HighPrecisionTimer(Log2Histogram *recorder)
Definition: algorithm.h:203
bool TestBit(unsigned int bit, const T field)
Definition: algorithm.h:41
static bool g_is_enabled
Definition: algorithm.h:201
uint64_t MurmurHash64A(const void *key, int len, uint64_t seed)
Definition: murmur.hxx:83
void SortTeam(std::vector< T > *tractor, std::vector< U > *towed)
Definition: algorithm.h:68
timeval start_
Definition: algorithm.h:124
void Add(uint64_t value)
Definition: algorithm.h:147
uint32_t MurmurHash2(const void *key, int len, uint32_t seed)
Definition: murmur.hxx:23
uint32_t Next(const uint64_t boundary)
Definition: prng.h:49