0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-12-01 01:21:03 +01:00
mongodb/util/goodies.h

100 lines
2.2 KiB
C
Raw Normal View History

2007-11-04 22:35:08 +01:00
// goodies.h
// miscellaneous junk
#pragma once
#include "../stdafx.h"
2007-11-18 03:10:00 +01:00
/* set to TRUE if we are exiting */
extern bool goingAway;
2007-11-04 22:35:08 +01:00
inline void dumpmemory(const char *data, int len) {
2007-11-12 16:10:43 +01:00
if( len > 1024 )
len = 1024;
2007-11-04 22:35:08 +01:00
try {
const char *q = data;
const char *p = q;
while( len > 0 ) {
for( int i = 0; i < 16; i++ ) {
2007-11-12 16:10:43 +01:00
if( *p >= 32 && *p <= 126 )
cout << *p;
else
cout << '.';
2007-11-04 22:35:08 +01:00
p++;
}
cout << " ";
2007-11-12 16:10:43 +01:00
p -= 16;
2007-11-04 22:35:08 +01:00
for( int i = 0; i < 16; i++ )
2007-11-12 16:10:43 +01:00
cout << (unsigned) ((unsigned char)*p++) << ' ';
2007-11-04 22:35:08 +01:00
cout << endl;
len -= 16;
}
2007-11-12 16:10:43 +01:00
} catch(...) {
}
2007-11-04 22:35:08 +01:00
}
2007-11-05 20:44:26 +01:00
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
2007-11-18 03:10:00 +01:00
struct WrappingInt {
WrappingInt() { x = 0; }
WrappingInt(unsigned z) : x(z) { }
unsigned x;
operator unsigned() const { return x; }
WrappingInt& operator++() { x++; return *this; }
static int diff(unsigned a, unsigned b) { return a-b; }
bool operator<=(WrappingInt r) {
// platform dependent
int df = (r.x - x);
return df >= 0;
}
bool operator>(WrappingInt r) { return !(r<=*this); }
};
2007-11-05 20:44:26 +01:00
inline void sleepsecs(int s) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += s;
boost::thread::sleep(xt);
}
2007-11-13 22:44:01 +01:00
inline void sleepmillis(int s) {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.nsec += s * 1000000;
boost::thread::sleep(xt);
}
2007-11-18 03:10:00 +01:00
// note this wraps
inline int tdiff(unsigned told, unsigned tnew) {
return WrappingInt::diff(tnew, told);
}
2007-11-18 03:10:00 +01:00
inline unsigned curTimeMillis() {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
unsigned t = xt.nsec / 1000000;
return (xt.sec & 0xfffff) * 1000 + t;
}
// measures up to 1024 seconds. or, 512 seconds with tdiff that is...
inline unsigned curTimeMicros() {
boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
unsigned t = xt.nsec / 1000;
unsigned secs = xt.sec % 1024;
t = secs*1000000 + t;
return t;
2007-11-18 03:10:00 +01:00
}
using namespace boost;
typedef boost::mutex::scoped_lock lock;
// simple scoped timer
class Timer {
public:
Timer() { old = curTimeMicros(); }
int micros() {
unsigned n = curTimeMicros();
cout << "old:" << old << " new:" << n << endl;
return tdiff(old, n);
}
private:
unsigned old;
};