2016-07-31 03:53:16 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/randomSeed.h>
|
2018-03-03 15:36:20 +00:00
|
|
|
#include <Common/SipHash.h>
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Core/Types.h>
|
2016-10-26 22:27:38 +00:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#include <common/apple_rt.h>
|
|
|
|
#endif
|
2016-07-31 03:53:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
namespace ErrorCodes
|
|
|
|
{
|
|
|
|
extern const int CANNOT_CLOCK_GETTIME;
|
|
|
|
}
|
2016-07-31 03:53:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-26 22:27:38 +00:00
|
|
|
DB::UInt64 randomSeed()
|
2016-07-31 03:53:16 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
struct timespec times;
|
|
|
|
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, ×))
|
|
|
|
DB::throwFromErrno("Cannot clock_gettime.", DB::ErrorCodes::CANNOT_CLOCK_GETTIME);
|
2018-03-03 15:36:20 +00:00
|
|
|
|
|
|
|
/// Not cryptographically secure as time, pid and stack address can be predictable.
|
|
|
|
|
|
|
|
SipHash hash;
|
|
|
|
hash.update(times.tv_nsec);
|
|
|
|
hash.update(times.tv_sec);
|
|
|
|
hash.update(getpid());
|
|
|
|
hash.update(×);
|
|
|
|
return hash.get64();
|
2016-07-31 03:53:16 +00:00
|
|
|
}
|