YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/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
48.2k
int AppendHumanBytes(uint64_t bytes, char* output, int len) {
69
48.2k
  const uint64_t ull10 = 10;
70
48.2k
  if (bytes >= ull10 << 40) {
71
0
    return snprintf(output, len, "%" PRIu64 "TB", bytes >> 40);
72
48.2k
  } else if (bytes >= ull10 << 30) {
73
0
    return snprintf(output, len, "%" PRIu64 "GB", bytes >> 30);
74
48.2k
  } else if (bytes >= ull10 << 20) {
75
65
    return snprintf(output, len, "%" PRIu64 "MB", bytes >> 20);
76
48.1k
  } else if (bytes >= ull10 << 10) {
77
21.8k
    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
48.2k
}
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++10
) {
95
10
    char c = value[i];
96
10
    if (c >= ' ' && 
c <= '~'0
) {
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 ? 
-num0
: num;
116
70
  if (absnum < 10000) {
117
70
    snprintf(buf, sizeof(buf), "%" PRIi64, num);
118
70
  } else 
if (0
absnum < 100000000
) {
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
21.2M
bool ConsumeDecimalNumber(Slice* in, uint64_t* val) {
135
21.2M
  uint64_t v = 0;
136
21.2M
  int digits = 0;
137
117M
  while (!in->empty()) {
138
106M
    char c = (*in)[0];
139
106M
    if (c >= '0' && 
c <= '9'96.5M
) {
140
96.1M
      ++digits;
141
96.1M
      const unsigned int delta = (c - '0');
142
96.1M
      static const uint64_t kMaxUint64 = ~static_cast<uint64_t>(0);
143
96.1M
      if (v > kMaxUint64/10 ||
144
96.1M
          (v == kMaxUint64/10 && 
delta > kMaxUint64%105
)) {
145
        // Overflow
146
2
        return false;
147
2
      }
148
96.1M
      v = (v * 10) + delta;
149
96.1M
      in->remove_prefix(1);
150
96.1M
    } else {
151
10.4M
      break;
152
10.4M
    }
153
106M
  }
154
21.2M
  *val = v;
155
21.2M
  return (digits > 0);
156
21.2M
}
157
158
}  // namespace rocksdb