Skip to contentSkip to conversation

Monitoring a program

DISCUSSION [email protected] 3 participants 4 messages Mar 1, 2004

lore
Share & tools
lore
#0 lore raw

Hi,

I would like to monitor a program execution. I intend to have a program (or daemon) that, whenever the target program (whose name is to be passed as a paramenter) starts (i.e when ps -C progname returns any process), gets statistics like cpu usage and store them on a file, finishing when the target program finishes. What would be an efficient way to do that?

Thanks,
Leonardo.

#1 lore raw

Let me see if I understand what you are trying to do. Lets pretend the target program is ls.
Whenever ls is run by any user you would like you daemon to store statistical information about the execution of ls.

You might have to get this information from the kernels process table. While executing the information you want is available in the procfs, however when the process completes the information disappears. It has been awhile since I took my OS class, so I'm not sure if the information is still available in the kernel's data structures or not. I believe it would be until another process with the same pid runs, but there is a good chance that I'm wrong.

Depending on what you are trying to accomplish there may be better/easier ways to accomplish your goal.

--
John

----- Original Message -----
From: <[email protected]>
To: <[email protected]>
Sent: Monday, March 01, 2004 1:22 PM
Subject: Monitoring a program

Quoted 3 lines

Hi,

I would like to monitor a program execution. I intend to have a program

(or daemon) that, whenever the target program (whose name is to be passed as a paramenter) starts (i.e when ps -C progname returns any process), gets statistics like cpu usage and store them on a file, finishing when the target program finishes. What would be an efficient way to do that?

Quoted 5 lines

Thanks,
Leonardo.
-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 2 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#2 lore raw

Have you considered using the 'time' program?

You could do something like (say the target program is ls again)

time ls 2>> statfile

the only problem is that you wouldn't see anything sent to stderr while the program was executing
if you want your users to be unaware of this you could do something like

mv /bin/ls /bin/ls.bk

then make the file

/bin/ls
----------------------------------------
#!/bin/bash
time ls 2>> statfile
----------------end file---------------
----- Original Message -----
From: "John T. Williams" <[email protected]>
To: <[email protected]>; <[email protected]>
Sent: Monday, March 01, 2004 4:38 PM
Subject: Re: Monitoring a program

Quoted 7 lines

Let me see if I understand what you are trying to do. Lets pretend the target program is ls.
Whenever ls is run by any user you would like you daemon to store statistical information about the execution of ls.

You might have to get this information from the kernels process table. While executing the information you want is available in the procfs,

however

Quoted 19 lines

when the process completes the information disappears. It has been awhile since I took my OS class, so I'm not sure if the information is still available in the kernel's data structures or not. I believe it would be until another process with the same pid runs, but there is a good chance that I'm wrong.

Depending on what you are trying to accomplish there may be better/easier ways to accomplish your goal.

--
John

----- Original Message -----
From: <[email protected]>
To: <[email protected]>
Sent: Monday, March 01, 2004 1:22 PM
Subject: Monitoring a program

Quoted 4 lines
Quoted 3 lines

Hi,

I would like to monitor a program execution. I intend to have a program

(or daemon) that, whenever the target program (whose name is to be passed

as

Quoted 8 lines

a paramenter) starts (i.e when ps -C progname returns any process), gets statistics like cpu usage and store them on a file, finishing when the target program finishes. What would be an efficient way to do that?

Quoted 5 lines

Thanks,
Leonardo.
-
To unsubscribe from this list: send the line "unsubscribe

Quoted 6 lines

linux-c-programming" in

Quoted 2 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe

linux-c-programming" in

Quoted 2 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
In reply to [email protected]
Quoted 6 lines · [email protected] wrote:

I would like to monitor a program execution. I intend to have a program (or daemon) that, whenever the target program (whose name is to be passed as a paramenter) starts (i.e when ps -C progname returns any process), gets statistics like cpu usage and store them on a file, finishing when the target program finishes. What would be an efficient way to do that?

Suitable mechanisms include:

1. Modifying the target program to log its own statistics.

2. Modifying some other user-space component, (e.g. the loader or libc) to recognise when the target program is being run and call your monitoring code.

3. Modifying the kernel, e.g. extending the existing process accounting mechanism to record additional information. Or at least forcing the information to remain around long enough to be recorded.

Unsuitable mechanisms include continuously polling the process list in the hope that you can catch a process running the target program and attach to it with ptrace() before it terminates.

Signature · 1 line
Glynn Clements <[email protected]>