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

[Linux] new API to list swap_partitions() #1681

Open
frankenstein91 opened this issue Feb 7, 2020 · 8 comments
Open

[Linux] new API to list swap_partitions() #1681

frankenstein91 opened this issue Feb 7, 2020 · 8 comments

Comments

@frankenstein91
Copy link

frankenstein91 commented Feb 7, 2020

Platform

  • OS: Arch Linux x86_64
  • Kernel Linux 5.5.2-arch1-1
  • psutil version: 5.6.7

Bug description
As I understand it, the psutil.disk_partitions function should display all partitions. But my SWAP partitions are not listed.

NAME    MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sda       8:0    0 931,5G  0 disk
├─sda1    8:1    0     2G  0 part /boot 
├─sda2    8:2    0    20G  0 part [SWAP]
└─sda3    8:3    0 909,5G  0 part /run/btrfs-root
nvme0n1 259:0    0 465,8G  0 disk
frankenstein91 referenced this issue in frankenstein91/tuxedo-systeminfos Feb 7, 2020
swap is missing
@giampaolo
Copy link
Owner

did you try psutil.disk_partitions(all=True)?

@frankenstein91
Copy link
Author

yes
missing on both

@giampaolo
Copy link
Owner

Mmm I am not sure. Partitions are retrieved via getmntent() syscall which parses /etc/mtab:

while ((entry = getmntent(file))) {

I assume that means /etc/mtab does not list swap partitions (I checked on my system: it's the same).

@giampaolo
Copy link
Owner

giampaolo commented Feb 11, 2020

It turns out swap partions are listed in /proc/swaps. I implemented this in 00a3398. You'll have to pass all=True to show the swap partition.

@frankenstein91
Copy link
Author

First of all, thanks @giampaolo for the change. Unfortunately I still have a thought with your implementation. As I understand the documentation, all=False should filter on hardware. So USB sticks, hard drives and SSDs, but leave out virtual devices. I don't see the SWAP memory itself as such a virtual device, like a RAM disk. In my case it is not even a SWAP file, but a real partition. So I think it would be cool if this could be adjusted a bit more.

I hope it is possible

@giampaolo
Copy link
Owner

giampaolo commented Feb 13, 2020

You got me thinking here so I investigated this topic a bit.

The problem with swap partitions/files is that they are not like other regular partitions and most importantly they are not mounted (having disk_partitions()'s moutpoint value set to None is a first). You only have the reference to the device, which is a block type file ("b" column in ls). On my Linux box:

~/svn/psutil {master}$ ls -l /dev/nvme0n1p3
brw-rw---- 1 root disk 259, 3 2020-02-11 11:53 /dev/nvme0n1p3

Since it's a block file, if you do psutil.disk_usage("/dev/nvme0n1p3") you'll get a wrong result so you cannot get total/used size of the swap partition (you'd have to get it from /proc/swaps). The reality is that swap partitions are different than others. On Linux "df -a" won't show them. You'll have to use "swapon" command, which also reports the right partition size (from /proc/swaps). Furthermore, on Linux they don't necessarily have to be partitions, they can also be files. On Windows AFAIK it's always a file (the page file) and never a partition, even though I'm not sure if there can be more than one.

To me this suggests that:

  1. maybe disk_partitions() is not the right place to do this
  2. we may need a new API specific for this (listing swap partitions/files with size details). We already have swap_memory(). Perhaps we can add a swap_locations() or disk_swaps() function which on Linux will return something like this:
>>> psutil.swap_locations()
[sswap(path='/dev/nvme0n1p3', type='partition', total=1324423, used=1234, priority=-2),
 sswap(path='/dev/myswap', type='swapfile', total=12000, used=0, priority=-2)]

The problem with this is my ignorance: I have no idea how disk files/partitions work on Windows (what Windows API to use, what info we can extract) and also other UNIX systems. And knowing what info we can get is crucial to come up with an API which makes sense.

Anyway, for now I think it's safer to revert 00a3398 (sorry).

@giampaolo giampaolo reopened this Feb 13, 2020
@frankenstein91
Copy link
Author

So you don't have to apologize for that. I was about to suggest that we reopen the ticket. You were quicker than I was.

Maybe we should also think about the function name disk_partitions(). Maybe something where the mount approach comes across would be more understandable. In addition to the swap problem, I have found another problem that we might be able to improve by renaming the function.

btrfs filesystem show
Label: 'ARCH'  uuid: 59a7dd58-666c-4bc4-8409-0bf64cd82f36
	Total devices 2 FS bytes used 200.03GiB
	devid    1 size 909.51GiB used 441.03GiB path /dev/sda3
	devid    2 size 465.76GiB used 80.03GiB path /dev/nvme0n1

As you saw above, I use BTRFS. With this I have merged two partitions. Only one of them is shown in the list. But both are in use as one. I think it's a similar problem to SWAP, but also a little different. With SWAP it is a separate partition after all. Maybe in the future it would be possible to switch to something like "lsblk" as backend.

Please don't see this as a negative statement on your super tool, but I want to help you with my ideas to make it even better.

@giampaolo giampaolo changed the title [Arch Linux x86_64] psutil.disk_partitions does not show swap [Linux] psutil.disk_partitions() does not show swap Feb 13, 2020
@giampaolo
Copy link
Owner

giampaolo commented Feb 13, 2020

Please don't see this as a negative statement on your super tool, but I want to help you with my ideas to make it even better.

Of course. When it comes to API decisions any feedback is very welcome (including name bikeshedding =)). Actually I even tweeted about this:
https://twitter.com/grodola/status/1227937457901010946
These areas involving low-level system details are not easy to grasp (and I'm no sysop guy BTW). In order to move this forward I suggest to investigate how to do this at least on Windows and possibly on another UNIX OS (preferably macOS or FreeBSD), either by finding the right C/syscall or the shell command to list swap partitions (we may run it in a subprocess and parse its output as last resort, strace it or something).

As for the BTRFS issue, it seems unrelated to this one, but if you think psutil should work differently in that regard please open a new issue and we'll discuss it there.

giampaolo added a commit that referenced this issue Feb 13, 2020
@giampaolo giampaolo added new-api and removed linux labels Dec 29, 2020
@giampaolo giampaolo changed the title [Linux] psutil.disk_partitions() does not show swap [Linux] new API to list swap_partitions() Dec 29, 2020
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

2 participants