Clinky (stats tool)

Submitted scripts and programs
Forum rules
Your own work only.
User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Clinky (stats tool)

Unread post by DebianJoe » Wed Oct 01, 2014 11:21 am

So, the other day, we got to talking about different ways to extract memory information for the purpose of scripting, so just for giggles, I RTF(ibm/posix)M and made a little script that uses sys calls to extract existing data from the kernel using the already existing sys/sysinfo.h and unistd.h headers...and puts them in semi-human readable format.

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <math.h>

/* set conversion constants */
const double megabyte = 1024 * 1024;

int main() {
    struct sysinfo si;
    sysinfo (&si);

    unsigned long totalmem = si.totalram *(unsigned long long)si.mem_unit / megabyte;
    unsigned long usedmem = ((si.totalram *(unsigned long long)si.mem_unit) -
			     (si.freeram *(unsigned long long)si.mem_unit)) / megabyte;
    unsigned long buffermem = (si.bufferram *(unsigned long long)si.mem_unit) / megabyte;
    unsigned long freemem = si.freeram *(unsigned long long)si.mem_unit / megabyte;
    /* raw readings */
    printf("\033[1;33m+++ \033[1;32mClinky \033[1;33m+++\033[1;m\n\n");
    printf("Total:    \033[1;31m%lu M\033[1;m\n", totalmem);
    printf("Used :    \033[1;31m%lu M\033[1;m (cache not subtracted)\n", usedmem);
    printf("Free :    \033[1;31m%lu M\033[1;m\n", freemem);
    printf("Buffer:   \033[1;31m%lu M\033[1;m\n", buffermem);
    printf("Uptime:   \033[1;31m%ld\033[1;m in seconds.\n", si.uptime);
    printf("Processes:\033[1;31m%d\033[1;m\n", si.procs);
    printf("Page size:\033[1;31m%ld\033[1;m bytes\n", sysconf(_SC_PAGESIZE));
    long cpus = sysconf(_SC_NPROCESSORS_ONLN);
    long csize = sysconf(_SC_LEVEL1_ICACHE_SIZE) / pow(2, 10);
    long cassoc = sysconf(_SC_LEVEL1_ICACHE_ASSOC);
    long cline = sysconf(_SC_LEVEL1_ICACHE_LINESIZE);
    printf("CPUs:     \033[1;31m%ld\033[1;m\n", cpus);
    printf("lvl 1 icache size = %ldK, \n\tassoc = %ld, \n\tline size = %ld\n",
	   csize, cassoc, cline);
    csize = sysconf(_SC_LEVEL1_DCACHE_SIZE) / pow(2, 10);
    cassoc = sysconf(_SC_LEVEL1_DCACHE_ASSOC);
    cline = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
    printf("lvl 1 dcache size = %ldK, \n\tassoc = %ld, \n\tline size = %ld\n",
	   csize, cassoc, cline);
    csize = sysconf(_SC_LEVEL2_CACHE_SIZE) / pow(2, 10);
    cassoc = sysconf(_SC_LEVEL2_CACHE_ASSOC);
    cline = sysconf(_SC_LEVEL2_CACHE_LINESIZE);
    printf("lvl 2 cache size = %ldK, \n\tassoc = %ld, \n\tline size = %ld\n",
	   csize, cassoc, cline);

    return 0;
}
I've pushed it to git, mainly because I'm still trying to figure out a clean way to subtract active cache from usedmem without parsing a file at all. Please, hack on this and make cool things happen (little system meters, etc.)
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: Clinky (stats tool)

Unread post by wuxmedia » Wed Oct 01, 2014 1:08 pm

Nice work Joe.
Good name ;)
"Seek, and Ye shall find"
"Github | Chooons | Site"

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Clinky (stats tool)

Unread post by rhowaldt » Wed Oct 01, 2014 2:15 pm

very nice! cool idea. looking at the code, i still wish somebody made a less-complicated way of doing colours. damn, everytime i see that syntax i feel qeasy.
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Clinky (stats tool)

Unread post by GekkoP » Wed Oct 01, 2014 2:30 pm

Looks lovely, thanks Joe.

Code: Select all

~/bin $ clinky 
+++ Clinky +++

Total:    4042 M
Used :    592 M (cache not subtracted)
Free :    3449 M
Buffer:   48 M
Uptime:   1161 in seconds.
Processes:127
Page size:4096 bytes
CPUs:     2
lvl 1 icache size = 32K, 
	assoc = 8, 
	line size = 64
lvl 1 dcache size = 32K, 
	assoc = 8, 
	line size = 64
lvl 2 cache size = 2048K, 
	assoc = 8, 
	line size = 64

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Clinky (stats tool)

Unread post by DebianJoe » Wed Oct 01, 2014 2:43 pm

@Rho, it's just a quick way to do it. You could always define the colors by escape elsewhere as a string, but then you're still loading in some string into printf and you'd still end up with a syntax like:
"%s%ldK%s" to insert the strings. Not a huge improvement, IMHO.

At this point, I could care less about the output's format. I am just really interested in the methods that are in place to make kernel calls with standard system headers. Cool stuff...if just to know how it works. :) I could do without the escapes and make cool ncurses meters or something later, but first, I want to be able to really grasp what all CAN be pulled out.

...this is going to be an ongoing project of mine, most likely. :D
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
harveyhunt
Haxxor
Posts: 125
Joined: Mon Jul 07, 2014 3:06 am
Contact:

Re: Clinky (stats tool)

Unread post by harveyhunt » Wed Oct 01, 2014 3:23 pm

Looks good DJ, I just sent you a PR on github. :-)

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Clinky (stats tool)

Unread post by DebianJoe » Wed Oct 01, 2014 3:40 pm

TY sir, merged. Feel free to toss some neat tricks my way should you have them.

Edit: Specifically, if you have any ideas about how to figure active cached memory. I've been scratching my head the last hour over how that might be extracted cleanly.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
harveyhunt
Haxxor
Posts: 125
Joined: Mon Jul 07, 2014 3:06 am
Contact:

Re: Clinky (stats tool)

Unread post by harveyhunt » Wed Oct 01, 2014 3:55 pm

DebianJoe wrote:TY sir, merged. Feel free to toss some neat tricks my way should you have them.

Edit: Specifically, if you have any ideas about how to figure active cached memory. I've been scratching my head the last hour over how that might be extracted cleanly.
You could use /proc/meminfo?

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Clinky (stats tool)

Unread post by DebianJoe » Wed Oct 01, 2014 3:59 pm

Yes, but the point (at least for me) was to circumnavigate that method. That's one way I've pulled it in for shell scripts for instance here, but I'm curious as to how you could handle it at a slightly lower level. That data comes from somewhere, and that's what I want to know.

edit: Current reading. :D
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: Clinky (stats tool)

Unread post by dkeg » Wed Oct 01, 2014 4:20 pm

Great stuff! On mobile at the moment, will check it out later.
Love the name!

Work hard; Complain less

User avatar
harveyhunt
Haxxor
Posts: 125
Joined: Mon Jul 07, 2014 3:06 am
Contact:

Re: Clinky (stats tool)

Unread post by harveyhunt » Wed Oct 01, 2014 5:07 pm

@DJ I have sent another PR with uname stuff (I have never used syscalls before but am finding them fun).

When sysinfo returns the memory usage, is it MiB or MB?

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Clinky (stats tool)

Unread post by rhowaldt » Wed Oct 01, 2014 8:47 pm

@DJ: ja, that's true. and definitely understand your idea here, which i really like - go to the source. my mind just immediately jumps to "make the output look good" - but that's just my mind :)
curious to see how this will develop, and as an aside, it's good to have you a bit more active on the forums again :)
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Clinky (stats tool)

Unread post by machinebacon » Wed Oct 01, 2014 9:48 pm

Me gusta, thank you Joe.

clinky | xrootconsole -geometry 160x20+0+0 &

gives a nice lite conky-replacement :)

(osd_cat can't handle the escape sequences for colors, maybe it can, IDK - xrootconsole just accepts it, also has an --ansi-color option which does what is seen on the scrot: nothing)
Attachments
kinky.png
..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Clinky (stats tool)

Unread post by DebianJoe » Wed Oct 01, 2014 11:04 pm

harveyhunt wrote:When sysinfo returns the memory usage, is it MiB or MB?
Bytes, note the divide by 1024*1024 for the conversion.

Merged PR. Still not 100% on how (if doable) to extract pagecache as we'd discussed on IRC. Regardless, I'm learning new stuff in the process of reading anyhow. :)

Cool way to display stuff Baconista, and I may make it more pleasant on the eyes once I get the extractions and conversions done.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: Clinky (stats tool)

Unread post by DebianJoe » Fri Oct 03, 2014 9:02 am

Okay, for use in just a term (ie: not piped to anything fancy) I added a few switches at the beginning to allow it to clear, pause, and refresh term until killed without having a 'control script' because I'm really lazy and writing a shell script just to clear and sleep 10 and run forever was too much trouble, plus...someone would start talking about 9,000 ways to make the shell script better. Set flags as desired before building.

Still don't have the pagecache under control.
Attachments
clinkytest.png
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: Clinky (stats tool)

Unread post by rhowaldt » Fri Oct 03, 2014 1:39 pm

^ that Chthulu wall is fantastic.
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: Clinky (stats tool)

Unread post by dkeg » Sun Dec 18, 2016 3:25 pm

*bump*

Came up in an IRC conversation with rusty. Reminiscing on all the cool stuff Joe did; hcwm, clinky, roaster, dwm status, etc...

Work hard; Complain less

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: Clinky (stats tool)

Unread post by GekkoP » Sun Dec 18, 2016 5:59 pm

^ O Joe where art thou? :(

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: Clinky (stats tool)

Unread post by machinebacon » Mon Dec 19, 2016 5:16 am

^ Clean and jerk.
..gnutella..

Post Reply