/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/random.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | |
21 | | #include "yb/rocksdb/util/random.h" |
22 | | |
23 | | #include <stdint.h> |
24 | | #include <string.h> |
25 | | #include <thread> |
26 | | #include <utility> |
27 | | |
28 | | #include "yb/rocksdb/port/likely.h" |
29 | | #include "yb/rocksdb/util/thread_local.h" // For ROCKSDB_SUPPORT_THREAD_LOCAL |
30 | | |
31 | | #if ROCKSDB_SUPPORT_THREAD_LOCAL |
32 | | #define STORAGE_DECL static __thread |
33 | | #else |
34 | 327M | #define STORAGE_DECL static |
35 | | #endif |
36 | | |
37 | | namespace rocksdb { |
38 | | |
39 | 163M | Random* Random::GetTLSInstance() { |
40 | 163M | STORAGE_DECL Random* tls_instance; |
41 | 163M | STORAGE_DECL std::aligned_storage<sizeof(Random)>::type tls_instance_bytes; |
42 | | |
43 | 163M | auto rv = tls_instance; |
44 | 163M | if (UNLIKELY(rv == nullptr)) { |
45 | 8.80k | size_t seed = std::hash<std::thread::id>()(std::this_thread::get_id()); |
46 | 8.80k | rv = new (&tls_instance_bytes) Random((uint32_t)seed); |
47 | 8.80k | tls_instance = rv; |
48 | 8.80k | } |
49 | 163M | return rv; |
50 | 163M | } |
51 | | |
52 | 10.1M | Slice RandomString(Random* rnd, int len, std::string* dst) { |
53 | 10.1M | dst->resize(len); |
54 | 3.88G | for (int i = 0; i < len; i++) { |
55 | 3.87G | (*dst)[i] = static_cast<char>(' ' + rnd->Uniform(95)); // ' ' .. '~' |
56 | 3.87G | } |
57 | 10.1M | return Slice(*dst); |
58 | 10.1M | } |
59 | | |
60 | 11.9k | Slice CompressibleString(Random* rnd, double compressed_fraction, int len, std::string* dst) { |
61 | 11.9k | int raw = static_cast<int>(len * compressed_fraction); |
62 | 11.9k | if (raw < 1) raw = 1; |
63 | 11.9k | std::string raw_data; |
64 | 11.9k | RandomString(rnd, raw, &raw_data); |
65 | | |
66 | | // Duplicate the random data until we have filled "len" bytes |
67 | 11.9k | dst->clear(); |
68 | 31.3k | while (dst->size() < (unsigned int)len) { |
69 | 19.4k | dst->append(raw_data); |
70 | 19.4k | } |
71 | 11.9k | dst->resize(len); |
72 | 11.9k | return Slice(*dst); |
73 | 11.9k | } |
74 | | |
75 | | |
76 | | } // namespace rocksdb |