# [SOLVED] Insane ram usage.



## unique name (Aug 20, 2021)

I am using slstatus to display ram usage in percent, and it shows "-830053581%". How is this possible?
And I would also like to control volume from slstatus, and instructions are "vol_perc            OSS/ALSA volume in percent      mixer file (/dev/mixer)" but there is nothing like /dev/mixer. I did whereis mixer and then tried /usr/sbin/mixer but didn't work.
Thank you.


----------



## SirDice (Aug 20, 2021)

unique name said:


> I am using slstatus to display ram usage in percent, and it shows "-830053581%". How is this possible?


That looks like an overflow. As in a signed integer used as unsigned. In any case, how are you doing the calculation?


----------



## mer (Aug 20, 2021)

Is that "s-lowercase L-status"?  I'm not seeing that in either pkg search or grepping INDEX-13 from a ports tree.  I agree with SirDice but without being able to see source code it's hard to give definite answers.


----------



## eternal_noob (Aug 20, 2021)

mer said:


> I'm not seeing that in either pkg search or grepping INDEX-13 from a ports tree.


I believe it's this one: https://github.com/drkhsh/slstatus



SirDice said:


> how are you doing the calculation?


The code to compute RAM usage in percent would be this: https://github.com/drkhsh/slstatus/blob/master/components/ram.c

```
#elif defined(__FreeBSD__)
    #include <sys/sysctl.h>
    #include <sys/vmmeter.h>
    #include <unistd.h>
    #include <vm/vm_param.h>

   /* --- SNIP --- */

    const char *
    ram_perc(void) {
        long npages;
        long active;
        size_t len;

        len = sizeof(npages);
        if (sysctlbyname("vm.stats.vm.v_page_count", &npages, &len, NULL, 0) == -1
                || !len)
            return NULL;

        if (sysctlbyname("vm.stats.vm.v_active_count", &active, &len, NULL, 0) == -1
                || !len)
            return NULL;

        return bprintf("%d", active * 100 / npages);
    }

   /* --- SNIP --- */

#endif
```


----------



## unique name (Aug 20, 2021)

I downloaded slstatus from suckless.org. I am displaying it using %s%%, same as cpu, which is working fine.


----------



## eternal_noob (Aug 20, 2021)

unique name said:


> I am displaying it using %s%%


This is fine since after looking at the source we know that the RAM usage is reported back as a string. The fault must be in the source of slstatus then.


----------



## SirDice (Aug 20, 2021)

`active` appears to be a correct value but where is it getting `npages` from?


----------



## eternal_noob (Aug 20, 2021)

SirDice said:


> but where is it getting `npages` from?


The author uses `sysctlbyname` to read in the value from `vm.stats.vm.v_page_count`


----------



## SirDice (Aug 20, 2021)

Oh, right, missed that one. So on my system that would be get these values:

```
root@molly:~ # sysctl vm.stats.vm.v_page_count
vm.stats.vm.v_page_count: 4028834
root@molly:~ # sysctl vm.stats.vm.v_active_count
vm.stats.vm.v_active_count: 5578
```

And the calculation would produce: 

```
root@molly:~ # dc
>>> 2k
>>> 5578
>>> 100 *
>>> 4028834 /
>>> p
.13
```
Something looks off here.


----------



## eternal_noob (Aug 20, 2021)

I guess it's a 32 bit vs 64 bit issue since he does

```
len = sizeof(npages);
```
which produces different results in different architectures.

Edit:


SirDice said:


> Something looks off here.


Yup. 0.13% can't be right. The calculation smells.


----------



## unique name (Aug 20, 2021)

eternal_noob said:


> I guess it's a 32 bit vs 64 bit issue since he does
> 
> ```
> len = sizeof(npages);
> ...


So what should I do? And how do I add the volume control?


----------



## eternal_noob (Aug 20, 2021)

unique name said:


> So what should I do?


You could figure out how to properly compute the RAM usage in percent, check out the source, add your changes, compile and be happy.
If you want to be a hero you could then open a pull request and make your changes public. But unfortunately, the project seems inactive so i guess no luck with the pull request.

Don't know about the volume control


----------



## unique name (Aug 20, 2021)

eternal_noob said:


> You could figure out how to properly compute the RAM usage in percent, check out the source, add your changes, compile and be happy.
> If you want to be a hero you could then open a pull request and make your changes public. But unfortunately, the project seems inactive so i guess no luck with the pull request.
> 
> Don't know about the volume control


Instead of using ram percentage, i tried ram used / ram total. 
In both used and total, it says "512.0 Pi". 
Doen't matter if i close terminal and open new browser instance and tabs, it stays the same.


----------



## memreflect (Aug 20, 2021)

The problem is 'long' is size 8, or 64-bit, on 64-bit platforms, but the length of the data stored in 'len' by each call is 4, or 32-bit.  Printing 'len' after each call is what reveals that.  Initialize the 'active' and 'npages' variables to 0 and the problem disappears because the 32-bit values are written to the lower 32-bit half of those variables.


----------



## unique name (Aug 20, 2021)

memreflect said:


> The problem is 'long' is size 8, or 64-bit, on 64-bit platforms, but the length of the data stored in 'len' by each call is 4, or 32-bit.  Printing 'len' after each call is what reveals that.  Initialize the 'active' and 'npages' variables to 0 and the problem disappears because the 32-bit values are written to the lower 32-bit half of those variables.


I am sorry I don't know how to do that. Do I have to edit the ram.c file? I am guessing I have to do something like npages=0 and active=0. Can you please tell me what and where should I do this?


----------



## eternal_noob (Aug 20, 2021)

unique name said:


> I am sorry I don't know how to do that.


You need to `git clone` the repository, apply your changes and use `(g)make` to compile and install the new code.
See https://github.com/drkhsh/slstatus/blob/master/README

While you are at it, you can also apply Fixed: Set the locale to the environment default.


----------



## unique name (Aug 21, 2021)

eternal_noob said:


> You need to `git clone` the repository, apply your changes and use `(g)make` to compile and install the new code.
> See https://github.com/drkhsh/slstatus/blob/master/README
> 
> While you are at it, you can also apply Fixed: Set the locale to the environment default.


I can edit file manually and make it, I was asking what changes to make? i.e
Which file? ram.c?
What to change?


----------



## unique name (Aug 21, 2021)

It fixed automatically lol idk how!


----------



## eternal_noob (Aug 21, 2021)

unique name said:


> It fixed automatically lol idk how!


It's magic.


----------

