Skip to content
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

Expose private _UPTIME as public psutil.BOOT_TIME constant #140

Closed
giampaolo opened this issue May 23, 2014 · 13 comments
Closed

Expose private _UPTIME as public psutil.BOOT_TIME constant #140

giampaolo opened this issue May 23, 2014 · 13 comments

Comments

@giampaolo
Copy link
Owner

From tiran79 on December 08, 2010 02:12:52

psutil already has information for the system's uptime / boot time on Linux and 
Windows. How about adding an new API function that returns the difference 
between boot time and now as timedelta? It's a low hanging fruit.

I don't know if the information is easily available on OSX and BSD. But you 
could parse the output of the "uptime" command and retrieve both uptime and 
load average together. (See my other feature request)

Original issue: http://code.google.com/p/psutil/issues/detail?id=140

@giampaolo
Copy link
Owner Author

From g.rodola on December 07, 2010 17:26:58

System uptime is already retrieved across all platforms because that's the 
value we return for PID 0 (aka the "system process") create time property:

>>> psutil.Process(0).create_time
1291747130.0

As you pointed out it is not publicly available in terms of current API though.
Maybe it makes sense to expose it as a psutil.UPTIME constant.

I don't think it is worth to provide an extra function to calculate the time 
delta since you would easily get it by just doing:

>>> delta = time.time() - psutil.UPTIME

Status: WaitingForReview
Labels: -Type-Defect Type-Enhancement

@giampaolo
Copy link
Owner Author

From tiran79 on December 07, 2010 17:44:51

The name UPTIME seems a bit confusing. I'd call it BOOT_TIME or something similar.

As for the feature psutil.Process(0) returns the system process: I didn't know 
about the feature and I can't find it in the documentation, too. I'd even 
expected that psutil.Process(0) returns the information of the *current* 
process rather than the system. Some system APIs use 0 as an alias for the pid 
of the current process.

I agree that it's simple to get the uptime once once a developer knows about 
this feature. However a simple function get_system_load makes it really obvious 
that it's even possible with psutil. ;)

@giampaolo
Copy link
Owner Author

From g.rodola on December 07, 2010 18:08:50

> The name UPTIME seems a bit confusing. I'd call it BOOT_TIME or something similar.

Yes, it's probably better.

> As for the feature psutil.Process(0) returns the system process: I didn't know 
> about the feature and I can't find it in the documentation, too. I'd even expected 
> that psutil.Process(0) returns the information of the *current* process 
rather than 
> the system. Some system APIs use 0 as an alias for the pid of the current process.

PID 0 is a legal PID on Windows, OSX and FreeBSD, hence it would be just wrong 
making it an alias for current process pid.
It is true that there's no such thing as "PID 0" on Linux though.
When we wrote the first psutil draft we hard-coded this behavior in _pslinux.py 
for conformity with other platform implementations but on a second thought I'm 
not sure it's been a good idea.

> However a simple function get_system_load makes it really obvious that it's 
> even possible with psutil. ;)

I guess you meant get_system_uptime() here.
In this case I prefer a constant instead, since system boot time is something 
which does not change over time (just like psutil.TOTAL_PHYMEM).

@giampaolo
Copy link
Owner Author

From tiran79 on December 07, 2010 18:23:20

> PID 0 is a legal PID on Windows, OSX and FreeBSD, hence it would be just 
wrong making it an alias for current process pid.
> It is true that there's no such thing as "PID 0" on Linux though.

I wasn't aware that PID 0 are legal on Windows and BSDish platforms. On Linux 
PID 0 has special meaning for some syscalls. For example kill(0, SIG) sends the 
signal to all processes in the current process group. I know that I've used it 
as an alternative to getpid(), but I can't remember the context right now.

I didn't want to convince you to use 0 this way. I was merely trying to explain 
my (wrong) expectation. 

> I guess you meant get_system_uptime() here.

Yeah, sorry. I've confused both feature requests for a second.

> In this case I prefer a constant instead, since system boot time is something 
which does not change over time (just like psutil.TOTAL_PHYMEM).

You have convinced me. Make it so! :)

About not changing TOTAL_PHYMEM, IIRC there are plans regarding virtualization 
to change the amount of physical memory of a running guest. The assumption of a 
fixed TOTAL_PHYSMEM may become wrong in the future.

@giampaolo
Copy link
Owner Author

From [email protected] on December 07, 2010 19:42:42

> About not changing TOTAL_PHYMEM, IIRC there are plans regarding virtualization
> to change the amount of physical memory of a running guest. The assumption 
> of a fixed TOTAL_PHYSMEM may become wrong in the future.

That's an interesting observation... We can cross that bridge when we get to it 
I guess, but thanks for the tip!

@giampaolo
Copy link
Owner Author

From g.rodola on December 10, 2010 14:08:46

Implemented on Linux and FreeBSD in r847 and r848 .

Summary: Expose private _UPTIME as public psutil.BOOT_TIME constant
Status: Accepted
Labels: Milestone-0.2.2 Progress-2in4

@giampaolo
Copy link
Owner Author

From g.rodola on December 10, 2010 14:09:52

Sorry, r846 and r847 .

@giampaolo
Copy link
Owner Author

From g.rodola on December 10, 2010 14:19:59

Implemented on Windows in r848 .

Status: Started
Labels: -Progress-2in4 Progress-3in4

@giampaolo
Copy link
Owner Author

From g.rodola on December 10, 2010 14:23:11

Labels: -Milestone-0.2.2 Milestone-0.2.1

@giampaolo
Copy link
Owner Author

From g.rodola on February 16, 2011 18:17:12

Implemented on OSX in r915 .

Status: FixedInSVN

@giampaolo
Copy link
Owner Author

From g.rodola on February 16, 2011 18:17:21

Labels: -Progress-3in4 Progress-4in4

@giampaolo
Copy link
Owner Author

From g.rodola on March 20, 2011 14:55:37

Status: Fixed

@giampaolo
Copy link
Owner Author

From g.rodola on March 02, 2013 03:57:29

Updated csets after the SVN -> Mercurial migration: r846 == revision 
4153e742a98b r847 == revision ed93d5e8d12b r848 == revision 76fde21cdd2e r915 
== revision a5c46a7300dd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant