-
Notifications
You must be signed in to change notification settings - Fork 38.7k
[RPC] Add an uptime command that displays the amount of time (in seconds) bitcoind has been running #10400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There is no need to look at the timestamp of any files - the server could simply remember its own startup timestamp and use that as a reference. This would work just as well on windows. |
|
Thank you for replying Wladimir. You are correct of course. Checking the PID file was the most simple and with the least changes to the codebase I could think of - while still being useful (I am not familiar with the Bitcoin core and have not worked with C++ since 2010). So, if you think this command is useful for inclusion in the core, I could work on improving it and then squash the commits in the end. Should this information be its own command or should it be included in another existing command - if so, which one? Thank you, |
|
It seems to be useful. Agree with @laanwj. Just set a variable during startup with |
|
Concept ACK The other way could be adding starttime to |
A gentle reminder that /**
* @note Do not add or change anything in the information returned by this
* method. `getinfo` exists for backwards-compatibility only. It combines
* information from wildly different sources in the program, which is a mess,
* and is thus planned to be deprecated eventually.
*
* Based on the source of the information, new information should be added to:
* - `getblockchaininfo`,
* - `getnetworkinfo` or
* - `getwalletinfo`
*
* Or alternatively, create a specific query method for the information.
**One of the other |
|
There's already a variable in the GUI for getting the startup time: bitcoin/src/qt/clientmodel.cpp Line 29 in 86ea3c2
You could probably just move it so that it can be used for both the GUI and for this RPC call. |
|
Thank you for your feedback. I went ahead and made some modifications according to your suggestions. I have pushed a new commit that removes I called it I am not sure of the naming conventions. Should I use I have build the QT client (QT5 in my case) and the daemon and from my testing every is working well. I have also started the QT client with the I will add a functional test to the RPC interface for this command. I'm not sure how the testing is done for the QT client. |
src/util.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// Application startup time (used for uptime calculation)
src/rpc/server.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this at all?
src/rpc/server.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Started in the future!? ;-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagined that for some weird reason the clock would change to before the start and the user would get a negative value. Maybe I'm over-thinking this? ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I do not have a problem with this. Just return negative value and do not crash.
src/rpc/server.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document output as Result: as other RPCs. Add unit specification (seconds).
|
I have made a new commit with some changes suggested by Pavel. While searching for examples of documentation I noticed Here is a sample of the documentation when running I used the |
|
I've added a functional test to check for the uptime value. In this test I've placed a As I've noted in the test comment:
|
test/functional/server.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use setmocktime to not really sleep/slow down tests for 6 seconds.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arghh I didn't know about this setmocktime. Didn't think of searching either. Will get to it.
test/functional/test_runner.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the name uptime.py or so, not generic server.py.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took as an example the net.py test file that referred to tests in rpc/net.cpp. Since the uptime command is in the server.cpp - although there are no tests for this file - I chose to create a server.py test file. Do you still think it's best to use uptime.py?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rvelhote Yup, OK.
|
@paveljanik I made the change to Let me know when you think everything is in order and I will go ahead and squash the commits. Thank you for your help (and patience) in reviewing everything :) |
|
OK, let's squash please. The travis failure is unrelated (actually fixed in #10429). |
jnewbery
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few nits. The only important one is nStartupTime shouldn't be defined as static in the util.h header file.
src/rpc/server.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense for this RPC to live in src/rpc/misc.cpp. I think server.cpp should be kept as lightweight and generic as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for your feedback John.
I placed it on src/rpc/server.php because the command was information related to the server rather than a utility command (although it can certainly be considered an utility command). That is my only argument for using server.cpp. Maybe let's wait for other people's opinion on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe let's wait for other people's opinion on this?
certainly. Just my opinion that this should live in misc.cpp. I'm happy to defer to the consensus opinion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand both sides so don't have a strong opinion. We want to have multiple JSON-RPC endpoints at some point, so the class should be reusable without adding a lot of clutter, but it seems harmless if they all export uptime.
However: server.cpp should be independent of bitcoin, so if you plan on keeping it there, please remove the explicit reference to Bitcoin below in the documentation.
test/functional/uptime.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer not to add a whole extra test for just this one command. I think it'd be better to move the uptime rpc into misc.cpp and then combine signmessages.py, rpcnamedargs.py and this new test into a new test file names misc.py.
However, that tidying can definitely be done in a follow-up PR.
src/rpc/server.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I don't really like ttt here. I think it's fine just to have:
"\"time\" (numeric) The number of seconds that the Bitcoin server has been running\n"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used ttt because I saw it referenced in other commands for usage with time related values. For example in getpeerinfo you have conntime for a timestamp and also timeoffset for a time-based value in seconds. I think ttt will keep it consistent with other commands.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sure. Again, just my personal preference. ttt is fine.
src/util.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be static (a static variable in a header file means that any .cpp file that includes the header will have its own local variable called nStartupTime).
Instead, I think you should define this variable in util.cpp and then have an extern declaration here. See BITCOIN_CONF_FILENAME below for example.
I don't know if you just copied the DEFAULT_LOG... constants below, but I believe those should also be defined in util.cpp.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DEFAULT_LOG constants are already in master. Might be something to improve then.
Also, I don't know if you noticed, this variable was moved from src/qt/clientmodel.cpp to a more global area so that it can be reused by both the server and the QT client.
I will make this change as you suggested and force-push the commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry I wasn't clear - I didn't expect you to change the DEFAULT_LOG constants. I do however think you need to move the nStartupTime definition to avoid the following build warnings:
In file included from addrman.h:14:0,
from addrdb.cpp:8:
util.h:33:22: warning: ‘nStartupTime’ defined but not used [-Wunused-variable]
static const int64_t nStartupTime = GetTime();
^
CXX libbitcoin_server_a-bloom.o
In file included from addrman.h:14:0,
from addrman.cpp:6:
util.h:33:22: warning: ‘nStartupTime’ defined but not used [-Wunused-variable]
static const int64_t nStartupTime = GetTime();
^
CXX libbitcoin_server_a-blockencodings.o
/bin/bash ./config.status
In file included from bitcoind.cpp:18:0:
util.h:33:22: warning: ‘nStartupTime’ defined but not used [-Wunused-variable]
static const int64_t nStartupTime = GetTime();
^
...(repeated many times)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem John, I understood what you meant :) When I mentioned I would make the change I was referring to moving nStartupTime to util.cpp. I pushed a new commit with this change.
|
@jnewbery @paveljanik When you have the opportunity, please let me know if you have any further feedback on this PR? The TravisCI build is failing but it's because of #10429 and I have not updated my branch since I started the PR. Thank you. |
|
Needs rebase |
src/util.h
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a GetStartupTime function instead of exporting this variable.
…coind has been running
|
Tested ACK c074752 |
… time (in seconds) bitcoind has been running c074752 [RPC] Add an uptime command that displays the amount of time that bitcoind has been running (Ricardo Velhote) Tree-SHA512: 8f59d4205042885f23f5b87a0eae0f5d386e9c6134e5324598e7ee304728d4275f383cd154bf1fb25350f5a88cc0ed9f97edb099e9b50c4a0ba72d63ec5ca5b4
…ount of time (in seconds) bitcoind has been running c074752 [RPC] Add an uptime command that displays the amount of time that bitcoind has been running (Ricardo Velhote) Tree-SHA512: 8f59d4205042885f23f5b87a0eae0f5d386e9c6134e5324598e7ee304728d4275f383cd154bf1fb25350f5a88cc0ed9f97edb099e9b50c4a0ba72d63ec5ca5b4
Hello everyone,
I am working on a tool that displays information about a bitcoin node using RPC. One of the parameters I would like to include is the
uptimeof the bitcoin daemon. Currently, as far as I can tell, there is no way of obtaining such information.After thinking a bit on how to perform this task, I concluded that the easiest way to do it is to check the modification time of the
bitcoind.pidfile. This has the disadvantage that it does not work in Windows becausebitcoind.pidis not created.I propose the following solution in this PR and submit it for your review and opinion concerning this new RPC command before I work further on this topic. Do you think this new command makes sense? Please consider it a WIP/PoC as it does not have any error checking or tests.
Here is a sample return from the RPC command:
and the help function:
Best regards,
Ricardo Velhote