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/app/rdb/backend/RelationalDBInterfaceSqlite.h>
26 #include <ripple/basics/UptimeClock.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 #include <ripple/shamap/ShardFamily.h>
36 
37 namespace ripple {
38 
39 static void
41  std::string& text,
42  UptimeClock::time_point& seconds,
43  const char* unitName,
44  std::chrono::seconds unitVal)
45 {
46  auto i = seconds.time_since_epoch() / unitVal;
47 
48  if (i == 0)
49  return;
50 
51  seconds -= unitVal * i;
52 
53  if (!text.empty())
54  text += ", ";
55 
56  text += std::to_string(i);
57  text += " ";
58  text += unitName;
59 
60  if (i > 1)
61  text += "s";
62 }
63 
65 getCountsJson(Application& app, int minObjectCount)
66 {
67  auto objectCounts = CountedObjects::getInstance().getCounts(minObjectCount);
68 
70 
71  for (auto const& [k, v] : objectCounts)
72  {
73  ret[k] = v;
74  }
75 
76  if (!app.config().reporting() && app.config().useTxTables())
77  {
78  auto dbKB = dynamic_cast<RelationalDBInterfaceSqlite*>(
80  ->getKBUsedAll();
81 
82  if (dbKB > 0)
83  ret[jss::dbKBTotal] = dbKB;
84 
85  dbKB = dynamic_cast<RelationalDBInterfaceSqlite*>(
87  ->getKBUsedLedger();
88 
89  if (dbKB > 0)
90  ret[jss::dbKBLedger] = dbKB;
91 
92  dbKB = dynamic_cast<RelationalDBInterfaceSqlite*>(
94  ->getKBUsedTransaction();
95 
96  if (dbKB > 0)
97  ret[jss::dbKBTransaction] = dbKB;
98 
99  {
100  std::size_t c = app.getOPs().getLocalTxCount();
101  if (c > 0)
102  ret[jss::local_txs] = static_cast<Json::UInt>(c);
103  }
104  }
105 
106  ret[jss::write_load] = app.getNodeStore().getWriteLoad();
107 
108  ret[jss::historical_perminute] =
109  static_cast<int>(app.getInboundLedgers().fetchRate());
110  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
111  ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
112  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
113 
114  ret[jss::fullbelow_size] =
115  static_cast<int>(app.getNodeFamily().getFullBelowCache(0)->size());
116  ret[jss::treenode_cache_size] =
117  app.getNodeFamily().getTreeNodeCache(0)->getCacheSize();
118  ret[jss::treenode_track_size] =
119  app.getNodeFamily().getTreeNodeCache(0)->getTrackSize();
120 
121  std::string uptime;
122  auto s = UptimeClock::now();
123  using namespace std::chrono_literals;
124  textTime(uptime, s, "year", 365 * 24h);
125  textTime(uptime, s, "day", 24h);
126  textTime(uptime, s, "hour", 1h);
127  textTime(uptime, s, "minute", 1min);
128  textTime(uptime, s, "second", 1s);
129  ret[jss::uptime] = uptime;
130 
131  if (auto shardStore = app.getShardStore())
132  {
133  auto shardFamily{dynamic_cast<ShardFamily*>(app.getShardFamily())};
134  auto const [cacheSz, trackSz] = shardFamily->getTreeNodeCacheSize();
135  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
136 
137  jv[jss::fullbelow_size] = shardFamily->getFullBelowCacheSize();
138  jv[jss::treenode_cache_size] = cacheSz;
139  jv[jss::treenode_track_size] = trackSz;
140  ret[jss::write_load] = shardStore->getWriteLoad();
141  jv[jss::node_writes] = std::to_string(shardStore->getStoreCount());
142  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
143  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
144  jv[jss::node_written_bytes] =
145  std::to_string(shardStore->getStoreSize());
146  jv[jss::node_read_bytes] = shardStore->getFetchSize();
147  }
148  else
149  {
150  app.getNodeStore().getCountsJson(ret);
151  }
152 
153  return ret;
154 }
155 
156 // {
157 // min_count: <number> // optional, defaults to 10
158 // }
161 {
162  int minCount = 10;
163 
164  if (context.params.isMember(jss::min_count))
165  minCount = context.params[jss::min_count].asUInt();
166 
167  return getCountsJson(context.app, minCount);
168 }
169 
170 } // namespace ripple
ripple::Application
Definition: Application.h:115
ripple::Application::getNodeFamily
virtual Family & getNodeFamily()=0
ripple::RPC::JsonContext
Definition: Context.h:53
ripple::Family::getTreeNodeCache
virtual std::shared_ptr< TreeNodeCache > getTreeNodeCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Tree Node Cache.
ripple::doGetCounts
Json::Value doGetCounts(RPC::JsonContext &context)
Definition: GetCounts.cpp:160
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::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
ripple::Application::getRelationalDBInterface
virtual RelationalDBInterface & getRelationalDBInterface()=0
std::chrono::seconds
ripple::Application::getShardStore
virtual NodeStore::DatabaseShard * getShardStore()=0
ripple::Family::getFullBelowCache
virtual std::shared_ptr< FullBelowCache > getFullBelowCache(std::uint32_t ledgerSeq)=0
Return a pointer to the Family Full Below Cache.
ripple::Application::getOPs
virtual NetworkOPs & getOPs()=0
ripple::Application::cachedSLEs
virtual CachedSLEs & cachedSLEs()=0
ripple::CountedObjects::getInstance
static CountedObjects & getInstance() noexcept
Definition: CountedObject.cpp:27
ripple::Application::getInboundLedgers
virtual InboundLedgers & getInboundLedgers()=0
std::chrono::time_point::time_since_epoch
T time_since_epoch(T... args)
ripple::getKBUsedAll
std::uint32_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:130
ripple::Config::reporting
bool reporting() const
Definition: Config.h:316
ripple::UptimeClock::now
static time_point now()
Definition: UptimeClock.cpp:63
Json::objectValue
@ objectValue
object value (collection of name/value pairs).
Definition: json_value.h:43
ripple::Application::getLedgerMaster
virtual LedgerMaster & getLedgerMaster()=0
ripple::RelationalDBInterfaceSqlite
Definition: RelationalDBInterfaceSqlite.h:27
ripple::Application::config
virtual Config & config()=0
ripple::Config::useTxTables
bool useTxTables() const
Definition: Config.h:322
std::to_string
T to_string(T... args)
ripple::RPC::Context::app
Application & app
Definition: Context.h:42
std::chrono::time_point
ripple::ShardFamily
Definition: ShardFamily.h:30
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::TaggedCache::rate
double rate() const
Returns the fraction of cache hits.
Definition: TaggedCache.h:472
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
ripple::getCountsJson
Json::Value getCountsJson(Application &app, int minObjectCount)
Definition: GetCounts.cpp:65
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
ripple::ShardFamily::getTreeNodeCacheSize
std::pair< int, int > getTreeNodeCacheSize()
Return a pair where the first item is the number of items cached and the second item is the number of...
Definition: ShardFamily.cpp:94
ripple::Application::getShardFamily
virtual Family * getShardFamily()=0
Json::Value::asUInt
UInt asUInt() const
Definition: json_value.cpp:545
ripple::NodeStore::Database::getCountsJson
void getCountsJson(Json::Value &obj)
Definition: Database.cpp:331
ripple::CountedObjects::getCounts
List getCounts(int minimumThreshold) const
Definition: CountedObject.cpp:39
ripple::textTime
static void textTime(std::string &text, UptimeClock::time_point &seconds, const char *unitName, std::chrono::seconds unitVal)
Definition: GetCounts.cpp:40
std::string::empty
T empty(T... args)
std::size_t
ripple::RPC::JsonContext::params
Json::Value params
Definition: Context.h:64
ripple::LedgerMaster::getCacheHitRate
float getCacheHitRate()
Definition: LedgerMaster.cpp:1846
Json::Value
Represents a JSON value.
Definition: json_value.h:145