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 #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  int dbKB = getKBUsedAll(app.getLedgerDB().getSession());
79 
80  if (dbKB > 0)
81  ret[jss::dbKBTotal] = dbKB;
82 
83  dbKB = getKBUsedDB(app.getLedgerDB().getSession());
84 
85  if (dbKB > 0)
86  ret[jss::dbKBLedger] = dbKB;
87 
88  dbKB = getKBUsedDB(app.getTxnDB().getSession());
89 
90  if (dbKB > 0)
91  ret[jss::dbKBTransaction] = dbKB;
92 
93  {
95  if (c > 0)
96  ret[jss::local_txs] = static_cast<Json::UInt>(c);
97  }
98  }
99 
100  ret[jss::write_load] = app.getNodeStore().getWriteLoad();
101 
102  ret[jss::historical_perminute] =
103  static_cast<int>(app.getInboundLedgers().fetchRate());
104  ret[jss::SLE_hit_rate] = app.cachedSLEs().rate();
105  ret[jss::ledger_hit_rate] = app.getLedgerMaster().getCacheHitRate();
106  ret[jss::AL_hit_rate] = app.getAcceptedLedgerCache().getHitRate();
107 
108  ret[jss::fullbelow_size] =
109  static_cast<int>(app.getNodeFamily().getFullBelowCache(0)->size());
110  ret[jss::treenode_cache_size] =
111  app.getNodeFamily().getTreeNodeCache(0)->getCacheSize();
112  ret[jss::treenode_track_size] =
113  app.getNodeFamily().getTreeNodeCache(0)->getTrackSize();
114 
115  std::string uptime;
116  auto s = UptimeClock::now();
117  using namespace std::chrono_literals;
118  textTime(uptime, s, "year", 365 * 24h);
119  textTime(uptime, s, "day", 24h);
120  textTime(uptime, s, "hour", 1h);
121  textTime(uptime, s, "minute", 1min);
122  textTime(uptime, s, "second", 1s);
123  ret[jss::uptime] = uptime;
124 
125  if (auto shardStore = app.getShardStore())
126  {
127  auto shardFamily{dynamic_cast<ShardFamily*>(app.getShardFamily())};
128  auto const [cacheSz, trackSz] = shardFamily->getTreeNodeCacheSize();
129  Json::Value& jv = (ret[jss::shards] = Json::objectValue);
130 
131  jv[jss::fullbelow_size] = shardFamily->getFullBelowCacheSize();
132  jv[jss::treenode_cache_size] = cacheSz;
133  jv[jss::treenode_track_size] = trackSz;
134  ret[jss::write_load] = shardStore->getWriteLoad();
135  jv[jss::node_writes] = std::to_string(shardStore->getStoreCount());
136  jv[jss::node_reads_total] = shardStore->getFetchTotalCount();
137  jv[jss::node_reads_hit] = shardStore->getFetchHitCount();
138  jv[jss::node_written_bytes] =
139  std::to_string(shardStore->getStoreSize());
140  jv[jss::node_read_bytes] = shardStore->getFetchSize();
141  }
142  else
143  {
144  app.getNodeStore().getCountsJson(ret);
145  }
146 
147  return ret;
148 }
149 
150 // {
151 // min_count: <number> // optional, defaults to 10
152 // }
155 {
156  int minCount = 10;
157 
158  if (context.params.isMember(jss::min_count))
159  minCount = context.params[jss::min_count].asUInt();
160 
161  return getCountsJson(context.app, minCount);
162 }
163 
164 } // namespace ripple
ripple::Application
Definition: Application.h:101
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:154
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:141
ripple::getKBUsedAll
size_t getKBUsedAll(soci::session &s)
Definition: SociDB.cpp:132
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::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::Config::reporting
bool reporting() const
Definition: Config.h:267
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::Application::config
virtual Config & config()=0
ripple::Config::useTxTables
bool useTxTables() const
Definition: Config.h:273
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::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::Application::getLedgerDB
virtual DatabaseCon & getLedgerDB()=0
ripple::NetworkOPs::getLocalTxCount
virtual std::size_t getLocalTxCount()=0
ripple::DatabaseCon::getSession
soci::session & getSession()
Definition: DatabaseCon.h:172
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:93
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:319
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:1815
Json::Value
Represents a JSON value.
Definition: json_value.h:145
ripple::Application::getTxnDB
virtual DatabaseCon & getTxnDB()=0