Memory Monitoring for each process in linux - c

I would like to implement my kernel to be able to monitoring memory of each process. However, all I can do is to only print out the process and pid. I can't find a function that can help in monitoring memory. Here is the code in the kernel that i Implemented. I use Linux kernel version 4.11.0-rc7.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/resource.h>
#include <linux/sched.h>
#include <linux/syscalls.h>
asmlinkage long sys_listProcessInfo(void){
struct task_struct *process;
struct rusage usage;
int i = 0;
for_each_process(process){
if(i%10 == 0){
printk("Process %s\n PID: %ld\n",process->comm,(long)task_pid_nr(process));
i++;
}
}
return 0;
}
This one can only view process and pid. I would like to know if there is any function that can look for the memory of the process. Thank you in advance.

For a process other than the current one, use the /proc file system.
char fName[32]; // you should really only need 24
sprintf(fName, "/proc/%d/status", pid);
FILE* status = fopen(fName , "r" );
That file will have information about the process's memory allocation (virtual, resident, etc.) See man7's page on /proc for details.
This is similar but not identical to this post.

This is the standard wrong kernel code (ignoring locking requirements etc.). The function name and intended purpose strongly hint this is just a college assignment. There were several other people coming up with problems of similar quality.
As each time it was clear the person asking is fundamentally not prepared to do this work, each time I ask who gave them the assignment and what material were they provided with.
Can you please answer the above?
Thanks.
EDIT
See this for a general overview what's wrong with code samples of the sort:
http://codingtragedy.blogspot.com/2016/12/bad-kernel-tutorials-for-beginners.html
This website is unsuitable for kernel-related questions (or most lower level stuff from that matter). I'm afraid you will have to consult your fellow students. Preferably though someone would make sure tasks of the sort are not assigned to unprepared students, or even at all.

Related

Counter for main() in Linux OS

i want to implement a counter in Linux which keep a track of how many times
main() is called by any process.
when i start this counter thing as a process, from that time it will tell me how many times main() was called not by my program but in the entire OS system
example:
i start this as a daemon and then i create a simple code
#include <stdio.h>
int main(){
//some code
return 0;
}
Now here main is called so the counter will increment by one.
Can anyone explain me how can this be done.?
thanks
You might want to take a look at: Proc connector and socket filters
The Proc Connector and Socket Filters Posted on February 9, 2011 by scott
The proc connector is one of those interesting kernel features that most people rarely come across, and even more rarely find documentation on. Likewise the socket filter. This is a shame, because they’re both really quite useful interfaces that might serve a variety of purposes if they were better documented.
The proc connector allows you to receive notification of process events such fork and exec calls, as well as changes to a process’s uid, gid or sid (session id). These are provided through a socket-based interface by reading instances of struct proc_event defined in the kernel header....
main() is just a abstract code; assembler uses functions' name as a hint, and converts them into numbers. So you can't implement such counter.
main() is usually called once, what you mean may be a program which counts how many programs are executed.
There is popen in Linux which performs the given command, and yields the result as FILE *, so you can execute the ps command and parse it to get the list of the processes. You can continuously invoke popen and count the number of programs.
char Buffer[1024];
sprintf(Buffer,"ps ");
FILE *ptr = popen(Buffer, "r");
if(NULL != ptr)
{
while(fgets(Buffer, sizeof(Buffer),ptr));
pclose(ptr);
}
// Now the list of processes are in Buffer

concurrent readers and mutually excluding writers in C using pthreads

I was hoping if someone could forward me or show me a program that has multiple readers yet mutually excluding writers in C. I searched the entire internet for it, and could not find a single example that displays this behavior using coarse-grained locking. I know I can use pthread_rwlock_init, pthread_rwlock_rdlock, etc, I just don know how to use it. I learn by examples, which is why Im here.
Suppose I have a region of code(not a shared variable)and I want multiple reads, yet a single writer, this is what I don't know how to accompolish using pthreads rwlocks. I don't understand how the code will know that now it is being written to, compared to now it is being read.
Thanks.
You can take a look at page 24 of Peter Chapin's Pthread Tutorial for an example. I added it below.
#include<pthread.h>
int shared;
pthread_rwlock_t lock ;
void∗ thread_function (void∗ arg)
{
pthread_rwlock_rdlock (&lock);
//Read from the shared resource.
pthread_rwlock_unlock(&lock);
}
void main( void )
{
pthread_rwlock_init(&lock, NULL);
// Start threads here.
pthread_rwlock_wrlock(& lock );
// Write to the shared resource .
pthread_rwlock_unlock(&lock);
// Join_with threads here .
pthread_rwlock_destroy(&lock);
return 0 ;
}

Is it possible to make a list of all the sk_buff's that are currently <LIVE> in the system?

I intend to develop a application that monitors the traffic on particular ports. For this I need to list all the sk_buff data of all the LIVE sk_buff's in the system. How to do this ?
I have written the following code (basically a kernel module.)
include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include </usr/src/linux-headers-2.6.38-8-generic/include/linux/skbuff.h>
int init_module(void)
{
struct sk_buff *skb;
printk(KERN_INFO "SKB 1.\n");
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "Done 1.\n");
}
But I dont know how I catch the sk)buff's. I have simply declared a sk_buff instance .. thats all ..
Please help me to actually catch them live Sk_buff's in the system.
EDIT
I have tried all the top google search results. They give a very good description of the sk_buff itself, but none of them actually show how to do what I am particularly interested in.
There is no standardized way. Newly created skbs are not put into any list by default that you could read (that is, when they come fresh out of skb_alloc), therefore, there is no way to know all skbs are active from a random code point in the kernel, such as your module. You have at least two options though (both entail modifying core kernel code):
Since all skbuffs are allocated from a kmem_cache pool, you could augment the kmem_cache functionality by some function that tells you about all allocated objects.
Within the __alloc_skb function, add all newly allocated skbs into a data structure of your liking (and don't forget to remove them again when the skb is freed). This is going to be a major bottleneck, but that's what you have to pay.
As usual, the question: why?

Number of Running Processes on a Minix system from C code

So, this seemed simple at first, but after crawling Google and here, the answer doesn't seem as simple as I first thought.
Basically, I'm editing a MINIX kernel as part of a practical for my Operating Systems course, and I have to add a little function that spits out the number of running processes when you hit a function key in the Information Server. I've figured out how to integrate the functionality so all the other stuff works, but for the life of me, I can not figure out how to get the current number of processes running in the system into my C code and into a variable to print out.
First I thought there'd be a nifty Syscall like SYS_NUMPROCS or something that'd return the value, but no luck.
Then, I tried piping output from a system("ps -ax | wc -l") to a file and the file wouldn't create. I tried using popen() and no luck there either - even with a simple "ls" read into a buffer, it just bombs the code and "hangs" the run of the code, so there's no output.
So now I'm truly stumped, and any help would be super awesome, because at this point I've exhausted all the obvious options.
The only two things I can think of now would be a loop counting all the processes, but first you have to get to the system's process list, and I've heard vague things said about /proc/ as a directory, but I haven't a clue how to access/run through that or how it'd link up to getting the number of processes in the first place.
Thanks a stack (lol pun), guys :)
Also, I haven't included code explicitly because nothing I've written aside from basic printf'ing for cosmetic output, because none of the things I've tried gave me any joy :/
Edit notes: Guys, this is a kernel edit - I'm writing the function to printf the information in a system C file, then recompiling the kernel and rebooting the system to test. It's a UNIX (MINIX) kernel, not a Linux kernel, and it's not a user mode program.
My code for popen(), as some of you requested, is as follows:
public void cos_dmp(){
char buffer[512];
FILE * f;
f = popen("ps -ax | wc -l","r");
fgets(buffer, sizeof(buffer),f);
//buffer should now contain result of popen()
printf(buffer);
}
That's a bit of a hacked together version from what I remember and keeping it ultra simple and showing you guys that's what I was trying to do. Again though, there must be a better way to do this aside from essentially calling the output of a system() call.
Edit again: the above code woks perfectly from a user program but won't work from the kernel function. Anybody have an idea why?:/
struct kinfo kinfo;
int nr_tasks, nr_procs;
getsysinfo(PM_PROC_NR, SI_KINFO, &kinfo);
nr_procs = kinfo.nr_pro;
This will get you the number of processes running
try looking to see what ps does. Look at its source code; it knows how many processes there are
Perhaps you could show us the code your wrote for capturing the result of system("ps -ax | wc -l"), or the code you wrote to use popen and we could help you diagnose the problem with it.
Regardless, the most efficient way I can think of to count the number of existing (not the same as running) processes on the system is to opendir("/proc") and count the number of entries that are strings of decimal digits. Each process in the system will be represented by a subdirectory of /proc, named after the decimal process id number of that process.
So, if you find "/proc/3432", for example, then you know that there exists a process with a pid of "3432". Simply count the number of subdirectories you find whose names are decimal numbers.
Assumptions:
You are asking about Linux, not MINIX.
You are writing a user-mode program, not modifiying the kernel.
So I have been having the same problem and found a solution. (MINIX 3.1) within the method to count the processes use this code:
(This is ANSI C)
It simply runs through the process table and counts the number of processes.
I know this is an old thread but it might help someone in the future.
#include "../pm/mproc.h"
/* inside function */
struct mproc *mp;
int i, n=0;
printf("Number of running processes:\n");
getsysinfo(PM_PROC_NR, SI_PROC_TAB, mproc);
for (i = 0; i<NR_PROCS; i++) {
mp = &mprocs[i];
if (mp->mp_pid == 0 && i != PM_PROCS_NR) continue;
n++;
}
printf("%d", n);
/* function end */
I had the same assignment at my university so i will post my solution if someone needs it in future. I am using Minix 3.3 and VMware player for virtual machines.
In pm server at location /usr/src/minix/servers/pm there is glo.h file which contains various global variables used by pm server. In that file, there is fortunately one variable called procs_in_use defined as EXTERN int procs_in_use;
So simple printf("%d\n",procs_in_use); from a system call will show the number of current processes running. You can test this by adding fork() function in your user space program in the middle of a loop.
One more mention : first answer that says
struct kinfo kinfo;
int nr_tasks, nr_procs;
getsysinfo(PM_PROC_NR, SI_KINFO, &kinfo);
nr_procs = kinfo.nr_procs;
didn't work out for me. SI_KINFO no more exists so you should use SI_PROC_TABLE. Also there can be problems with permissions, so you will not be able to call this function from your regular system call. There is alternative function sys_getkinfo(&kinfo) that can be called from your fresh system call and that will do the same as the above. The problem is kinfo.nr_procs will not return number of current processes but number of maximum user processes that can be in operative system which is 256 by default, and can be changed manually in file where NR_PROCS is defined. On the other hand kinfo.nr_taskswill return maximum number of kernel processes that can be held by operative system, which is 5 by default.
Check this out: http://procps.sourceforge.net/
It's got source to a number of small utilities that do these kinds of things. It'll be a good learning experience :) and I think PS is i n there as pm100 noted.
If you're editing the kernel, the most efficient way to solve this problem is to maintain a count every time a process (i.e., a task_struct entry) is created (and make sure you decrement the count every where a process terminates).
You could always loop through the list of processes in the kernel using the built-in macro (but its expensive, so you should try to avoid it):
struct task_struct *p;
unsigned int count = 0;
for_each_process(task) {
count++;
}
Check this out: http://sourceforge.net/p/readproc/code/ci/master/tree/
#include"read_proc.h"
int main(void)
{
struct Root * root=read_proc();
printf("%lu\n",root->len);
return 0;
}

Stack and Heap Space for Modern Computers

When writing in C, how can I tell how much stack space is available in memory when I launch a program? How about heap space?
How can I tell how much memory is being used during the execution of my program?
This is all Win32-specific (not really C-specific, all just OS API):
When a thread is created, it gets 1MB stack space by default, by that can be modified in whatever CreateThread API you use.
You can peek into the thread information block to find the actual stack info, but even though this is documented, this technique isn't officially supported, see http://en.wikipedia.org/wiki/Win32_Thread_Information_Block .
Also, for a 32-bit application, you can only address up to 2GB, so for an app that by design uses lots of memory, then the thing to watch out for is the total size of the process' virtual address space (committed + reserved), which includes all heap allocations. You can programmatically access the process' virtual memory with the GlobalMemoryStatusEx API, look at the ullTotalVirtual param for virtual address space. Once your process gets close to 1.8 or 1.9GB of VAS, then heap allocations and VirtualAlloc calls begin to fail. For "normal" apps, you don't have to worry about running out of VAS, but it's always good to check for fail allocs. Also, you shouldn't get a stack overflow, unless you have a bug, or a bad design.
There is a philosophy that when you need to ask these kinds of questions, for practical and not educational or informational reasons, then you are doing something seriously wrong.
If you are asking this for error-checking or to make sure your program has enough memory, ect... then don't wrorry about it, seriously. As for your programs memory, you can use the task manager (on windows) if this is just for debugging. If you need to know this in your program, I wouldn't count on any non-hacky solution.
Abstractions for a reason
Really, your program shouldn't have this as a concern. It is an OS concern, your problem should just be efficient with what it needs and let the OS do its job.
If you insist, you could look into /proc/meminfo, brk(), getrlimit() and setrlimit() (here are some docs) with the RLIMIT_STACK and RLIMIT_DATA values for approximations and rough-ishes.
#include <sys/resource.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
int main (int argc, char *argv[])
{
struct rlimit limit;
/* Get the stack limit. */
if (getrlimit(RLIMIT_STACK, &limit) != 0) {
printf("getrlimit() failed with errno=%d\n", errno);
exit(1);
}
printf("The stack soft limit is %llu\n", limit.rlim_cur);
printf("The stack hard limit is %llu\n", limit.rlim_max);
exit(0);
}
Modified from here also see man getrlimit on your system
If you state what and why you want to do this, someone may have a better method or way of doing what you want.

Resources