/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/logging.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 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | |
24 | | #include "yb/rocksdb/util/logging.h" |
25 | | |
26 | | #ifndef __STDC_FORMAT_MACROS |
27 | | #define __STDC_FORMAT_MACROS |
28 | | #endif |
29 | | |
30 | | #include <inttypes.h> |
31 | | #include <stdio.h> |
32 | | #include "yb/util/slice.h" |
33 | | |
34 | | namespace rocksdb { |
35 | | |
36 | | // for micros < 10ms, print "XX us". |
37 | | // for micros < 10sec, print "XX ms". |
38 | | // for micros >= 10 sec, print "XX sec". |
39 | | // for micros <= 1 hour, print Y:X M:S". |
40 | | // for micros > 1 hour, print Z:Y:X H:M:S". |
41 | | int AppendHumanMicros(uint64_t micros, char* output, int len, |
42 | 14 | bool fixed_format) { |
43 | 14 | if (micros < 10000 && !fixed_format) { |
44 | 0 | return snprintf(output, len, "%" PRIu64 " us", micros); |
45 | 14 | } else if (micros < 10000000 && !fixed_format) { |
46 | 0 | return snprintf(output, len, "%.3lf ms", |
47 | 0 | static_cast<double>(micros) / 1000); |
48 | 14 | } else if (micros < 1000000l * 60 && !fixed_format) { |
49 | 0 | return snprintf(output, len, "%.3lf sec", |
50 | 0 | static_cast<double>(micros) / 1000000); |
51 | 14 | } else if (micros < 1000000ll * 60 * 60 && !fixed_format) { |
52 | 0 | return snprintf(output, len, "%02" PRIu64 ":%05.3f M:S", |
53 | 0 | micros / 1000000 / 60, |
54 | 0 | static_cast<double>(micros % 60000000) / 1000000); |
55 | 14 | } else { |
56 | 14 | return snprintf(output, len, |
57 | 14 | "%02" PRIu64 ":%02" PRIu64 ":%05.3f H:M:S", |
58 | 14 | micros / 1000000 / 3600, |
59 | 14 | (micros / 1000000 / 60) % 60, |
60 | 14 | static_cast<double>(micros % 60000000) / 1000000); |
61 | 14 | } |
62 | 14 | } |
63 | | |
64 | | // for sizes >=10TB, print "XXTB" |
65 | | // for sizes >=10GB, print "XXGB" |
66 | | // etc. |
67 | | // append file size summary to output and return the len |
68 | 45.6k | int AppendHumanBytes(uint64_t bytes, char* output, int len) { |
69 | 45.6k | const uint64_t ull10 = 10; |
70 | 45.6k | if (bytes >= ull10 << 40) { |
71 | 0 | return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40); |
72 | 45.6k | } else if (bytes >= ull10 << 30) { |
73 | 0 | return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30); |
74 | 45.6k | } else if (bytes >= ull10 << 20) { |
75 | 56 | return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20); |
76 | 45.5k | } else if (bytes >= ull10 << 10) { |
77 | 19.2k | return snprintf(output, len, "%" PRIu64 "KB", bytes >> 10); |
78 | 26.3k | } else { |
79 | 26.3k | return snprintf(output, len, "%" PRIu64 "B", bytes); |
80 | 26.3k | } |
81 | 45.6k | } |
82 | | |
83 | 13.0k | void AppendNumberTo(std::string* str, uint64_t num) { |
84 | 13.0k | char buf[30]; |
85 | 13.0k | snprintf(buf, sizeof(buf), "%" PRIu64, num); |
86 | 13.0k | str->append(buf); |
87 | 13.0k | } |
88 | | |
89 | 0 | void AppendBoolTo(std::string* str, const bool b) { |
90 | 0 | str->append(b ? "true" : "false"); |
91 | 0 | } |
92 | | |
93 | 4 | void AppendEscapedStringTo(std::string* str, const Slice& value) { |
94 | 14 | for (size_t i = 0; i < value.size(); i++) { |
95 | 10 | char c = value[i]; |
96 | 10 | if (c >= ' ' && c <= '~') { |
97 | 0 | str->push_back(c); |
98 | 10 | } else { |
99 | 10 | char buf[10]; |
100 | 10 | snprintf(buf, sizeof(buf), "\\x%02x", |
101 | 10 | static_cast<unsigned int>(c) & 0xff); |
102 | 10 | str->append(buf); |
103 | 10 | } |
104 | 10 | } |
105 | 4 | } |
106 | | |
107 | 9.12k | std::string NumberToString(uint64_t num) { |
108 | 9.12k | std::string r; |
109 | 9.12k | AppendNumberTo(&r, num); |
110 | 9.12k | return r; |
111 | 9.12k | } |
112 | | |
113 | 70 | std::string NumberToHumanString(int64_t num) { |
114 | 70 | char buf[24]; |
115 | 70 | int64_t absnum = num < 0 ? -num : num; |
116 | 70 | if (absnum < 10000) { |
117 | 70 | snprintf(buf, sizeof(buf), "%" PRIi64, num); |
118 | 0 | } else if (absnum < 10000000) { |
119 | 0 | snprintf(buf, sizeof(buf), "%" PRIi64 "K", num / 1000); |
120 | 0 | } else if (absnum < 10000000000LL) { |
121 | 0 | snprintf(buf, sizeof(buf), "%" PRIi64 "M", num / 1000000); |
122 | 0 | } else { |
123 | 0 | snprintf(buf, sizeof(buf), "%" PRIi64 "G", num / 1000000000); |
124 | 0 | } |
125 | 70 | return std::string(buf); |
126 | 70 | } |
127 | | |
128 | 4 | std::string EscapeString(const Slice& value) { |
129 | 4 | std::string r; |
130 | 4 | AppendEscapedStringTo(&r, value); |
131 | 4 | return r; |
132 | 4 | } |
133 | | |
134 | 17.2M | bool ConsumeDecimalNumber(Slice* in, uint64_t* val) { |
135 | 17.2M | uint64_t v = 0; |
136 | 17.2M | int digits = 0; |
137 | 97.3M | while (!in->empty()) { |
138 | 88.5M | char c = (*in)[0]; |
139 | 88.5M | if (c >= '0' && c <= '9') { |
140 | 80.0M | ++digits; |
141 | 80.0M | const unsigned int delta = (c - '0'); |
142 | 80.0M | static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0); |
143 | 80.0M | if (v > kMaxUint64/10 || |
144 | 80.0M | (v == kMaxUint64/10 && delta > kMaxUint64%10)) { |
145 | | // Overflow |
146 | 2 | return false; |
147 | 2 | } |
148 | 80.0M | v = (v * 10) + delta; |
149 | 80.0M | in->remove_prefix(1); |
150 | 8.45M | } else { |
151 | 8.45M | break; |
152 | 8.45M | } |
153 | 88.5M | } |
154 | 17.2M | *val = v; |
155 | 17.2M | return (digits > 0); |
156 | 17.2M | } |
157 | | |
158 | | } // namespace rocksdb |