rippled
GetCounts.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012-2014 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/app/ledger/AcceptedLedger.h>
21 #include <ripple/app/ledger/InboundLedgers.h>
22 #include <ripple/app/ledger/LedgerMaster.h>
23 #include <ripple/app/main/Application.h>
24 #include <ripple/app/misc/NetworkOPs.h>
25 #include <ripple/basics/UptimeClock.h>
26 #include <ripple/core/DatabaseCon.h>
27 #include <ripple/json/json_value.h>
28 #include <ripple/ledger/CachedSLEs.h>
29 #include <ripple/net/RPCErr.h>
30 #include <ripple/nodestore/Database.h>
31 #include <ripple/nodestore/DatabaseShard.h>
32 #include <ripple/protocol/ErrorCodes.h>
33 #include <ripple/protocol/jss.h>
34 #include <ripple/rpc/Context.h>
35 
36 namespace ripple {
37 
38 static void
40  std::string& text,
41  UptimeClock::time_point& seconds,
42  const char* unitName,
43  std::chrono::seconds unitVal)
44 {
45  auto i = seconds.time_since_epoch() / unitVal;
46 
47  if (i == 0)
48  return;
49 
50  seconds -= unitVal * i;
51 
52  if (!text.empty())
53  text += ", ";
54 
55  text += std::to_string(i);
56  text += " ";
57  text += unitName;
58 
59  if (i > 1)
60  text += "s";
61 }
62 
64 getCountsJson(Application& app, int minObjectCount)
65 {
66  auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
67 
69 
70  for (auto const& [k, v] : objectCounts)
71  {
72  ret[k] = v;
73  }
74 
75  int dbKB = getKBUsedAll(app.getLedgerDB().getSession());
76 
77  if (dbKB > 0)
78  ret[jss::dbKBTotal] = dbKB;
79 
80  dbKB = getKBUsedDB(app.getLedgerDB().getSession());
81 
82  if (dbKB > 0)
83  ret[jss::dbKBLedger] = dbKB;
84 
85  dbKB = getKBUsedDB(app.getTxnDB().getSession());
86 
87  if (dbKB > 0)
88  ret[jss::dbKBTransaction] = dbKB;
89 
90  {
92  if (c > 0)
93  ret[jss::local_txs] = static_cast<Json::UInt>(c);
94  }
95 
96  ret[jss::write_load] = app.getNodeStore().getWriteLoad();
97 
98  ret[jss::historical_perminute] =
99  static_cast<int>(app.getInboundLedgers().fetchRate());
100  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
101  ret[jss::node_hit_rate] = app.getNodeStore().getCacheHitRate();
102  ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
103  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
104 
105  ret[jss::fullbelow_size] =
106  static_cast<int>(app.family().fullbelow().size());
107  ret[jss::treenode_cache_size] = app.family().treecache().getCacheSize();
108  ret[jss::treenode_track_size] = app.family().treecache().getTrackSize();
109 
110  std::string uptime;
111  auto s = UptimeClock::now();
112  using namespace std::chrono_literals;
113  textTime(uptime, s, "year", 365 * 24h);
114  textTime(uptime, s, "day", 24h);
115  textTime(uptime, s, "hour", 1h);
116  textTime(uptime, s, "minute", 1min);
117  textTime(uptime, s, "second", 1s);
118  ret[jss::uptime] = uptime;
119 
120  ret[jss::node_writes] = app.getNodeStore().getStoreCount();
121  ret[jss::node_reads_total] = app.getNodeStore().getFetchTotalCount();
122  ret[jss::node_reads_hit] = app.getNodeStore().getFetchHitCount();
123  ret[jss::node_written_bytes] = app.getNodeStore().getStoreSize();
124  ret[jss::node_read_bytes] = app.getNodeStore().getFetchSize();
125 
126  if (auto shardStore = app.getShardStore())
127  {
128  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
129  jv[jss::fullbelow_size] =
130  static_cast<int>(app.shardFamily()->fullbelow().size());
131  jv[jss::treenode_cache_size] =
133  jv[jss::treenode_track_size] =
135  ret[jss::write_load] = shardStore->getWriteLoad();
136  ret[jss::node_hit_rate] = shardStore->getCacheHitRate();
137  jv[jss::node_writes] = shardStore->getStoreCount();
138  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
139  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
140  jv[jss::node_written_bytes] = shardStore->getStoreSize();
141  jv[jss::node_read_bytes] = shardStore->getFetchSize();
142  }
143 
144  return ret;
145 }
146 
147 // {
148 // min_count: <number> // optional, defaults to 10
149 // }
152 {
153  int minCount = 10;
154 
155  if (context.params.isMember(jss::min_count))
156  minCount = context.params[jss::min_count].asUInt();
157 
158  return getCountsJson(context.app, minCount);
159 }
160 
161 } // namespace ripple
ripple::NodeStore::Database::getFetchSize
std::uint32_t getFetchSize() const
Definition: Database.h:215
ripple::Application
Definition: Application.h:97
ripple::RPC::JsonContext
Definition: Context.h:52
ripple::doGetCounts
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:151
ripple::NodeStore::Database::getWriteLoad
virtual std::int32_t getWriteLoad() const =0
Retrieve the estimated number of pending write operations.
ripple::Application::getAcceptedLedgerCache
virtual TaggedCache< uint256, AcceptedLedger > & getAcceptedLedgerCache()=0
std::string
STL class.
ripple::getKBUsedDB
size_t getKBUsedDB(soci::session &s)
Definition: SociDB.cpp:140
ripple::getKBUsedAll
size_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:131
ripple::InboundLedgers::fetchRate
virtual std::size_t fetchRate()=0
Returns the rate of historical ledger fetches per minute.
Json::UInt
unsigned int UInt
Definition: json_forwards.h:27
std::chrono::seconds
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::NodeStore::Database::getCacheHitRate
virtual float getCacheHitRate()=0
Get the positive cache hits to total attempts ratio.
ripple::Application::getOPs
virtual NetworkOPs & getOPs()=0
ripple::Application::cachedSLEs
virtual CachedSLEs & cachedSLEs()=0
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:26
ripple::Application::getInboundLedgers
virtual InboundLedgers & getInboundLedgers()=0
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::Application::shardFamily
virtual Family * shardFamily()=0
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:63
ripple::NodeStore::Database::getStoreSize
std::uint32_t getStoreSize() const
Definition: Database.h:209
ripple::Application::family
virtual Family & family()=0
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::TaggedCache::getCacheSize
int getCacheSize() const
Definition: TaggedCache.h:129
std::to_string
T to_string(T... args)
ripple::CachedSLEs::rate
double rate() const
Returns the fraction of cache hits.
Definition: CachedSLEs.cpp:48
ripple::NodeStore::Database::getStoreCount
std::uint32_t getStoreCount() const
Gather statistics pertaining to read and write activities.
Definition: Database.h:191
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
std::chrono::time_point
Json::Value::isMember
bool isMember(const char *key) const
Return true if the object has a member named key.
Definition: json_value.cpp:932
ripple::Application::getLedgerDB
virtual DatabaseCon & getLedgerDB()=0
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
ripple::TaggedCache::getTrackSize
int getTrackSize() const
Definition: TaggedCache.h:136
ripple::Family::treecache
virtual TreeNodeCache & treecache()=0
ripple::DatabaseCon::getSession
soci::session & getSession()
Definition: DatabaseCon.h:123
ripple::getCountsJson
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition: GetCounts.cpp:64
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Application::getNodeStore
virtual NodeStore::Database & getNodeStore()=0
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:38
ripple::textTime
static void textTime(std::string &text, UptimeClock::time_point &seconds, const char *unitName, std::chrono::seconds unitVal)
Definition: GetCounts.cpp:39
std::string::empty
T empty(T... args)
ripple::detail::BasicFullBelowCache::size
size_type size() const
Return the number of elements in the cache.
Definition: FullBelowCache.h:79
ripple::NodeStore::Database::getFetchHitCount
std::uint32_t getFetchHitCount() const
Definition: Database.h:203
std::size_t
ripple::Family::fullbelow
virtual FullBelowCache & fullbelow()=0
ripple::NodeStore::Database::getFetchTotalCount
std::uint32_t getFetchTotalCount() const
Definition: Database.h:197
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:63
ripple::LedgerMaster::getCacheHitRate
float getCacheHitRate()
Definition: LedgerMaster.cpp:1688
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::Application::getTxnDB
virtual DatabaseCon & getTxnDB()=0