I want to write a program like system monitor.
I want to have a list of programs with their process ID and usage of CPU and RAM.
I know Linux writes this information in the /proc folder but somebody told me that I can use some functions to get it too. For example a program that will return a list like:
name PID RAM
sh 3904 72KIB
And I want to code in C++.
Why don't you look at the source code for top, which displays these and many more process statistics?
Here is the busybox version, which is comparatively short and simple. It gets the information by reading the proc filesystem, that logic is here.
Related
I'm working on a C++ program and I want to find the total memory used by it in its execution. My operating system is Ubuntu 19.10. I found this as a related question, but it seems to address a much different problem. Any help/directions would be great. Thanks!
You can use the command line tool top to monitor the memory usage of a process. Simply run:
top -p PID
where PID is the process ID of the C++ executable that you want to monitor the memory usage of.
I need to read information, code, flags, address, etc from a memory.dmp file generated from a windows BSOD through C++. The basic idea is that status info can be requested from a remote site and one of the requested pieces of information is some basic info from the last BSOD that occured on the machine thus I need to open the kernel/memory dump file through C++ (Im using MSVC 2005).
Start here, then realize using scripted commands in WinDBG is much easier.
Note: you only need WinDBG on the analysis machine, not the crashing one. You retrieve the minidump and analyse it externally. The only difficulty you will have is getting the right symbols - for Windows, Microsoft makes them available via their symbol servers, but applications that caused the crash may not supply the right symbols you need. IF they are you own applications causing the crash, get a symbol server and use it.
I would configure Windows to create small kernel memory dumps which will include the parameter of the bugcheck you are after.
On XP it was 64KB on my Win8.1 x64 it is 256KB. These files compress well. You should be able to get away with a zip file of 10-60KB size depending on the bitness of the OS. If bandwidth is of of utmost importance to you, you can use 7z which compresses about 50% better than the plain zip algo at the expense of much longer compression times (5-6 longer) but for such small files the CPU time difference should be irrelevant.
If you do not want your users to configure dump reporting you need to set the DWORD
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CrashControl
to 3 for a small kernel dump programatically.
For an explanation of the values see http://technet.microsoft.com/en-us/library/cc976050.aspx
0 Debugging information is not written to a file.
1 Complete crash dump is written to a file.
2 Kernel memory dump is written to a file.
3 Small memory dump is written to a file.
You will then get by default a small kernel dump in %SystemRoot%\MEMORY.DMP.
I am trying to obtain the memory working set value for a given PID in my C++ application running on LINUX. In Windows I can get this info using GetProcessWorkingSetSize function. Is there anything like this function I can call in LINUX?
The only sensible solution that comes to mind is accessing the relevant information via the /proc filesystem. It seems weird that a process would have to read out its own information from /proc, though, but I don't know about any other system calls that might make this easier.
The information you're probably most interested in is located in /proc/[pid]/statm, which includes :
total program size,
resident set size,
shared pages,
text (code) size,
library (unused in Linux 2.6),
data and stack size,
dirty pages (unused in Linux 2.6).
Keep in mind that all those measurements are given in the number of pages.
I want to use valgrind to do some profiling, since it does not need re-build the program. (the program I want to profile is already build with “-g")
But valgrind(callgrind) is quite slow ... so here's what I to do:
start the server ( I want to profile that server)
kind of attach to that server
before I do some operation on server, start collect profile data
after the operation is done, end collecting profile data
analyze the profiling data.
I can do this kind of thing using sun studio on Solaris. (using dbx ). I just want to know is it possible to do the same thing using valgrind(callgrind)?
Thanks
You should look at callgrind documentation, and read about callgrind_control.
Launch your app : valgrind --tool=callgrind --instr-atstart=no your_server.x
See 1.
start collect profile data: callgrind_control -i on
end collect profile data: callgrind_control -i off
Analyze data with kcachegrind or callgrind_annotate/cg_annotate
For profiling only some function you can also find useful CALLGRIND_START_INSTRUMENTATION and CALLGRIND_STOP_INSTRUMENTATION from <valgrind/callgrind.h> header and using callgrind's --instr-atstart=no option as suggested in Doomsday's answer.
You don't say what OS - I'm assuming Linux - in which case you might want to look at oprofile (free) or Zoom (not free, but you can get an evaluation licence), both of which are sampling profilers and can profile existing code without re-compilation. Zoom is much nicer and easier to use (it has a GUI and some nice additional features), but you probably already have oprofile on your system.
Is there a C/C++ library, and documentation about how to collect system and process information on Solaris?
Although I could parse command-line tools, I'd rather use a library that makes the task easier to do.
Thanks
Edit: It has been suggested to use the /proc virtual directory to collect information, however its not much better than parsing command-line tools, in the sense that I'll need to implement some sort of custom parsing for every piece of data I need.
I'm looking for something along the lines of c libraries for Windows or MacOS that provides this information through a c-based systems API, however I'm having no luck with Google.
You can get this kind of information with kstat API.
man -s 3KSTAT kstat
You can see how it is used in OpenSolaris vmstat and iostat source.
For information about processus, I'd look at ps.
Solaris has the /proc virtual directory, which allows you to gather all sorts of information about processes using filesystem I/O functions.
I would use the /proc virutal dir as CrashWorks has suggested. I've done this on both aux and linux. One thing to keep in mind is when I did use the /proc dir on linux the format of the files varied from one kernel to another.
I don't know what the situation is like on the Solaris side but this could mean that your solution will not be portable from one solaris platform to another.
what about getrusage()?
I'm definately not an expert on the subject, but I did something very similar for an assignment last semester when we were required to take snapshots of processes. Unfortunately this method requires digging into the kernel which is probably not what you want to do.
I found this article helpful.
Anyways here are some snippets.
write_lock_irq(&tasklist_lock);
for_each_process(task) {
if (system_or_user == 0)
print_mem_user(task);
if (system_or_user == 1)
print_mem_system(task);
}
write_unlock_irq(&tasklist_lock);
The idea you need to lock down some data structures or sometimes the kernel will hang. "for_each_process" is a macro defined somewhere but I don't remember how it works D:
static void print_mem_system(struct task_struct *task)
{
struct mm_struct *mm;
if (task -> mm == NULL){ // this is how you distinguish system processes from user processes
myarraypid[totalnumberofprocesses] = task -> pid; // store process id's into myarraypid[], which you can later copy back to user space for printing/display. Additional information would be found in a "task_struct" which is Linux's implementation of a process.
}
}
Some of my classmates took different approaches and dived into the source of the "ps" utility. I believe I was working on Linux 2.6.18-92.1.13.e15. Disclaimer: This worked for me but your mileage may vary. I could very well be off the wall and I don't want to lead you down the wrong direction.